-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathgenerate-project.js
More file actions
86 lines (80 loc) · 3.09 KB
/
generate-project.js
File metadata and controls
86 lines (80 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const {runGeneratorWithResponses} = require('./execute-shell-commands.js')
const config = require('../config.js')
const {program} = require('commander')
const {mkdirIfNotExists} = require('./utils.js')
const main = async (opts) => {
const {projectKey, projectConfig} = opts
if (!projectKey && !projectConfig) {
console.error('You must provide either <project-key> or <project-config>.')
console.log(program.helpInformation())
process.exit(1)
}
try {
let cliResponses = []
let projectDir = projectKey
let preset
if (projectKey) {
cliResponses = config.CLI_RESPONSES[projectKey]
preset = config.PRESET[projectKey]
} else {
projectDir = projectConfig['projectDir']
let cliResponsesJsonArr = projectConfig['responses']
cliResponsesJsonArr.forEach((item) => {
cliResponses.push({
expectedPrompt: new RegExp(item.expectedPrompt, 'i'),
response: item.response
})
})
}
// Explicitly create outputDir because generator runs into permissions issue when generating no-ext projects.
await mkdirIfNotExists(config.GENERATED_PROJECTS_DIR)
const outputDir = `${config.GENERATED_PROJECTS_DIR}/${projectDir}`
let generateAppCommand = `${config.GENERATOR_CMD} ${outputDir}`
// TODO: Update script to setup local verdaccio npm repo to allow running 'npx @salesforce/pwa-kit-create-app' to generate apps
if (preset) {
generateAppCommand = `${config.GENERATOR_CMD} ${outputDir} --preset ${preset}`
}
return await runGeneratorWithResponses(generateAppCommand, cliResponses)
} catch (err) {
// Generator failed to create project
console.error('Generator failed to create project', err)
process.exit(1)
}
}
// Define the program with description and arguments
program
.description(
'Generate a retail-react-app project using the key <project-key> or the JSON <project-config>'
)
.option('--project-key <key>', 'Project key', (value) => {
const validKeys = [
'retail-app-demo',
'retail-app-ext',
'retail-app-no-ext',
'retail-app-private-client',
'retail-react-app-bug-bounty',
'retail-react-app-demo-site'
]
if (!validKeys.includes(value)) {
throw new Error('Invalid project key.')
}
return value
})
.option('--project-config <config>', 'Project config as JSON string', (value) => {
try {
return JSON.parse(value)
} catch (e) {
throw new Error('Invalid JSON string.')
}
})
.action((options) => {
// Call the main function with parsed options
main(options)
})
program.parse(process.argv)