kist
Version:
Package Pipeline Processor
148 lines (147 loc) • 6.86 kB
JavaScript
;
// ============================================================================
// Import
// ============================================================================
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VersionWriteAction = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const Action_1 = require("../../core/pipeline/Action");
// ============================================================================
// Classes
// ============================================================================
/**
* VersionWriteAction is a step action responsible for replacing a version
* string in one or more specified files. The version is located and replaced
* based on a specified key and pattern.
*/
class VersionWriteAction extends Action_1.Action {
// Methods
// ========================================================================
/**
* Executes the version writing action.
* @param options - The options specific to version writing, including the
* files, key, and version string or a flag to retrieve it from
* `package.json`.
* @returns A Promise that resolves when the version has been successfully
* replaced in all specified files, or rejects with an error if the
* action fails.
*/
execute(options) {
return __awaiter(this, void 0, void 0, function* () {
const files = options.files;
let version = options.version;
if (!files) {
throw new Error(`Missing required option: files.`);
}
// Retrieve version from package.json if not explicitly provided
if (!version) {
version = yield this.getVersionFromPackageJson();
if (!version) {
throw new Error(`Version is not provided in options and could not be retrieved from package.json.`);
}
}
this.logInfo(`Replacing version "${version}" in specified files.`);
try {
// Replace the version in each file
yield Promise.all(files.map(({ path: filePath, key }) => this.replaceVersionInFile(filePath, version, key || "version:")));
this.logInfo(`Version "${version}" replaced successfully in all specified files.`);
}
catch (error) {
this.logError(`Failed to replace version in one or more files.`, error);
throw error;
}
});
}
/**
* Retrieves the version string from the `package.json` file.
* @returns A promise that resolves to the version string or `undefined`
* if not found.
* @throws {Error} Throws an error if `package.json` cannot be read.
*/
getVersionFromPackageJson() {
return __awaiter(this, void 0, void 0, function* () {
try {
const packageJsonPath = path_1.default.resolve(process.cwd(), "package.json");
const packageJsonContent = yield fs_1.promises.readFile(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
const version = packageJson.version;
if (version && /^\d+\.\d+\.\d+$/.test(version)) {
this.logInfo(`Version "${version}" retrieved from package.json.`);
return version;
}
else {
this.logWarn(`Invalid or missing version in package.json.`);
return undefined;
}
}
catch (error) {
this.logError(`Error reading package.json for version retrieval.`, error);
return undefined;
}
});
}
/**
* Replaces a version string in a file for a specific key.
* This method reads the file, replaces the version for the key, and
* writes the updated content back to the file.
*
* @param filePath - The file path where the version should be replaced.
* @param version - The new version string to replace in the file.
* @param key - The key prefix that identifies the version line (e.g.,
* `version:`).
* @returns A promise that resolves when the version has been successfully
* replaced.
* @throws {Error} Throws an error if reading or writing the file fails.
*/
replaceVersionInFile(filePath, version, key) {
return __awaiter(this, void 0, void 0, function* () {
try {
const content = yield fs_1.promises.readFile(filePath, "utf8");
const lines = content.split("\n");
const updatedLines = lines.map((line) => {
const regex = new RegExp(`^\\s*${key}\\s*\\d+\\.\\d+\\.\\d+`);
if (regex.test(line)) {
return line.replace(/\d+\.\d+\.\d+$/, version);
}
return line;
});
yield fs_1.promises.writeFile(filePath, updatedLines.join("\n"), "utf8");
this.logInfo(`Version replaced in file "${filePath}" for key "${key}".`);
}
catch (error) {
throw new Error(`Error replacing version in file "${filePath}":
${error.message}`);
}
});
}
/**
* Provides a description of the action.
* @returns A string description of the action.
*/
describe() {
let description = `
Replaces a version string in one or more specified files based on
a key and pattern. Can retrieve the version from package.json or
set it manually.
`;
return description;
}
}
exports.VersionWriteAction = VersionWriteAction;
// ============================================================================
// Export
// ============================================================================
// export default VersionWriteAction;