-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathplugin-add-utils.ts
219 lines (193 loc) · 5.2 KB
/
plugin-add-utils.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import * as fs from "fs-extra"
import execa from "execa"
import _ from "lodash"
import {
readConfigFile,
lock,
getConfigPath,
getConfigStore,
} from "gatsby-core-utils"
import { transform, TransformOptions } from "@babel/core"
import BabelPluginAddPluginsToGatsbyConfig from "./plugin-babel-utils"
const addPluginToConfig = (
src: string,
srcPath: string,
{
name,
options,
key,
}: {
name: string
options: Record<string, unknown> | undefined
key: string
}
): string => {
let code
try {
const transformOptions: TransformOptions = {
plugins: [
[
BabelPluginAddPluginsToGatsbyConfig,
{
pluginOrThemeName: name,
options,
key,
},
],
],
filename: srcPath,
configFile: false,
}
// Use the Babel TS preset if we're operating on `gatsby-config.ts`
if (srcPath.endsWith(`ts`)) {
transformOptions.presets = [require.resolve(`@babel/preset-typescript`)]
}
code = transform(src, transformOptions)?.code
// Add back stripped type import, do light formatting, remove added empty module export.
// Use semicolon since Babel does that anyway, and we might as well be consistent.
if (srcPath.endsWith(`ts`)) {
code = `import type { GatsbyConfig } from "gatsby";\n\n${code}`
code = code.replace(`export {};`, ``)
code = code.replace(`export default config;`, `\nexport default config;`)
}
} catch (error) {
console.error(`Failed to transform gatsby config`, error)
}
return code
}
interface IGatsbyPluginCreateInput {
root: string
name: string
options: Record<string, unknown> | undefined
key: string
}
export const GatsbyPluginCreate = async ({
root,
name,
options,
key,
}: IGatsbyPluginCreateInput): Promise<void> => {
const release = await lock(`gatsby-config.js`)
const configSrcPath = getConfigPath(root)
const configSrc = await readConfigFile(root)
const code = addPluginToConfig(configSrc, configSrcPath, {
name,
options,
key,
})
await fs.writeFile(getConfigPath(root), code)
release()
}
const packageMangerConfigKey = `cli.packageManager`
const PACKAGE_MANAGER = getConfigStore().get(packageMangerConfigKey) || `yarn`
const getPackageNames = (
packages: Array<{ name: string; version: string }>
): Array<string> => packages.map(n => `${n.name}@${n.version}`)
const generateClientCommands = ({
packageManager,
depType,
packageNames,
}: {
packageManager: string
depType: string
packageNames: Array<string>
}): Array<string> | undefined => {
const commands: Array<string> = []
if (packageManager === `yarn`) {
commands.push(`add`)
// Needed for Yarn Workspaces and is a no-opt elsewhere.
commands.push(`-W`)
if (depType === `development`) {
commands.push(`--dev`)
}
return commands.concat(packageNames)
} else if (packageManager === `npm`) {
commands.push(`install`)
if (depType === `development`) {
commands.push(`--save-dev`)
}
return commands.concat(packageNames)
}
return undefined
}
let installs: Array<{
outsideResolve: any
outsideReject: any
resource: any
}> = []
const executeInstalls = async (root: string): Promise<void> => {
// @ts-ignore - fix me
const types = _.groupBy(installs, c => c.resource.dependencyType)
// Grab the key of the first install & delete off installs these packages
// then run intall
// when done, check again & call executeInstalls again.
// @ts-ignore - fix me
const depType = installs[0].resource.dependencyType
const packagesToInstall = types[depType]
installs = installs.filter(
// @ts-ignore - fix me
i => !packagesToInstall.some(p => i.resource.name === p.resource.name)
)
// @ts-ignore - fix me
const pkgs = packagesToInstall.map(p => p.resource)
const packageNames = getPackageNames(pkgs)
const commands = generateClientCommands({
packageNames,
depType,
packageManager: PACKAGE_MANAGER,
})
const release = await lock(`package.json`)
try {
await execa(PACKAGE_MANAGER, commands, {
cwd: root,
})
} catch (e) {
// A package failed so call the rejects
return packagesToInstall.forEach(p => {
// @ts-ignore - fix me
p.outsideReject(
JSON.stringify({
message: e.shortMessage,
installationError: `Could not install package`,
})
)
})
}
release()
// @ts-ignore - fix me
packagesToInstall.forEach(p => p.outsideResolve())
// Run again if there's still more installs.
if (installs.length > 0) {
executeInstalls(root)
}
return undefined
}
const debouncedExecute = _.debounce(executeInstalls, 25)
interface IPackageCreateInput {
root: string
name: string
}
const createInstall = async ({
root,
name,
}: IPackageCreateInput): Promise<unknown> => {
let outsideResolve
let outsideReject
const promise = new Promise((resolve, reject) => {
outsideResolve = resolve
outsideReject = reject
})
installs.push({
outsideResolve,
outsideReject,
resource: name,
})
debouncedExecute(root)
return promise
}
export const NPMPackageCreate = async ({
root,
name,
}: IPackageCreateInput): Promise<void> => {
await createInstall({ root, name })
}