|
| 1 | +#!/usr/bin/env node |
| 2 | +import chalk from "chalk"; |
| 3 | +import { Command } from "commander"; |
| 4 | +import spawn from "cross-spawn"; |
| 5 | +import fs from "fs"; |
| 6 | +import mustache from "mustache"; |
| 7 | +import path from "path"; |
| 8 | + |
| 9 | +const packageJson = require("../package.json"); |
| 10 | +import { loadTemplatePlan } from "./plan"; |
| 11 | +import { |
| 12 | + askProjectName, |
| 13 | + failOnError, |
| 14 | + getPackageManager, |
| 15 | + installPackages, |
| 16 | +} from "./util"; |
| 17 | + |
| 18 | +const program = new Command(); |
| 19 | + |
| 20 | +let projectName: string | undefined; |
| 21 | +let templateName = "default"; |
| 22 | + |
| 23 | +program |
| 24 | + .name(packageJson.name) |
| 25 | + .version(packageJson.version) |
| 26 | + .arguments("[projectName]") |
| 27 | + .option( |
| 28 | + "-t --template [templateName]", |
| 29 | + "name of the template to use [default]" |
| 30 | + ) |
| 31 | + .action((name, options) => { |
| 32 | + if (typeof name === "string") { |
| 33 | + projectName = name; |
| 34 | + } |
| 35 | + if (typeof options.template === "string") { |
| 36 | + templateName = options.template; |
| 37 | + } |
| 38 | + }) |
| 39 | + .parse(process.argv); |
| 40 | + |
| 41 | +run().catch((err) => { |
| 42 | + console.error(err); |
| 43 | + process.exit(1); |
| 44 | +}); |
| 45 | + |
| 46 | +async function run() { |
| 47 | + const packageMangager = getPackageManager(); |
| 48 | + |
| 49 | + const templatePlan = await loadTemplatePlan(templateName); |
| 50 | + |
| 51 | + if (!projectName) { |
| 52 | + projectName = await askProjectName(); |
| 53 | + } |
| 54 | + |
| 55 | + console.log(); |
| 56 | + console.log(`Creating ${chalk.green(projectName)}...`); |
| 57 | + |
| 58 | + const root = path.resolve(projectName); |
| 59 | + |
| 60 | + try { |
| 61 | + await fs.promises.mkdir(root); |
| 62 | + } catch (err: any) { |
| 63 | + if (err.code === "EEXIST") { |
| 64 | + console.error(`${chalk.red(`Folder already exists: ${projectName}`)}`); |
| 65 | + } |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + const renderTemplate = async ( |
| 70 | + sourcePath: string, |
| 71 | + localPath: string, |
| 72 | + data: Record<string, unknown> |
| 73 | + ) => { |
| 74 | + const templateContent = mustache.render( |
| 75 | + await fs.promises.readFile(sourcePath, "utf-8"), |
| 76 | + data |
| 77 | + ); |
| 78 | + // Npm won't include `.gitignore` files in a package. |
| 79 | + // This allows you to add .template as a file ending |
| 80 | + // and it will be removed when rendered in the end |
| 81 | + // project. |
| 82 | + const destinationPath = path.join(root, localPath.replace(".template", "")); |
| 83 | + await fs.promises.mkdir(path.dirname(destinationPath), { |
| 84 | + recursive: true, |
| 85 | + }); |
| 86 | + await fs.promises.writeFile(destinationPath, templateContent); |
| 87 | + }; |
| 88 | + |
| 89 | + const templateData = { |
| 90 | + projectName, |
| 91 | + }; |
| 92 | + |
| 93 | + for (const mapping of templatePlan.fileMappings) { |
| 94 | + // Sequential because order of file rendering matters |
| 95 | + // for templates that extend other templates. |
| 96 | + await renderTemplate(mapping.sourcePath, mapping.localPath, templateData); |
| 97 | + } |
| 98 | + |
| 99 | + process.chdir(root); |
| 100 | + |
| 101 | + console.log(); |
| 102 | + console.log("Installing packages..."); |
| 103 | + console.log(); |
| 104 | + |
| 105 | + installPackages(packageMangager, templatePlan.devDependencies); |
| 106 | + |
| 107 | + console.log(); |
| 108 | + console.log("Initializing git repository..."); |
| 109 | + console.log(); |
| 110 | + |
| 111 | + const gitErrorMessage = "Error initializing git repository."; |
| 112 | + |
| 113 | + failOnError( |
| 114 | + spawn.sync("git", ["init", "-q"], { |
| 115 | + stdio: "inherit", |
| 116 | + }), |
| 117 | + gitErrorMessage |
| 118 | + ); |
| 119 | + |
| 120 | + failOnError( |
| 121 | + spawn.sync("git", ["add", "."], { |
| 122 | + stdio: "inherit", |
| 123 | + }), |
| 124 | + gitErrorMessage |
| 125 | + ); |
| 126 | + |
| 127 | + failOnError( |
| 128 | + spawn.sync("git", ["commit", "-q", "-m", "initial commit"], { |
| 129 | + stdio: "inherit", |
| 130 | + }), |
| 131 | + gitErrorMessage |
| 132 | + ); |
| 133 | + |
| 134 | + console.log(chalk.green("Project ready!")); |
| 135 | + console.log(); |
| 136 | + console.log(`Run ${chalk.yellow(`cd ./${projectName}`)} to get started.`); |
| 137 | + |
| 138 | + process.exit(0); |
| 139 | +} |
0 commit comments