|
| 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 | +import packageJson from "../package.json"; |
| 10 | +import { |
| 11 | + askProjectName, |
| 12 | + failOnError, |
| 13 | + getPackageManager, |
| 14 | + installPackages, |
| 15 | +} from "./util"; |
| 16 | + |
| 17 | +const program = new Command(); |
| 18 | + |
| 19 | +let projectName: string | undefined; |
| 20 | + |
| 21 | +program |
| 22 | + .name(packageJson.name) |
| 23 | + .version(packageJson.version) |
| 24 | + .arguments("[projectName]") |
| 25 | + .action((name) => { |
| 26 | + if (typeof name === "string") { |
| 27 | + projectName = name; |
| 28 | + } |
| 29 | + }) |
| 30 | + .parse(process.argv); |
| 31 | + |
| 32 | +run().catch((err) => { |
| 33 | + console.error(err); |
| 34 | + process.exit(1); |
| 35 | +}); |
| 36 | + |
| 37 | +async function run() { |
| 38 | + const packageMangager = getPackageManager(); |
| 39 | + |
| 40 | + const templateName = "default"; |
| 41 | + const templateRoot = path.join(__dirname, "..", "templates", templateName); |
| 42 | + const templateManifestPath = path.join(templateRoot, "manifest.json"); |
| 43 | + const templateManifest: string[] = JSON.parse( |
| 44 | + await fs.promises.readFile(templateManifestPath, "utf-8") |
| 45 | + ); |
| 46 | + |
| 47 | + if (!projectName) { |
| 48 | + projectName = await askProjectName(); |
| 49 | + } |
| 50 | + |
| 51 | + console.log(); |
| 52 | + console.log(`Creating ${chalk.green(projectName)}...`); |
| 53 | + |
| 54 | + const root = path.resolve(projectName); |
| 55 | + |
| 56 | + try { |
| 57 | + await fs.promises.mkdir(root); |
| 58 | + } catch (err: any) { |
| 59 | + if (err.code === "EEXIST") { |
| 60 | + console.error(`${chalk.red(`Folder already exists: ${projectName}`)}`); |
| 61 | + } |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | + |
| 65 | + const renderTemplate = async ( |
| 66 | + localPath: string, |
| 67 | + data: Record<string, unknown> |
| 68 | + ) => { |
| 69 | + const templateFilePath = path.join(templateRoot, localPath); |
| 70 | + const templateContent = mustache.render( |
| 71 | + await fs.promises.readFile(templateFilePath, "utf-8"), |
| 72 | + data |
| 73 | + ); |
| 74 | + // Npm won't include `.gitignore` files in a package. |
| 75 | + // This allows you to add .template as a file ending |
| 76 | + // and it will be removed when rendered in the end |
| 77 | + // project. |
| 78 | + const destinationPath = path.join(root, localPath.replace(".template", "")); |
| 79 | + await fs.promises.mkdir(path.dirname(destinationPath), { |
| 80 | + recursive: true, |
| 81 | + }); |
| 82 | + await fs.promises.writeFile(destinationPath, templateContent); |
| 83 | + }; |
| 84 | + |
| 85 | + const templateData = { |
| 86 | + projectName, |
| 87 | + }; |
| 88 | + |
| 89 | + await Promise.all( |
| 90 | + templateManifest.map((path) => renderTemplate(path, templateData)) |
| 91 | + ); |
| 92 | + |
| 93 | + process.chdir(root); |
| 94 | + |
| 95 | + console.log(); |
| 96 | + console.log("Installing packages..."); |
| 97 | + console.log(); |
| 98 | + |
| 99 | + const dependencies = [ |
| 100 | + "@aws-cdk/aws-appsync-alpha", |
| 101 | + "@functionless/ast-reflection", |
| 102 | + "@functionless/language-service", |
| 103 | + "aws-cdk", |
| 104 | + "aws-cdk-lib", |
| 105 | + "aws-sdk", |
| 106 | + "constructs", |
| 107 | + "esbuild", |
| 108 | + "functionless", |
| 109 | + "typesafe-dynamodb", |
| 110 | + "typescript", |
| 111 | + ]; |
| 112 | + |
| 113 | + installPackages(packageMangager, dependencies); |
| 114 | + |
| 115 | + console.log(); |
| 116 | + console.log("Initializing git repository..."); |
| 117 | + console.log(); |
| 118 | + |
| 119 | + const gitErrorMessage = "Error initializing git repository."; |
| 120 | + |
| 121 | + failOnError( |
| 122 | + spawn.sync("git", ["init", "-q"], { |
| 123 | + stdio: "inherit", |
| 124 | + }), |
| 125 | + gitErrorMessage |
| 126 | + ); |
| 127 | + |
| 128 | + failOnError( |
| 129 | + spawn.sync("git", ["add", "."], { |
| 130 | + stdio: "inherit", |
| 131 | + }), |
| 132 | + gitErrorMessage |
| 133 | + ); |
| 134 | + |
| 135 | + failOnError( |
| 136 | + spawn.sync("git", ["commit", "-q", "-m", "initial commit"], { |
| 137 | + stdio: "inherit", |
| 138 | + }), |
| 139 | + gitErrorMessage |
| 140 | + ); |
| 141 | + |
| 142 | + console.log(chalk.green("Project ready!")); |
| 143 | + console.log(); |
| 144 | + console.log(`Run ${chalk.yellow(`cd ./${projectName}`)} to get started.`); |
| 145 | + |
| 146 | + process.exit(0); |
| 147 | +} |
0 commit comments