Skip to content

Commit

Permalink
refactor: use zx@8 api
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 26, 2024
1 parent 9337af3 commit 17b4407
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 51 deletions.
57 changes: 27 additions & 30 deletions src/main/js/index.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import {$ as _$, quiet, ProcessPromise, within, ProcessOutput} from 'zx'
import {$ as _$, ProcessPromise, within, ProcessOutput} from 'zx'
import childProcess from 'node:child_process'
import process from 'node:process'
import {DeepProxy} from '@qiwi/deep-proxy'
import {injectNmBinToPathEnv, isTemplateSignature, randomId} from './util.mjs'
import {isTemplateSignature, randomId} from './util.mjs'
import {semver} from './goods.mjs'

export * from 'zx'
export * from './goods.mjs'

ProcessOutput.prototype.valueOf = function () {
return this.toString()
}

ProcessOutput.prototype.toString = function () {
const str = this._combined.toString()
return $.trim
Expand All @@ -20,17 +16,17 @@ ProcessOutput.prototype.toString = function () {
}

export const $ = new DeepProxy(_$, ({name, DEFAULT, target: t, trapName, args}) => {
if (trapName === 'apply') {
if (!t.preferLocal) return DEFAULT

const env = t.env
try {
t.env = injectNmBinToPathEnv(t.env, t.cwd)
return t(...args)
} finally {
t.env = env
}
}
// if (trapName === 'apply') {
// if (!t.preferLocal) return DEFAULT
//
// const env = t.env
// try {
// t.env = injectNmBinToPathEnv(t.env, t.cwd)
// return t(...args)
// } finally {
// t.env = env
// }
// }

return DEFAULT
})
Expand All @@ -41,34 +37,35 @@ $.verbose = process.env.VERBOSE === 'true'

$.trim = true

$.raw = async (...args) => $.o({quote: v => v})(...args)
$.raw = async (...args) => $({quote: v => v})(...args)

// https://github.com/google/zx/pull/134
$.silent = async (...args) => quiet($(...args))
$.silent = async (...args) => $({quiet: true})(...args)

// https://github.com/google/zx/blob/c73ccb468cfd2340fb296e17a543eb2399b449ec/src/core.ts#L174
$.cwd = process.cwd()

$.o = $.opt =
(opts) =>
(...args) =>
ctx(($) => {
Object.assign(_$, opts)
const p = $(...args)
if (p._snapshot.nothrow) p._nothrow = true
return p
})
$.o = $.opt = $
// $.o = $.opt =
// (opts) =>
// (...args) =>
// ctx(($) => {
// Object.assign(_$, opts)
// const p = $(...args)
// if (p._snapshot.nothrow) p._nothrow = true
// return p
// })

export const createHook = (opts, name = randomId(), cb = (v) => v, configurable) => {
ProcessPromise.prototype[name] = function (...args) {
Object.assign(this._snapshot, opts)
if (this._snapshot.nothrow) this._nothrow = true
// if (this._snapshot.nothrow) this._nothrow = true

return cb(this, ...args)
}

const getP = (p, opts, $args) =>
p instanceof ProcessPromise ? p : $.opt(opts)(...$args)
p instanceof ProcessPromise ? p : $(opts)(...$args)

return (...args) => {
if (!configurable) {
Expand Down
21 changes: 0 additions & 21 deletions src/main/js/util.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'node:path'

export const isString = (obj) => typeof obj === 'string'

export const randomId = () => Math.random().toString(36).slice(2)
Expand All @@ -9,22 +7,3 @@ export const isTemplateSignature = (pieces, ...args) => {

return Array.isArray(pieces) && pieces.every(isString) && lastIdx === args.length
}

export const injectNmBinToPathEnv = (env, ...dirs) => {
const pathKey =
process.platform === 'win32'
? Object.keys(env)
.reverse()
.find((key) => key.toUpperCase() === 'PATH') || 'Path'
: 'PATH'
const pathValue = dirs
.map((c) => c && path.resolve(c, 'node_modules', '.bin'))
.concat(env[pathKey])
.filter(Boolean)
.join(path.delimiter)

return {
...env,
[pathKey]: pathValue,
}
}

0 comments on commit 17b4407

Please sign in to comment.