mili
Version:
Scaffolding with continuous control over the development of the project.
43 lines (42 loc) • 2.1 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 R from 'ramda';
import * as micromatch from 'micromatch';
import { execHandler } from './exec-handler';
import { readdeepdir } from "../util/readdeepdir";
import { TEMPLATE_CACHE_FILEPATH } from "../const";
import { nanoid } from 'nanoid';
import * as fs from 'fs-extra';
import * as path from 'path';
export function compile(cwd, templatePath, templates, base) {
return __awaiter(this, void 0, void 0, function* () {
const resource = new Map(base);
const cacheDir = path.join(TEMPLATE_CACHE_FILEPATH, nanoid());
yield fs.ensureDir(cacheDir);
yield fs.emptyDir(cacheDir);
yield fs.copy(templatePath, cacheDir);
for (const filename of yield readdeepdir(cacheDir)) {
for (const template of templates) {
if (!micromatch.isMatch(filename, template.path, { dot: true }))
continue;
let filepath = filename;
const encoding = template.encoding;
for (const handler of template.handlers) {
const subpath = yield execHandler(cwd, cacheDir, filepath, resource, R.mergeDeepLeft(handler, { options: { encoding } }));
if (subpath)
filepath = subpath;
if (filepath === '/dev/null')
break;
}
break;
}
}
});
}