Formatter integration guide
  • 1 Minute To Read
  • Print
  • Share
  • Dark
    Light

Formatter integration guide

  • Print
  • Share
  • Dark
    Light

Follow the steps below to integrate Formatter intor your project.

1. Install the dependencies

To install the dependencies:

  1. Install Prettier:
npm install [email protected] --save-dev
  1. Install config for Prettier:
npm install @spryker/frontend-config.prettier --save-dev
  1. Install the CLI parser:
npm install [email protected] --save-dev

2. Update the scripts

To update the scripts:

  1. Add formatting file extensions to the global settings /frontend/settings.js:
    ....
    
    formatter: [
        `**/*.(scss|css|less|js|ts|json|html)`,
    ],
};
  1. 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.

  1. Adjust the /package.json scripts:
"scripts": {
    ....
    "formatter": "node ./frontend/libs/formatter",
    "formatter:fix": "node ./frontend/libs/formatter --fix"
}
  1. Add the ignore file /.prettierignore:
# Ignore paths
/.github/
/.yarn/
/docker/
/frontend/
/vendor/
/public/Yves/assets/
/public/Zed/assets/
**/dist/**

/composer.json
/package-lock.json
Was This Article Helpful?