UNPKG

kist

Version:

Lightweight Package Pipeline Processor with Plugin Architecture

88 lines 3.65 kB
import { __awaiter } from "tslib"; import fs from "fs/promises"; import path from "path"; import { Action } from "../../core/pipeline/Action.js"; import packageConfig from "./package.config.js"; export class PackageManagerAction extends Action { execute(options) { return __awaiter(this, void 0, void 0, function* () { const { packageJsonPath, outputDir, fields = [], customConfig = {}, } = options; if (!packageJsonPath) { throw new Error("The 'packageJsonPath' option is required."); } if (!outputDir) { throw new Error("The 'outputDir' option is required."); } const existingConfig = yield this.readPackageJson(packageJsonPath); const filteredConfig = this.filterFields(existingConfig, fields); yield this.createPackageJson(outputDir, filteredConfig, customConfig); }); } readPackageJson(packageJsonPath) { return __awaiter(this, void 0, void 0, function* () { const fullPath = path.resolve(packageJsonPath); try { const fileContent = yield fs.readFile(fullPath, "utf-8"); const parsedContent = JSON.parse(fileContent); this.logInfo(`Successfully read package.json from ${fullPath}`); return parsedContent; } catch (error) { if (error.code === "ENOENT") { throw new Error(`File not found at ${fullPath}. Please ensure the path is correct.`); } else if (error.name === "SyntaxError") { throw new Error(`Invalid JSON in ${fullPath}: ${error.message}`); } else { throw new Error(`Unexpected error while reading ${fullPath}: ${error.message}`); } } }); } filterFields(config, fields) { if (!fields.length) { return config; } const filteredConfig = {}; for (const field of fields) { if (config[field] !== undefined) { filteredConfig[field] = config[field]; } } this.logInfo(`Filtered package.json fields: ${JSON.stringify(filteredConfig, null, 2)}`); return filteredConfig; } createPackageJson(outputDir, filteredConfig, customConfig) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.join(outputDir, "package.json"); const finalConfig = Object.assign(Object.assign(Object.assign({}, packageConfig), filteredConfig), customConfig); const data = JSON.stringify(finalConfig, null, 2); try { yield this.ensureDirectoryExists(outputDir); yield fs.writeFile(filePath, data, "utf-8"); this.logInfo(`package.json successfully created at ${filePath}`); } catch (error) { this.logError("Error creating package.json", error); throw error; } }); } ensureDirectoryExists(dirPath) { return __awaiter(this, void 0, void 0, function* () { try { yield fs.mkdir(dirPath, { recursive: true }); } catch (error) { if (error.code !== "EEXIST") { throw error; } } }); } describe() { return "Reads an existing package.json, extracts selected fields, and creates a new one."; } } //# sourceMappingURL=PackageManagerAction.js.map