@decaf-ts/utils
Version:
module management utils for decaf-ts
87 lines • 3.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NpmTokenCommand = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const command_js_1 = require("./../command.cjs");
const modules_command_js_1 = require("./modules.command.cjs");
const help_command_js_1 = require("./help.command.cjs");
const options = {
maxTraversal: {
type: "string",
default: "2",
},
tokenFiles: {
type: "string",
multiple: true,
default: [".token", ".npmtoken"],
},
};
function normalizeList(value) {
if (Array.isArray(value)) {
return value.map((item) => `${item}`.trim()).filter(Boolean);
}
if (typeof value === "string" && value.length > 0) {
return value
.split(",")
.map((item) => item.trim())
.filter(Boolean);
}
return [];
}
class NpmTokenCommand extends command_js_1.Command {
constructor() {
super("NpmTokenCommand", options);
}
help() {
(0, help_command_js_1.printCommandHelp)(this.log, "npm-token", "Link token files into every module discovered in .gitmodules.", "npm-token [options]", [
{
flag: "--max-traversal <depth>",
description: "How many nested .gitmodules levels to traverse",
defaultValue: "2",
},
{
flag: "--token-files <files...>",
description: "Token files to link into each selected module",
defaultValue: ".token,.npmtoken",
},
{
flag: "-h, --help",
description: "Show this help text and exit",
},
], [
"The command creates symlinks from each selected module back to the token files in the repository root.",
], [
"npm-token",
"npm-token --token-files .token .npmtoken secrets/npm.token",
]);
}
async run(answers) {
const maxTraversal = Number.parseInt(`${answers.maxTraversal || "2"}`, 10);
const tokenFiles = normalizeList(answers.tokenFiles);
const effectiveTokenFiles = tokenFiles.length > 0 ? tokenFiles : [".token", ".npmtoken"];
const modules = (0, modules_command_js_1.readGitModulesDeep)(process.cwd(), Number.isFinite(maxTraversal) ? maxTraversal : 2);
for (const moduleName of modules) {
const moduleRoot = node_path_1.default.join(process.cwd(), moduleName);
try {
for (const tokenFile of effectiveTokenFiles) {
console.log(`linking ${tokenFile} to ${moduleName}`);
node_fs_1.default.rmSync(node_path_1.default.join(moduleRoot, tokenFile), {
force: true,
recursive: true,
});
node_fs_1.default.symlinkSync(node_path_1.default.join("..", tokenFile), node_path_1.default.join(moduleRoot, tokenFile));
}
}
catch {
process.exit(1);
}
}
}
}
exports.NpmTokenCommand = NpmTokenCommand;
//# sourceMappingURL=npm-token.command.js.map
//# sourceMappingURL=npm-token.command.cjs.map