project-setup-validation-yaml
Version:
Validate environment variables, files and directories. Supports YAML configuration
146 lines • 6.3 kB
JavaScript
;
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const yaml_1 = __importDefault(require("yaml"));
const ajv_1 = __importDefault(require("ajv"));
const path_1 = __importDefault(require("path"));
const validators_1 = require("./validators");
function projectSetupWithYAML(projectSetupValidationYAMLPath) {
return new ProjectSetupWithYAML(projectSetupValidationYAMLPath);
}
exports.default = projectSetupWithYAML;
class ProjectSetupWithYAML {
constructor(projectSetupValidationYAMLPath) {
this.YAMLSchemaPath = path_1.default.join(__dirname, '/schemas/project-setup-validation.schema.yaml');
this.customVariablesYAML = {
CWD: process.cwd()
};
this.config = {
projectSetupValidationYAMLPath: "",
projectSetupValidationSchema: {},
projectSetupValidationYAMLStr: "",
projectSetupValidationYAMLJson: {},
customReporter: undefined
};
this.config.projectSetupValidationYAMLPath = projectSetupValidationYAMLPath;
if (!fs.existsSync(this.YAMLSchemaPath)) {
throw new Error("Failed to locate YAML schema. Contact package developer");
}
const schemaAsString = fs.readFileSync(this.YAMLSchemaPath, 'utf8');
this.config.projectSetupValidationSchema = yaml_1.default.parse(schemaAsString);
this.loadYAMLConfiguration(this.config.projectSetupValidationYAMLPath);
}
validate() {
this.setDefaultVariablesValues();
this.validateYAMLProjectSetupAgainstSchema();
const safeEnv = this.validateProjectSetup();
return safeEnv;
}
setCustomReporter(reporter) {
this.config.customReporter = reporter;
return this;
}
setCustomVariable(customVariableName, value) {
this.config.projectSetupValidationYAMLStr = this.config.projectSetupValidationYAMLStr
.replace(new RegExp(`%${customVariableName}%`, 'g'), value);
return this;
}
validateProjectSetup() {
const { config = {}, environment = [], files = [], dirs = [] } = this.config.projectSetupValidationYAMLJson;
// Validate Environment
const environmentToValidate = {};
for (const { name, type } of environment) {
environmentToValidate[name] = this.getValidatorFromType(type)();
}
let options = undefined;
if (this.config.customReporter) {
options = {
reporter: this.config.customReporter
};
}
const safeEnv = (0, validators_1.validateProjectEnvironment)(process.env, environmentToValidate, options);
// Validate Dirs and files
const directorySetup = {
baseDir: config.baseDir,
dirs: [],
files: []
};
for (const dir of dirs) {
directorySetup.dirs.push({
path: dir.path,
options: {
baseDir: dir.baseDir,
ensurePath: dir.ensureExists
}
});
}
for (const file of files) {
directorySetup.files.push({
path: file.path,
options: {
baseDir: file.baseDir,
ensurePath: file.ensureExists
}
});
}
(0, validators_1.validateProjectDirectories)(directorySetup, this.config.customReporter);
return safeEnv;
}
getValidatorFromType(type) {
const validatorType = type;
if (!validators_1.envalidValidationTypes[validatorType]) {
throw new Error(`Unknown environment variable type: '${type}'`);
}
return validators_1.envalidValidationTypes[validatorType];
}
validateYAMLProjectSetupAgainstSchema() {
const validate = this.compileYAMLSchema();
this.config.projectSetupValidationYAMLJson = yaml_1.default.parse(this.config.projectSetupValidationYAMLStr);
const valid = validate(this.config.projectSetupValidationYAMLJson);
if (!valid) {
throw new Error(`Error while validating '${this.config.projectSetupValidationYAMLPath}': ${validate.errors && JSON.stringify(validate.errors, null, 2)}`);
}
}
compileYAMLSchema() {
return new ajv_1.default().compile(this.config.projectSetupValidationSchema);
}
loadYAMLConfiguration(projectSetupValidationYAMLPath) {
if (!fs.existsSync(projectSetupValidationYAMLPath)) {
throw new Error(`YAML file at location '${projectSetupValidationYAMLPath}' doesn't exist`);
}
this.config.projectSetupValidationYAMLStr = fs.readFileSync(projectSetupValidationYAMLPath, 'utf8');
}
setDefaultVariablesValues() {
Object.keys(this.customVariablesYAML).forEach((varName) => {
this.config.projectSetupValidationYAMLStr = this.config.projectSetupValidationYAMLStr
.replace(new RegExp(`%${varName}%`, 'g'), this.customVariablesYAML[varName]);
});
}
}
//# sourceMappingURL=index.js.map