@practica/create-node-app
Version:
Create Node.js app that is packed with best practices AND strive for simplicity
66 lines (65 loc) • 3.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateApp = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const execa_1 = __importDefault(require("execa"));
const error_handling_1 = require("../error-handling");
const choose_orm_1 = require("./features/choose-orm");
const choose_web_framework_1 = require("./features/choose-web-framework");
// This is where the code generation logic lives. In high-level, based on the provided option, it creates
// a folder, decides which code to generate, run the code through a templating engine and emit it to the target folder
const generateApp = async (options) => {
const targetPath = path_1.default.join(options.targetDirectory, options.appName);
await createTargetPathOrThrowIfExists(targetPath, options.overrideIfExists);
await copyAppFilesToTargetPath(targetPath);
await adjustCodeBasedOnFeatures(targetPath, options);
if (options.installDependencies) {
await installDependencies(targetPath);
}
return;
};
exports.generateApp = generateApp;
async function createTargetPathOrThrowIfExists(targetPath, overrideIfExists) {
const targetPathExists = await fs_extra_1.default.pathExists(targetPath);
if (targetPathExists) {
const isTargetDirectoryEmpty = (await fs_extra_1.default.readdir(targetPath)).length === 0;
if (!isTargetDirectoryEmpty && !overrideIfExists) {
throw new error_handling_1.AppError("directory-is-not-empty", "The target directory is not empty, if you want to override it please provide option --overrideIfExists=true or -ov=true");
}
else {
await fs_extra_1.default.rm(targetPath, { recursive: true });
await fs_extra_1.default.mkdir(targetPath, {});
}
}
}
async function copyAppFilesToTargetPath(targetPath) {
const sourceDirectory = path_1.default.join(__dirname, "../../code-templates");
await fs_extra_1.default.copy(sourceDirectory, targetPath, {
// We don't want to copy the node_modules folder since it's slow and error-prone
filter: (copyFromPath, copyToPath) => {
if (path_1.default.basename(copyFromPath) === "node_modules") {
return false;
}
else {
return true;
}
},
overwrite: true,
});
}
async function installDependencies(targetPath) {
await (0, execa_1.default)("npm", ["install"], {
cwd: targetPath,
});
await (0, execa_1.default)("npx", ["turbo", "run", "build"], {
cwd: targetPath,
});
}
async function adjustCodeBasedOnFeatures(generatedAppRoot, options) {
await (0, choose_orm_1.chooseORM)(generatedAppRoot, options);
await (0, choose_web_framework_1.chooseWebFramework)(generatedAppRoot, options);
}