@@ -2,75 +2,49 @@ const crypto = require("crypto");
22const fs = require ( "fs/promises" ) ;
33const path = require ( "path" ) ;
44
5- const toml = require ( "@iarna/toml" ) ;
6- const sort = require ( "sort-package-json" ) ;
7-
8- function escapeRegExp ( string ) {
9- // $& means the whole matched string
10- return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
11- }
12-
135function getRandomString ( length ) {
146 return crypto . randomBytes ( length ) . toString ( "hex" ) ;
157}
168
179async function main ( { rootDirectory } ) {
18- const README_PATH = path . join ( rootDirectory , "README.md" ) ;
19- const FLY_TOML_PATH = path . join ( rootDirectory , "fly.toml" ) ;
20- const EXAMPLE_ENV_PATH = path . join ( rootDirectory , ".env.example" ) ;
21- const ENV_PATH = path . join ( rootDirectory , ".env" ) ;
22- const PACKAGE_JSON_PATH = path . join ( rootDirectory , "package.json" ) ;
23-
24- const REPLACER = "blues-stack-template" ;
25-
26- const DIR_NAME = path . basename ( rootDirectory ) ;
27- const SUFFIX = getRandomString ( 2 ) ;
28-
29- const APP_NAME = ( DIR_NAME + "-" + SUFFIX )
10+ const APP_NAME = path . basename ( rootDirectory ) ;
11+ const APP_ID = ( APP_NAME + "-" + getRandomString ( 2 ) )
3012 // get rid of anything that's not allowed in an app name
3113 . replace ( / [ ^ a - z A - Z 0 - 9 - _ ] / g, "-" ) ;
3214
33- const [ prodContent , readme , env , packageJson ] = await Promise . all ( [
34- fs . readFile ( FLY_TOML_PATH , "utf-8" ) ,
35- fs . readFile ( README_PATH , "utf-8" ) ,
36- fs . readFile ( EXAMPLE_ENV_PATH , "utf-8" ) ,
37- fs . readFile ( PACKAGE_JSON_PATH , "utf-8" ) ,
38- ] ) ;
39-
15+ // update env to have SESSION_SECRET
16+ const EXAMPLE_ENV_PATH = path . join ( rootDirectory , ".env.example" ) ;
17+ const ENV_PATH = path . join ( rootDirectory , ".env" ) ;
18+ const env = await fs . readFile ( EXAMPLE_ENV_PATH , "utf-8" ) ;
4019 const newEnv = env . replace (
4120 / ^ S E S S I O N _ S E C R E T = .* $ / m,
4221 `SESSION_SECRET="${ getRandomString ( 16 ) } "`
4322 ) ;
44-
45- const prodToml = toml . parse ( prodContent ) ;
46- prodToml . app = prodToml . app . replace ( REPLACER , APP_NAME ) ;
47-
48- const newReadme = readme . replace (
49- new RegExp ( escapeRegExp ( REPLACER ) , "g" ) ,
50- APP_NAME
51- ) ;
52-
53- const newPackageJson =
54- JSON . stringify (
55- sort ( { ...JSON . parse ( packageJson ) , name : APP_NAME } ) ,
56- null ,
57- 2
58- ) + "\n" ;
59-
60- await Promise . all ( [
61- fs . writeFile ( FLY_TOML_PATH , toml . stringify ( prodToml ) ) ,
62- fs . writeFile ( README_PATH , newReadme ) ,
63- fs . writeFile ( ENV_PATH , newEnv ) ,
64- fs . writeFile ( PACKAGE_JSON_PATH , newPackageJson ) ,
65- fs . copyFile (
66- path . join ( rootDirectory , "remix.init" , "gitignore" ) ,
67- path . join ( rootDirectory , ".gitignore" )
68- ) ,
69- fs . rm ( path . join ( rootDirectory , ".github/ISSUE_TEMPLATE" ) , {
70- recursive : true ,
71- } ) ,
72- fs . rm ( path . join ( rootDirectory , ".github/PULL_REQUEST_TEMPLATE.md" ) ) ,
73- ] ) ;
23+ await fs . writeFile ( ENV_PATH , newEnv ) ;
24+
25+ // delete files only needed for the template
26+ const filesToDelete = [
27+ ".github/ISSUE_TEMPLATE" ,
28+ ".github/PULL_REQUEST_TEMPLATE.md" ,
29+ ] ;
30+ for ( const file of filesToDelete ) {
31+ await fs . rm ( path . join ( rootDirectory , file ) , { recursive : true } ) ;
32+ }
33+
34+ // replace "blues-stack-template" in all files with the app name
35+ const filesToReplaceIn = [
36+ "README.md" ,
37+ "package.json" ,
38+ "fly.toml" ,
39+ "project.json" ,
40+ "nx.json" ,
41+ ] ;
42+ for ( const file of filesToReplaceIn ) {
43+ const filePath = path . join ( rootDirectory , file ) ;
44+ const contents = await fs . readFile ( filePath , "utf-8" ) ;
45+ const newContents = contents . replace ( / b l u e s - s t a c k - t e m p l a t e / g, APP_ID ) ;
46+ await fs . writeFile ( filePath , newContents ) ;
47+ }
7448
7549 console . log (
7650 `
0 commit comments