Skip to content

Commit

Permalink
fix remix init
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed May 9, 2022
1 parent 5453ed7 commit 53e3dec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 67 deletions.
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"cli": {
"defaultProjectName": "blues-stack"
"defaultProjectName": "blues-stack-template"
},
"pluginsConfig": {
"@nrwl/js": {
Expand Down
10 changes: 5 additions & 5 deletions project.json
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down
88 changes: 31 additions & 57 deletions remix.init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`
Expand Down
5 changes: 1 addition & 4 deletions remix.init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@
"private": true,
"main": "index.js",
"license": "MIT",
"dependencies": {
"@iarna/toml": "^2.2.5",
"sort-package-json": "^1.55.0"
}
"dependencies": {}
}

0 comments on commit 53e3dec

Please sign in to comment.