@backstage/cli
Version:
CLI for developing Backstage plugins and apps
84 lines (78 loc) • 3.12 kB
JavaScript
var fs = require('fs-extra');
var path = require('path');
var cliNode = require('@backstage/cli-node');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
const configArgPattern = /--config[=\s][^\s$]+/;
const noStartRoles = ["cli", "common-library"];
async function command() {
const packages = await cliNode.PackageGraph.listTargetPackages();
await Promise.all(
packages.map(async ({ dir, packageJson }) => {
const role = cliNode.PackageRoles.getRoleFromPackage(packageJson);
if (!role) {
return;
}
const roleInfo = cliNode.PackageRoles.getRoleInfo(role);
const hasStart = !noStartRoles.includes(role);
const needsPack = !(roleInfo.output.includes("bundle") || role === "cli");
const scripts = packageJson.scripts ?? {};
const startCmd = ["start"];
if (scripts.start?.includes("--check")) {
startCmd.push("--check");
}
if (scripts.start?.includes("--config")) {
startCmd.push(...scripts.start.match(configArgPattern) ?? []);
}
const buildCmd = ["build"];
if (scripts.build?.includes("--minify")) {
buildCmd.push("--minify");
}
if (scripts.build?.includes("--config")) {
buildCmd.push(...scripts.build.match(configArgPattern) ?? []);
}
const testCmd = ["test"];
if (scripts.test?.startsWith("backstage-cli test")) {
const args = scripts.test.slice("backstage-cli test".length).split(" ").filter(Boolean);
if (args.includes("--passWithNoTests")) {
args.splice(args.indexOf("--passWithNoTests"), 1);
}
testCmd.push(...args);
}
const expectedScripts = {
...hasStart && {
start: `backstage-cli package ${startCmd.join(" ")}`
},
build: `backstage-cli package ${buildCmd.join(" ")}`,
lint: "backstage-cli package lint",
test: `backstage-cli package ${testCmd.join(" ")}`,
clean: "backstage-cli package clean",
...needsPack && {
postpack: "backstage-cli package postpack",
prepack: "backstage-cli package prepack"
}
};
let changed = false;
const currentScripts = packageJson.scripts = packageJson.scripts || {};
for (const [name, value] of Object.entries(expectedScripts)) {
const currentScript = currentScripts[name];
const isMissing = !currentScript;
const isDifferent = currentScript !== value;
const isBackstageScript = currentScript?.includes("backstage-cli");
if (isMissing || isDifferent && isBackstageScript) {
changed = true;
currentScripts[name] = value;
}
}
if (changed) {
console.log(`Updating scripts for ${packageJson.name}`);
await fs__default.default.writeJson(path.resolve(dir, "package.json"), packageJson, {
spaces: 2
});
}
})
);
}
exports.command = command;
//# sourceMappingURL=packageScripts.cjs.js.map
;