-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
executable file
·138 lines (116 loc) · 4.28 KB
/
cli.ts
File metadata and controls
executable file
·138 lines (116 loc) · 4.28 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
#!/usr/bin/env bun
import { join } from 'node:path'
import { createCLI } from '@rubriclab/cli'
import { $ } from 'bun'
import { z } from 'zod/v4'
const cli = createCLI({
commands: [
{
args: z.object({}),
description: 'Prepares a submodule',
handler: async () => {
// Skip in CI environments
if (process.env.CI) {
console.log('CI detected, skipping prepare')
return
}
try {
const gitDir = (await $`git rev-parse --git-dir`.text()).replace('\n', '')
try {
await $`rm -r ${gitDir}/hooks`
} catch (_e) {
console.log('Hooks already removed')
}
await $`mkdir -p ${gitDir}/hooks`
await $`git config core.hooksPath ${gitDir}/hooks`
await $`bun x simple-git-hooks`
console.log('Git hooks setup complete')
} catch (error) {
console.log(
'Git hooks setup failed (this may be fine):',
error.stderr?.toString() || error.message
)
}
},
name: 'prepare'
},
{
args: z.object({
'working-dir': z.string().optional().describe('Working directory for the package (defaults to current directory)')
}),
description: 'Runs the post-commit hook',
handler: async (args) => {
if (process.env.AMENDING) {
process.exit(0)
}
// Use specified working directory or current working directory
const workingDir = args['working-dir'] || process.cwd()
const CHANGELOG_PATH = join(workingDir, 'CHANGELOG.md')
const PACKAGE_PATH = join(workingDir, 'package.json')
const packageJsonContent = Bun.file(PACKAGE_PATH)
const packageJson = await packageJsonContent.json()
console.log({ packageJson })
const versionParts = packageJson.version.split('.').map(Number)
versionParts[2] += 1
packageJson.version = versionParts.join('.')
await Bun.write(PACKAGE_PATH, `${JSON.stringify(packageJson, null, 2)}\n`)
const commitHash = (await $`git rev-parse HEAD`).text().trim()
const commitMessage = (await $`git log -1 --pretty=%B`).text().trim()
const commitDate = (await $`git log -1 --pretty=%cd --date=short`).text().trim()
let repositoryUrl = (await $`git config --get remote.origin.url`).text().trim()
if (repositoryUrl.endsWith('.git')) {
repositoryUrl = repositoryUrl.slice(0, -4)
}
if (repositoryUrl.startsWith('git@')) {
repositoryUrl = repositoryUrl.replace('git@', 'https://').replace(':', '/')
}
const commitUrl = `${repositoryUrl}/commit/${commitHash}`
const changelogEntry = `- [${commitDate}] [${commitMessage}](${commitUrl})\n`
let changelogContent = ''
const changelog = Bun.file(CHANGELOG_PATH)
const changelogExists = await changelog.exists()
if (changelogExists) {
changelogContent = await changelog.text()
} else {
changelogContent = '# Changelog\n\n'
}
await Bun.write(CHANGELOG_PATH, `${changelogEntry}${changelogContent}`)
await $`bun x biome format --write ./package.json`
await $`git add package.json CHANGELOG.md`
process.env.AMENDING = 'true'
await $`git commit --amend --no-verify --no-edit`
process.env.AMENDING = undefined
},
name: 'post-commit'
},
{
args: z.object({}),
description: 'Sets up a new package',
handler: async () => {
const projectRoot = process.cwd()
const pkgJsonPath = join(projectRoot, 'package.json')
const pkgJson = await Bun.file(pkgJsonPath).json()
pkgJson['simple-git-hooks'] = { 'post-commit': 'rubriclab-package post-commit' }
pkgJson.scripts = pkgJson.scripts || {}
// pkgJson.scripts.prepare = 'rubriclab-package prepare'
pkgJson.scripts.bleed = 'bun x npm-check-updates -u'
pkgJson.scripts.clean = 'rm -rf .next && rm -rf node_modules'
pkgJson.scripts.format = 'bun x biome format --write .'
pkgJson.scripts.lint = 'bun x biome check .'
pkgJson.scripts['lint:fix'] = 'bun x biome lint . --write --unsafe'
pkgJson.publishConfig = { access: 'public' }
const file = Bun.file(join(import.meta.dir, 'workflows/publish-package.yml'))
await Promise.all([
Bun.write(pkgJsonPath, JSON.stringify(pkgJson, null, 2)),
Bun.write(join(projectRoot, '.github/workflows/publish.yml'), file)
])
console.log('Set up package successfully.')
},
name: 'setup-package'
}
],
description: 'Package CLI tool',
name: 'package cli',
version: '0.0.0'
})
cli.parse()