-
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathpackage-json.ts
More file actions
108 lines (96 loc) · 3.07 KB
/
package-json.ts
File metadata and controls
108 lines (96 loc) · 3.07 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
import { render } from 'ejs'
import { formatCommand, sortObject } from './utils.js'
import { getPackageManagerExecuteCommand } from './package-manager.js'
import type { Options } from './types.js'
export function mergePackageJSON(
packageJSON: Record<string, any>,
overlayPackageJSON?: Record<string, any>,
) {
return {
...packageJSON,
...(overlayPackageJSON || {}),
dependencies: {
...packageJSON.dependencies,
...(overlayPackageJSON?.dependencies || {}),
},
devDependencies: {
...packageJSON.devDependencies,
...(overlayPackageJSON?.devDependencies || {}),
},
scripts: {
...packageJSON.scripts,
...(overlayPackageJSON?.scripts || {}),
},
}
}
export function createPackageJSON(options: Options) {
const packageManager = options.packageManager
function getPackageManagerExecuteScript(
pkg: string,
args: Array<string> = [],
) {
return formatCommand(getPackageManagerExecuteCommand(packageManager, pkg, args))
}
let packageJSON = {
...JSON.parse(JSON.stringify(options.framework.basePackageJSON)),
name: options.projectName,
}
const additions: Array<Record<string, any> | undefined> = [
options.framework.optionalPackages.typescript,
options.framework.optionalPackages.tailwindcss,
options.mode ? options.framework.optionalPackages[options.mode] : undefined,
]
for (const addition of additions.filter(Boolean)) {
packageJSON = mergePackageJSON(packageJSON, addition)
}
for (const addOn of options.chosenAddOns) {
let addOnPackageJSON = addOn.packageAdditions
// Process EJS template if present
if (addOn.packageTemplate) {
const templateValues = {
packageManager: options.packageManager,
projectName: options.projectName,
typescript: true,
tailwind: true,
js: 'ts',
jsx: 'tsx',
fileRouter: options.mode === 'file-router',
codeRouter: options.mode === 'code-router',
addOnEnabled: options.chosenAddOns.reduce<Record<string, boolean>>(
(acc, addon) => {
acc[addon.id] = true
return acc
},
{},
),
addOnOption: options.addOnOptions,
addOns: options.chosenAddOns,
getPackageManagerExecuteScript,
}
try {
const renderedTemplate = render(addOn.packageTemplate, templateValues)
addOnPackageJSON = JSON.parse(renderedTemplate)
} catch (error) {
console.error(
`Error processing package.json.ejs for add-on ${addOn.id}:`,
error,
)
// Fall back to packageAdditions if template processing fails
}
}
packageJSON = mergePackageJSON(packageJSON, addOnPackageJSON)
}
if (options.starter) {
packageJSON = mergePackageJSON(
packageJSON,
options.starter.packageAdditions,
)
}
packageJSON.dependencies = sortObject(
(packageJSON.dependencies ?? {}) as Record<string, string>,
)
packageJSON.devDependencies = sortObject(
(packageJSON.devDependencies ?? {}) as Record<string, string>,
)
return packageJSON
}