This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgenerate.ts
More file actions
64 lines (53 loc) · 2.32 KB
/
generate.ts
File metadata and controls
64 lines (53 loc) · 2.32 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
import * as path from 'path'
import { copy, emptyDir, ensureDir } from 'fs-extra'
import * as fs from 'mz/fs'
import readdir from 'recursive-readdir'
import { LanguageSpec } from '../template/src/language-specs/spec'
import { findLanguageSpecs } from './args'
async function main(): Promise<void> {
const specs = findLanguageSpecs()
await Promise.all(specs.map(spec => generate(spec)))
}
async function generate({ languageID, stylized, additionalLanguages = [] }: LanguageSpec): Promise<void> {
console.log(`Generating ${languageID} extension`)
const langDirectory = `generated-${languageID}`
const iconFilename = path.join('icons', `${languageID}.png`)
const packageFilename = path.join(langDirectory, 'package.json')
const readmeFilename = path.join(langDirectory, 'README.md')
await ensureDir(langDirectory)
await emptyDir(langDirectory)
await copy('template', langDirectory)
// Update package.json contents
const packageContents = (await fs.readFile(packageFilename)).toString()
await fs.writeFile(
packageFilename,
JSON.stringify(
{
// N.B. This needs to be first so we can overwrite default
// fields for this extension (notably "name").
...JSON.parse(packageContents),
name: languageID,
title: `${stylized} code intelligence`,
description: `Provides search-based code intelligence for ${stylized} using the Sourcegraph search API`,
activationEvents: [
`onLanguage:${languageID}`,
...additionalLanguages?.map(language => `onLanguage:${language}`),
],
icon: `data:image/png;base64,${(await fs.readFile(iconFilename)).toString('base64')}`,
},
null,
2
)
)
// Update LANG/LANGID placeholders with language name
const templateFiles = [packageFilename, readmeFilename, ...(await readdir(path.join(langDirectory, 'src')))]
for (const filename of templateFiles) {
const old = await fs.readFile(filename, 'utf8')
const new_ = old.replace(/LANG\b/g, stylized).replace(/LANGID\b/g, languageID)
await fs.writeFile(filename, new_)
}
}
main().catch(error => {
console.error(error?.message)
process.exit(1)
})