microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
gulp_scripts/formatter.js
91lines · modecode
| 1 | const { series } = require("gulp"); |
| 2 | const cp = require("child_process"); |
| 3 | |
| 4 | const runPrettier = async fix => { |
| 5 | const child = cp.fork( |
| 6 | "./node_modules/@mixer/parallel-prettier/dist/index.js", |
| 7 | [ |
| 8 | fix ? "--write" : "--list-different", |
| 9 | "test/**/*.ts", |
| 10 | "gulpfile.js", |
| 11 | "*.md", |
| 12 | "!CHANGELOG.md", |
| 13 | "!test/smoke/**", |
| 14 | "!src/**/*.d.ts", |
| 15 | "!SECURITY.md", |
| 16 | ], |
| 17 | { |
| 18 | stdio: "inherit", |
| 19 | }, |
| 20 | ); |
| 21 | |
| 22 | await new Promise((resolve, reject) => { |
| 23 | child.on("exit", code => { |
| 24 | code ? reject(`Prettier exited with code ${code}`) : resolve(); |
| 25 | }); |
| 26 | }); |
| 27 | }; |
| 28 | |
| 29 | function formatPrettier(cb) { |
| 30 | runPrettier(true); |
| 31 | cb(); |
| 32 | } |
| 33 | |
| 34 | function lintPrettier(cb) { |
| 35 | runPrettier(false); |
| 36 | cb(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @typedef {{color: boolean, fix: boolean}} OptionsT |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * @param {OptionsT} options_ |
| 45 | */ |
| 46 | const runEslint = async options_ => { |
| 47 | /** @type {OptionsT} */ |
| 48 | const options = Object.assign({ color: true, fix: false }, options_); |
| 49 | |
| 50 | const files = ["../src/**/*.ts"]; |
| 51 | |
| 52 | const args = [ |
| 53 | ...(options.color ? ["--color"] : ["--no-color"]), |
| 54 | ...(options.fix ? ["--fix"] : []), |
| 55 | ...files, |
| 56 | ]; |
| 57 | |
| 58 | const child = cp.fork("../node_modules/eslint/bin/eslint.js", args, { |
| 59 | stdio: "inherit", |
| 60 | cwd: __dirname, |
| 61 | }); |
| 62 | |
| 63 | await new Promise((resolve, reject) => { |
| 64 | child.on("exit", code => { |
| 65 | code ? reject(`Eslint exited with code ${code}`) : resolve(); |
| 66 | }); |
| 67 | }); |
| 68 | }; |
| 69 | |
| 70 | function formatEslint(cb) { |
| 71 | runEslint({ fix: true }); |
| 72 | cb(); |
| 73 | } |
| 74 | |
| 75 | function lintEslint(cb) { |
| 76 | runEslint({ fix: false }); |
| 77 | cb(); |
| 78 | } |
| 79 | |
| 80 | const lint = series(lintPrettier, lintEslint); |
| 81 | |
| 82 | const format = series(formatPrettier, formatEslint); |
| 83 | |
| 84 | module.exports = { |
| 85 | formatPrettier, |
| 86 | formatEslint, |
| 87 | format, |
| 88 | lintPrettier, |
| 89 | lintEslint, |
| 90 | lint: lint, |
| 91 | }; |