mili
Version:
Scaffolding with continuous control over the development of the project.
44 lines (43 loc) • 1.91 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());
});
};
import * as fs from 'fs-extra';
import * as path from 'path';
import * as schema from './schema.json';
import Ajv from 'ajv8';
const ajv = new Ajv();
const validate = ajv.compile(schema);
export 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;
});
};