Skip to content

Commit 4c998bd

Browse files
committed
Use top-level await in scripts
1 parent 41e5018 commit 4c998bd

File tree

2 files changed

+84
-94
lines changed

2 files changed

+84
-94
lines changed

script/generate-fixtures.js

+60-66
Original file line numberDiff line numberDiff line change
@@ -66,91 +66,85 @@ const otherTests = [
6666
{ name: 'Gemoji (4)', input: ':ok_hand: :hatched_chick: Two in a row' }
6767
]
6868

69-
main()
69+
const files = await fs.readdir(categoryBase)
70+
/** @type {Array<{name: string, input: string, markdownOverwrite?: string, expected?: string}>} */
71+
const tests = [...otherTests]
72+
let index = -1
7073

71-
async function main () {
72-
const files = await fs.readdir(categoryBase)
73-
/** @type {Array<{name: string, input: string, markdownOverwrite?: string, expected?: string}>} */
74-
const tests = [...otherTests]
75-
let index = -1
74+
// Create a test case with a bunch of examples.
75+
while (++index < files.length) {
76+
const name = files[index]
7677

77-
// Create a test case with a bunch of examples.
78-
while (++index < files.length) {
79-
const name = files[index]
78+
if (name === 'index.js') continue
8079

81-
if (name === 'index.js') continue
80+
// These result in Git(Hub) thinking it’s a binary file.
81+
if (name === 'Control' || name === 'Surrogate') continue
8282

83-
// These result in Git(Hub) thinking it’s a binary file.
84-
if (name === 'Control' || name === 'Surrogate') continue
83+
// This prevents GH from rendering markdown to HTML.
84+
if (name === 'Other') continue
8585

86-
// This prevents GH from rendering markdown to HTML.
87-
if (name === 'Other') continue
86+
/** @type {{default: Array<number>}} */
87+
const { default: codePoints } = await import(new URL(name + '/code-points.js', categoryBase).href)
88+
/** @type {Array<number>} */
89+
const subs = []
8890

89-
const fp = `./${name}/code-points.js`
91+
let n = -1
9092

91-
/** @type {{default: Array<number>}} */
92-
const { default: codePoints } = await import(new URL(fp, categoryBase).href)
93-
/** @type {Array<number>} */
94-
const subs = []
95-
96-
let n = -1
97-
98-
while (++n < samples) {
99-
subs.push(codePoints[Math.floor(codePoints.length / samples * n)])
100-
}
93+
while (++n < samples) {
94+
subs.push(codePoints[Math.floor(codePoints.length / samples * n)])
95+
}
10196

102-
subs.push(codePoints[codePoints.length - 1])
97+
subs.push(codePoints[codePoints.length - 1])
10398

104-
tests.push({ name, input: 'a' + [...new Set(subs)].map(d => String.fromCodePoint(d)).join(' ') + 'b' })
105-
}
99+
tests.push({ name, input: 'a' + [...new Set(subs)].map(d => String.fromCodePoint(d)).join(' ') + 'b' })
100+
}
106101

107-
// Create a Gist.
108-
const filename = 'readme.md'
109-
const gistResult = await octo.gists.create({
110-
files: {
111-
[filename]: {
112-
content: tests.map(d => {
113-
return d.markdownOverwrite || toMarkdown({ type: 'heading', depth: 1, children: [{ type: 'text', value: d.input }] }, { extensions: [gfmToMarkdown()] })
114-
}).join('\n\n')
115-
}
102+
// Create a Gist.
103+
const filename = 'readme.md'
104+
const gistResult = await octo.gists.create({
105+
files: {
106+
[filename]: {
107+
content: tests.map(d => {
108+
return d.markdownOverwrite || toMarkdown({ type: 'heading', depth: 1, children: [{ type: 'text', value: d.input }] }, { extensions: [gfmToMarkdown()] })
109+
}).join('\n\n')
116110
}
117-
})
111+
}
112+
})
118113

119-
const file = (gistResult.data.files || {})[filename]
114+
const file = (gistResult.data.files || {})[filename]
120115

121-
if (!file || !gistResult.data.html_url || !gistResult.data.id) {
122-
throw new Error('Something weird happened contacting GitHub')
123-
}
116+
if (!file || !gistResult.data.html_url || !gistResult.data.id) {
117+
throw new Error('Something weird happened contacting GitHub')
118+
}
124119

125-
if (!file.language) {
126-
throw new Error('The generated markdown was seen as binary data instead of text by GitHub. This is likely because there are weird characters (such as control characters or lone surrogates) in it')
127-
}
120+
if (!file.language) {
121+
throw new Error('The generated markdown was seen as binary data instead of text by GitHub. This is likely because there are weird characters (such as control characters or lone surrogates) in it')
122+
}
128123

129-
// Fetch the rendered page.
130-
const response = await fetch(gistResult.data.html_url, {
131-
headers: { Authorization: 'token ' + ghToken }
132-
})
124+
// Fetch the rendered page.
125+
const response = await fetch(gistResult.data.html_url, {
126+
headers: { Authorization: 'token ' + ghToken }
127+
})
133128

134-
const doc = await response.text()
129+
const doc = await response.text()
135130

136-
// Remove the Gist.
137-
await octo.gists.delete({ gist_id: gistResult.data.id })
131+
// Remove the Gist.
132+
await octo.gists.delete({ gist_id: gistResult.data.id })
138133

139-
const tree = unified().use(rehypeParse).parse(doc)
140-
const markdownBody = select('.markdown-body', tree)
134+
const tree = unified().use(rehypeParse).parse(doc)
135+
const markdownBody = select('.markdown-body', tree)
141136

142-
if (!markdownBody) {
143-
throw new Error('The generated markdown could not be rendered by GitHub as HTML. This is likely because there are weird characters in it')
144-
}
137+
if (!markdownBody) {
138+
throw new Error('The generated markdown could not be rendered by GitHub as HTML. This is likely because there are weird characters in it')
139+
}
145140

146-
const anchors = selectAll('h1 .anchor', markdownBody)
141+
const anchors = selectAll('h1 .anchor', markdownBody)
147142

148-
anchors.forEach((node, i) => {
149-
const href = (node.properties || {}).href
150-
if (typeof href === 'string') {
151-
tests[i].expected = href.slice(1)
152-
}
153-
})
143+
anchors.forEach((node, i) => {
144+
const href = (node.properties || {}).href
145+
if (typeof href === 'string') {
146+
tests[i].expected = href.slice(1)
147+
}
148+
})
154149

155-
await fs.writeFile(new URL('../test/fixtures.json', import.meta.url), JSON.stringify(tests, null, 2) + '\n')
156-
}
150+
await fs.writeFile(new URL('../test/fixtures.json', import.meta.url), JSON.stringify(tests, null, 2) + '\n')

script/generate-regex.js

+24-28
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,31 @@ const ranges = [
3030
'Separator'
3131
]
3232

33-
main()
33+
const generator = regenerate()
3434

35-
async function main () {
36-
const generator = regenerate()
35+
let index = -1
3736

38-
let index = -1
37+
// Add code points to strip.
38+
while (++index < ranges.length) {
39+
const name = ranges[index]
40+
const fp = `./${name}/code-points.js`
41+
/** @type {{default: Array<number>}} */
42+
const { default: codePoints } = await import(new URL(fp, categoryBase).href)
3943

40-
// Add code points to strip.
41-
while (++index < ranges.length) {
42-
const name = ranges[index]
43-
const fp = `./${name}/code-points.js`
44-
/** @type {{default: Array<number>}} */
45-
const { default: codePoints } = await import(new URL(fp, categoryBase).href)
46-
47-
generator.add(codePoints)
48-
}
49-
50-
generator
51-
// Some overlap between letters and Other Symbol.
52-
.remove(alphabetics)
53-
// Spaces are turned to `-`
54-
.remove(' ')
55-
// Dash is kept.
56-
.remove('-')
57-
58-
await fs.writeFile('regex.js', [
59-
'// This module is generated by `script/`.',
60-
'/* eslint-disable no-control-regex, no-misleading-character-class, no-useless-escape */',
61-
'export const regex = ' + generator.toRegExp() + 'g',
62-
''
63-
].join('\n'))
44+
generator.add(codePoints)
6445
}
46+
47+
generator
48+
// Some overlap between letters and Other Symbol.
49+
.remove(alphabetics)
50+
// Spaces are turned to `-`
51+
.remove(' ')
52+
// Dash is kept.
53+
.remove('-')
54+
55+
await fs.writeFile('regex.js', [
56+
'// This module is generated by `script/`.',
57+
'/* eslint-disable no-control-regex, no-misleading-character-class, no-useless-escape */',
58+
'export const regex = ' + generator.toRegExp() + 'g',
59+
''
60+
].join('\n'))

0 commit comments

Comments
 (0)