Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce Safe flag #153

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ npx buildstamp --output='buildstamp.json' --extra='{"foo": "bar"}'
| `--git` | Collect git info | `true` |
| `--ci` | Capture CI digest | `true` |
| `--date` | Attach ISO8601 date | `true` |
| `--safe` | Suppress errors | `false` |
| `--extra` | JSON mixin to inject | `{}` |
| `--help` | Print help info | |

Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"devDependencies": {
"@abstractest/core": "^0.4.4",
"@qiwi/buildstamp-infra": "workspace:*"
"@qiwi/buildstamp-infra": "workspace:*",
"@types/minimist": "^1.2.5"
},
"scripts": {
"build": "concurrently --kill-others-on-fail 'npm:build:*'",
Expand Down
27 changes: 20 additions & 7 deletions packages/core/src/main/ts/buildstamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
IBuildstamp,
IBuildstampOptions,
IBuildstampOptionsNormalized,
IGitInfo
IGitInfo,
ICallable
} from './interface'

export const normalizeOpts = ({
Expand All @@ -16,6 +17,7 @@ export const normalizeOpts = ({
ci = true,
git = true,
date = true,
safe = false,
extra = {}
}: IBuildstampOptions = {}): IBuildstampOptionsNormalized => ({
ci,
Expand All @@ -24,25 +26,27 @@ export const normalizeOpts = ({
git,
date,
extra,
safe
})

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

if (date) {
stamp.date = new Date().toISOString()
}
if (git) {
Object.assign(stamp, await getGitInfo(cwd, process.env))
Object.assign(stamp, await s(getGitInfo)(cwd, process.env))
}
if (ci) {
Object.assign(stamp, getCIInfo(process.env))
Object.assign(stamp, s(getCIInfo)(process.env))
}
if (output) {
const file = path.resolve(cwd, output)
await fs.mkdir(path.dirname(file), { recursive: true })
await fs.writeFile(file, JSON.stringify(stamp, null, 2))
const file = s(path.resolve)(cwd, output)
await s(fs.mkdir)(s(path.dirname)(file), { recursive: true })
await s(fs.writeFile)(file, JSON.stringify(stamp, null, 2))
}

return stamp
Expand Down Expand Up @@ -106,3 +110,12 @@ export const spawn = (
})
})
})

export const safify = <T extends ICallable>(fn: T, value: any = {}): T => ((...args: any[]) => {
try {
const result = fn(...args)
return result?.then((v: any) => v, () => value) || result
} catch {
return value
}
}) as T
4 changes: 3 additions & 1 deletion packages/core/src/main/ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const camelize = (s: string) => s.replace(/-./g, x => x[1].toUpperCase())
const normalizeFlags = (flags = {}): Record<string, any> => Object.fromEntries(Object.entries(flags).map(([k, v]) =>
[camelize(k), v === 'false' ? false : v]))

const { cwd, git, date, output, version, help, extra } = normalizeFlags(minimist(process.argv.slice(2), {
const { cwd, git, date, output, version, help, extra, safe } = normalizeFlags(minimist(process.argv.slice(2), {
alias: {
help: ['h'],
version: ['v'],
Expand All @@ -28,6 +28,7 @@ const { cwd, git, date, output, version, help, extra } = normalizeFlags(minimist
--git Inject git info. True by default
--date Inject date. True by default
--extra JSON to mixin
--safe Suppress any errors. Defaults to false
--help, -h Print help digest
--version, -v Print version

Expand All @@ -48,6 +49,7 @@ const { cwd, git, date, output, version, help, extra } = normalizeFlags(minimist
date,
git,
output,
safe,
extra: extra ? JSON.parse(extra) : {}
})
})()
3 changes: 3 additions & 0 deletions packages/core/src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface IBuildstampOptionsNormalized {
date: boolean | string
git: boolean
ci: boolean
safe: boolean
extra: Record<string, string>
}

Expand All @@ -25,3 +26,5 @@ export interface IBuildstamp extends Partial<IGitInfo>, Partial<ICIInfo> {
date?: string
[e: string]: string | undefined
}

export interface ICallable { (...args: any[]): any }
14 changes: 13 additions & 1 deletion packages/core/src/test/ts/buildstamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@ describe('buildstamp', () => {
foo: 'bar'
}
})
expect(result.git_repo_name).toEqual('qiwi/buildstamp')
expect(result.git_repo_name).toEqual('qiwi/buildstamp') // eslint-disable-line sonarjs/no-duplicate-string
expect(result.foo).toEqual('bar')
})

it('suppresses errors in `safe` mode', async () => {
const output = '\0invali::?d_path.json'
try {
await buildstamp({output})
} catch (e) {
expect(e.message).toMatch(/The argument 'path' must be a string, Uint8Array, or URL without null bytes/)
}

const result = await buildstamp({output, safe: true})
expect(result.git_repo_name).toEqual('qiwi/buildstamp')
})
})

describe('getGitInfo()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
"ts-node": "^10.9.2",
"typedoc": "^0.26.3",
"typescript": "^5.5.3",
"zx-bulk-release": "^2.15.18"
"zx-bulk-release": "^2.15.19"
}
}
Loading
Loading