mili
Version:
Scaffolding with continuous control over the development of the project.
48 lines (47 loc) • 2.04 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = void 0;
const fs = require("fs-extra");
const path = require("path");
const schema = require("./schema.json");
const ajv8_1 = require("ajv8");
const ajv = new ajv8_1.default();
const validate = ajv.compile(schema);
const exec = function (cwd, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!validate(options))
throw new Error(ajv.errorsText(validate.errors));
const filepath = path.join(cwd, options.filepath);
if (!(yield fs.pathExists(filepath))) {
return {
[options.key]: {},
};
}
const content = yield fs.readFile(filepath, options.encoding || 'utf8');
const resource = {
[options.key]: {},
};
for (const section of options.sections) {
const tag = `<!-- ${section} -->`;
let beginIndex = content.indexOf(tag);
if (beginIndex === -1)
continue;
beginIndex += tag.length;
const endIndex = content.indexOf(tag, beginIndex);
if (endIndex === -1)
continue;
resource[options.key][section] = content.substring(beginIndex, endIndex);
}
return resource;
});
};
exports.exec = exec;