UNPKG

project-setup-validation-yaml

Version:

Validate environment variables, files and directories. Supports YAML configuration

162 lines 6.18 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateProjectDirectories = void 0; const path = __importStar(require("path")); const fs = __importStar(require("fs")); /** * * Validates whether files and directories exists, creates them if ensurePath is set to true * * Directories will be evaluated first, then files * * Example: * * ``` * validateProjectDirectories({ baseDir: __dirname, // this is optional, defaults to process.cwd() files: [ "index.ts", "src/validators/index.ts", // if exists - do nothing { path: "src/components/test.ts", // if exists do nothing, otherwise create empty file options: { ensurePath: true } }, "src/doesntexist/a.ts" // Doesn't exist, will log error and terminate ], dirs: [ "/src/validators", // if exists - do nothing { path: "src/test/test", // if exists do nothing, otherwise create empty directories recursively options: { ensurePath: true } }, "/src/doesntexist/" // Doesn't exist, will log error and terminate ] }) ``` */ function validateProjectDirectories(obj, reporter) { var _a, _b, _c, _d, _e, _f; const { baseDir = process.cwd() } = obj; const errors = { dirs: [], files: [] }; const reporterType = reporter ? 'custom' : 'console'; reporter = reporter !== null && reporter !== void 0 ? reporter : console.error.bind(console); if (obj.dirs) { for (const dir of obj.dirs) { let dirPath; if (typeof dir === 'string') { dirPath = path.join(baseDir, dir); } else { dirPath = path.join((_b = (_a = dir.options) === null || _a === void 0 ? void 0 : _a.baseDir) !== null && _b !== void 0 ? _b : baseDir, dir.path); if ((_c = dir.options) === null || _c === void 0 ? void 0 : _c.ensurePath) { // Create directory if ensurePath option is true fs.mkdirSync(dirPath, { recursive: true }); } } if (!fs.existsSync(dirPath)) { errors.dirs.push({ path: dirPath, error: `ERROR: Directory '${dirPath}' does not exist.` }); // process.exit(1); } else if (!fs.statSync(dirPath).isDirectory()) { errors.dirs.push({ path: dirPath, error: `ERROR: ${dirPath} is not a directory.` }); // process.exit(1); } } } if (errors.dirs.length) { if (reporterType === 'console') { reporter('"""""""""""""""""""""""""""""""""""""""""""""""""""""""'); errors.dirs.forEach((o) => { reporter(o.error); }); reporter('"""""""""""""""""""""""""""""""""""""""""""""""""""""""'); process.exit(1); } else { reporter(errors.dirs); } return; // early return, because some file paths might depend on dirs } if (obj.files) { for (const file of obj.files) { let filePath; if (typeof file === 'string') { filePath = path.join(baseDir, file); } else { filePath = path.join((_e = (_d = file.options) === null || _d === void 0 ? void 0 : _d.baseDir) !== null && _e !== void 0 ? _e : baseDir, file.path); if ((_f = file.options) === null || _f === void 0 ? void 0 : _f.ensurePath) { // Create empty file if ensurePath option is true fs.writeFileSync(filePath, ''); } } if (!fs.existsSync(filePath)) { errors.files.push({ path: filePath, error: `ERROR: File ${filePath} does not exist.` }); // console.error(`ERROR: File ${filePath} does not exist.`); // process.exit(1); } else if (!fs.statSync(filePath).isFile()) { errors.files.push({ path: filePath, error: `ERROR: ${filePath} is not a file.` }); // console.error(`ERROR: ${filePath} is not a file.`); // process.exit(1); } } if (errors.files.length) { if (reporterType === 'console') { reporter('"""""""""""""""""""""""""""""""""""""""""""""""""""""""'); errors.files.forEach((o) => { reporter(o.error); }); reporter('"""""""""""""""""""""""""""""""""""""""""""""""""""""""'); process.exit(1); } else { reporter(errors.files); } } } } exports.validateProjectDirectories = validateProjectDirectories; //# sourceMappingURL=projectDirectoryValidator.js.map