@mongez/gnz
Version:
Generator Z, the next generation of scaffolding tools.
80 lines (79 loc) • 4.42 kB
JavaScript
;var fs=require('@mongez/fs'),reinforcements=require('@mongez/reinforcements'),chalk=require('chalk'),path=require('path'),pluralize=require('pluralize');require('os'),require('eslint'),require('prettier');var throwIf=require('../../utils/throw-if.js'),main=require('../../main.js'),index=require('../generate-client-service-file/index.js'),index$1=require('../generate-react-component/index.js'),template=require('./template.js');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var chalk__default=/*#__PURE__*/_interopDefault(chalk);var path__default=/*#__PURE__*/_interopDefault(path);var pluralize__default=/*#__PURE__*/_interopDefault(pluralize);const generate = async (options) => {
options.name = reinforcements.toKebabCase(options.name);
const now = Date.now();
const { saveTo } = options;
const directoryPath = saveTo + "/" + options.name;
options.appName ||= reinforcements.toKebabCase(path__default.default.basename(options.saveTo));
throwIf.throwIf(fs.directoryExists(directoryPath), chalk__default.default.redBright(options.name) +
" directory exists in " +
chalk__default.default.yellow(saveTo));
fs.ensureDirectory(saveTo + "/" + options.name);
let endpointPath = "shared/endpoint";
if (fs.fileExists(saveTo + "/utils/endpoints.ts")) {
endpointPath = `apps/${options.appName}/utils/endpoint`;
}
else if (fs.fileExists(path__default.default.resolve(saveTo, "../", "shared/endpoint.ts"))) {
endpointPath = "shared/endpoint";
}
// create service file
await main.gnz.execute(index.generateGenerateClientServiceFile.execute({
name: options.name,
saveTo: directoryPath + "/services",
endpointPath,
}));
fs.makeDirectory(directoryPath + "/utils");
// create a types.ts file
fs.putFile(directoryPath + "/utils/types.ts", "// types.ts file");
// create a locales.ts file
fs.putFile(directoryPath + "/utils/locales.ts", "// locales.ts file");
fs.makeDirectory(directoryPath + "/components");
let saveComponentsIn = "components";
if (options.usingPages) {
fs.makeDirectory(directoryPath + "/pages");
saveComponentsIn = "pages";
}
// now create the page
await main.gnz.execute(index$1.generateReactComponent.execute({
saveTo: directoryPath + "/" + saveComponentsIn,
name: options.name + "Page",
memo: true,
withIndex: true,
}));
// now create the details page
if (options.withDetailsPage) {
await main.gnz.execute(index$1.generateReactComponent.execute({
saveTo: directoryPath + "/" + saveComponentsIn,
name: pluralize__default.default(options.name, 1) + "DetailsPage",
memo: true,
withIndex: true,
}));
}
// now let's create the provider.ts file
fs.putFile(directoryPath + "/provider.ts", `import "./routes";`);
console.log(`Generating ${chalk__default.default.green("routes.ts")} file in ${chalk__default.default.yellow(directoryPath)}`);
// now let's create the routes.ts file
fs.putFile(directoryPath + "/routes.ts", await template.generateRoutesFile({
...options,
saveComponentsIn,
}));
// now check if the app has teh $app-modules.json file
// if so, then add to modules array a new object
if (options.addToAppModules &&
fs.fileExists(saveTo + `/${options.appName}-modules.json`)) {
const content = fs.getJsonFile(saveTo + `/${options.appName}-modules.json`);
content.modules.push({
entry: [`/${reinforcements.toKebabCase(options.name)}`],
name: options.name,
});
fs.putJsonFile(saveTo + `/${options.appName}-modules.json`, content);
}
// now let's update the urls.ts file
if (options.updateUrls && fs.fileExists(saveTo + "/utils/urls.ts")) {
console.log(`Updating ${chalk__default.default.green("urls.ts")} file in ${chalk__default.default.yellow(saveTo)}`);
fs.putFile(saveTo + "/utils/urls.ts", await template.updateUrlsFile({
...options,
urlsFilePath: saveTo + "/utils/urls.ts",
}));
}
console.log(`${chalk__default.default.green(options.name)} module has been generated successfully. ${chalk__default.default.gray(`(${Date.now() - now}ms)`)}`);
};exports.generate=generate;//# sourceMappingURL=generator.js.map