-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format): Add Prettier to new Format script and linting process (#21
) * perf(format): Update prettier and related format changes * feat(format): Add sku format script * feat(format): Add Prettier to linting process * fix(format): Use Prettier path relative to require.resolve * fix(format): Use package.json to resolve bin location * docs(format): Update comment typo * fix(format): remove overly verbose function calls
- Loading branch information
1 parent
8cf8927
commit 03e9039
Showing
9 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
singleQuote: true | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const { spawn } = require('child_process'); | ||
const path = require('path'); | ||
|
||
const cwd = process.cwd(); | ||
|
||
const filePattern = 'src/**/*.js'; | ||
|
||
/* | ||
* Prettier must be ran from | ||
* command line to make use of command line output. | ||
* Use the bin file identified by Prettier's package.json | ||
*/ | ||
const getPrettierBinLocation = () => { | ||
const prettierPackageJson = require('prettier/package.json'); | ||
return require.resolve( | ||
path.join('prettier', prettierPackageJson.bin.prettier) | ||
); | ||
}; | ||
|
||
const runPrettier = ({ write, listDifferent, config }) => { | ||
const prettierConfig = config || {}; | ||
|
||
const prettierBinPath = getPrettierBinLocation(); | ||
|
||
const prettierArgs = [filePattern]; | ||
|
||
if (prettierConfig.singleQuote) { | ||
prettierArgs.push('--single-quote'); | ||
} | ||
if (write) { | ||
prettierArgs.push('--write'); | ||
} | ||
if (listDifferent) { | ||
prettierArgs.push('--list-different'); | ||
} | ||
|
||
/* | ||
* Show Prettier output with stdio: inherit | ||
* The child process will use the parent process's stdin/stdout/stderr | ||
* See https://nodejs.org/api/child_process.html#child_process_options_stdio | ||
*/ | ||
const processOptions = { | ||
stdio: 'inherit' | ||
}; | ||
|
||
const prettierProcess = spawn(prettierBinPath, prettierArgs, processOptions); | ||
|
||
return new Promise((resolve, reject) => { | ||
prettierProcess.on('exit', exitCode => { | ||
if (exitCode === 0) { | ||
resolve(exitCode); | ||
return; | ||
} | ||
reject(exitCode); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
check: config => runPrettier({ listDifferent: true, config }), | ||
write: config => runPrettier({ write: true, config }) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const chalk = require('chalk'); | ||
|
||
const prettierWrite = require('../lib/runPrettier').write; | ||
const prettierConfig = require('../config/prettier/prettier.config.js'); | ||
|
||
console.log(chalk.cyan('Formatting source code with Prettier')); | ||
|
||
prettierWrite(prettierConfig) | ||
.then(() => { | ||
console.log(chalk.cyan('Prettier format complete')); | ||
}) | ||
.catch(exitCode => { | ||
console.error('Error: Prettier exited with exit code', exitCode); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters