Skip to content

Commit

Permalink
Merge pull request #6 from nullndr/refactor-for-stack
Browse files Browse the repository at this point in the history
Refactor for stack
  • Loading branch information
nullndr authored May 19, 2022
2 parents 7227abb + 542b4c0 commit 4fef4c0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions remix.init/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules

/server/index.js
/public/build
83 changes: 83 additions & 0 deletions remix.init/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const crypto = require("crypto");
const fs = require("fs/promises");
const path = require("path");

const getRandomString = (length) => {
return crypto.randomBytes(length).toString("hex");
};

async function main({ rootDirectory }) {
const README_PATH = path.join(rootDirectory, "README.md");
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 = "matador-template";

const DIR_NAME = path.basename(rootDirectory);
const SUFFIX = getRandomString(2);

const APP_NAME = (DIR_NAME + "-" + SUFFIX)
// get rid of anything that's not allowed in an app name
.replace(/[^a-zA-Z0-9-_]/g, "-");

const [env, packageJson, readme] = await Promise.all([
fs.readFile(EXAMPLE_ENV_PATH, "utf-8"),
fs.readFile(PACKAGE_JSON_PATH, "utf-8"),
fs.readFile(README_PATH, "utf-8"),
]);

const newPackageJson = `${JSON.stringify(
sort({ ...JSON.parse(packageJson), name: APP_NAME }),
null,
2
)}\n`;

await Promise.all([
fs.writeFile(ENV_PATH, newEnv),
fs.writeFile(PACKAGE_JSON_PATH, newPackageJson),
fs.writeFile(
README_PATH,
readme.replace(new RegExp(REPLACER, "g"), toLogicalID(APP_NAME))
),
fs.copyFile(
path.join(rootDirectory, "remix.init", "gitignore"),
path.join(rootDirectory, ".gitignore")
),
]);

const newEnv = env.replace(
/^SESSION_SECRET=.*$/m,
`SESSION_SECRET="${getRandomString(16)}"`
);

await askSetupQuestions({ rootDirectory }).catch((error) => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
throw error;
}
});
}

async function askSetupQuestions({ rootDirectory }) {
const answers = await inquirer.prompt([
{
name: "validate",
type: "confirm",
default: false,
message:
"Do you want to run the build/tests/etc to verify things are setup properly?",
},
]);

if (answers.validate) {
console.log(
`Running the validate script to make sure everything was set up properly`
);
execSync(`npm run validate`, { stdio: "inherit", cwd: rootDirectory });
}
console.log(`✅ Project is ready! Start development with "npm run dev"`);
}

module.exports = main;
6 changes: 6 additions & 0 deletions remix.init/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "remix.init",
"private": true,
"main": "index.js",
"dependencies": {}
}

0 comments on commit 4fef4c0

Please sign in to comment.