2017-03-15 14:20:08 -07:00
|
|
|
/**
|
2022-10-18 11:19:24 -04:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2017-03-15 14:20:08 -07:00
|
|
|
*
|
2017-09-24 13:48:13 -07:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-03-15 14:20:08 -07:00
|
|
|
*/
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Based on similar script in Jest
|
2018-01-07 22:51:59 +11:00
|
|
|
// https://github.com/facebook/jest/blob/a7acc5ae519613647ff2c253dd21933d6f94b47f/scripts/prettier.js
|
2017-03-15 14:20:08 -07:00
|
|
|
|
|
|
|
|
const chalk = require('chalk');
|
|
|
|
|
const glob = require('glob');
|
2017-11-05 13:06:08 -08:00
|
|
|
const prettier = require('prettier');
|
|
|
|
|
const fs = require('fs');
|
2023-10-30 15:32:40 +00:00
|
|
|
const path = require('path');
|
2017-11-27 21:30:36 -02:00
|
|
|
const listChangedFiles = require('../shared/listChangedFiles');
|
2018-01-07 22:51:59 +11:00
|
|
|
const prettierConfigPath = require.resolve('../../.prettierrc');
|
2017-03-15 14:20:08 -07:00
|
|
|
|
2017-05-11 14:47:29 -07:00
|
|
|
const mode = process.argv[2] || 'check';
|
|
|
|
|
const shouldWrite = mode === 'write' || mode === 'write-changed';
|
|
|
|
|
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
|
|
|
|
|
|
2017-12-11 23:52:46 +08:00
|
|
|
const changedFiles = onlyChanged ? listChangedFiles() : null;
|
2017-03-15 14:20:08 -07:00
|
|
|
|
2023-10-30 15:32:40 +00:00
|
|
|
const prettierIgnoreFilePath = path.join(
|
|
|
|
|
__dirname,
|
|
|
|
|
'..',
|
|
|
|
|
'..',
|
|
|
|
|
'.prettierignore'
|
|
|
|
|
);
|
|
|
|
|
const prettierIgnore = fs.readFileSync(prettierIgnoreFilePath, {
|
|
|
|
|
encoding: 'utf8',
|
|
|
|
|
});
|
|
|
|
|
const ignoredPathsListedInPrettierIgnore = prettierIgnore
|
|
|
|
|
.toString()
|
|
|
|
|
.replace(/\r\n/g, '\n')
|
|
|
|
|
.split('\n')
|
|
|
|
|
.filter(line => !!line && !line.startsWith('#'));
|
|
|
|
|
|
|
|
|
|
const ignoredPathsListedInPrettierIgnoreInGlobFormat =
|
|
|
|
|
ignoredPathsListedInPrettierIgnore.map(ignoredPath => {
|
|
|
|
|
const existsAndDirectory =
|
|
|
|
|
fs.existsSync(ignoredPath) && fs.lstatSync(ignoredPath).isDirectory();
|
|
|
|
|
|
|
|
|
|
if (existsAndDirectory) {
|
|
|
|
|
return path.join(ignoredPath, '/**');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ignoredPath;
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-07 22:51:59 +11:00
|
|
|
const files = glob
|
2024-06-21 11:41:25 -04:00
|
|
|
.sync('**/*.{js,jsx,ts,tsx}', {
|
2021-07-01 14:39:18 -04:00
|
|
|
ignore: [
|
2024-06-21 11:41:25 -04:00
|
|
|
'**/*.d.ts',
|
2021-07-01 14:39:18 -04:00
|
|
|
'**/node_modules/**',
|
|
|
|
|
'**/cjs/**',
|
2024-06-21 11:41:25 -04:00
|
|
|
'**/dist/**',
|
|
|
|
|
'**/__snapshots__/**',
|
|
|
|
|
'packages/**/*.ts', // runtime prettier uses Flow parser
|
2023-10-30 15:32:40 +00:00
|
|
|
...ignoredPathsListedInPrettierIgnoreInGlobFormat,
|
2021-07-01 14:39:18 -04:00
|
|
|
],
|
|
|
|
|
})
|
2018-01-07 22:51:59 +11:00
|
|
|
.filter(f => !onlyChanged || changedFiles.has(f));
|
2017-05-11 14:47:29 -07:00
|
|
|
|
2018-01-07 22:51:59 +11:00
|
|
|
if (!files.length) {
|
2023-01-31 08:25:05 -05:00
|
|
|
process.exit(0);
|
2018-01-07 22:51:59 +11:00
|
|
|
}
|
2017-03-15 14:20:08 -07:00
|
|
|
|
2023-10-31 23:32:31 -04:00
|
|
|
async function main() {
|
|
|
|
|
let didWarn = false;
|
|
|
|
|
let didError = false;
|
|
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
|
files.map(async file => {
|
|
|
|
|
const options = await prettier.resolveConfig(file, {
|
|
|
|
|
config: prettierConfigPath,
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
const input = fs.readFileSync(file, 'utf8');
|
|
|
|
|
if (shouldWrite) {
|
|
|
|
|
const output = await prettier.format(input, options);
|
|
|
|
|
if (output !== input) {
|
|
|
|
|
fs.writeFileSync(file, output, 'utf8');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const isFormatted = await prettier.check(input, options);
|
|
|
|
|
if (!isFormatted) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
2017-11-05 13:06:08 -08:00
|
|
|
}
|
2023-10-31 23:32:31 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
didError = true;
|
|
|
|
|
console.log('\n\n' + error.message);
|
2018-01-07 22:51:59 +11:00
|
|
|
console.log(file);
|
2017-11-05 13:06:08 -08:00
|
|
|
}
|
2023-10-31 23:32:31 -04:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
if (didWarn || didError) {
|
|
|
|
|
process.exit(1);
|
2018-01-07 22:51:59 +11:00
|
|
|
}
|
2023-10-31 23:32:31 -04:00
|
|
|
}
|
2017-11-05 13:06:08 -08:00
|
|
|
|
2023-10-31 23:32:31 -04:00
|
|
|
main().catch(error => {
|
|
|
|
|
console.error(error);
|
2017-11-05 13:06:08 -08:00
|
|
|
process.exit(1);
|
2023-10-31 23:32:31 -04:00
|
|
|
});
|