1
1
#!/usr/bin/env bun
2
2
3
3
import { spawnSync } from 'bun'
4
- import { readFileSync , writeFileSync , existsSync } from 'node:fs'
5
4
6
5
if ( process . env . AMENDING ) {
7
6
process . exit ( 0 )
8
7
}
9
8
9
+ const run = (
10
+ command : `${string } ${string } `,
11
+ options ?: {
12
+ env ?: { [ key : string ] : string }
13
+ }
14
+ ) => {
15
+ const { stdout } = spawnSync ( command . split ( ' ' ) , options )
16
+ return stdout . toString ( ) . trim ( )
17
+ }
18
+
19
+ const CHANGELOG_PATH = 'CHANGELOG.md'
20
+ const PACKAGE_PATH = 'package.json'
21
+
10
22
try {
11
- const packageJsonContent = readFileSync ( 'package.json' , 'utf-8' )
12
- const packageJson = JSON . parse ( packageJsonContent )
23
+ const packageJsonContent = Bun . file ( PACKAGE_PATH )
24
+ const packageJson = await packageJsonContent . json ( )
25
+
26
+ console . log ( { packageJson } )
13
27
14
28
const versionParts = packageJson . version . split ( '.' ) . map ( Number )
15
29
versionParts [ 2 ] += 1
16
30
packageJson . version = versionParts . join ( '.' )
17
31
18
- writeFileSync ( 'package.json' , `${ JSON . stringify ( packageJson , null , 2 ) } \n` )
32
+ await Bun . write ( PACKAGE_PATH , `${ JSON . stringify ( packageJson , null , 2 ) } \n` )
19
33
20
- const commitHash = spawnSync ( [ 'git' , 'rev-parse' , 'HEAD' ] ) . stdout . toString ( ) . trim ( )
21
-
22
- const commitMessage = spawnSync ( [ 'git' , 'log' , '-1' , '--pretty=%B' ] ) . stdout . toString ( ) . trim ( )
23
-
24
- const commitDate = spawnSync ( [ 'git' , 'log' , '-1' , '--pretty=%cd' , '--date=short' ] )
25
- . stdout . toString ( )
26
- . trim ( )
27
-
28
- let repositoryUrl = spawnSync ( [ 'git' , 'config' , '--get' , 'remote.origin.url' ] )
29
- . stdout . toString ( )
30
- . trim ( )
34
+ const commitHash = run ( 'git rev-parse HEAD' )
35
+ const commitMessage = run ( 'git log -1 --pretty=%B' )
36
+ const commitDate = run ( 'git log -1 --pretty=%cd --date=short' )
37
+ let repositoryUrl = run ( 'git config --get remote.origin.url' )
31
38
32
39
if ( repositoryUrl . endsWith ( '.git' ) ) {
33
40
repositoryUrl = repositoryUrl . slice ( 0 , - 4 )
@@ -37,22 +44,21 @@ try {
37
44
}
38
45
39
46
const commitUrl = `${ repositoryUrl } /commit/${ commitHash } `
40
-
41
47
const changelogEntry = `- [${ commitDate } ] [${ commitMessage } ](${ commitUrl } )\n`
42
48
43
- const changelogPath = 'CHANGELOG.md'
44
49
let changelogContent = ''
45
- if ( existsSync ( changelogPath ) ) {
46
- changelogContent = readFileSync ( changelogPath , 'utf-8' )
50
+ const changelog = Bun . file ( CHANGELOG_PATH )
51
+ const changelogExists = await changelog . exists ( )
52
+ if ( changelogExists ) {
53
+ changelogContent = await changelog . text ( )
47
54
} else {
48
55
changelogContent = '# Changelog\n\n'
49
56
}
50
57
51
- writeFileSync ( changelogPath , `${ changelogEntry } ${ changelogContent } ` )
52
-
53
- spawnSync ( [ 'git' , 'add' , 'package.json' , 'CHANGELOG.md' ] )
58
+ await Bun . write ( CHANGELOG_PATH , `${ changelogEntry } ${ changelogContent } ` )
54
59
55
- spawnSync ( [ 'git' , 'commit' , '--amend' , '--no-verify' , '--no-edit' ] , {
60
+ run ( 'git add package.json CHANGELOG.md' )
61
+ run ( 'git commit --amend --no-verify --no-edit' , {
56
62
env : { ...process . env , AMENDING : 'true' }
57
63
} )
58
64
} catch ( error ) {
0 commit comments