Skip to content

Commit

Permalink
refactor: move cmd builder to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Mar 16, 2024
1 parent e2310cb commit 60123a5
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Webpod
Copyright (c) 2024 Webpod

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
– eat all kinds of materials.
</sup>

<p style="font-size: 280%">🔬🧫 </p>
## 🔬🧫

> This subproject is a kind of experiment, addressed to the [google/zx/issues/589](https://github.com/google/zx/issues/589).
Just a testing ground for verifying ideas and approaches aimed at improve the [zx](https://github.com/google/zx) architecture.
Expand Down
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
"require": "./target/cjs/spawn.cjs",
"import": "./target/esm/spawn.mjs",
"default": "./target/esm/spawn.mjs"
},
"./zurk": {
"types": "./target/dts/zurk.d.ts",
"require": "./target/cjs/zurk.cjs",
"import": "./target/esm/zurk.mjs",
"default": "./target/esm/zurk.mjs"
},
"./util": {
"types": "./target/dts/util.d.ts",
"require": "./target/cjs/util.cjs",
"import": "./target/esm/util.mjs",
"default": "./target/esm/util.mjs"
}
},
"module": "target/esm/index.mjs",
Expand All @@ -28,7 +40,7 @@
],
"scripts": {
"build": "concurrently 'npm:build:*'",
"build:js": "node ./src/scripts/build.mjs --entry='./src/main/ts/index.ts:./src/main/ts/spawn.ts'",
"build:js": "node ./src/scripts/build.mjs --entry='./src/main/ts/index.ts:./src/main/ts/util.ts:./src/main/ts/spawn.ts:./src/main/ts/zurk.ts'",
"build:dts": "tsc --emitDeclarationOnly --outDir target/dts",
"build:docs": "typedoc --options src/main/typedoc",
"build:stamp": "npx buildstamp",
Expand All @@ -49,12 +61,12 @@
"homepage": "https://github.com/webpod/zurk#readme",
"dependencies": {},
"devDependencies": {
"@types/node": "^20.11.27",
"@types/node": "^20.11.28",
"c8": "^9.1.0",
"concurrently": "^8.2.2",
"esbuild": "^0.20.2",
"esbuild-node-externals": "^1.13.0",
"esbuild-plugin-entry-chunks": "^0.1.8",
"esbuild-plugin-entry-chunks": "^0.1.11",
"eslint": "^8.57.0",
"eslint-config-qiwi": "^2.1.3",
"fast-glob": "^3.3.2",
Expand Down
12 changes: 12 additions & 0 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Stream } from 'node:stream'

export const noop = () => { /* noop */ }

export type PromiseResolve<T = any> = (value: T | PromiseLike<T>) => void
Expand Down Expand Up @@ -70,3 +72,13 @@ export const substitute: TSubstitute = (arg: any) =>
(typeof arg?.stdout === 'string')
? arg.stdout.replace(/\n$/, '')
: `${arg}`

export const parseInput = (input: any): string | Buffer | Stream | null => {
if (typeof input === 'string' || input instanceof Buffer || input instanceof Stream) return input

if (typeof input?.stdout === 'string') return input.stdout

if (input?.ctx) return parseInput(input.ctx.stdout)

return null
}
10 changes: 2 additions & 8 deletions src/main/ts/x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
isStringLiteral,
assign,
quote,
buildCmd
buildCmd,
parseInput
} from './util.js'
import { pipeMixin } from './mixin/pipe.js'
import { killMixin } from './mixin/kill.js'
Expand Down Expand Up @@ -126,10 +127,3 @@ export const applyMixins = ($: TShell, result: TZurk | TZurkPromise | TShellOpti
return m($, r as any, ctx)
}, result)
}

export const parseInput = (input: TShellOptions['input']): TShellCtx['input'] => {
if (typeof (input as TShellResponseSync)?.stdout === 'string') return (input as TShellResponseSync).stdout
if ((input as TShellResponse)?.ctx) return (input as TShellResponse).ctx.stdout

return input as TShellCtx['input']
}
13 changes: 9 additions & 4 deletions src/main/ts/zurk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import util from 'node:util'
import {
invoke,
normalizeCtx,
TSpawnCtxNormalized,
TSpawnResult,
TSpawnListeners,
type TSpawnCtxNormalized,
type TSpawnResult,
type TSpawnListeners,
} from './spawn.js'
import { isPromiseLike, makeDeferred, type Promisified, type TVoidCallback } from './util.js'
import {
isPromiseLike,
makeDeferred,
type Promisified,
type TVoidCallback
} from './util.js'

