/
hook.ts
64 lines (53 loc) · 2.23 KB
/
hook.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
import {Args, Errors, Flags} from '@oclif/core'
import chalk from 'chalk'
import * as fs from 'fs-extra'
import {writeFile} from 'node:fs/promises'
import {join, resolve} from 'node:path'
import {GeneratorCommand, readPJSON} from '../../generator'
import {castArray, uniq} from '../../util'
export default class GenerateHook extends GeneratorCommand<typeof GenerateHook> {
static args = {
name: Args.string({description: 'Name of hook (snake_case).', required: true}),
}
static description = 'Add a hook to an existing CLI or plugin.'
static flags = {
event: Flags.string({
default: 'init',
description: 'Event to run hook on.',
}),
force: Flags.boolean({
description: 'Overwrite existing files.',
}),
}
async run(): Promise<void> {
const packageJSON = await readPJSON(process.cwd())
if (!packageJSON) throw new Errors.CLIError('not in a project directory')
this.log(`Adding a ${chalk.dim(this.flags.event)} hook to ${packageJSON.name}!`)
const source = join(this.templatesDir, 'src', 'hook.ts.ejs')
const dest = join(process.cwd(), 'src', 'hooks', this.flags.event, `${this.args.name}.ts`)
await this.template(source, dest, {event: this.flags.event})
if (packageJSON.devDependencies?.mocha) {
const testSource = join(this.templatesDir, 'test', 'hook.test.ts.ejs')
const testDest = join(process.cwd(), 'test', 'hooks', this.flags.event, `${this.args.name}.test.ts`)
await this.template(testSource, testDest)
}
const tsConfigPath = resolve(process.cwd(), 'tsconfig.json')
const tsConfig = await fs.readJSON(tsConfigPath).catch(() => ({}))
const outDir = tsConfig.compilerOptions?.outDir ?? 'dist'
const hooks = packageJSON.oclif?.hooks ?? {}
hooks[this.flags.event] = hooks[this.flags.event]
? uniq([
...castArray<string>(hooks[this.flags.event]),
`./${outDir}/hooks/${this.flags.event}/${this.args.name}`,
]).sort()
: `./${outDir}/hooks/${this.flags.event}/${this.args.name}`
const updatedPackageJSON = {
...packageJSON,
oclif: {
...packageJSON.oclif,
hooks,
},
}
await writeFile(resolve(process.cwd(), 'package.json'), JSON.stringify(updatedPackageJSON, null, 2))
}
}