forked from npm/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.js
More file actions
176 lines (148 loc) · 4.5 KB
/
extract.js
File metadata and controls
176 lines (148 loc) · 4.5 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
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
const pacote = require('pacote')
const tar = require('tar')
const { join, sep, dirname, posix } = require('path')
const fs = require('fs/promises')
const yaml = require('yaml')
const Transform = require('./transform')
const gh = require('./gh')
const log = require('./log')
const unpackTarball = async ({ release, cwd, dir }) => {
const strip = 1
const result = []
const dirParts = dir.split(sep)
log.verbose('tarball', release.resolved, { cwd, dir })
const extract = () =>
tar.x({
cwd,
strip: dirParts.length + strip,
transform: ({ path }) => {
result.push(path)
log.verbose(release.id, path)
return new Transform({
path,
release,
})
},
filter: (path) => {
const pathParts = path.split(posix.sep)
const prefixParts = pathParts.slice(strip, dirParts.length + strip)
return join(...prefixParts) === join(...dirParts)
},
})
await pacote.tarball.stream(`npm@${release.version}`, (stream) =>
new Promise((res, rej) => {
stream.on('end', res)
stream.on('error', rej)
stream.pipe(extract())
}))
return result
}
const getNav = async ({ path, release }) => {
const nav = await gh.getFile({ ref: release.branch, path })
const rewriteUrls = (nodes) =>
nodes?.map((n) => {
n.url = release.url + n.url
n.children = rewriteUrls(n.children)
return n
})
return {
path,
children: rewriteUrls(yaml.parse(nav.toString())),
}
}
const writeChangelog = async ({ release, nav, cwd, srcPath, contentPath }) => {
const title = 'Changelog'
const changelog = await gh.getFile({ ref: release.branch, path: srcPath })
await fs.writeFile(
join(cwd, contentPath + '.md'),
// mdx needs `>` escaped
Transform.sync(changelog.toString().replace(/([^\\])>/g, '$1\\>'), {
release,
path: contentPath,
frontmatter: {
github_path: srcPath,
title,
},
}),
'utf-8'
)
nav.children[nav.children.length - 1].children.push({
title,
url: `${release.url}/${contentPath}`,
description: 'Changelog notes for each version',
})
}
const unpackRelease = async (
release,
{ contentPath, baseNav, prerelease = false }
) => {
if (release.prerelease && !prerelease) {
log.info(`Skipping ${release.id} due to prerelease ${release.version}`)
return
}
log.info(release.id, release)
const cwd = join(contentPath, release.id)
const builtPath = join('docs', 'content')
const srcPath = join('docs', 'lib', 'content')
// this is the src dir for the docs that we link to for the edit links
release.src = await gh.pathExists(release.branch, srcPath)
?? await gh.pathExists(release.branch, builtPath)
/* istanbul ignore next */
if (!release.src) {
throw new Error(`Could not find source dir for ${release.id}`)
}
const nav = await getNav({
release,
// the nav file can also be in a few different places
path: await gh.pathExists(release.branch, join(srcPath, 'nav.yml'))
?? await gh.pathExists(release.branch, join('docs', 'nav.yml')),
})
await fs
.rm(cwd, { force: true, recursive: true })
.then(() => fs.mkdir(cwd, { recursive: true }))
const files = await unpackTarball({
release,
cwd,
dir: builtPath,
})
const dirs = ['', ...new Set(files.map((f) => dirname(f)))]
// The docs in the cli contains all the content pagess and the nav
// but no index pages. So this builts empty index pages for each
// directory with the correct frontmatter. The context is an mdx
// component which will end up showing the nav for this directory.
const indexes = await Promise.all(
dirs.map(async (dir) => {
const path = join(dir, 'index.mdx')
const navSection = dir
? nav.children.find((c) => posix.basename(c.url) === dir)
: baseNav.find((c) => posix.basename(c.url) === release.urlPrefix)
await fs.writeFile(
join(cwd, path),
Transform.sync('<Index depth="1" />\n', {
release,
path,
frontmatter: {
github_path: nav.path,
title: navSection.title,
shortName: navSection.shortName,
},
}),
'utf-8'
)
return path
})
)
await writeChangelog({
release,
nav,
cwd,
srcPath: 'CHANGELOG.md',
contentPath: posix.join('using-npm', 'changelog'),
})
log.info(release.id, `${[...files, ...indexes].length} files`)
return {
...release,
nav: nav.children,
}
}
module.exports = unpackRelease