kist
Version:
Package Pipeline Processor
86 lines (85 loc) • 3.87 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.DirectoryCreateAction = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const Action_1 = require("../../core/pipeline/Action");
// ============================================================================
// Classes
// ============================================================================
/**
* DirectoryCreatorAction ensures that specified directory structures
* exist within a base path. It creates missing directories recursively.
*/
class DirectoryCreateAction extends Action_1.Action {
/**
* Executes the directory creation action.
*
* @param options - The options specifying the base directory and
* the list of directories to create.
* @returns A Promise that resolves when all directories are created.
*/
execute(options) {
return __awaiter(this, void 0, void 0, function* () {
const { basePath, directories } = options;
if (!basePath || !directories || !Array.isArray(directories)) {
throw new Error("Invalid options: 'basePath' (string) and 'directories' (array) are required.");
}
this.logInfo(`Ensuring directory structure under base path: ${basePath}`);
try {
yield this.createDirectories(basePath, directories);
this.logInfo("All specified directories have been created successfully.");
}
catch (error) {
this.logError("Failed to create directories.", error);
throw error;
}
});
}
/**
* Ensures that directories exist under the specified base path.
*
* @param basePath - The base directory where subdirectories should be created.
* @param directories - An array of relative paths for directories to create.
* @returns A Promise that resolves when all directories exist.
*/
createDirectories(basePath, directories) {
return __awaiter(this, void 0, void 0, function* () {
for (const dir of directories) {
const dirPath = path_1.default.join(basePath, dir);
try {
yield fs_1.promises.mkdir(dirPath, { recursive: true });
this.logDebug(`Directory ensured: ${dirPath}`);
}
catch (error) {
this.logError(`Error creating directory: ${dirPath}`, error);
throw error;
}
}
});
}
/**
* Provides a description of the action.
*
* @returns A string description of the action.
*/
describe() {
return "Creates specified directory structures under a given base path.";
}
}
exports.DirectoryCreateAction = DirectoryCreateAction;