-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate.ts
64 lines (58 loc) 路 1.66 KB
/
generate.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
/**
* Script for generating list of packages to include in images from a
* JSON spec file and package download stats.
*
* See `../compile.sh` for example usages.
* See `/packages/*` for specs and download stats.
*/
import fs from 'fs'
import path from 'path'
for (let specFile of process.argv.slice(2)) {
specFile = path.join(__dirname, 'packages', specFile)
const spec: {
dest: string
stats: string
number: number
exclude: string[]
include: string[]
} = JSON.parse(fs.readFileSync(specFile, 'utf-8'))
const statsFile = path.join(path.dirname(specFile), spec.stats)
const destFile = path.join(path.dirname(specFile), spec.dest)
const packages = fs
.readFileSync(statsFile, 'utf-8')
.split('\n')
.slice(1)
.reduce((prev: string[], line: string) => {
const parts = line.split('\t')
const name = parts[0]
return name.length > 0 && !spec.exclude.includes(name)
? [...prev, name]
: prev
}, [])
.slice(0, spec.number - spec.include.length)
.concat(spec.include)
.reduce(
(prev: string[], curr: string) =>
prev.includes(curr) ? prev : [...prev, curr],
[]
)
.sort((a, b) => a.localeCompare(b))
if (destFile.endsWith('/DESCRIPTION')) {
fs.writeFileSync(
destFile,
fs
.readFileSync(destFile, 'utf-8')
.replace(
/Imports:(.|\n)*/m,
'Imports:\n ' + packages.join(',\n ') + '\n'
)
)
} else if (destFile.endsWith('/requirements.txt')) {
fs.writeFileSync(
destFile,
fs
.readFileSync(destFile, 'utf-8')
.replace(/\n\n(.|\n)*/m, '\n\n' + packages.join('\n') + '\n')
)
}
}