templates-mo
Version:
Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting
122 lines (121 loc) • 5.4 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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const error_exit_1 = require("../utils/error-exit");
const directoryNode_1 = require("../../fileSystemTree/directoryNode");
const fileNode_1 = require("../../fileSystemTree/fileNode");
const TPS = __importStar(require("../../utilities/constants"));
const fileSystem_1 = require("../../utilities/fileSystem");
const changes = [
{
match: 'it.component.name',
changeTo: 'tps.name',
},
{
match: 'it.component.dir',
changeTo: 'tps.dir',
},
{
match: 'it.packages',
changeTo: 'tps.packages',
},
{
match: 'it.settings',
changeTo: 'tps.answers',
},
];
const convertText = (text) => changes.reduce((acc, { match, changeTo }) => acc.replaceAll(match, changeTo), text);
exports.default = {
command: 'migrate [name]',
description: 'Migrate from create components react',
builder: {},
handler(argv) {
const name = argv.name || 'react-component';
const localTps = TPS.LOCAL_PATH;
const cwd = process.cwd();
const ccrPath = path_1.default.join(cwd, '.ccr');
const ccrTemplatesPath = path_1.default.join(ccrPath, 'templates');
if (!TPS.IS_TPS_INITIALIZED) {
(0, error_exit_1.errorExit)(new Error(`Directory must be initialized with templates. Please run 'tps init'`));
}
// check to see if .ccr exists
if (!(0, fileSystem_1.isDir)(ccrPath)) {
(0, error_exit_1.errorExit)(new Error('No create components react directory. You sure your in the right place?'));
process.exit(1);
}
const ccrTemplates = new directoryNode_1.DirectoryNode(ccrTemplatesPath, null, false);
const newTemplatePath = path_1.default.join(localTps, name);
// fs.ensureDirSync(newTemplatePath);
fs_1.default.mkdirSync(newTemplatePath, { recursive: true });
ccrTemplates.depthFirstEach((child) => {
if (ccrTemplates === child)
return;
if (child instanceof fileNode_1.FileNode) {
// eslint-disable-next-line no-underscore-dangle
const data = child._getFileData().toString();
const newFilePath = path_1.default.join(newTemplatePath, convertText(child.pathFromRoot));
console.log('Creating File: ', newFilePath);
fs_1.default.writeFileSync(newFilePath, convertText(data));
}
else {
const newDirPath = path_1.default.join(newTemplatePath, convertText(child.pathFromRoot));
console.log('Creating Dir: ', newDirPath);
fs_1.default.mkdirSync(newDirPath, { recursive: true });
}
});
const _a = (0, fileSystem_1.json)(path_1.default.join(ccrPath, 'settings.json')), { extendCwd,
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- not supported
default: defaultPackages,
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- not supported
templates } = _a, answers = __rest(_a, ["extendCwd", "default", "templates"]);
const tpsrcPath = path_1.default.join(TPS.DEFAULT_TPS, '.tpsrc');
const tpsrc = (0, fileSystem_1.json)(tpsrcPath);
const opts = {};
if (extendCwd)
opts.extendedDest = extendCwd;
tpsrc[name] = {
opts,
answers,
};
// fs.writeJSONSync(tpsrcPath, tpsrc, { EOL: '\r\n', spaces: 2 });
fs_1.default.writeFileSync(tpsrcPath, JSON.stringify(tpsrc, null, '\t'));
},
};