83 lines
2.9 KiB
JavaScript
83 lines
2.9 KiB
JavaScript
"use strict";
|
|
|
|
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
|
|
|
/**
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
// Mostly taken from a script in React
|
|
// https://github.com/facebook/react/blob/master/scripts/prettier/index.js
|
|
|
|
var chalk = require("chalk");
|
|
var glob = require("glob");
|
|
var path = require("path");
|
|
var execFileSync = require("child_process").execFileSync;
|
|
|
|
var mode = process.argv[2] || "check";
|
|
var shouldWrite = mode === "write" || mode === "write-changed";
|
|
var onlyChanged = mode === "check-changed" || mode === "write-changed";
|
|
|
|
var isWindows = process.platform === "win32";
|
|
var prettier = isWindows ? "prettier.cmd" : "prettier";
|
|
var prettierCmd = path.resolve(__dirname, "../node_modules/.bin/" + prettier);
|
|
var defaultOptions = {
|
|
"trailing-comma": "es5",
|
|
"print-width": 120
|
|
};
|
|
var config = {
|
|
default: {
|
|
patterns: ["src/**/*.js"]
|
|
},
|
|
scripts: {
|
|
patterns: ["scripts/**/*.js"]
|
|
}
|
|
};
|
|
|
|
function exec(command, args) {
|
|
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
|
|
console.log("> " + [command].concat(args).join(" "));
|
|
return execFileSync(command, args, options).toString();
|
|
}
|
|
|
|
var mergeBase = exec("git", ["merge-base", "HEAD", "master"]).trim();
|
|
var changedFiles = new Set(exec("git", ["diff", "-z", "--name-only", "--diff-filter=ACMRTUB", mergeBase]).match(/[^\0]+/g));
|
|
|
|
Object.keys(config).forEach(function (key) {
|
|
var patterns = config[key].patterns;
|
|
var options = config[key].options;
|
|
var ignore = config[key].ignore;
|
|
|
|
var globPattern = patterns.length > 1 ? "{" + patterns.join(",") + "}" : "" + patterns.join(",");
|
|
var files = glob.sync(globPattern, { ignore: ignore }).filter(function (f) {
|
|
return !onlyChanged || changedFiles.has(f);
|
|
});
|
|
|
|
if (!files.length) {
|
|
return;
|
|
}
|
|
|
|
var args = Object.keys(defaultOptions).map(function (k) {
|
|
return "--" + k + "=" + (options && options[k] || defaultOptions[k]);
|
|
});
|
|
args.push("--" + (shouldWrite ? "write" : "l"));
|
|
|
|
var result = void 0;
|
|
try {
|
|
result = exec(prettierCmd, [].concat(_toConsumableArray(args), _toConsumableArray(files)));
|
|
} catch (e) {
|
|
if (!shouldWrite) {
|
|
console.log("\n" + e.output[1] + "\n" + chalk.red(" This project uses prettier to format all JavaScript code.\n") + chalk.dim(" Please run ") + chalk.reset("yarn prettier") + chalk.dim(" and add changes to files listed above to your commit.") + "\n");
|
|
process.exit(1);
|
|
}
|
|
throw e;
|
|
}
|
|
console.log("\n" + result);
|
|
});
|
|
//# sourceMappingURL=prettier.js.map
|