-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
a327413
commit 74c0186
Showing
11 changed files
with
876 additions
and
524 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
node scripts/dot.script.js | ||
node scripts/dot.script.js --count 3 | ||
*/ | ||
|
||
const { parseArgs } = require('node:util') | ||
const { pDelay } = require('@naturalcycles/js-lib') | ||
const { count: countStr, error } = parseArgs({ | ||
options: { | ||
count: { | ||
type: 'string', | ||
default: '3', | ||
}, | ||
error: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}, | ||
}).values | ||
|
||
const count = Number(countStr) | ||
|
||
console.log({ | ||
count, | ||
error, | ||
}) | ||
;(async () => { | ||
for (let i = 1; i <= count; i++) { | ||
await pDelay(1000) | ||
console.log(i) | ||
} | ||
if (error) { | ||
console.log('the error') | ||
return process.exit(1) | ||
} | ||
console.log('done') | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
yarn tsn exec2.script.ts | ||
*/ | ||
|
||
import { runScript } from '../src' | ||
import { exec2 } from '../src/util/exec2' | ||
|
||
runScript(async () => { | ||
// exec2.spawn({ | ||
// cmd: 'node scripts/dot.script.js --error', | ||
// log: true, | ||
// }) | ||
|
||
// const s = exec2.exec({ | ||
// cmd: 'node scripts/dot.script.js --error', | ||
// log: true, | ||
// }) | ||
// console.log(s) | ||
|
||
// exec2.spawn({ | ||
// cmd: 'git status', | ||
// log: true, | ||
// }) | ||
// | ||
// exec2.spawn({ | ||
// cmd: 'git stat', | ||
// log: true, | ||
// }) | ||
|
||
// const s = exec2.exec({ | ||
// cmd: 'git status', | ||
// log: true, | ||
// }) | ||
const { stdout } = await exec2.spawnAsync('git status', { | ||
log: true, | ||
}) | ||
console.log(stdout) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { _expectedErrorString, _stringify, pExpectedError } from '@naturalcycles/js-lib' | ||
import { exec2, SpawnError } from './exec2' | ||
|
||
test('spawn ok', () => { | ||
exec2.spawn('git status', { | ||
log: true, | ||
}) | ||
// no error | ||
}) | ||
|
||
test('spawn error', () => { | ||
const err = _expectedErrorString(() => | ||
exec2.spawn('git stat', { | ||
log: true, | ||
}), | ||
) | ||
expect(err).toMatchInlineSnapshot(`"Error: spawn exited with code 1: git stat"`) | ||
}) | ||
|
||
test('exec ok', () => { | ||
const s = exec2.exec('git version', { | ||
log: true, | ||
}) | ||
expect(s.startsWith('git version')).toBe(true) | ||
}) | ||
|
||
test('exec error', () => { | ||
const err = _expectedErrorString(() => | ||
exec2.exec('git stat', { | ||
log: true, | ||
}), | ||
) | ||
expect(err).toMatchInlineSnapshot(`"Error: exec exited with code 1: git stat"`) | ||
}) | ||
|
||
test('spawnAsync ok', async () => { | ||
const s = await exec2.spawnAsync('git version', { | ||
log: true, | ||
}) | ||
expect(s.exitCode).toBe(0) | ||
expect(s.stderr).toBe('') | ||
expect(s.stdout.startsWith('git version')).toBe(true) | ||
}) | ||
|
||
test('spawnAsync error with throw', async () => { | ||
const err = await pExpectedError(exec2.spawnAsync('git stat'), SpawnError) | ||
expect(_stringify(err)).toMatchInlineSnapshot( | ||
`"SpawnError: spawnAsync exited with code 1: git stat"`, | ||
) | ||
expect(err.data.exitCode).toBe(1) | ||
expect(err.data.stdout).toBe('') | ||
expect(err.data.stderr).toMatchInlineSnapshot(` | ||
"git: 'stat' is not a git command. See 'git --help'. | ||
The most similar commands are | ||
status | ||
stage | ||
stash" | ||
`) | ||
}) | ||
|
||
test('spawnAsync error without throw', async () => { | ||
const { exitCode, stdout, stderr } = await exec2.spawnAsync('git stat', { | ||
log: true, | ||
throwOnNonZeroCode: false, | ||
}) | ||
expect(exitCode).toBe(1) | ||
expect(stdout).toBe('') | ||
expect(stderr).toMatchInlineSnapshot(` | ||
"git: 'stat' is not a git command. See 'git --help'. | ||
The most similar commands are | ||
status | ||
stage | ||
stash" | ||
`) | ||
}) |
Oops, something went wrong.