@kcws/lintstaged-config
Version:
kamontat's lintstaged config
79 lines • 2.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rush = exports.rushOn = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const cmd_1 = require("../utils/cmd");
const ROOT = "/";
const walkDirectory = (file, cb) => {
const next = node_path_1.default.dirname(file);
if (next === ROOT)
return undefined;
const nextStat = (0, node_fs_1.statSync)(next);
if (nextStat.isDirectory()) {
const result = cb(next);
return result !== null && result !== void 0 ? result : walkDirectory(next, cb);
}
return undefined;
};
const resolveRushCommand = () => {
const rush = (0, cmd_1.getCommand)("rush");
if (rush === "rush") {
return ["node", "common/scripts/install-run-rush.js"];
}
return [rush];
};
const resolvePackageName = (files) => {
for (const file of files) {
const name = walkDirectory(file, (directory) => {
const pkg = node_path_1.default.resolve(directory, "package.json");
if ((0, node_fs_1.existsSync)(pkg)) {
const content = (0, node_fs_1.readFileSync)(pkg);
const json = JSON.parse(content.toString("utf8"));
return json.name;
}
});
if (name)
return name;
}
};
/**
* build rush command using internal command finder.
*
* @param pkg - package name that will run on
* @param cmd - rush command to execute
* @param args - additional command arguments
* @returns command string
*
* @beta
*/
const rushOn = (pkg, cmd, ...args) => {
const base = [...resolveRushCommand(), cmd];
if (pkg)
base.push("--only", pkg);
base.push(...args);
return base.join(" ");
};
exports.rushOn = rushOn;
/**
* build rush command using internal command finder.
* this will automatically resolve package to run
* using cwd and package.json file.
*
* @param cmd - rush command to execute
* @param args - additional command arguments
* @returns command string
*
* @beta
*/
const rush = (cmd, ...args) => {
return (files) => {
const pkg = resolvePackageName(files);
return (0, exports.rushOn)(pkg, cmd, ...args);
};
};
exports.rush = rush;
//# sourceMappingURL=rush.js.map