From 3cbee742a792413211eea1e4fe2d0dad26ff37ea Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 3 Apr 2022 00:04:45 +0800 Subject: [PATCH] Add `please-upgrade-node` back to CLI (#12448) --- .eslintrc.cjs | 20 ++++++++-------- .github/workflows/prod-test.yml | 41 ++++++++++++++++----------------- bin/prettier.cjs | 24 +++++++++++++++++++ bin/prettier.js | 5 ++-- package.json | 2 +- scripts/build/config.mjs | 6 ++--- 6 files changed, 60 insertions(+), 38 deletions(-) create mode 100755 bin/prettier.cjs diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 26b5ccb96..84c7056e5 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -333,15 +333,15 @@ module.exports = { "import/no-extraneous-dependencies": "error", }, }, - // TODO[@fisker]: Fix this - // { - // files: ["bin/prettier.js"], - // parserOptions: { - // ecmaVersion: 5, - // }, - // rules: { - // "no-var": "off", - // }, - // }, + { + files: ["bin/prettier.cjs"], + parserOptions: { + ecmaVersion: 5, + }, + rules: { + "no-var": "off", + "prefer-arrow-callback": "off", + }, + }, ], }; diff --git a/.github/workflows/prod-test.yml b/.github/workflows/prod-test.yml index c4775de9b..41b31915f 100644 --- a/.github/workflows/prod-test.yml +++ b/.github/workflows/prod-test.yml @@ -137,28 +137,27 @@ jobs: NODE_ENV: production PRETTIER_FALLBACK_RESOLVE: true - # TODO[@fisker]: Fix this - # cli: - # name: CLI - # runs-on: ubuntu-latest - # needs: [build] - # steps: - # - name: Checkout - # uses: actions/checkout@v2.4.0 + 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: 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: 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: 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 + - 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 12.17.0 of Node, please upgrade" || exit 1 diff --git a/bin/prettier.cjs b/bin/prettier.cjs new file mode 100755 index 000000000..ee8bd4ed2 --- /dev/null +++ b/bin/prettier.cjs @@ -0,0 +1,24 @@ +#!/usr/bin/env node + +"use strict"; + +var pleaseUpgradeNode = require("please-upgrade-node"); +var packageJson = require("../package.json"); + +pleaseUpgradeNode(packageJson); + +function runCli(cli) { + return cli.run(process.argv.slice(2)); +} + +module.exports.promise = (function () { + if (process.env.NODE_ENV !== "production") { + var dynamicImport = new Function("module", "return import(module)"); + + return dynamicImport("../src/cli/index.js").then(runCli); + } + + var cli = require("../src/cli/index.js"); + + return runCli(cli); +})(); diff --git a/bin/prettier.js b/bin/prettier.js index 491ba18c8..429fd1410 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -1,5 +1,6 @@ #!/usr/bin/env node -import * as cli from "../src/cli/index.js"; +// Add this file so we can use `node bin/prettier` or `node bin/prettier.js` +// instead of `node bin/prettier.cjs`. -export const promise = cli.run(process.argv.slice(2)); +import "./prettier.cjs"; diff --git a/package.json b/package.json index a9f9c01cb..d0f5b1219 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "prettier", "version": "2.7.0-dev", "description": "Prettier is an opinionated code formatter", - "bin": "./bin/prettier.js", + "bin": "./bin/prettier.cjs", "repository": "prettier/prettier", "funding": "https://github.com/prettier/prettier?sponsor=1", "homepage": "https://prettier.io", diff --git a/scripts/build/config.mjs b/scripts/build/config.mjs index efb9b759e..b5a9b3e40 100644 --- a/scripts/build/config.mjs +++ b/scripts/build/config.mjs @@ -312,11 +312,9 @@ const coreBundles = [ ], }, { - input: "bin/prettier.js", + input: "bin/prettier.cjs", output: "bin-prettier.js", - // TODO[@fisker]: Fix this - // esbuildTarget: ["node0.10"], - interopDefault: false, + esbuildTarget: ["node0.10"], }, { input: "src/cli/index.js",