diff --git a/nx.json b/nx.json index 6e5744b4..655c9d1d 100644 --- a/nx.json +++ b/nx.json @@ -20,7 +20,7 @@ } }, "cli": { - "defaultProjectName": "blues-stack" + "defaultProjectName": "blues-stack-template" }, "pluginsConfig": { "@nrwl/js": { diff --git a/project.json b/project.json index 09201a3e..aa650c9b 100644 --- a/project.json +++ b/project.json @@ -1,27 +1,27 @@ { - "name": "blues-stack", + "name": "blues-stack-template", "targets": { "dev-all": { "executor": "nx:run-commands", "options": { "commands": [ { - "command": "nx dev:server blues-stack", + "command": "nx dev:server blues-stack-template", "prefix": "[SERVER]", "color": "blue" }, { - "command": "nx dev:build blues-stack", + "command": "nx dev:build blues-stack-template", "prefix": "[BUILD-]", "color": "green" }, { - "command": "nx dev:remix blues-stack", + "command": "nx dev:remix blues-stack-template", "prefix": "[REMIX-]", "color": "yellow" }, { - "command": "nx dev:css blues-stack", + "command": "nx dev:css blues-stack-template", "prefix": "[CSS---]", "color": "cyan" } diff --git a/remix.init/index.js b/remix.init/index.js index 56f718f0..5f00d477 100644 --- a/remix.init/index.js +++ b/remix.init/index.js @@ -2,75 +2,49 @@ const crypto = require("crypto"); const fs = require("fs/promises"); const path = require("path"); -const toml = require("@iarna/toml"); -const sort = require("sort-package-json"); - -function escapeRegExp(string) { - // $& means the whole matched string - return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); -} - function getRandomString(length) { return crypto.randomBytes(length).toString("hex"); } async function main({ rootDirectory }) { - const README_PATH = path.join(rootDirectory, "README.md"); - const FLY_TOML_PATH = path.join(rootDirectory, "fly.toml"); - const EXAMPLE_ENV_PATH = path.join(rootDirectory, ".env.example"); - const ENV_PATH = path.join(rootDirectory, ".env"); - const PACKAGE_JSON_PATH = path.join(rootDirectory, "package.json"); - - const REPLACER = "blues-stack-template"; - - const DIR_NAME = path.basename(rootDirectory); - const SUFFIX = getRandomString(2); - - const APP_NAME = (DIR_NAME + "-" + SUFFIX) + const APP_NAME = path.basename(rootDirectory); + const APP_ID = (APP_NAME + "-" + getRandomString(2)) // get rid of anything that's not allowed in an app name .replace(/[^a-zA-Z0-9-_]/g, "-"); - const [prodContent, readme, env, packageJson] = await Promise.all([ - fs.readFile(FLY_TOML_PATH, "utf-8"), - fs.readFile(README_PATH, "utf-8"), - fs.readFile(EXAMPLE_ENV_PATH, "utf-8"), - fs.readFile(PACKAGE_JSON_PATH, "utf-8"), - ]); - + // update env to have SESSION_SECRET + const EXAMPLE_ENV_PATH = path.join(rootDirectory, ".env.example"); + const ENV_PATH = path.join(rootDirectory, ".env"); + const env = await fs.readFile(EXAMPLE_ENV_PATH, "utf-8"); const newEnv = env.replace( /^SESSION_SECRET=.*$/m, `SESSION_SECRET="${getRandomString(16)}"` ); - - const prodToml = toml.parse(prodContent); - prodToml.app = prodToml.app.replace(REPLACER, APP_NAME); - - const newReadme = readme.replace( - new RegExp(escapeRegExp(REPLACER), "g"), - APP_NAME - ); - - const newPackageJson = - JSON.stringify( - sort({ ...JSON.parse(packageJson), name: APP_NAME }), - null, - 2 - ) + "\n"; - - await Promise.all([ - fs.writeFile(FLY_TOML_PATH, toml.stringify(prodToml)), - fs.writeFile(README_PATH, newReadme), - fs.writeFile(ENV_PATH, newEnv), - fs.writeFile(PACKAGE_JSON_PATH, newPackageJson), - fs.copyFile( - path.join(rootDirectory, "remix.init", "gitignore"), - path.join(rootDirectory, ".gitignore") - ), - fs.rm(path.join(rootDirectory, ".github/ISSUE_TEMPLATE"), { - recursive: true, - }), - fs.rm(path.join(rootDirectory, ".github/PULL_REQUEST_TEMPLATE.md")), - ]); + await fs.writeFile(ENV_PATH, newEnv); + + // delete files only needed for the template + const filesToDelete = [ + ".github/ISSUE_TEMPLATE", + ".github/PULL_REQUEST_TEMPLATE.md", + ]; + for (const file of filesToDelete) { + await fs.rm(path.join(rootDirectory, file), { recursive: true }); + } + + // replace "blues-stack-template" in all files with the app name + const filesToReplaceIn = [ + "README.md", + "package.json", + "fly.toml", + "project.json", + "nx.json", + ]; + for (const file of filesToReplaceIn) { + const filePath = path.join(rootDirectory, file); + const contents = await fs.readFile(filePath, "utf-8"); + const newContents = contents.replace(/blues-stack-template/g, APP_ID); + await fs.writeFile(filePath, newContents); + } console.log( ` diff --git a/remix.init/package.json b/remix.init/package.json index b8fe14b4..d219d9f2 100644 --- a/remix.init/package.json +++ b/remix.init/package.json @@ -3,8 +3,5 @@ "private": true, "main": "index.js", "license": "MIT", - "dependencies": { - "@iarna/toml": "^2.2.5", - "sort-package-json": "^1.55.0" - } + "dependencies": {} }