-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathvalidate-generated-project.js
More file actions
108 lines (98 loc) · 3.56 KB
/
validate-generated-project.js
File metadata and controls
108 lines (98 loc) · 3.56 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* 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 {program, Argument} = require('commander')
const {diffArrays} = require('./utils.js')
const fs = require('fs')
const config = require('../config.js')
const path = require('path')
const validateGeneratedArtifacts = async (project) => {
try {
const generatedProjectDirPath = path.join(
process.cwd(),
config.GENERATED_PROJECTS_DIR,
project
)
const generatedArtifacts = fs.readdirSync(generatedProjectDirPath)
return new Promise((resolve, reject) => {
const missingArtifacts = diffArrays(
config.EXPECTED_GENERATED_ARTIFACTS[project] || [],
generatedArtifacts
)
if (missingArtifacts && missingArtifacts.length > 0) {
reject(
`Generated project (${project}) is missing one or more artifacts: ${missingArtifacts}`
)
} else {
resolve(`Successfully validated generated artifacts for: ${project} `)
}
})
} catch (err) {
return Promise.reject(
`Generated project (${project}) is missing one or more artifacts: ${err}`
)
}
}
const validateExtensibilityConfig = async (project, templateVersion) => {
const pkgPath = path.join(process.cwd(), config.GENERATED_PROJECTS_DIR, project, 'package.json')
const pkg = require(pkgPath)
return new Promise((resolve, reject) => {
if (
!Object.hasOwn(pkg, 'ccExtensibility') ||
!Object.hasOwn(pkg['ccExtensibility'], 'extends') ||
!Object.hasOwn(pkg['ccExtensibility'], 'overridesDir') ||
pkg['ccExtensibility'].extends !== '@salesforce/retail-react-app' ||
pkg['ccExtensibility'].overridesDir !== 'overrides'
) {
reject(`Generated project ${project} is missing extensibility config in package.json`)
}
if (templateVersion && pkg.version !== templateVersion) {
reject(
`Generated project ${project} is using an incorrect template version. Expected ${templateVersion}, but got ${pkg.version}.`
)
}
resolve(`Successfully validated extensibility config for ${project}`)
})
}
const main = async (opts) => {
const {args} = opts
const [project, templateVersion] = args
if (opts.args.length !== 1) {
console.log(program.helpInformation())
process.exit(1)
}
try {
await validateGeneratedArtifacts(project)
if (project === 'retail-app-ext') {
await validateExtensibilityConfig(project, templateVersion)
}
} catch (err) {
console.error(err)
throw err
}
}
program
.description(`Validate project generated by generator using the key <project-key>`)
.addArgument(
new Argument('<project-key>', 'project key').choices([
'retail-app-demo',
'retail-app-ext',
'retail-app-no-ext',
'retail-app-private-client'
])
)
.option('--templateVersion <templateVersion>', 'Template version used to generate the project')
// Export functions for testing
module.exports = {
validateGeneratedArtifacts,
validateExtensibilityConfig,
main
}
// Only run CLI when file is executed directly
if (require.main === module) {
program.parse(process.argv)
main(program)
}