@naturalcycles/nodejs-lib 13.31.1 → 13.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/csv/csvReader.js +2 -2
- package/dist/fs/fs2.js +6 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/slack/slack.service.js +2 -4
- package/dist/stream/ndjson/transformJsonParse.js +1 -1
- package/dist/stream/transform/transformMap.d.ts +4 -2
- package/dist/stream/transform/transformMap.js +2 -2
- package/dist/stream/transform/transformSplit.js +2 -2
- package/dist/stream/transform/worker/workerClassProxy.js +2 -2
- package/dist/stream/writable/writableLimit.js +1 -1
- package/dist/stream/writable/writableVoid.js +1 -1
- package/dist/util/buildInfo.util.js +5 -5
- package/dist/util/exec2.d.ts +167 -0
- package/dist/util/exec2.js +204 -0
- package/dist/util/git2.d.ts +25 -0
- package/dist/util/git2.js +95 -0
- package/dist/validation/joi/number.extensions.d.ts +2 -1
- package/dist/validation/joi/string.extensions.d.ts +2 -1
- package/package.json +3 -2
- package/src/csv/csvReader.ts +2 -7
- package/src/fs/fs2.ts +11 -7
- package/src/index.ts +2 -2
- package/src/secret/secrets-decrypt.util.ts +1 -1
- package/src/secret/secrets-encrypt.util.ts +1 -1
- package/src/slack/slack.service.ts +2 -3
- package/src/stream/ndjson/transformJsonParse.ts +1 -1
- package/src/stream/stream.model.ts +2 -2
- package/src/stream/transform/transformMap.ts +5 -3
- package/src/stream/transform/transformSplit.ts +3 -3
- package/src/stream/transform/worker/workerClassProxy.js +2 -2
- package/src/stream/writable/writableLimit.ts +1 -1
- package/src/stream/writable/writableVoid.ts +1 -1
- package/src/util/buildInfo.util.ts +5 -10
- package/src/util/exec2.ts +326 -0
- package/src/util/git2.ts +105 -0
- package/src/validation/joi/number.extensions.ts +2 -1
- package/src/validation/joi/string.extensions.ts +2 -1
- package/dist/util/exec.util.d.ts +0 -10
- package/dist/util/exec.util.js +0 -58
- package/dist/util/git.util.d.ts +0 -18
- package/dist/util/git.util.js +0 -109
- package/src/util/exec.util.ts +0 -79
- package/src/util/git.util.ts +0 -113
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import cp from 'node:child_process'
|
|
2
|
+
import {
|
|
3
|
+
_since,
|
|
4
|
+
AnyObject,
|
|
5
|
+
AppError,
|
|
6
|
+
NumberOfMilliseconds,
|
|
7
|
+
UnixTimestampMillisNumber,
|
|
8
|
+
} from '@naturalcycles/js-lib'
|
|
9
|
+
import { dimGrey, white } from '../colors/colors'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Set of utility functions to work with Spawn / Exec.
|
|
13
|
+
*
|
|
14
|
+
* How to decide between Spawn and Exec?
|
|
15
|
+
*
|
|
16
|
+
* Long-running job that prints output, and no need to return the output - use Spawn.
|
|
17
|
+
*
|
|
18
|
+
* Short-running job, no need to print the output, might want to return the output - use Exec.
|
|
19
|
+
*
|
|
20
|
+
* Need to both print and return the output - use SpawnAsync.
|
|
21
|
+
*
|
|
22
|
+
* ***
|
|
23
|
+
*
|
|
24
|
+
* Spawn is good for long-running large-output processes, that continuously output data.
|
|
25
|
+
* E.g running `jest`.
|
|
26
|
+
*
|
|
27
|
+
* Exec is the opposite - good for short-running processes that output small data.
|
|
28
|
+
* Exec allows to return the output as a string.
|
|
29
|
+
* Exec doesn't stream data during execution, so the output/error will only be printed
|
|
30
|
+
* at the end.
|
|
31
|
+
* Exec always uses the shell (there's no option to disable it).
|
|
32
|
+
*/
|
|
33
|
+
class Exec2 {
|
|
34
|
+
/**
|
|
35
|
+
* Advanced/async version of Spawn.
|
|
36
|
+
* Consider simpler `spawn` or `exec` first, which are also sync.
|
|
37
|
+
*
|
|
38
|
+
* spawnAsync features:
|
|
39
|
+
*
|
|
40
|
+
* 1. Async
|
|
41
|
+
* 2. Allows to collect the output AND print it while running.
|
|
42
|
+
* 3. Returns SpawnOutput with stdout, stderr and exitCode.
|
|
43
|
+
* 4. Allows to not throw on error, but just return SpawnOutput for further inspection.
|
|
44
|
+
*
|
|
45
|
+
* Defaults:
|
|
46
|
+
*
|
|
47
|
+
* shell: true
|
|
48
|
+
* printWhileRunning: true
|
|
49
|
+
* collectOutputWhileRunning: true
|
|
50
|
+
* throwOnNonZeroCode: true
|
|
51
|
+
* log: true
|
|
52
|
+
*/
|
|
53
|
+
async spawnAsync(cmd: string, opt: SpawnAsyncOptions = {}): Promise<SpawnOutput> {
|
|
54
|
+
const started = Date.now()
|
|
55
|
+
this.logStart(cmd, opt)
|
|
56
|
+
const {
|
|
57
|
+
shell = true,
|
|
58
|
+
printWhileRunning = true,
|
|
59
|
+
collectOutputWhileRunning = true,
|
|
60
|
+
throwOnNonZeroCode = true,
|
|
61
|
+
cwd,
|
|
62
|
+
env,
|
|
63
|
+
} = opt
|
|
64
|
+
let stdout = ''
|
|
65
|
+
let stderr = ''
|
|
66
|
+
|
|
67
|
+
return await new Promise<SpawnOutput>((resolve, reject) => {
|
|
68
|
+
const p = cp.spawn(cmd, opt.args || [], {
|
|
69
|
+
shell,
|
|
70
|
+
cwd,
|
|
71
|
+
env,
|
|
72
|
+
// ...process.env, // not passing by default for security reasons
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
p.stdout.on('data', data => {
|
|
76
|
+
if (collectOutputWhileRunning) {
|
|
77
|
+
stdout += data.toString()
|
|
78
|
+
// console.log('stdout:', data.toString())
|
|
79
|
+
}
|
|
80
|
+
if (printWhileRunning) {
|
|
81
|
+
process.stdout.write(data)
|
|
82
|
+
// console.log('stderr:', data.toString())
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
p.stderr.on('data', data => {
|
|
86
|
+
if (collectOutputWhileRunning) {
|
|
87
|
+
stderr += data.toString()
|
|
88
|
+
}
|
|
89
|
+
if (printWhileRunning) {
|
|
90
|
+
process.stderr.write(data)
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
p.on('close', code => {
|
|
95
|
+
this.logFinish(cmd, opt, started)
|
|
96
|
+
const exitCode = code || 0
|
|
97
|
+
const o: SpawnOutput = {
|
|
98
|
+
exitCode,
|
|
99
|
+
stdout: stdout.trim(),
|
|
100
|
+
stderr: stderr.trim(),
|
|
101
|
+
}
|
|
102
|
+
if (throwOnNonZeroCode && code) {
|
|
103
|
+
return reject(new SpawnError(`spawnAsync exited with code ${code}: ${cmd}`, o))
|
|
104
|
+
}
|
|
105
|
+
resolve(o)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Reasons to use it:
|
|
112
|
+
* - Sync
|
|
113
|
+
* - Need to print output while running
|
|
114
|
+
*
|
|
115
|
+
* Limitations:
|
|
116
|
+
* - Cannot return stdout/stderr (use exec or spawnAsync for that)
|
|
117
|
+
*
|
|
118
|
+
* Defaults:
|
|
119
|
+
*
|
|
120
|
+
* shell: true
|
|
121
|
+
* log: true
|
|
122
|
+
*/
|
|
123
|
+
spawn(cmd: string, opt: SpawnOptions = {}): void {
|
|
124
|
+
const started = Date.now()
|
|
125
|
+
this.logStart(cmd, opt)
|
|
126
|
+
const { shell = true, cwd, env } = opt
|
|
127
|
+
|
|
128
|
+
const r = cp.spawnSync(cmd, opt.args, {
|
|
129
|
+
encoding: 'utf8',
|
|
130
|
+
stdio: 'inherit',
|
|
131
|
+
shell,
|
|
132
|
+
cwd,
|
|
133
|
+
env,
|
|
134
|
+
// ...process.env, // not passing by default for security reasons
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
this.logFinish(cmd, opt, started)
|
|
138
|
+
|
|
139
|
+
if (r.error) {
|
|
140
|
+
throw r.error
|
|
141
|
+
}
|
|
142
|
+
if (r.status) {
|
|
143
|
+
throw new Error(`spawn exited with code ${r.status}: ${cmd}`)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Reasons to use it:
|
|
149
|
+
*
|
|
150
|
+
* - Sync
|
|
151
|
+
* - Need to return output
|
|
152
|
+
*
|
|
153
|
+
* Limitations:
|
|
154
|
+
* - Cannot print while running (use spawn or spawnAsync for that)
|
|
155
|
+
*
|
|
156
|
+
* Defaults:
|
|
157
|
+
*
|
|
158
|
+
* shell: true
|
|
159
|
+
* log: true
|
|
160
|
+
*/
|
|
161
|
+
exec(cmd: string, opt: ExecOptions = {}): string {
|
|
162
|
+
const started = Date.now()
|
|
163
|
+
this.logStart(cmd, opt)
|
|
164
|
+
const { cwd, env, timeout } = opt
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
return cp
|
|
168
|
+
.execSync(cmd, {
|
|
169
|
+
encoding: 'utf8',
|
|
170
|
+
// stdio: 'inherit', // no, otherwise we don't get the output returned
|
|
171
|
+
stdio: undefined,
|
|
172
|
+
// shell: undefined,
|
|
173
|
+
cwd,
|
|
174
|
+
timeout,
|
|
175
|
+
env,
|
|
176
|
+
// ...process.env, // not passing by default for security reasons
|
|
177
|
+
})
|
|
178
|
+
.trim()
|
|
179
|
+
} catch (err) {
|
|
180
|
+
// Not logging stderr, as it's printed by execSync by default (somehow)
|
|
181
|
+
// stdout is not printed by execSync though, therefor we print it here
|
|
182
|
+
// if ((err as any).stderr) {
|
|
183
|
+
// process.stderr.write((err as any).stderr)
|
|
184
|
+
// }
|
|
185
|
+
if ((err as any).stdout) {
|
|
186
|
+
process.stdout.write((err as any).stdout)
|
|
187
|
+
}
|
|
188
|
+
throw new Error(`exec exited with code ${(err as any).status}: ${cmd}`)
|
|
189
|
+
} finally {
|
|
190
|
+
this.logFinish(cmd, opt, started)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
throwOnNonZeroExitCode(o: SpawnOutput): void {
|
|
195
|
+
if (o.exitCode) {
|
|
196
|
+
throw new SpawnError(`spawn exited with code ${o.exitCode}`, o)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private logStart(cmd: string, opt: SpawnOptions | ExecOptions): void {
|
|
201
|
+
if (!opt.logStart && !opt.log) return
|
|
202
|
+
|
|
203
|
+
console.log(
|
|
204
|
+
[
|
|
205
|
+
dimGrey(...Object.entries(opt.env || {}).map(([k, v]) => [k, v].join('='))),
|
|
206
|
+
white(opt.name || cmd),
|
|
207
|
+
...((!opt.name && (opt as SpawnOptions).args) || []),
|
|
208
|
+
]
|
|
209
|
+
.filter(Boolean)
|
|
210
|
+
.join(' '),
|
|
211
|
+
)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private logFinish(
|
|
215
|
+
cmd: string,
|
|
216
|
+
opt: SpawnOptions | ExecOptions,
|
|
217
|
+
started: UnixTimestampMillisNumber,
|
|
218
|
+
): void {
|
|
219
|
+
if (!opt.logFinish && !opt.log) return
|
|
220
|
+
|
|
221
|
+
console.log([white(opt.name || cmd), dimGrey('took ' + _since(started))].join(' '))
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export const exec2 = new Exec2()
|
|
226
|
+
|
|
227
|
+
export class SpawnError extends AppError<SpawnErrorData> {
|
|
228
|
+
constructor(message: string, data: SpawnErrorData) {
|
|
229
|
+
super(message, data, { name: 'SpawnError' })
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface SpawnErrorData extends SpawnOutput {}
|
|
234
|
+
|
|
235
|
+
export interface SpawnOutput {
|
|
236
|
+
/**
|
|
237
|
+
* Exit code of the spawned process.
|
|
238
|
+
* 0 means success, anything else means failure.
|
|
239
|
+
*/
|
|
240
|
+
exitCode: number
|
|
241
|
+
stdout: string
|
|
242
|
+
stderr: string
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export interface SpawnAsyncOptions extends SpawnOptions {
|
|
246
|
+
/**
|
|
247
|
+
* Defaults to true.
|
|
248
|
+
* If true - prints both stdout and stderr to console while running,
|
|
249
|
+
* otherwise runs "silently".
|
|
250
|
+
* Returns SpawnOutput in the same way, regardless of `printWhileRunning` setting.
|
|
251
|
+
*/
|
|
252
|
+
printWhileRunning?: boolean
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Defaults to true.
|
|
256
|
+
* If true - collects stdout and stderr while running, and return it in the end.
|
|
257
|
+
* stdout/stderr are collected and returned regardless if it returns with error or not.
|
|
258
|
+
* On success - stdout/stderr are available from `SpawnOutput`.
|
|
259
|
+
* On error - stdout/stderr are available from `SpawnError.data`.
|
|
260
|
+
*/
|
|
261
|
+
collectOutputWhileRunning?: boolean
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Defaults to true.
|
|
265
|
+
* If true - throws SpawnError if non-zero code is returned.
|
|
266
|
+
* SpawnError conveniently contains .data.stdout and .data.strerr for inspection.
|
|
267
|
+
* If false - will not throw, but return SpawnOutput with stdout, stderr and exitCode.
|
|
268
|
+
*/
|
|
269
|
+
throwOnNonZeroCode?: boolean
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export interface SpawnOptions {
|
|
273
|
+
args?: string[]
|
|
274
|
+
/**
|
|
275
|
+
* Defaults to true.
|
|
276
|
+
*/
|
|
277
|
+
logStart?: boolean
|
|
278
|
+
/**
|
|
279
|
+
* Defaults to true.
|
|
280
|
+
*/
|
|
281
|
+
logFinish?: boolean
|
|
282
|
+
/**
|
|
283
|
+
* Defaults to true.
|
|
284
|
+
* Controls/overrides both logStart and logFinish simultaneously.
|
|
285
|
+
*/
|
|
286
|
+
log?: boolean
|
|
287
|
+
/**
|
|
288
|
+
* Defaults to true.
|
|
289
|
+
*/
|
|
290
|
+
shell?: boolean
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* If specified - will be used as "command name" for logging purposes,
|
|
294
|
+
* instead of "cmd + args"
|
|
295
|
+
*/
|
|
296
|
+
name?: string
|
|
297
|
+
cwd?: string
|
|
298
|
+
|
|
299
|
+
env?: AnyObject
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export interface ExecOptions {
|
|
303
|
+
/**
|
|
304
|
+
* Defaults to false.
|
|
305
|
+
*/
|
|
306
|
+
logStart?: boolean
|
|
307
|
+
/**
|
|
308
|
+
* Defaults to false.
|
|
309
|
+
*/
|
|
310
|
+
logFinish?: boolean
|
|
311
|
+
/**
|
|
312
|
+
* Defaults to false.
|
|
313
|
+
* Controls/overrides both logStart and logFinish simultaneously.
|
|
314
|
+
*/
|
|
315
|
+
log?: boolean
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* If specified - will be used as "command name" for logging purposes,
|
|
319
|
+
* instead of "cmd + args"
|
|
320
|
+
*/
|
|
321
|
+
name?: string
|
|
322
|
+
cwd?: string
|
|
323
|
+
timeout?: NumberOfMilliseconds
|
|
324
|
+
|
|
325
|
+
env?: AnyObject
|
|
326
|
+
}
|
package/src/util/git2.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import cp from 'node:child_process'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import type { UnixTimestampNumber } from '@naturalcycles/js-lib'
|
|
4
|
+
import { grey } from '../colors/colors'
|
|
5
|
+
import { exec2 } from './exec2'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Set of utility functions to work with git.
|
|
9
|
+
*/
|
|
10
|
+
class Git2 {
|
|
11
|
+
getLastGitCommitMsg(): string {
|
|
12
|
+
return exec2.exec('git log -1 --pretty=%B')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
commitMessageToTitleMessage(msg: string): string {
|
|
16
|
+
const firstLine = msg.split('\n')[0]!
|
|
17
|
+
const [preTitle, title] = firstLine.split(': ')
|
|
18
|
+
return title || preTitle!
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
gitHasUncommittedChanges(): boolean {
|
|
22
|
+
// git diff-index --quiet HEAD -- || echo "untracked"
|
|
23
|
+
try {
|
|
24
|
+
cp.execSync('git diff-index --quiet HEAD --', {
|
|
25
|
+
encoding: 'utf8',
|
|
26
|
+
})
|
|
27
|
+
return false
|
|
28
|
+
} catch {
|
|
29
|
+
return true
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns true if there were changes
|
|
35
|
+
*/
|
|
36
|
+
gitCommitAll(msg: string): boolean {
|
|
37
|
+
// git commit -a -m "style(lint-all): $GIT_MSG" || true
|
|
38
|
+
const cmd = `git commit -a --no-verify -m "${msg}"`
|
|
39
|
+
// const cmd = `git`
|
|
40
|
+
// const args = ['commit', '-a', '--no-verify', '-m', msg]
|
|
41
|
+
console.log(grey(cmd))
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
cp.execSync(cmd, {
|
|
45
|
+
stdio: 'inherit',
|
|
46
|
+
})
|
|
47
|
+
return true
|
|
48
|
+
} catch {
|
|
49
|
+
return false
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @returns true if there are not pushed commits.
|
|
55
|
+
*/
|
|
56
|
+
gitIsAhead(): boolean {
|
|
57
|
+
// ahead=`git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`
|
|
58
|
+
const cmd = `git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`
|
|
59
|
+
const stdout = exec2.exec(cmd)
|
|
60
|
+
// console.log(`gitIsAhead: ${stdout}`)
|
|
61
|
+
return Number(stdout) > 0
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
gitPull(): void {
|
|
65
|
+
const cmd = 'git pull'
|
|
66
|
+
try {
|
|
67
|
+
cp.execSync(cmd, {
|
|
68
|
+
stdio: 'inherit',
|
|
69
|
+
})
|
|
70
|
+
} catch {}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
gitPush(): void {
|
|
74
|
+
// git push --set-upstream origin $CIRCLE_BRANCH && echo "pushed, exiting" && exit 0
|
|
75
|
+
let cmd = 'git push'
|
|
76
|
+
|
|
77
|
+
const branchName = this.gitCurrentBranchName()
|
|
78
|
+
|
|
79
|
+
if (branchName) {
|
|
80
|
+
cmd += ` --set-upstream origin ${branchName}`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
exec2.spawn(cmd, { logStart: true })
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
gitCurrentCommitSha(full = false): string {
|
|
87
|
+
const sha = exec2.exec('git rev-parse HEAD')
|
|
88
|
+
return full ? sha : sha.slice(0, 7)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
gitCurrentCommitTimestamp(): UnixTimestampNumber {
|
|
92
|
+
return Number(exec2.exec('git log -1 --format=%ct'))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
gitCurrentBranchName(): string {
|
|
96
|
+
return exec2.exec('git rev-parse --abbrev-ref HEAD')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
gitCurrentRepoName(): string {
|
|
100
|
+
const originUrl = exec2.exec('git config --get remote.origin.url')
|
|
101
|
+
return path.basename(originUrl, '.git')
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const git2 = new Git2()
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import Joi
|
|
1
|
+
import type Joi from 'joi'
|
|
2
|
+
import { Extension, NumberSchema as JoiNumberSchema } from 'joi'
|
|
2
3
|
|
|
3
4
|
export interface NumberSchema<TSchema = number> extends JoiNumberSchema<TSchema> {
|
|
4
5
|
dividable: (q: number) => this
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { localTime } from '@naturalcycles/js-lib'
|
|
2
|
-
import Joi
|
|
2
|
+
import type Joi from 'joi'
|
|
3
|
+
import { Extension, StringSchema as JoiStringSchema } from 'joi'
|
|
3
4
|
|
|
4
5
|
export interface StringSchema<TSchema = string> extends JoiStringSchema<TSchema> {
|
|
5
6
|
dateString: (min?: string, max?: string) => this
|
package/dist/util/exec.util.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { SpawnOptions } from 'node:child_process';
|
|
2
|
-
export interface ExecOptions extends SpawnOptions {
|
|
3
|
-
/**
|
|
4
|
-
* Defaults to true.
|
|
5
|
-
* Set to false to skip logging.
|
|
6
|
-
*/
|
|
7
|
-
log?: boolean;
|
|
8
|
-
}
|
|
9
|
-
export declare function execVoidCommand(cmd: string, args?: string[], opt?: ExecOptions): Promise<void>;
|
|
10
|
-
export declare function execVoidCommandSync(cmd: string, args?: string[], opt?: ExecOptions): void;
|
package/dist/util/exec.util.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.execVoidCommand = execVoidCommand;
|
|
4
|
-
exports.execVoidCommandSync = execVoidCommandSync;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
|
-
const node_child_process_1 = tslib_1.__importDefault(require("node:child_process"));
|
|
7
|
-
async function execVoidCommand(cmd, args = [], opt = {}) {
|
|
8
|
-
logExec(cmd, args, opt);
|
|
9
|
-
await new Promise(resolve => {
|
|
10
|
-
const p = node_child_process_1.default.spawn(cmd, [...args], {
|
|
11
|
-
stdio: 'inherit',
|
|
12
|
-
// shell: true,
|
|
13
|
-
...opt,
|
|
14
|
-
env: {
|
|
15
|
-
...process.env,
|
|
16
|
-
...opt.env,
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
p.on('close', code => {
|
|
20
|
-
if (code) {
|
|
21
|
-
console.log(`${cmd} exited with code ${code}`);
|
|
22
|
-
process.exit(code);
|
|
23
|
-
}
|
|
24
|
-
resolve();
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
function execVoidCommandSync(cmd, args = [], opt = {}) {
|
|
29
|
-
logExec(cmd, args, opt);
|
|
30
|
-
const r = node_child_process_1.default.spawnSync(cmd, [...args], {
|
|
31
|
-
encoding: 'utf8',
|
|
32
|
-
stdio: 'inherit',
|
|
33
|
-
// shell: true, // removing shell breaks executing `tsc`
|
|
34
|
-
...opt,
|
|
35
|
-
env: {
|
|
36
|
-
...process.env,
|
|
37
|
-
...opt.env,
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
if (r.status) {
|
|
41
|
-
console.log(`${cmd} exited with code ${r.status}`);
|
|
42
|
-
process.exit(r.status);
|
|
43
|
-
}
|
|
44
|
-
if (r.error) {
|
|
45
|
-
console.log(r.error);
|
|
46
|
-
process.exit(r.error.errno || 1);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function logExec(cmd, args = [], opt = {}) {
|
|
50
|
-
if (opt.log === false)
|
|
51
|
-
return;
|
|
52
|
-
const cmdline = [
|
|
53
|
-
...Object.entries(opt.env || {}).map(([k, v]) => [k, v].join('=')),
|
|
54
|
-
cmd,
|
|
55
|
-
...args,
|
|
56
|
-
].join(' ');
|
|
57
|
-
console.log(cmdline);
|
|
58
|
-
}
|
package/dist/util/git.util.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { UnixTimestampNumber } from '@naturalcycles/js-lib';
|
|
2
|
-
export declare function getLastGitCommitMsg(): string;
|
|
3
|
-
export declare function commitMessageToTitleMessage(msg: string): string;
|
|
4
|
-
export declare function gitHasUncommittedChanges(): boolean;
|
|
5
|
-
/**
|
|
6
|
-
* @returns true if there were changes
|
|
7
|
-
*/
|
|
8
|
-
export declare function gitCommitAll(msg: string): boolean;
|
|
9
|
-
/**
|
|
10
|
-
* @returns true if there are not pushed commits.
|
|
11
|
-
*/
|
|
12
|
-
export declare function gitIsAhead(): boolean;
|
|
13
|
-
export declare function gitPull(): void;
|
|
14
|
-
export declare function gitPush(): void;
|
|
15
|
-
export declare function gitCurrentCommitSha(full?: boolean): string;
|
|
16
|
-
export declare function gitCurrentCommitTimestamp(): UnixTimestampNumber;
|
|
17
|
-
export declare function gitCurrentBranchName(): string;
|
|
18
|
-
export declare function gitCurrentRepoName(): string;
|
package/dist/util/git.util.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLastGitCommitMsg = getLastGitCommitMsg;
|
|
4
|
-
exports.commitMessageToTitleMessage = commitMessageToTitleMessage;
|
|
5
|
-
exports.gitHasUncommittedChanges = gitHasUncommittedChanges;
|
|
6
|
-
exports.gitCommitAll = gitCommitAll;
|
|
7
|
-
exports.gitIsAhead = gitIsAhead;
|
|
8
|
-
exports.gitPull = gitPull;
|
|
9
|
-
exports.gitPush = gitPush;
|
|
10
|
-
exports.gitCurrentCommitSha = gitCurrentCommitSha;
|
|
11
|
-
exports.gitCurrentCommitTimestamp = gitCurrentCommitTimestamp;
|
|
12
|
-
exports.gitCurrentBranchName = gitCurrentBranchName;
|
|
13
|
-
exports.gitCurrentRepoName = gitCurrentRepoName;
|
|
14
|
-
const tslib_1 = require("tslib");
|
|
15
|
-
const node_child_process_1 = tslib_1.__importDefault(require("node:child_process"));
|
|
16
|
-
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
17
|
-
const colors_1 = require("../colors/colors");
|
|
18
|
-
function getLastGitCommitMsg() {
|
|
19
|
-
return execSync('git log -1 --pretty=%B');
|
|
20
|
-
}
|
|
21
|
-
function commitMessageToTitleMessage(msg) {
|
|
22
|
-
const firstLine = msg.split('\n')[0];
|
|
23
|
-
const [preTitle, title] = firstLine.split(': ');
|
|
24
|
-
return title || preTitle;
|
|
25
|
-
}
|
|
26
|
-
function gitHasUncommittedChanges() {
|
|
27
|
-
// git diff-index --quiet HEAD -- || echo "untracked"
|
|
28
|
-
try {
|
|
29
|
-
node_child_process_1.default.execSync('git diff-index --quiet HEAD --', {
|
|
30
|
-
encoding: 'utf8',
|
|
31
|
-
});
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
catch {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* @returns true if there were changes
|
|
40
|
-
*/
|
|
41
|
-
function gitCommitAll(msg) {
|
|
42
|
-
// git commit -a -m "style(lint-all): $GIT_MSG" || true
|
|
43
|
-
const cmd = `git commit -a --no-verify -m "${msg}"`;
|
|
44
|
-
// const cmd = `git`
|
|
45
|
-
// const args = ['commit', '-a', '--no-verify', '-m', msg]
|
|
46
|
-
console.log((0, colors_1.grey)(cmd));
|
|
47
|
-
try {
|
|
48
|
-
node_child_process_1.default.execSync(cmd, {
|
|
49
|
-
stdio: 'inherit',
|
|
50
|
-
});
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* @returns true if there are not pushed commits.
|
|
59
|
-
*/
|
|
60
|
-
function gitIsAhead() {
|
|
61
|
-
// ahead=`git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`
|
|
62
|
-
const cmd = `git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`;
|
|
63
|
-
const stdout = execSync(cmd);
|
|
64
|
-
// console.log(`gitIsAhead: ${stdout}`)
|
|
65
|
-
return Number(stdout) > 0;
|
|
66
|
-
}
|
|
67
|
-
function gitPull() {
|
|
68
|
-
const cmd = 'git pull';
|
|
69
|
-
try {
|
|
70
|
-
node_child_process_1.default.execSync(cmd, {
|
|
71
|
-
stdio: 'inherit',
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
catch { }
|
|
75
|
-
}
|
|
76
|
-
function gitPush() {
|
|
77
|
-
// git push --set-upstream origin $CIRCLE_BRANCH && echo "pushed, exiting" && exit 0
|
|
78
|
-
let cmd = 'git push';
|
|
79
|
-
const branchName = gitCurrentBranchName();
|
|
80
|
-
if (branchName) {
|
|
81
|
-
cmd += ` --set-upstream origin ${branchName}`;
|
|
82
|
-
}
|
|
83
|
-
console.log((0, colors_1.grey)(cmd));
|
|
84
|
-
node_child_process_1.default.execSync(cmd, {
|
|
85
|
-
stdio: 'inherit',
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
function gitCurrentCommitSha(full = false) {
|
|
89
|
-
const sha = execSync('git rev-parse HEAD');
|
|
90
|
-
return full ? sha : sha.slice(0, 7);
|
|
91
|
-
}
|
|
92
|
-
function gitCurrentCommitTimestamp() {
|
|
93
|
-
return Number(execSync('git log -1 --format=%ct'));
|
|
94
|
-
}
|
|
95
|
-
function gitCurrentBranchName() {
|
|
96
|
-
return execSync('git rev-parse --abbrev-ref HEAD');
|
|
97
|
-
}
|
|
98
|
-
function gitCurrentRepoName() {
|
|
99
|
-
const originUrl = execSync('git config --get remote.origin.url');
|
|
100
|
-
return node_path_1.default.basename(originUrl, '.git');
|
|
101
|
-
}
|
|
102
|
-
function execSync(cmd) {
|
|
103
|
-
return node_child_process_1.default
|
|
104
|
-
.execSync(cmd, {
|
|
105
|
-
encoding: 'utf8',
|
|
106
|
-
// stdio: 'inherit', // no, otherwise we don't get the output returned
|
|
107
|
-
})
|
|
108
|
-
.trim();
|
|
109
|
-
}
|