export const ZURK = Symbol('Zurk')

Expand Down
4 changes: 4 additions & 0 deletions src/test/js/index.test.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const assert = require('node:assert')
const { describe, it } = require('node:test')
const { $ } = require('zurk')
const { buildCmd } = require('zurk/util')

describe('cjs entry', () => {
it('$ is callable', () => {
assert.equal(typeof $, 'function')
})
it('buildCmd is callable', () => {
assert.equal(typeof buildCmd, 'function')
})
})
4 changes: 4 additions & 0 deletions src/test/js/index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import assert from 'node:assert'
import { describe, it } from 'node:test'
import { $ } from 'zurk'
import { buildCmd } from 'zurk/util'

describe('mjs entry', () => {
it('$ is callable', () => {
assert.equal(typeof $, 'function')
})
it('buildCmd is callable', () => {
assert.equal(typeof buildCmd, 'function')
})
})
2 changes: 1 addition & 1 deletion src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'node:assert'
import { describe, it } from 'node:test'
import { invoke, zurk, $, buildCmd, exec } from '../../main/ts'
import { invoke, zurk, $, buildCmd, exec } from '../../main/ts/index.js'

describe('index', () => {
it('has proper exports', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ts/x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as fs from 'node:fs'
import * as path from 'node:path'
import * as os from 'node:os'
import { describe, it } from 'node:test'
import { $ } from '../../main/ts/x.js'
import { Stream } from 'node:stream'
import { $ } from '../../main/ts/x.js'

const __dirname = new URL('.', import.meta.url).pathname
const fixtures = path.resolve(__dirname, '../fixtures')
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==

"@types/node@^20.11.27":
version "20.11.27"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.27.tgz#debe5cfc8a507dd60fe2a3b4875b1604f215c2ac"
integrity sha512-qyUZfMnCg1KEz57r7pzFtSGt49f6RPkPBis3Vo4PbS7roQEDn22hiHzl/Lo1q4i4hDEgBJmBF/NTNg2XR0HbFg==
"@types/node@^20.11.28":
version "20.11.28"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.28.tgz#4fd5b2daff2e580c12316e457473d68f15ee6f66"
integrity sha512-M/GPWVS2wLkSkNHVeLkrF2fD5Lx5UC4PxA0uZcKc6QqbIQUJyW1jVjueJYi1z8n0I5PxYrtpnPnWglE+y9A0KA==
dependencies:
undici-types "~5.26.4"

Expand Down Expand Up @@ -883,10 +883,10 @@ esbuild-node-externals@^1.13.0:
find-up "^5.0.0"
tslib "^2.4.1"

esbuild-plugin-entry-chunks@^0.1.8:
version "0.1.8"
resolved "https://registry.yarnpkg.com/esbuild-plugin-entry-chunks/-/esbuild-plugin-entry-chunks-0.1.8.tgz#66600276510c1b5a042b14cded95e9d9b74dfcb6"
integrity sha512-iKX18Uj8WTiRygwOGNa4S66pvNeexcvH1ohs/P5wV0fVGCRRzHzCiyI1/HShCVkC8HOnxvGWiG006PmsBHmS9Q==
esbuild-plugin-entry-chunks@^0.1.11:
version "0.1.11"
resolved "https://registry.yarnpkg.com/esbuild-plugin-entry-chunks/-/esbuild-plugin-entry-chunks-0.1.11.tgz#6397a0a6f50b6f319df01b56a6ea326bd3eb3dd2"
integrity sha512-lSFwPa7YYdp0iSg3ukKKlHrJ735WpsqlkfsYzUHXYhFYRHPuYaf5qkNyWulyDL7ycPlguCo/Q4ALCsi1mu6waA==
dependencies:
depseek "0.4.0"

Expand Down

0 comments on commit 60123a5

Please sign in to comment.