create-pact-toolbox-app
Version:
Create Pact Toolbox apps with one command
134 lines (131 loc) • 5.06 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __pact_toolbox_utils = __toESM(require("@pact-toolbox/utils"));
const citty = __toESM(require("citty"));
const node_fs_promises = __toESM(require("node:fs/promises"));
const pathe = __toESM(require("pathe"));
const node_url = __toESM(require("node:url"));
const glob = __toESM(require("glob"));
//#region src/index.ts
const __filename$1 = (0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href);
const __dirname$1 = (0, pathe.dirname)(__filename$1);
const main = (0, citty.defineCommand)({
meta: {
name: "create-pact-toolbox-app",
description: "Create a new Pact Toolbox app",
version: "0.0.1"
},
args: {
name: {
type: "string",
name: "name",
alias: "n",
description: "The name of the project to create"
},
template: {
type: "string",
alias: "t",
name: "template",
description: "The template to use for the new project"
},
git: {
type: "boolean",
alias: "g",
name: "git",
defaultValue: true,
description: "Initialize a git repository"
}
},
run: async ({ args }) => {
let projectName = args.name;
let template = args.template;
if (!projectName) {
const selectedName = await (0, __pact_toolbox_utils.text)({
message: "What would you like to name your project?",
placeholder: "my-pact-app",
defaultValue: "my-pact-app"
});
if ((0, __pact_toolbox_utils.isCancel)(selectedName)) {
__pact_toolbox_utils.logger.info("Operation cancelled.");
process.exit(0);
}
if (!selectedName) {
__pact_toolbox_utils.logger.info("You must provide a project name.");
process.exit(0);
}
projectName = selectedName;
}
if (!template) {
const templatesPath = (0, pathe.resolve)(__dirname$1, "..", "templates");
const availableTemplates = await (0, node_fs_promises.readdir)(templatesPath);
const selectedTemplate = await (0, __pact_toolbox_utils.select)({
message: "Which template would you like to use?",
options: availableTemplates.map((t) => ({
value: t,
label: t
}))
});
if ((0, __pact_toolbox_utils.isCancel)(selectedTemplate)) {
__pact_toolbox_utils.logger.info("Operation cancelled.");
process.exit(0);
}
template = selectedTemplate;
}
__pact_toolbox_utils.logger.info(`Creating a new Pact Toolbox app named ${projectName} with template ${template}`);
const projectPath = (0, pathe.resolve)(process.env.INIT_CWD || process.cwd(), projectName);
const templatePath = (0, pathe.resolve)(__dirname$1, "..", "templates", template);
try {
const templateFiles = await (0, glob.glob)("**/*", {
cwd: templatePath,
nodir: true,
dot: true
});
for (const file of templateFiles) {
const sourcePath = (0, pathe.join)(templatePath, file);
const destPath = (0, pathe.join)(projectPath, file);
let content = await (0, node_fs_promises.readFile)(sourcePath, "utf-8");
if (file === "package.json" || file === "README.md") content = (0, __pact_toolbox_utils.fillTemplatePlaceholders)(content, { "project-name": projectName });
await (0, __pact_toolbox_utils.writeFile)(destPath, content);
}
if (args.git) {
process.chdir(projectPath);
const s = (0, __pact_toolbox_utils.spinner)();
s.start("Initializing git repository");
await (0, __pact_toolbox_utils.execAsync)("git init", { cwd: projectPath });
await (0, __pact_toolbox_utils.execAsync)("git add .", { cwd: projectPath });
await (0, __pact_toolbox_utils.execAsync)("git commit -m 'Initial commit'", { cwd: projectPath });
s.stop("Git repository initialized");
process.chdir(process.cwd());
}
__pact_toolbox_utils.logger.success(`Successfully created ${projectName} at ${projectPath}`);
__pact_toolbox_utils.logger.info("To get started, run the following commands:");
__pact_toolbox_utils.logger.info(` cd ${projectName}`);
__pact_toolbox_utils.logger.info(" pnpm install");
__pact_toolbox_utils.logger.info(" pnpm dev");
} catch (err) {
__pact_toolbox_utils.logger.error("Error creating the project:", err);
}
}
});
(0, citty.runMain)(main);
//#endregion