mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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
This commit is contained in:
Vendored
+1
-1
@@ -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
|
||||
|
||||
@@ -2,4 +2,8 @@
|
||||
**/dist
|
||||
**/__tests__/fixtures/**/*.expect.md
|
||||
**/.next
|
||||
/test262
|
||||
test262/
|
||||
*.md
|
||||
*.json
|
||||
*.css
|
||||
*.webmanifest
|
||||
@@ -1,3 +1,4 @@
|
||||
{
|
||||
"requirePragma": false
|
||||
"requirePragma": false,
|
||||
"parser": "babel-ts"
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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==
|
||||
|
||||
Reference in New Issue
Block a user