init-dex-template
Version:
DEX CLI tool to scaffold a new template from the Template Master repo.
113 lines (101 loc) ⢠3.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const inquirer_1 = __importDefault(require("inquirer"));
function showHelp() {
console.log(`
init-dex-template
DEX CLI tool to scaffold a new template from the Template Master repo.
------------------------------------------
š IMPORTANT:
You MUST have an up-to-date Bitbucket App Password:
ā https://bitbucket.org/account/settings/app-passwords/
------------------------------------------
Usage:
npx init-dex-template
Or install globally:
npm install -g init-dex-template
Then simply run:
init-dex-template
... you'll be prompted a few questions and you're done!
`);
}
if (process.argv.includes("-h") || process.argv.includes("--help")) {
showHelp();
process.exit(0);
}
function run(command, args, cwd) {
const result = (0, child_process_1.spawnSync)(command, args, {
cwd,
stdio: "inherit",
shell: false,
});
if (result.status !== 0) {
console.error(`\nā Command failed: ${command} ${args.join(" ")}`);
process.exit(result.status || 1);
}
}
async function askQuestions() {
const answers = await inquirer_1.default.prompt([
{
type: "input",
name: "username",
message: "What's your Bitbucket username?",
validate: (input) => !!input || "Username cannot be empty",
},
{
type: "input",
name: "target",
message: "Where should we put your new project?",
default: "my-new-project",
},
{
type: "confirm",
name: "install",
message: "Install dependencies after setup?",
default: true,
},
]);
return answers;
}
async function initProject() {
const { username, target, install } = await askQuestions();
const repo = `https://${username}@bitbucket.org/dexmanager/tpl_master.git`;
console.log(`\nCloning Template Master into ${target}...`);
run("git", ["clone", "--depth", "1", repo, target]);
const gitFolder = path_1.default.join(target, ".git");
if ((0, fs_1.existsSync)(gitFolder)) {
console.log("Removing old .git history...");
(0, fs_1.rmSync)(gitFolder, { recursive: true, force: true });
}
const packageJsonPath = path_1.default.join(target, "package.json");
if ((0, fs_1.existsSync)(packageJsonPath)) {
console.log("Resetting package.json...");
const pkg = JSON.parse((0, fs_1.readFileSync)(packageJsonPath, "utf-8"));
pkg.name = target;
pkg.version = "1.0.0";
delete pkg.repository;
delete pkg.bugs;
delete pkg.homepage;
(0, fs_1.writeFileSync)(packageJsonPath, JSON.stringify(pkg, null, 2));
}
console.log("Initializing new Git repository...");
run("git", ["init", "--initial-branch=master"], target);
run("git", ["add", "."], target);
run("git", ["commit", "-m", "chore: initial commit"], target);
if (install && (0, fs_1.existsSync)(packageJsonPath)) {
console.log("Installing dependencies...");
run("npm", ["install"], target);
}
console.log(`\nā
Project setup complete! Start coding in ${target}.\n`);
}
initProject().catch((err) => {
console.error("\nā Error initializing project:", err);
process.exit(1);
});