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