Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 74c0186

Browse files
feat: exec2, git2 - grouped utility classes
Replacing previous set of function with grouped utility classes. git2 is almost 1-1 as before, exec2 is fully rethought/refactored implementation.
1 parent a327413 commit 74c0186

File tree

11 files changed

+876
-524
lines changed

11 files changed

+876
-524
lines changed

scripts/dot.script.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
node scripts/dot.script.js
4+
node scripts/dot.script.js --count 3
5+
6+
*/
7+
8+
const { parseArgs } = require('node:util')
9+
const { pDelay } = require('@naturalcycles/js-lib')
10+
const { count: countStr, error } = parseArgs({
11+
options: {
12+
count: {
13+
type: 'string',
14+
default: '3',
15+
},
16+
error: {
17+
type: 'boolean',
18+
default: false,
19+
},
20+
},
21+
}).values
22+
23+
const count = Number(countStr)
24+
25+
console.log({
26+
count,
27+
error,
28+
})
29+
;(async () => {
30+
for (let i = 1; i <= count; i++) {
31+
await pDelay(1000)
32+
console.log(i)
33+
}
34+
if (error) {
35+
console.log('the error')
36+
return process.exit(1)
37+
}
38+
console.log('done')
39+
})()

scripts/exec2.script.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
yarn tsn exec2.script.ts
4+
5+
*/
6+
7+
import { runScript } from '../src'
8+
import { exec2 } from '../src/util/exec2'
9+
10+
runScript(async () => {
11+
// exec2.spawn({
12+
// cmd: 'node scripts/dot.script.js --error',
13+
// log: true,
14+
// })
15+
16+
// const s = exec2.exec({
17+
// cmd: 'node scripts/dot.script.js --error',
18+
// log: true,
19+
// })
20+
// console.log(s)
21+
22+
// exec2.spawn({
23+
// cmd: 'git status',
24+
// log: true,
25+
// })
26+
//
27+
// exec2.spawn({
28+
// cmd: 'git stat',
29+
// log: true,
30+
// })
31+
32+
// const s = exec2.exec({
33+
// cmd: 'git status',
34+
// log: true,
35+
// })
36+
const { stdout } = await exec2.spawnAsync('git status', {
37+
log: true,
38+
})
39+
console.log(stdout)
40+
})

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export * from './stream/writable/writableVoid'
7272
export * from './string/inspect'
7373
export * from './util/buildInfo.util'
7474
export * from './util/env.util'
75-
export * from './util/exec.util'
76-
export * from './util/git.util'
75+
export * from './util/exec2'
76+
export * from './util/git2'
7777
export * from './util/lruMemoCache'
7878
export * from './util/zip.util'
7979
export * from './validation/ajv/ajv.util'

src/util/buildInfo.util.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import {
66
UnixTimestampNumber,
77
} from '@naturalcycles/js-lib'
88
import { fs2 } from '../fs/fs2'
9-
import {
10-
gitCurrentBranchName,
11-
gitCurrentCommitSha,
12-
gitCurrentCommitTimestamp,
13-
gitCurrentRepoName,
14-
} from './git.util'
9+
import { git2 } from './git2'
1510

1611
export interface GenerateBuildInfoOptions {
1712
/**
@@ -24,10 +19,10 @@ export function generateBuildInfo(opt: GenerateBuildInfoOptions = {}): BuildInfo
2419
const now = localTime.orNow(opt.overrideTimestamp)
2520
const ts = now.unix
2621

27-
const rev = gitCurrentCommitSha()
28-
const branchName = gitCurrentBranchName()
29-
const repoName = gitCurrentRepoName()
30-
const tsCommit = gitCurrentCommitTimestamp()
22+
const rev = git2.gitCurrentCommitSha()
23+
const branchName = git2.gitCurrentBranchName()
24+
const repoName = git2.gitCurrentRepoName()
25+
const tsCommit = git2.gitCurrentCommitTimestamp()
3126

3227
const ver = [now.toStringCompact(), repoName, branchName, rev].join('_')
3328

src/util/exec.util.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/util/exec2.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { _expectedErrorString, _stringify, pExpectedError } from '@naturalcycles/js-lib'
2+
import { exec2, SpawnError } from './exec2'
3+
4+
test('spawn ok', () => {
5+
exec2.spawn('git status', {
6+
log: true,
7+
})
8+
// no error
9+
})
10+
11+
test('spawn error', () => {
12+
const err = _expectedErrorString(() =>
13+
exec2.spawn('git stat', {
14+
log: true,
15+
}),
16+
)
17+
expect(err).toMatchInlineSnapshot(`"Error: spawn exited with code 1: git stat"`)
18+
})
19+
20+
test('exec ok', () => {
21+
const s = exec2.exec('git version', {
22+
log: true,
23+
})
24+
expect(s.startsWith('git version')).toBe(true)
25+
})
26+
27+
test('exec error', () => {
28+
const err = _expectedErrorString(() =>
29+
exec2.exec('git stat', {
30+
log: true,
31+
}),
32+
)
33+
expect(err).toMatchInlineSnapshot(`"Error: exec exited with code 1: git stat"`)
34+
})
35+
36+
test('spawnAsync ok', async () => {
37+
const s = await exec2.spawnAsync('git version', {
38+
log: true,
39+
})
40+
expect(s.exitCode).toBe(0)
41+
expect(s.stderr).toBe('')
42+
expect(s.stdout.startsWith('git version')).toBe(true)
43+
})
44+
45+
test('spawnAsync error with throw', async () => {
46+
const err = await pExpectedError(exec2.spawnAsync('git stat'), SpawnError)
47+
expect(_stringify(err)).toMatchInlineSnapshot(
48+
`"SpawnError: spawnAsync exited with code 1: git stat"`,
49+
)
50+
expect(err.data.exitCode).toBe(1)
51+
expect(err.data.stdout).toBe('')
52+
expect(err.data.stderr).toMatchInlineSnapshot(`
53+
"git: 'stat' is not a git command. See 'git --help'.
54+
55+
The most similar commands are
56+
status
57+
stage
58+
stash"
59+
`)
60+
})
61+
62+
test('spawnAsync error without throw', async () => {
63+
const { exitCode, stdout, stderr } = await exec2.spawnAsync('git stat', {
64+
log: true,
65+
throwOnNonZeroCode: false,
66+
})
67+
expect(exitCode).toBe(1)
68+
expect(stdout).toBe('')
69+
expect(stderr).toMatchInlineSnapshot(`
70+
"git: 'stat' is not a git command. See 'git --help'.
71+
72+
The most similar commands are
73+
status
74+
stage
75+
stash"
76+
`)
77+
})

0 commit comments

Comments
 (0)