diff --git a/.eslintrc.js b/.eslintrc.js index 4f9231b90..45ea57473 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -306,5 +306,14 @@ module.exports = { sourceType: "module", }, }, + { + files: ["bin/prettier.js"], + parserOptions: { + ecmaVersion: 5, + }, + rules: { + "no-var": "off", + }, + }, ], }; diff --git a/.github/workflows/prod-test.yml b/.github/workflows/prod-test.yml index f62a34f63..affdbcb3d 100644 --- a/.github/workflows/prod-test.yml +++ b/.github/workflows/prod-test.yml @@ -144,3 +144,28 @@ jobs: env: NODE_ENV: production PRETTIER_FALLBACK_RESOLVE: true + + cli: + name: CLI + runs-on: ubuntu-latest + needs: [build] + steps: + - name: Checkout + uses: actions/checkout@v2.4.0 + + - name: Setup Node.js + uses: actions/setup-node@v2.5.1 + with: + node-version: "0.10.48" + + - name: Download Artifact + uses: actions/download-artifact@v2 + with: + name: dist + path: dist + + - name: Validate Node.js Version + run: node -v | grep "v0.10.48" || exit 1 + + - name: Run CLI on Node.js v0.10.48 + run: node dist/bin-prettier --version 2>&1 >/dev/null | grep "prettier requires at least version 10.13.0 of Node, please upgrade" || exit 1 diff --git a/bin/prettier.js b/bin/prettier.js index afd811615..0e1f3fc3c 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -2,4 +2,11 @@ "use strict"; -module.exports = require("../src/cli/index.js").run(process.argv.slice(2)); +var pleaseUpgradeNode = require("please-upgrade-node"); +var packageJson = require("../package.json"); + +pleaseUpgradeNode(packageJson); + +var cli = require("../src/cli/index.js"); + +module.exports = cli.run(process.argv.slice(2)); diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index 1706ccc2d..b4979083a 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -166,16 +166,18 @@ function* getEsbuildOptions(bundle, buildOptions) { ].filter(Boolean), minify: shouldMinify, legalComments: "none", - external: [...(bundle.external || [])], + external: [...(bundle.external ?? [])], // Disable esbuild auto discover `tsconfig.json` file tsconfig: path.join(__dirname, "empty-tsconfig.json"), mainFields: ["main"], - target: ["node10"], + target: [...(bundle.esbuildTarget ?? ["node10"])], logLevel: "error", }; if (bundle.target === "universal") { - esbuildOptions.target.push(...umdTarget); + if (!bundle.esbuildTarget) { + esbuildOptions.target.push(...umdTarget); + } yield { ...esbuildOptions, @@ -207,7 +209,7 @@ function* getEsbuildOptions(bundle, buildOptions) { } async function runBuild(bundle, esbuildOptions, buildOptions) { - if (!buildOptions.babel) { + if (!buildOptions.babel || bundle.skipBabel) { await esbuild.build(esbuildOptions); return; } diff --git a/scripts/build/config.mjs b/scripts/build/config.mjs index 475149719..05ea8abb4 100644 --- a/scripts/build/config.mjs +++ b/scripts/build/config.mjs @@ -16,6 +16,8 @@ const { require } = createEsmUtils(import.meta); * @property {Object.} replace - map of strings to replace when processing the bundle * @property {string[]} babelPlugins - babel plugins * @property {boolean?} minify - minify + * @property {string[]?} esbuildTarget - ESBuild target + * @property {boolean?} skipBabel - Skip babel transform * @typedef {Object} CommonJSConfig * @property {string[]} ignore - paths of CJS modules to ignore @@ -196,6 +198,12 @@ const coreBundles = [ { input: "bin/prettier.js", output: "bin-prettier.js", + esbuildTarget: ["node0.10"], + skipBabel: true, + }, + { + input: "src/cli/index.js", + output: "cli.js", external: ["benchmark"], }, { diff --git a/scripts/bundle-eslint-config.cjs b/scripts/bundle-eslint-config.cjs index e1f3c4bb8..bcdb281ef 100644 --- a/scripts/bundle-eslint-config.cjs +++ b/scripts/bundle-eslint-config.cjs @@ -40,7 +40,16 @@ module.exports = { }, }, { - files: ["index.js", "bin-prettier.js", "third-party.js"], + files: ["bin-prettier.js"], + parserOptions: { + ecmaVersion: 5, + }, + rules: { + "compat/compat": "error", + }, + }, + { + files: ["index.js", "bin-prettier.js", "cli.js", "third-party.js"], rules: { "no-restricted-syntax": [ "error", diff --git a/src/cli/index.js b/src/cli/index.js index 699f8044b..8017c615d 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -1,10 +1,5 @@ "use strict"; -// eslint-disable-next-line no-restricted-modules -const packageJson = require("../../package.json"); -require("please-upgrade-node")(packageJson); - -// eslint-disable-next-line import/order const stringify = require("fast-json-stable-stringify"); // eslint-disable-next-line no-restricted-modules const prettier = require("../index.js"); diff --git a/tests/integration/__tests__/early-exit.js b/tests/integration/__tests__/early-exit.js index 8cf36ed21..8aa37ec9c 100644 --- a/tests/integration/__tests__/early-exit.js +++ b/tests/integration/__tests__/early-exit.js @@ -2,7 +2,6 @@ const prettier = require("prettier-local"); const runPrettier = require("../run-prettier.js"); -const { isProduction } = require("../env.js"); describe("show version with --version", () => { runPrettier("cli/with-shebang", ["--version"]).test({ @@ -90,30 +89,3 @@ describe("throw error and show usage with something unexpected", () => { status: "non-zero", }); }); - -test("node version error", async () => { - const originalProcessVersion = process.version; - let result; - - Object.defineProperty(process, "version", { - value: "v8.0.0", - writable: false, - }); - try { - result = await runPrettier("cli", ["--help"]); - } finally { - Object.defineProperty(process, "version", { - value: originalProcessVersion, - writable: false, - }); - } - - expect(result.status).toBe(1); - expect(result.stderr).toBe( - `prettier requires at least version ${ - isProduction ? "10.13.0" : "12.17.0" - } of Node, please upgrade\n` - ); - expect(result.stdout).toBe(""); - expect(result.write).toEqual([]); -});