-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
package.js
197 lines (164 loc) · 4.62 KB
/
package.js
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
import execa from "execa"
import _ from "lodash"
import * as Joi from "@hapi/joi"
import path from "path"
import fs from "fs-extra"
import { getConfigStore } from "gatsby-core-utils"
import resolveFrom from "resolve-from"
import lock from "../lock"
import resourceSchema from "../resource-schema"
const packageMangerConfigKey = `cli.packageManager`
const PACKAGE_MANGER = getConfigStore().get(packageMangerConfigKey) || `yarn`
const readPackageJson = async (root, pkg) => {
let obj
try {
const fullPath = resolveFrom(root, path.join(pkg, `package.json`))
const contents = await fs.readFile(fullPath, `utf8`)
obj = JSON.parse(contents)
} catch (e) {
// ignore
}
return obj
}
const getPackageNames = packages => packages.map(n => `${n.name}@${n.version}`)
// Generate install commands
export const generateClientComands = ({
packageManager,
depType,
packageNames,
}) => {
const commands = []
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 = []
const executeInstalls = async root => {
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.
const depType = installs[0].resource.dependencyType
const packagesToInstall = types[depType]
installs = installs.filter(
i => !packagesToInstall.some(p => i.resource.name === p.resource.name)
)
const pkgs = packagesToInstall.map(p => p.resource)
const packageNames = getPackageNames(pkgs)
const commands = generateClientComands({
packageNames,
depType,
packageManager: PACKAGE_MANGER,
})
const release = await lock(`package.json`)
try {
await execa(PACKAGE_MANGER, commands, {
cwd: root,
})
} catch (e) {
// A package failed so call the rejects
return packagesToInstall.forEach(p => {
p.outsideReject(
JSON.stringify({
message: e.shortMessage,
installationError: `Could not install package`,
})
)
})
}
release()
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)
// Collect installs run at the same time so we can batch them.
const createInstall = async ({ root }, resource) => {
let outsideResolve
let outsideReject
const promise = new Promise((resolve, reject) => {
outsideResolve = resolve
outsideReject = reject
})
installs.push({
outsideResolve,
outsideReject,
resource,
})
debouncedExecute(root)
return promise
}
const create = async ({ root }, resource) => {
const { err, value } = validate(resource)
if (err) {
return err
}
await createInstall({ root }, value)
return read({ root }, value.name)
}
const read = async ({ root }, id) => {
const pkg = await readPackageJson(root, id)
if (pkg) {
return {
id,
name: id,
description: pkg.description,
version: pkg.version,
_message: `Installed NPM package ${id}@${pkg.version}`,
}
} else {
return undefined
}
}
const schema = {
name: Joi.string().required(),
version: Joi.string().default(`latest`, `Defaults to "latest"`),
dependencyType: Joi.string().default(
`dependency`,
`defaults to regular dependency`
),
description: Joi.string(),
...resourceSchema,
}
export const validate = resource =>
Joi.validate(resource, schema, { abortEarly: false })
const destroy = async ({ root }, resource) => {
const readResource = await read({ root }, resource.id)
if (!readResource) {
return undefined
}
await execa(`yarn`, [`remove`, resource.name, `-W`], {
cwd: root,
})
return readResource
}
export { schema, create, create as update, read, destroy }
export const config = {}
export const plan = async (context, resource) => {
const {
value: { name, version },
} = validate(resource)
const currentState = await read(context, resource.name)
return {
currentState:
currentState && `${currentState.name}@${currentState.version}`,
newState: `${name}@${version}`,
describe: `Install ${name}@${version}`,
}
}