-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nullndr/refactor-for-stack
Refactor for stack
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
|
||
/server/index.js | ||
/public/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "remix.init", | ||
"private": true, | ||
"main": "index.js", | ||
"dependencies": {} | ||
} |