@samitouri / QOS-React / commits / cf7176d3f2

Add prettier script

Copies over the prettier script from the React repo with a few tiny tweaks for a nicer experience running prettier on changed files

Lauren Tan committed Nov 17, 2022 at 10:31 UTC cf7176d3f24e0b116cd9bee3878480e0010e1176
7 files changed +140 -5
compiler/.github/workflows/ci.yml
+1 -1
@@ -17,7 +17,7 @@ jobs:
17 cache-dependency-path: forget/yarn.lock
18 - run: yarn install --immutable --immutable-cache
19 working-directory: forget
20 - - run: yarn prettier --check .
20 + - run: yarn prettier:ci
21 working-directory: forget
22 - run: yarn build
23 working-directory: forget
compiler/forget/.prettierignore
+5 -1
@@ -2,4 +2,8 @@
2 **/dist
3 **/__tests__/fixtures/**/*.expect.md
4 **/.next
5 -/test262
\ No newline at end of file
5 +test262/
6 +*.md
7 +*.json
8 +*.css
9 +*.webmanifest
\ No newline at end of file
compiler/forget/.prettierrc.json
+2 -1
@@ -1,3 +1,4 @@
1 {
2 - "requirePragma": false
2 + "requirePragma": false,
3 + "parser": "babel-ts"
4 }
compiler/forget/package.json
+6 -1
@@ -16,7 +16,10 @@
16 "ts:analyze-trace": "scripts/ts-analyze-trace.sh",
17 "test262": "yarn run --silent test262-harness --preprocessor=scripts/test262-preprocessor.js",
18 "test262:all": "yarn run --silent test262 'test262/test/**/*.js'",
19 - "test262:ci": "scripts/test262.sh"
19 + "test262:ci": "scripts/test262.sh",
20 + "prettier": "node ./scripts/prettier.js write-changed",
21 + "prettier:all": "node ./scripts/prettier.js write",
22 + "prettier:ci": "prettier --check ."
23 },
24 "repository": {
25 "type": "git",
@@ -47,8 +50,10 @@
50 "@types/jest": "^29.0.3",
51 "@types/node": "^18.7.18",
52 "babel-jest": "^29.0.3",
53 + "chalk": "^3.0.0",
54 "concurrently": "^7.4.0",
55 "eslint": "^8.25.0",
56 + "glob": "^7.1.6",
57 "hermes-eslint": "^0.9.0",
58 "jest": "^29.0.3",
59 "jest-environment-jsdom": "^29.0.3",
compiler/forget/scripts/prettier.js new
+85
@@ -0,0 +1,85 @@
1 +/**
2 + * Copyright (c) Meta Platforms, Inc. and affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +"use strict";
8 +
9 +// Based on similar script in React
10 +// https://github.com/facebook/react/blob/main/scripts/prettier/index.js
11 +
12 +const chalk = require("chalk");
13 +const glob = require("glob");
14 +const prettier = require("prettier");
15 +const fs = require("fs");
16 +const listChangedFiles = require("./shared/list-changed-files");
17 +const prettierConfigPath = require.resolve("../.prettierrc");
18 +
19 +const mode = process.argv[2] || "check";
20 +const shouldWrite = mode === "write" || mode === "write-changed";
21 +const onlyChanged = mode === "check-changed" || mode === "write-changed";
22 +
23 +const changedFiles = onlyChanged ? listChangedFiles() : null;
24 +let didWarn = false;
25 +let didError = false;
26 +
27 +const files = glob
28 + .sync("**/*.js", {
29 + ignore: [
30 + "**/node_modules/**",
31 + "packages/demo-2021Q3/**",
32 + "packages/demo-todolist-live/**",
33 + "packages/demo-todolist-next/**",
34 + "packages/demo-todolist-playground/**",
35 + "packages/eslint-browser/**",
36 + "test262/**",
37 + ],
38 + })
39 + .filter((f) => !onlyChanged || changedFiles.has(f));
40 +
41 +if (!files.length) {
42 + return;
43 +}
44 +
45 +files.forEach((file) => {
46 + const options = prettier.resolveConfig.sync(file, {
47 + config: prettierConfigPath,
48 + });
49 + try {
50 + const input = fs.readFileSync(file, "utf8");
51 + if (shouldWrite) {
52 + const output = prettier.format(input, options);
53 + if (output !== input) {
54 + fs.writeFileSync(file, output, "utf8");
55 + }
56 + } else {
57 + if (!prettier.check(input, options)) {
58 + if (!didWarn) {
59 + console.log(
60 + "\n" +
61 + chalk.red(
62 + ` This project uses prettier to format all JavaScript code.\n`
63 + ) +
64 + chalk.dim(` Please run `) +
65 + chalk.reset("yarn prettier:all") +
66 + chalk.dim(
67 + ` and add changes to files listed below to your commit:`
68 + ) +
69 + `\n\n`
70 + );
71 + didWarn = true;
72 + }
73 + console.log(file);
74 + }
75 + }
76 + } catch (error) {
77 + didError = true;
78 + console.log("\n\n" + error.message);
79 + console.log(file);
80 + }
81 +});
82 +
83 +if (didWarn || didError) {
84 + process.exitCode = 1;
85 +}
compiler/forget/scripts/shared/list-changed-files.js new
+32
@@ -0,0 +1,32 @@
1 +/**
2 + * Copyright (c) Meta Platforms, Inc. and affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + */
7 +"use strict";
8 +
9 +const execFileSync = require("child_process").execFileSync;
10 +
11 +const exec = (command, args) => {
12 + console.log("> " + [command].concat(args).join(" "));
13 + const options = {
14 + cwd: process.cwd(),
15 + env: process.env,
16 + stdio: "pipe",
17 + encoding: "utf-8",
18 + };
19 + return execFileSync(command, args, options);
20 +};
21 +
22 +const execGitCmd = (args) => exec("git", args).trim().toString().split("\n");
23 +
24 +const listChangedFiles = () => {
25 + const mergeBase = execGitCmd(["merge-base", "HEAD", "main"]);
26 + return new Set([
27 + ...execGitCmd(["diff", "--name-only", "--diff-filter=ACMRTUB", mergeBase]),
28 + ...execGitCmd(["ls-files", "--others", "--exclude-standard"]),
29 + ]);
30 +};
31 +
32 +module.exports = listChangedFiles;
compiler/forget/yarn.lock
+9 -1
@@ -1464,6 +1464,14 @@ chalk@^2.0.0:
1464 escape-string-regexp "^1.0.5"
1465 supports-color "^5.3.0"
1466
1467 +chalk@^3.0.0:
1468 + version "3.0.0"
1469 + resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
1470 + integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
1471 + dependencies:
1472 + ansi-styles "^4.1.0"
1473 + supports-color "^7.1.0"
1474 +
1475 chalk@^4.0.0, chalk@^4.1.0:
1476 version "4.1.2"
1477 resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
@@ -2172,7 +2180,7 @@ glob-parent@^6.0.1:
2180 dependencies:
2181 is-glob "^4.0.3"
2182
2175 -glob@^7.1.3, glob@^7.1.4:
2183 +glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
2184 version "7.2.3"
2185 resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b"
2186 integrity sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==