UNPKG

boats

Version:

Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.

46 lines (45 loc) 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const upath_1 = tslib_1.__importDefault(require("upath")); const dirListFilesSync_1 = require("./utils/dirListFilesSync"); const toNjk = (file) => { // ensure we don't double up the njk exts if (file.substring(file.length - 4) === '.njk') { return; } const newTarget = file + '.njk'; fs_extra_1.default.moveSync(file, newTarget); // read file to string and replace .yml with .yml.njk let string = fs_extra_1.default.readFileSync(newTarget, { encoding: 'utf8' }).toString(); const pattern = '.yml'; const regex = new RegExp(pattern, 'g'); string = string.replace(regex, '.yml.njk'); fs_extra_1.default.writeFileSync(newTarget, string, { encoding: 'utf8' }); }; const toYml = (file) => { const newTarget = file.replace('.njk', ''); fs_extra_1.default.moveSync(file, newTarget); // read file to string and replace .yml with .yml.njk let string = fs_extra_1.default.readFileSync(newTarget, { encoding: 'utf8' }).toString(); const pattern = '.yml.njk'; const regex = new RegExp(pattern, 'g'); string = string.replace(regex, '.yml'); fs_extra_1.default.writeFileSync(newTarget, string, { encoding: 'utf8' }); }; exports.default = (dir, type) => { dir = upath_1.default.join(process.cwd(), dir); console.log(`Converting to ${type}: ${dir}`); const files = (0, dirListFilesSync_1.dirListFilesSync)(dir); files.forEach((file) => { switch (type) { case 'yml': toYml(file); break; case 'njk': toNjk(file); break; } }); };