Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·127 lines (107 loc) · 4.21 KB
/
Copy pathindex.js
File metadata and controls
executable file
·127 lines (107 loc) · 4.21 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
#!/usr/bin/env node
import { argv, cwd, exit } from 'node:process'
import path from 'node:path'
import * as fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { exec } from 'node:child_process'
// Usage: npm create wiki-plugin pluginName
// The first argument will be the plugin name
const pluginName = argv[2]
// check we have been given a plugin name
if (typeof pluginName != 'string') {
console.log('Usage: npm create wiki-plugin <new-plugin-name>')
console.log('e.g. npm create wiki-plugin CoolThing')
exit(0)
}
// check for plugin name capitalization
if (!/^[A-Z]/.test(pluginName)) {
console.log('Expecting capitalised name')
console.log('e.g. CoolThing')
exit(2)
}
// Name the new plugin repo will given
const pluginRepo = `wiki-plugin-${pluginName.toLowerCase()}`
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const currentDir = cwd()
const pluginDir = path.resolve(currentDir, pluginRepo)
const dateStamp = new Date().getTime()
const randomByte = () => (((1 + Math.random()) * 0x100) | 0).toString(16).substring(1)
const randomBytes = n => [...Array(n)].map(() => randomByte()).join('')
const itemId = () => randomBytes(8)
// does directory already exist?
await fs
.stat(pluginDir)
.then(stats => {
if (stats.isDirectory()) {
console.log('Directory already exists for plug-in')
exit(1)
}
if (stats.isFile()) {
console.log('File with the same name already exists')
exit(1)
}
})
.catch(e => {
console.log(`Creating ${pluginRepo}`)
})
await fs.mkdir(pluginDir)
const templateDir = path.resolve(__dirname, 'template')
await fs.cp(templateDir, pluginDir, { recursive: true })
// List folders and files that need a dot adding
const dotFolders = ['github', 'vscode', 'zed']
const dotFiles = ['editorconfig', 'gitignore', 'npmignore', 'prettierignore', 'prettierrc.json']
;[...dotFolders, ...dotFiles].forEach(async name => {
await fs.rename(path.join(pluginDir, name), path.join(pluginDir, `.${name}`))
})
// We need to update some files to replace placeholder name with the plugin name.
// 0. a helper function (replace 'template' with plugin name)
const replacePlaceholder = async filePath => {
await fs
.readFile(filePath, { encoding: 'utf8' })
.then(source => source.replace(/Template/g, pluginName))
.then(source => source.replace(/template/g, pluginName.toLowerCase()))
.then(source => fs.writeFile(filePath, source, { encoding: 'utf8' }))
.catch(e => {
console.error(e.message)
exit(1)
})
}
// 1. client source, this also needs renaming.
await replacePlaceholder(path.join(pluginDir, 'src', 'client', 'template.js')).then(() =>
fs.rename(
path.join(pluginDir, 'src', 'client', 'template.js'),
path.join(pluginDir, 'src', 'client', `${pluginName.toLowerCase()}.js`),
),
)
// 2. client test
await replacePlaceholder(path.join(pluginDir, 'test', 'test.js'))
// 3. client build
await replacePlaceholder(path.join(pluginDir, 'scripts', 'build-client.js'))
// 4. README.md
await replacePlaceholder(path.join(pluginDir, 'README.md'))
// 5. package.json
await replacePlaceholder(path.join(pluginDir, 'package.json'))
// 6. gitignore
await replacePlaceholder(path.join(pluginDir, '.gitignore'))
// 7. about plugin package
const aboutPage = path.join(pluginDir, 'pages', 'about-template-plugin')
// first replace placeholders that are unique to the about page
await fs
.readFile(aboutPage, { encoding: 'utf8' })
.then(source => source.replace(/created/g, dateStamp))
.then(source => source.replace(/id1/g, itemId()))
.then(source => source.replace(/id2/g, itemId()))
.then(source => fs.writeFile(aboutPage, source, { encoding: 'utf8' }))
.catch(e => {
console.error(e.message)
exit(1)
})
await replacePlaceholder(aboutPage)
await fs.rename(aboutPage, path.join(pluginDir, 'pages', `about-${pluginName.toLowerCase()}-plugin`))
// install developer dependencies
console.log('Installing developer dependencies')
const devDependencies = ['@eslint/js', 'esbuild', 'eslint', 'globals', 'prettier']
const installCommand = `npm install --save-dev ${devDependencies.join(' ')}`
const installOptions = { cwd: pluginDir }
const npmProcess = exec(installCommand, installOptions)
npmProcess.stdout.pipe(process.stdout)