-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
devenv.ts
441 lines (396 loc) · 13.7 KB
/
devenv.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { PlainObject, isArray, isNumber, isPlainObject, isString } from "is-what"
import { PackageRequirement, Path, semver, hooks, utils } from "pkgx"
import { validatePackageRequirement } from "pkgx/hooks/usePantry.ts"
import { parse as parseYaml } from "deno/yaml/parse.ts"
import parse_pkg_str from "../prefab/parse-pkg-str.ts"
import { readLines } from "deno/io/read_lines.ts"
import { ProgrammerError } from "./error.ts"
import * as JSONC from "deno/jsonc/mod.ts"
const { useMoustaches } = hooks
export default async function(dir: Path) {
if (!dir.isDirectory()) {
throw new ProgrammerError(`not a directory: ${dir}`)
}
const constraint = new semver.Range('*')
let has_package_json = false
const pkgs: PackageRequirement[] = []
const env: Record<string, string> = {}
for await (const [path, {name, isFile, isSymlink, isDirectory}] of dir.ls()) {
if (isFile || isSymlink) {
switch (name) {
case "deno.json":
case "deno.jsonc":
await deno(path)
break
case ".nvmrc":
case ".node-version":
await version_file(path, 'nodejs.org')
break
case ".ruby-version":
await version_file(path, 'ruby-lang.org')
break
case ".python-version":
await python_version(path)
break
case ".terraform-version":
await terraform_version(path)
break
case "package.json":
await package_json(path)
break
case "action.yml":
case "action.yaml":
await github_actions(path)
break
case "Cargo.toml":
pkgs.push({project: "rust-lang.org", constraint})
await read_YAML_FM(path) //TODO use dedicated TOML section in preference
break
case "skaffold.yaml":
pkgs.push({project: "skaffold.dev", constraint})
await skaffold_yaml(path)
break
case "go.mod":
case "go.sum":
pkgs.push({project: "go.dev", constraint})
await read_YAML_FM(path)
break
case "requirements.txt":
case "pipfile":
case "pipfile.lock":
case "setup.py":
pkgs.push({project: "pip.pypa.io", constraint})
await read_YAML_FM(path)
break
case "pyproject.toml":
await pyproject(path)
break
case "Gemfile":
pkgs.push({project: "ruby-lang.org", constraint})
await read_YAML_FM(path)
break
case ".yarnrc":
pkgs.push({ project: "classic.yarnpkg.com", constraint })
await read_YAML_FM(path)
break
case "yarn.lock":
pkgs.push({ project: "yarnpkg.com", constraint })
break
case ".yarnrc.yml":
pkgs.push({ project: "yarnpkg.com", constraint })
await read_YAML_FM(path)
break
case "bun.lockb":
pkgs.push({ project: "bun.sh", constraint: new semver.Range(">=1") })
break
case "pnpm-lock.yaml":
pkgs.push({ project: "pnpm.io", constraint })
break
case "pixi.toml":
pkgs.push({ project: 'prefix.dev', constraint })
await read_YAML_FM(path)
break
case "pkgx.yml":
case "pkgx.yaml":
case ".pkgx.yml":
case ".pkgx.yaml":
await parse_well_formatted_node(await path.readYAML())
break
case "cdk.json":
pkgs.push({ project: 'aws.amazon.com/cdk', constraint })
break
case "justfile":
case "Justfile":
pkgs.push({ project: 'just.systems', constraint })
break
}
} else if (isDirectory) {
switch (name) {
case ".git":
pkgs.push({project: "git-scm.org", constraint})
break
case ".hg":
pkgs.push({project: "mercurial-scm.org", constraint})
break
case ".svn":
pkgs.push({project: "apache.org/subversion", constraint})
break
}
}
}
if (has_package_json && !pkgs.some(pkg => pkg.project === 'bun.sh') && !pkgs.some(pkg => pkg.project === 'nodejs.org')) {
pkgs.push({project: "nodejs.org", constraint})
}
return { pkgs, env }
//---------------------------------------------- parsers
async function deno(path: Path) {
pkgs.push({project: "deno.land", constraint})
const json = JSONC.parse(await path.read())
// deno-lint-ignore no-explicit-any
if (isPlainObject(json) && (json as any).pkgx) {
// deno-lint-ignore no-explicit-any
let node = (json as any).pkgx
if (isString(node) || isArray(node)) node = { dependencies: node }
await parse_well_formatted_node(node)
}
}
async function version_file(path: Path, project: string) {
let s = (await path.read()).trim()
if (s.startsWith('v')) s = s.slice(1) // v prefix has no effect but is allowed
s = `${project}@${s}`
pkgs.push(utils.pkg.parse(s))
}
async function python_version(path: Path) {
const s = (await path.read()).trim()
const lines = s.split("\n")
for (let l of lines) {
l = l.trim()
if (!l) continue // skip empty lines
if (l.startsWith('#')) continue // skip commented lines
// TODO: How to handle 'system'?
// TODO: How to handle non-bare versions like pypy3.9-7.3.11, stackless-3.7.5, etc. in pyenv install --list?
l = `python.org@${l}`
try {
pkgs.push(utils.pkg.parse(l))
break // only one thanks
} catch {
//noop pyenv sticks random shit in here
}
}
}
async function terraform_version(path: Path) {
const terraform_version = (await path.read()).trim()
const package_descriptor = `terraform.io@${terraform_version}`
pkgs.push(utils.pkg.parse(package_descriptor))
}
async function package_json(path: Path) {
const json = JSON.parse(await path.read());
let node = json?.pkgx;
if (isString(node) || isArray(node)) node = { dependencies: node }
if (!node) {
if (json?.engines) {
node = {
dependencies: {
...(json.engines.node && { 'nodejs.org': json.engines.node }),
...(json.engines.npm && { 'npmjs.com': json.engines.npm }),
...(json.engines.yarn && { 'yarnpkg.com': json.engines.yarn }),
...(json.engines.pnpm && { 'pnpm.io': json.engines.pnpm }),
},
};
}
if (json?.packageManager) { // corepack
// example: "pnpm@7.33.7+sha256.d1581d46ed10f54ff0cbdd94a2373b1f070202b0fbff29f27c2ce01460427043"
const match = json.packageManager.match(/^(?<pkg>[^@]+)@(?<version>[^+]+)/);
if (match) {
const { pkg, version } = match.groups as { pkg: string, version: string };
switch (pkg) {
case 'npm':
node = {
dependencies: {
'npmjs.com': version,
},
};
break;
case 'yarn':
node = {
dependencies: {
'yarnpkg.com': version,
},
};
break;
case 'pnpm':
node = {
dependencies: {
'pnpm.io': version,
},
};
break;
};
}
}
if (json?.volta) {
node = {
dependencies: {
...(json.volta.node && { 'nodejs.org': json.volta.node }),
...(json.volta.npm && { 'npmjs.com': json.volta.npm }),
...(json.volta.yarn && { 'yarnpkg.com': json.volta.yarn }),
...(json.volta.pnpm && { 'pnpm.io': json.volta.pnpm }),
}
}
}
}
await parse_well_formatted_node(node)
has_package_json = true
}
async function skaffold_yaml(path: Path){
const yaml = await path.readYAML()
if (!isPlainObject(yaml)) return
if (yaml.build?.local?.useDockerCLI?.toString() === true || yaml.deploy?.docker){
pkgs.push({
project: "docker.com/cli",
constraint: new semver.Range(`*`)
})
}
if (yaml.deploy?.kubectl){
pkgs.push({
project: "kubernetes.io/kubectl",
constraint: new semver.Range(`*`)
})
}
if (yaml.deploy?.kubeContext?.match("minikube")){
pkgs.push({
project: "kubernetes.io/minikube",
constraint: new semver.Range(`*`)
})
}
if (yaml.deploy?.helm || yaml.manifests?.helm){
pkgs.push({
project: "helm.sh",
constraint: new semver.Range(`*`)
})
}
if (yaml.deploy?.kpt || yaml.manifests?.kpt){
pkgs.push({
project: "kpt.dev",
constraint: new semver.Range(`*`)
})
}
if (yaml.manifests?.kustomize){
pkgs.push({
project: "kubernetes.io/kustomize",
constraint: new semver.Range(`*`)
})
}
}
async function github_actions(path: Path) {
const yaml = await path.readYAML()
if (!isPlainObject(yaml)) return
const rv = yaml.runs?.using?.match(/node(\d+)/)
if (rv?.[1]) {
pkgs.push({
project: "nodejs.org",
constraint: new semver.Range(`^${rv?.[1]}`)
})
}
await parse_well_formatted_node(yaml.pkgx)
}
async function pyproject(path: Path) {
//TODO parse the TOML lol!
const content = await path.read()
if (content.includes("poetry.core.masonry.api")) {
pkgs.push({project: "python-poetry.org", constraint})
} else {
//TODO other pkging systems…?
pkgs.push({project: "pip.pypa.io", constraint})
}
await read_YAML_FM(path)
}
//---------------------------------------------- YAML FM utils
async function read_YAML_FM(path: Path) {
//TODO be smart with knowing the comment types
// this parsing logic should be in the pantry ofc
//TODO should only parse blank lines and comments before bailing
// at the first non-comment line
//TODO should be savvy to what comment type is acceptable!
let yaml: string | undefined
const fd = await Deno.open(path.string, { read: true })
try {
for await (const line of readLines(fd)) {
if (yaml !== undefined) {
if (/^((#|\/\/)\s*)?---(\s*\*\/)?$/.test(line.trim())) {
let node = parseYaml(yaml)
/// using a `pkgx` node is safer (YAML-FM is a free-for-all) but is not required
if (isPlainObject(node) && node.pkgx) {
node = isString(node.pkgx) || isArray(node.pkgx) ? { dependencies: node.pkgx } : node.pkgx
}
return await parse_well_formatted_node(node)
}
yaml += line?.replace(/^(#|\/\/)/, '')
yaml += "\n"
} else if (/^((\/\*|#|\/\/)\s*)?---/.test(line.trim())) {
yaml = ''
}
}
} finally {
fd.close()
}
}
async function parse_well_formatted_node(obj: unknown) {
if (!isPlainObject(obj)) {
return //TODO diagnostics in verbose mode, error if `pkgx` node
}
const yaml = await extract_well_formatted_entries(obj)
for (let [k, v] of Object.entries(yaml.env)) {
if (isNumber(v)) v = v.toString()
if (isString(v)) {
//TODO provide diagnostics if verbose, throw if part of a `pkgx` node
env[k] = fix(v)
}
}
pkgs.push(...yaml.deps)
function fix(input: string): string {
const moustaches = useMoustaches()
//TODO deprecate moustaches and instead use env vars
const foo = [
//FIXME ...moustaches.tokenize.host(),
{ from: "home", to: Path.home().string }, //TODO deprecate and use $HOME once pantry is migrated
{ from: "srcroot", to: dir.string } //TODO deprecate and use $PWD once pantry is migrated
]
const out = moustaches.apply(input, foo)
_internals.validateDollarSignUsage(out)
return out
}
}
}
function validateDollarSignUsage(str: string): void {
let currentIndex = 0;
while ((currentIndex = str.indexOf('$', currentIndex)) !== -1) {
const substring = str.substring(currentIndex);
// Check for ${FOO} format
const isValidCurlyFormat = /^\$\{[A-Za-z_][A-Za-z0-9_]*\}/.test(substring);
// Check for $FOO format
const isValidDirectFormat = /^\$[A-Za-z_][A-Za-z0-9_]*/.test(substring);
if (!isValidCurlyFormat && !isValidDirectFormat) {
throw new Error("Invalid dollar sign usage detected.");
}
// Move past this $ instance
currentIndex++;
}
}
/// YAML-FM must be explicitly marked with a `dependencies` node
async function extract_well_formatted_entries(yaml: PlainObject): Promise<{ deps: PackageRequirement[], env: Record<string, unknown> }> {
const deps = await parse_deps(yaml.dependencies)
const env = isPlainObject(yaml.env) ? yaml.env : {} //TODO provide diagnostics if verbose, throw if part of a `pkgx` node
return { deps, env }
}
async function parse_deps(node: unknown) {
if (isString(node)) node = node.split(/\s+/).filter(x => x)
function parse(input: string) {
// @latest means '*' here, we refuse to always check for newer versions
// that is up to the user to initiate, however we should allow the spec since
// users expect it. Maybe we should console.warn?
// discussion: https://github.com/pkgxdev/pkgx/issues/797
if (input.endsWith('@latest')) input = input.slice(0, -6)
return utils.pkg.parse(input)
}
if (isArray(node)) node = node.map(parse).reduce((acc, curr) => {
acc[curr.project] = curr.constraint.toString()
return acc
}, {} as Record<string, string>)
if (!isPlainObject(node)) {
return [] //TODO provide diagnostics if verbose, throw if part of a `pkgx` node
}
const pkgs = Object.entries(node)
.compact(([project, constraint]) => {
// see comment above in parse() about @latest
if (/^@?latest$/.test(constraint)) constraint = '*'
return validatePackageRequirement(project, constraint)
})
.map(utils.pkg.str) //FIXME lol inefficient
.map(x => _internals.find(x))
return await Promise.all(pkgs)
}
export const _internals = {
find: parse_pkg_str,
validateDollarSignUsage
}