|
| 1 | +const crypto = require("crypto"); |
| 2 | +const fs = require("fs/promises"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +const getRandomString = (length) => { |
| 6 | + return crypto.randomBytes(length).toString("hex"); |
| 7 | +}; |
| 8 | + |
| 9 | +async function main({ rootDirectory }) { |
| 10 | + const README_PATH = path.join(rootDirectory, "README.md"); |
| 11 | + const EXAMPLE_ENV_PATH = path.join(rootDirectory, ".env.example"); |
| 12 | + const ENV_PATH = path.join(rootDirectory, ".env"); |
| 13 | + const PACKAGE_JSON_PATH = path.join(rootDirectory, "package.json"); |
| 14 | + |
| 15 | + const REPLACER = "matador-template"; |
| 16 | + |
| 17 | + const DIR_NAME = path.basename(rootDirectory); |
| 18 | + const SUFFIX = getRandomString(2); |
| 19 | + |
| 20 | + const APP_NAME = (DIR_NAME + "-" + SUFFIX) |
| 21 | + // get rid of anything that's not allowed in an app name |
| 22 | + .replace(/[^a-zA-Z0-9-_]/g, "-"); |
| 23 | + |
| 24 | + const [env, packageJson, readme] = await Promise.all([ |
| 25 | + fs.readFile(EXAMPLE_ENV_PATH, "utf-8"), |
| 26 | + fs.readFile(PACKAGE_JSON_PATH, "utf-8"), |
| 27 | + fs.readFile(README_PATH, "utf-8"), |
| 28 | + ]); |
| 29 | + |
| 30 | + const newPackageJson = `${JSON.stringify( |
| 31 | + sort({ ...JSON.parse(packageJson), name: APP_NAME }), |
| 32 | + null, |
| 33 | + 2 |
| 34 | + )}\n`; |
| 35 | + |
| 36 | + await Promise.all([ |
| 37 | + fs.writeFile(ENV_PATH, newEnv), |
| 38 | + fs.writeFile(PACKAGE_JSON_PATH, newPackageJson), |
| 39 | + fs.writeFile( |
| 40 | + README_PATH, |
| 41 | + readme.replace(new RegExp(REPLACER, "g"), toLogicalID(APP_NAME)) |
| 42 | + ), |
| 43 | + fs.copyFile( |
| 44 | + path.join(rootDirectory, "remix.init", "gitignore"), |
| 45 | + path.join(rootDirectory, ".gitignore") |
| 46 | + ), |
| 47 | + ]); |
| 48 | + |
| 49 | + const newEnv = env.replace( |
| 50 | + /^SESSION_SECRET=.*$/m, |
| 51 | + `SESSION_SECRET="${getRandomString(16)}"` |
| 52 | + ); |
| 53 | + |
| 54 | + await askSetupQuestions({ rootDirectory }).catch((error) => { |
| 55 | + if (error.isTtyError) { |
| 56 | + // Prompt couldn't be rendered in the current environment |
| 57 | + } else { |
| 58 | + throw error; |
| 59 | + } |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +async function askSetupQuestions({ rootDirectory }) { |
| 64 | + const answers = await inquirer.prompt([ |
| 65 | + { |
| 66 | + name: "validate", |
| 67 | + type: "confirm", |
| 68 | + default: false, |
| 69 | + message: |
| 70 | + "Do you want to run the build/tests/etc to verify things are setup properly?", |
| 71 | + }, |
| 72 | + ]); |
| 73 | + |
| 74 | + if (answers.validate) { |
| 75 | + console.log( |
| 76 | + `Running the validate script to make sure everything was set up properly` |
| 77 | + ); |
| 78 | + execSync(`npm run validate`, { stdio: "inherit", cwd: rootDirectory }); |
| 79 | + } |
| 80 | + console.log(`✅ Project is ready! Start development with "npm run dev"`); |
| 81 | +} |
| 82 | + |
| 83 | +module.exports = main; |
0 commit comments