Skip to content

Commit

Permalink
feat: exec2, git2 - grouped utility classes
Browse files Browse the repository at this point in the history
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
kirillgroshkov committed Aug 31, 2024
1 parent a327413 commit 74c0186
Show file tree
Hide file tree
Showing 11 changed files with 876 additions and 524 deletions.
39 changes: 39 additions & 0 deletions scripts/dot.script.js
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')
})()
40 changes: 40 additions & 0 deletions scripts/exec2.script.ts
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)
})
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export * from './stream/writable/writableVoid'
export * from './string/inspect'
export * from './util/buildInfo.util'
export * from './util/env.util'
export * from './util/exec.util'
export * from './util/git.util'
export * from './util/exec2'
export * from './util/git2'
export * from './util/lruMemoCache'
export * from './util/zip.util'
export * from './validation/ajv/ajv.util'
Expand Down
15 changes: 5 additions & 10 deletions src/util/buildInfo.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import {
UnixTimestampNumber,
} from '@naturalcycles/js-lib'
import { fs2 } from '../fs/fs2'
import {
gitCurrentBranchName,
gitCurrentCommitSha,
gitCurrentCommitTimestamp,
gitCurrentRepoName,
} from './git.util'
import { git2 } from './git2'

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

const rev = gitCurrentCommitSha()
const branchName = gitCurrentBranchName()
const repoName = gitCurrentRepoName()
const tsCommit = gitCurrentCommitTimestamp()
const rev = git2.gitCurrentCommitSha()
const branchName = git2.gitCurrentBranchName()
const repoName = git2.gitCurrentRepoName()
const tsCommit = git2.gitCurrentCommitTimestamp()

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

Expand Down
79 changes: 0 additions & 79 deletions src/util/exec.util.ts

This file was deleted.

77 changes: 77 additions & 0 deletions src/util/exec2.test.ts
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"
`)
})
Loading

0 comments on commit 74c0186

Please sign in to comment.