Integrate Formatter
Edit on GitHubFollow the steps below to integrate Formatter into your project.
1. Install the dependencies
To install the dependencies:
- Install Prettier:
npm install prettier@2.0.x --save-dev
- Install config for Prettier:
npm install @spryker/frontend-config.prettier --save-dev
- Install the CLI parser:
npm install commander@4.0.x --save-dev
2. Update the scripts
To update the scripts:
- Add formatting file extensions to the global settings
/frontend/settings.js
:
> ....
formatter: [
`**/*.(scss|css|less|js|ts|json|html)`,
],
};
- Add the formatter script to
/frontend/libs/formatter.js
:
const { spawn } = require('child_process');
const { globalSettings } = require('../settings');
const commandLineParser = require('commander');
const configPath = 'node_modules/@spryker/frontend-config.prettier/.prettierrc.json';
commandLineParser
.option('-f, --fix', 'execute stylelint in the fix mode.')
.option('-p, --file-path <path>', 'execute stylelint only for this file.')
.option('-i, --ignore-path <path>', 'path to prettier ignore file.')
.parse(process.argv);
const mode = commandLineParser.fix ? '--write' : '--check';
const filePaths = commandLineParser.filePath ? [commandLineParser.filePath] : globalSettings.formatter;
const ignorePath = commandLineParser.ignorePath ? commandLineParser.ignorePath : './.prettierignore';
spawn(
'npx',
['prettier', '--config', configPath, '--ignore-path', ignorePath, mode, ...filePaths],
{ stdio: 'inherit' }
);
Check here for the example file.
- Adjust the
/package.json
scripts:
"scripts": {
....
"formatter": "node ./frontend/libs/formatter",
"formatter:fix": "node ./frontend/libs/formatter --fix"
}
- Add the ignore file
/.prettierignore
:
# Ignore paths
/.github/
/.yarn/
/docker/
/frontend/
/vendor/
/public/Yves/assets/
/public/Zed/assets/
**/dist/**
/composer.json
/package-lock.json
Thank you!
For submitting the form