1
- import { mkdir , writeFile } from "fs/promises" ;
1
+ import { mkdir , writeFile , readFile } from "fs/promises" ;
2
2
import { parse , stringify } from "envfile" ;
3
3
import simpleGit from "simple-git" ;
4
4
import wget from "../utils/wget.js" ;
@@ -9,6 +9,20 @@ import Logger from "../utils/logger.js";
9
9
10
10
const reactionRepoRoot = "https://raw.githubusercontent.com/reactioncommerce/reaction/trunk" ;
11
11
12
+ /**
13
+ * @summary create the git instance
14
+ * @param {String } projectName - The name of the directory to create
15
+ * @returns {Object } - the git instance
16
+ */
17
+ function getGitInstance ( projectName ) {
18
+ const gitOptions = {
19
+ baseDir : `${ process . cwd ( ) } /${ projectName } ` ,
20
+ binary : "git" ,
21
+ maxConcurrentProcesses : 6
22
+ } ;
23
+ return simpleGit ( gitOptions ) ;
24
+ }
25
+
12
26
/**
13
27
* @summary create project directory
14
28
* @param {String } projectName - The name of the directory to create
@@ -68,21 +82,28 @@ async function getFileFromCore(fileName) {
68
82
/**
69
83
* @summary update dotenv file to point to local mongo
70
84
* @param {String } envData - file extracted from the reaction repo
85
+ * @param {Object } options - Any options for project creation
71
86
* @returns {String } updated env file
72
87
*/
73
- function updateEnv ( envData ) {
88
+ function updateEnv ( envData , options = { } ) {
74
89
const env = parse ( envData ) ;
75
90
env . MONGO_URL = "mongodb://localhost:27017/reaction" ;
91
+
92
+ if ( options . populate ) {
93
+ env . LOAD_SAMPLE_DATA = true ;
94
+ }
95
+
76
96
const updatedEnv = stringify ( env ) ;
77
97
return updatedEnv ;
78
98
}
79
99
80
100
/**
81
101
* @summary get files directory from core repo
82
102
* @param {String } projectName - The name of the project we are creating
103
+ * @param {Object } options - Any options for project creation
83
104
* @returns {Promise<Boolean> } True if success
84
105
*/
85
- async function getFilesFromCore ( projectName ) {
106
+ async function getFilesFromCore ( projectName , options ) {
86
107
// get files directly from repo so it's always up-to-date
87
108
const packageJson = await getFileFromCore ( "package.json" ) ;
88
109
const updatedPackageJson = await updatePackageJson ( packageJson , projectName ) ;
@@ -104,7 +125,7 @@ async function getFilesFromCore(projectName) {
104
125
await writeFile ( `${ projectName } /.nvmrc` , nvmrc ) ;
105
126
106
127
const dotenv = await getFileFromCore ( ".env.example" ) ;
107
- const updatedDotEnv = updateEnv ( dotenv ) ;
128
+ const updatedDotEnv = updateEnv ( dotenv , options ) ;
108
129
await writeFile ( `${ projectName } /.env` , updatedDotEnv ) ;
109
130
return true ;
110
131
}
@@ -116,19 +137,40 @@ async function getFilesFromCore(projectName) {
116
137
* @returns {Promise<Boolean> } true if success
117
138
*/
118
139
async function gitInitDirectory ( projectName ) {
119
- const gitOptions = {
120
- baseDir : `${ process . cwd ( ) } /${ projectName } ` ,
121
- binary : "git" ,
122
- maxConcurrentProcesses : 6
123
- } ;
124
- const git = simpleGit ( gitOptions ) ;
140
+ const git = getGitInstance ( projectName ) ;
125
141
try {
126
142
await git . init ( ) ;
127
143
} catch ( error ) {
128
144
Logger . error ( error ) ;
129
145
}
130
146
}
131
147
148
+ /**
149
+ * @summary add the sample data plugin to project
150
+ * @param {String } projectName name of the project to create
151
+ * @param {String } pluginName The plugin name
152
+ * @returns {Promise<boolean> } true for success
153
+ */
154
+ async function addSampleDataPlugin ( projectName ) {
155
+ const git = getGitInstance ( projectName ) ;
156
+ try {
157
+ await git . clone ( "git@github.com:reactioncommerce/api-plugin-sample-data.git" , "custom-packages/api-plugin-sample-data" ) ;
158
+
159
+ const pluginJsonPath = `${ projectName } /plugins.json` ;
160
+ const plugins = JSON . parse ( await readFile ( pluginJsonPath ) ) ;
161
+ plugins . sampleData = "./custom-packages/api-plugin-sample-data/index.js" ;
162
+
163
+ await writeFile ( pluginJsonPath , JSON . stringify ( plugins , null , "\t" ) ) ;
164
+
165
+ Logger . info ( "Added the sample data plugin successfully." ) ;
166
+ return true ;
167
+ } catch ( error ) {
168
+ Logger . error ( error ) ;
169
+ Logger . warn ( "Can't add the sample data plugin by error. Please add it manual." ) ;
170
+ return false ;
171
+ }
172
+ }
173
+
132
174
133
175
/**
134
176
* @summary clones projects locally from repo
@@ -148,10 +190,14 @@ export default async function createProjectApi(projectName, options) {
148
190
await getFilesFromRepo ( "/templates/api-project/" , projectName ) ;
149
191
150
192
// copy files directly from core that we want to be current
151
- await getFilesFromCore ( projectName ) ;
193
+ await getFilesFromCore ( projectName , options ) ;
152
194
153
195
// git init the new project
154
196
await gitInitDirectory ( projectName ) ;
155
197
198
+ if ( options . populate ) {
199
+ await addSampleDataPlugin ( projectName ) ;
200
+ }
201
+
156
202
Logger . success ( "Project creation complete. Change to your directory and run `npm install`" ) ;
157
203
}
0 commit comments