@@ -2,71 +2,49 @@ const crypto = require("crypto");
2
2
const fs = require ( "fs/promises" ) ;
3
3
const path = require ( "path" ) ;
4
4
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
-
13
5
function getRandomString ( length ) {
14
6
return crypto . randomBytes ( length ) . toString ( "hex" ) ;
15
7
}
16
8
17
9
async 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 ) )
30
12
// get rid of anything that's not allowed in an app name
31
13
. replace ( / [ ^ a - z A - Z 0 - 9 - _ ] / g, "-" ) ;
32
14
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
- fs . rm ( path . join ( rootDirectory , ".github/ISSUE_TEMPLATE" ) , {
39
- recursive : true ,
40
- } ) ,
41
- fs . rm ( path . join ( rootDirectory , ".github/PULL_REQUEST_TEMPLATE.md" ) ) ,
42
- ] ) ;
43
-
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" ) ;
44
19
const newEnv = env . replace (
45
20
/ ^ S E S S I O N _ S E C R E T = .* $ / m,
46
21
`SESSION_SECRET="${ getRandomString ( 16 ) } "`
47
22
) ;
48
-
49
- const prodToml = toml . parse ( prodContent ) ;
50
- prodToml . app = prodToml . app . replace ( REPLACER , APP_NAME ) ;
51
-
52
- const newReadme = readme . replace (
53
- new RegExp ( escapeRegExp ( REPLACER ) , "g" ) ,
54
- APP_NAME
55
- ) ;
56
-
57
- const newPackageJson =
58
- JSON . stringify (
59
- sort ( { ...JSON . parse ( packageJson ) , name : APP_NAME } ) ,
60
- null ,
61
- 2
62
- ) + "\n" ;
63
-
64
- await Promise . all ( [
65
- fs . writeFile ( FLY_TOML_PATH , toml . stringify ( prodToml ) ) ,
66
- fs . writeFile ( README_PATH , newReadme ) ,
67
- fs . writeFile ( ENV_PATH , newEnv ) ,
68
- fs . writeFile ( PACKAGE_JSON_PATH , newPackageJson ) ,
69
- ] ) ;
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
+ }
70
48
71
49
console . log (
72
50
`
0 commit comments