Skip to content

Commit 58e43df

Browse files
committed
feat(js): introduce safe flag to suppress errors
1 parent 851d3fb commit 58e43df

File tree

8 files changed

+1376
-1351
lines changed

8 files changed

+1376
-1351
lines changed

packages/core/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ npx buildstamp --output='buildstamp.json' --extra='{"foo": "bar"}'
3737
| `--git` | Collect git info | `true` |
3838
| `--ci` | Capture CI digest | `true` |
3939
| `--date` | Attach ISO8601 date | `true` |
40+
| `--safe` | Suppress errors | `false` |
4041
| `--extra` | JSON mixin to inject | `{}` |
4142
| `--help` | Print help info | |
4243

packages/core/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"devDependencies": {
3030
"@abstractest/core": "^0.4.4",
31-
"@qiwi/buildstamp-infra": "workspace:*"
31+
"@qiwi/buildstamp-infra": "workspace:*",
32+
"@types/minimist": "^1.2.5"
3233
},
3334
"scripts": {
3435
"build": "concurrently --kill-others-on-fail 'npm:build:*'",

packages/core/src/main/ts/buildstamp.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
IBuildstamp,
88
IBuildstampOptions,
99
IBuildstampOptionsNormalized,
10-
IGitInfo
10+
IGitInfo,
11+
ICallable
1112
} from './interface'
1213

1314
export const normalizeOpts = ({
@@ -16,6 +17,7 @@ export const normalizeOpts = ({
1617
ci = true,
1718
git = true,
1819
date = true,
20+
safe = false,
1921
extra = {}
2022
}: IBuildstampOptions = {}): IBuildstampOptionsNormalized => ({
2123
ci,
@@ -24,25 +26,27 @@ export const normalizeOpts = ({
2426
git,
2527
date,
2628
extra,
29+
safe
2730
})
2831

2932
export const buildstamp = async (opts?: IBuildstampOptions): Promise<IBuildstamp> => {
30-
const {ci, git, date, cwd, output, extra} = normalizeOpts(opts)
33+
const {ci, git, date, cwd, output, extra, safe} = normalizeOpts(opts)
3134
const stamp: IBuildstamp = {...extra}
35+
const s = safe ? safify : (v: any) => v
3236

3337
if (date) {
3438
stamp.date = new Date().toISOString()
3539
}
3640
if (git) {
37-
Object.assign(stamp, await getGitInfo(cwd, process.env))
41+
Object.assign(stamp, await s(getGitInfo)(cwd, process.env))
3842
}
3943
if (ci) {
40-
Object.assign(stamp, getCIInfo(process.env))
44+
Object.assign(stamp, s(getCIInfo)(process.env))
4145
}
4246
if (output) {
43-
const file = path.resolve(cwd, output)
44-
await fs.mkdir(path.dirname(file), { recursive: true })
45-
await fs.writeFile(file, JSON.stringify(stamp, null, 2))
47+
const file = s(path.resolve)(cwd, output)
48+
await s(fs.mkdir)(s(path.dirname)(file), { recursive: true })
49+
await s(fs.writeFile)(file, JSON.stringify(stamp, null, 2))
4650
}
4751

4852
return stamp
@@ -106,3 +110,12 @@ export const spawn = (
106110
})
107111
})
108112
})
113+
114+
export const safify = <T extends ICallable>(fn: T, value: any = {}): T => ((...args: any[]) => {
115+
try {
116+
const result = fn(...args)
117+
return result?.then((v: any) => v, () => value) || result
118+
} catch {
119+
return value
120+
}
121+
}) as T

packages/core/src/main/ts/cli.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const camelize = (s: string) => s.replace(/-./g, x => x[1].toUpperCase())
88
const normalizeFlags = (flags = {}): Record<string, any> => Object.fromEntries(Object.entries(flags).map(([k, v]) =>
99
[camelize(k), v === 'false' ? false : v]))
1010

11-
const { cwd, git, date, output, version, help, extra } = normalizeFlags(minimist(process.argv.slice(2), {
11+
const { cwd, git, date, output, version, help, extra, safe } = normalizeFlags(minimist(process.argv.slice(2), {
1212
alias: {
1313
help: ['h'],
1414
version: ['v'],
@@ -28,6 +28,7 @@ const { cwd, git, date, output, version, help, extra } = normalizeFlags(minimist
2828
--git Inject git info. True by default
2929
--date Inject date. True by default
3030
--extra JSON to mixin
31+
--safe Suppress any errors. Defaults to false
3132
--help, -h Print help digest
3233
--version, -v Print version
3334
@@ -48,6 +49,7 @@ const { cwd, git, date, output, version, help, extra } = normalizeFlags(minimist
4849
date,
4950
git,
5051
output,
52+
safe,
5153
extra: extra ? JSON.parse(extra) : {}
5254
})
5355
})()

packages/core/src/main/ts/interface.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface IBuildstampOptionsNormalized {
44
date: boolean | string
55
git: boolean
66
ci: boolean
7+
safe: boolean
78
extra: Record<string, string>
89
}
910

@@ -25,3 +26,5 @@ export interface IBuildstamp extends Partial<IGitInfo>, Partial<ICIInfo> {
2526
date?: string
2627
[e: string]: string | undefined
2728
}
29+
30+
export interface ICallable { (...args: any[]): any }

packages/core/src/test/ts/buildstamp.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ describe('buildstamp', () => {
1212
expect(result.git_repo_name).toEqual('qiwi/buildstamp')
1313
expect(result.foo).toEqual('bar')
1414
})
15+
16+
it('suppresses errors in `safe` mode', async () => {
17+
const output = '\0invali::?d_path.json'
18+
try {
19+
await buildstamp({output})
20+
} catch (e) {
21+
expect(e.message).toMatch(/The argument 'path' must be a string, Uint8Array, or URL without null bytes/)
22+
}
23+
24+
const result = await buildstamp({output, safe: true})
25+
expect(result.git_repo_name).toEqual('qiwi/buildstamp')
26+
})
1527
})
1628

1729
describe('getGitInfo()', () => {

packages/infra/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
"ts-node": "^10.9.2",
3131
"typedoc": "^0.26.3",
3232
"typescript": "^5.5.3",
33-
"zx-bulk-release": "^2.15.18"
33+
"zx-bulk-release": "^2.15.19"
3434
}
3535
}

0 commit comments

Comments
 (0)