diff --git a/compiler/.github/workflows/ci.yml b/compiler/.github/workflows/ci.yml index 3c1e4c8e57..85e7592960 100644 --- a/compiler/.github/workflows/ci.yml +++ b/compiler/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: cache-dependency-path: forget/yarn.lock - run: yarn install --immutable --immutable-cache working-directory: forget - - run: yarn prettier --check . + - run: yarn prettier:ci working-directory: forget - run: yarn build working-directory: forget diff --git a/compiler/forget/.prettierignore b/compiler/forget/.prettierignore index 842c8fde83..9eb3b48441 100644 --- a/compiler/forget/.prettierignore +++ b/compiler/forget/.prettierignore @@ -2,4 +2,8 @@ **/dist **/__tests__/fixtures/**/*.expect.md **/.next -/test262 \ No newline at end of file +test262/ +*.md +*.json +*.css +*.webmanifest \ No newline at end of file diff --git a/compiler/forget/.prettierrc.json b/compiler/forget/.prettierrc.json index f0d0f091a2..d2203e6dc6 100644 --- a/compiler/forget/.prettierrc.json +++ b/compiler/forget/.prettierrc.json @@ -1,3 +1,4 @@ { - "requirePragma": false + "requirePragma": false, + "parser": "babel-ts" } diff --git a/compiler/forget/package.json b/compiler/forget/package.json index eac4449eb9..468530560c 100644 --- a/compiler/forget/package.json +++ b/compiler/forget/package.json @@ -16,7 +16,10 @@ "ts:analyze-trace": "scripts/ts-analyze-trace.sh", "test262": "yarn run --silent test262-harness --preprocessor=scripts/test262-preprocessor.js", "test262:all": "yarn run --silent test262 'test262/test/**/*.js'", - "test262:ci": "scripts/test262.sh" + "test262:ci": "scripts/test262.sh", + "prettier": "node ./scripts/prettier.js write-changed", + "prettier:all": "node ./scripts/prettier.js write", + "prettier:ci": "prettier --check ." }, "repository": { "type": "git", @@ -47,8 +50,10 @@ "@types/jest": "^29.0.3", "@types/node": "^18.7.18", "babel-jest": "^29.0.3", + "chalk": "^3.0.0", "concurrently": "^7.4.0", "eslint": "^8.25.0", + "glob": "^7.1.6", "hermes-eslint": "^0.9.0", "jest": "^29.0.3", "jest-environment-jsdom": "^29.0.3", diff --git a/compiler/forget/scripts/prettier.js b/compiler/forget/scripts/prettier.js new file mode 100644 index 0000000000..1054485a3c --- /dev/null +++ b/compiler/forget/scripts/prettier.js @@ -0,0 +1,85 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +"use strict"; + +// Based on similar script in React +// https://github.com/facebook/react/blob/main/scripts/prettier/index.js + +const chalk = require("chalk"); +const glob = require("glob"); +const prettier = require("prettier"); +const fs = require("fs"); +const listChangedFiles = require("./shared/list-changed-files"); +const prettierConfigPath = require.resolve("../.prettierrc"); + +const mode = process.argv[2] || "check"; +const shouldWrite = mode === "write" || mode === "write-changed"; +const onlyChanged = mode === "check-changed" || mode === "write-changed"; + +const changedFiles = onlyChanged ? listChangedFiles() : null; +let didWarn = false; +let didError = false; + +const files = glob + .sync("**/*.js", { + ignore: [ + "**/node_modules/**", + "packages/demo-2021Q3/**", + "packages/demo-todolist-live/**", + "packages/demo-todolist-next/**", + "packages/demo-todolist-playground/**", + "packages/eslint-browser/**", + "test262/**", + ], + }) + .filter((f) => !onlyChanged || changedFiles.has(f)); + +if (!files.length) { + return; +} + +files.forEach((file) => { + const options = prettier.resolveConfig.sync(file, { + config: prettierConfigPath, + }); + try { + const input = fs.readFileSync(file, "utf8"); + if (shouldWrite) { + const output = prettier.format(input, options); + if (output !== input) { + fs.writeFileSync(file, output, "utf8"); + } + } else { + if (!prettier.check(input, options)) { + if (!didWarn) { + console.log( + "\n" + + chalk.red( + ` This project uses prettier to format all JavaScript code.\n` + ) + + chalk.dim(` Please run `) + + chalk.reset("yarn prettier:all") + + chalk.dim( + ` and add changes to files listed below to your commit:` + ) + + `\n\n` + ); + didWarn = true; + } + console.log(file); + } + } + } catch (error) { + didError = true; + console.log("\n\n" + error.message); + console.log(file); + } +}); + +if (didWarn || didError) { + process.exitCode = 1; +} diff --git a/compiler/forget/scripts/shared/list-changed-files.js b/compiler/forget/scripts/shared/list-changed-files.js new file mode 100644 index 0000000000..30ad746787 --- /dev/null +++ b/compiler/forget/scripts/shared/list-changed-files.js @@ -0,0 +1,32 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +"use strict"; + +const execFileSync = require("child_process").execFileSync; + +const exec = (command, args) => { + console.log("> " + [command].concat(args).join(" ")); + const options = { + cwd: process.cwd(), + env: process.env, + stdio: "pipe", + encoding: "utf-8", + }; + return execFileSync(command, args, options); +}; + +const execGitCmd = (args) => exec("git", args).trim().toString().split("\n"); + +const listChangedFiles = () => { + const mergeBase = execGitCmd(["merge-base", "HEAD", "main"]); + return new Set([ + ...execGitCmd(["diff", "--name-only", "--diff-filter=ACMRTUB", mergeBase]), + ...execGitCmd(["ls-files", "--others", "--exclude-standard"]), + ]); +}; + +module.exports = listChangedFiles; diff --git a/compiler/forget/yarn.lock b/compiler/forget/yarn.lock index 487e7ab192..dc59b50076 100644 --- a/compiler/forget/yarn.lock +++ b/compiler/forget/yarn.lock @@ -1464,6 +1464,14 @@ chalk@^2.0.0: escape-string-regexp "^1.0.5" supports-color "^5.3.0" +chalk@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4" + integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg== + dependencies: + ansi-styles "^4.1.0" + supports-color "^7.1.0" + chalk@^4.0.0, chalk@^4.1.0: version "4.1.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" @@ -2172,7 +2180,7 @@ glob-parent@^6.0.1: dependencies: is-glob "^4.0.3" -glob@^7.1.3, glob@^7.1.4: +glob@^7.1.3, glob@^7.1.4, glob@^7.1.6: version "7.2.3" resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b" integrity sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==