Comparing version 0.0.3 to 0.0.4
126
dist/cli.js
@@ -100,2 +100,119 @@ // src/cli.ts | ||
} | ||
// src/encapsulate.ts | ||
var encapsulateConsole = function() { | ||
const createMockFn = () => { | ||
const calls = []; | ||
const mock = (...args) => calls.push([...args]); | ||
return [mock, calls]; | ||
}; | ||
const originalConsoleMethods = {}; | ||
const mockConsoleCalls = {}; | ||
for (const [key, value] of Object.entries(console)) { | ||
if (typeof value === `function`) { | ||
originalConsoleMethods[key] = value.bind(console); | ||
const [mockFn, calls] = createMockFn(); | ||
mockConsoleCalls[key] = calls; | ||
console[key] = mockFn; | ||
} | ||
} | ||
const restoreConsole = () => { | ||
for (const [key, value] of Object.entries(originalConsoleMethods)) { | ||
console[key] = value; | ||
} | ||
}; | ||
return { mockConsoleCalls, restoreConsole }; | ||
}; | ||
var withCapturedOutput = function(fn, options = { | ||
console: true, | ||
stdout: true, | ||
stderr: true | ||
}) { | ||
let originalStdoutWrite; | ||
let fakeStdout; | ||
if (options.stdout) { | ||
originalStdoutWrite = process.stdout.write.bind(process.stdout); | ||
fakeStdout = new FakeOut; | ||
process.stdout.write = fakeStdout.write.bind(fakeStdout); | ||
} | ||
let originalStderrWrite; | ||
let fakeStderr; | ||
if (options.stderr) { | ||
originalStderrWrite = process.stderr.write.bind(process.stderr); | ||
fakeStderr = new FakeOut; | ||
process.stderr.write = fakeStderr.write.bind(fakeStderr); | ||
} | ||
let restoreConsole; | ||
let mockConsoleCalls; | ||
if (options.console) { | ||
const consoleEncapsulation = encapsulateConsole(); | ||
restoreConsole = consoleEncapsulation.restoreConsole; | ||
mockConsoleCalls = consoleEncapsulation.mockConsoleCalls; | ||
} | ||
const returnValue = fn(); | ||
const restoreOutputs = () => { | ||
if (originalStdoutWrite) | ||
process.stdout.write = originalStdoutWrite; | ||
if (originalStderrWrite) | ||
process.stderr.write = originalStderrWrite; | ||
restoreConsole?.(); | ||
}; | ||
return { | ||
returnValue, | ||
capturedStdout: fakeStdout?.captured ?? [], | ||
capturedStderr: fakeStderr?.captured ?? [], | ||
mockConsoleCalls: mockConsoleCalls ?? {}, | ||
restoreOutputs | ||
}; | ||
}; | ||
function encapsulate(fn, options) { | ||
const { | ||
returnValue, | ||
capturedStdout, | ||
capturedStderr, | ||
mockConsoleCalls, | ||
restoreOutputs | ||
} = withCapturedOutput(fn, options); | ||
if (returnValue instanceof Promise) { | ||
const promise = returnValue.then((awaited) => { | ||
restoreOutputs(); | ||
return { | ||
returnValue: awaited, | ||
capturedStdout, | ||
capturedStderr, | ||
mockConsoleCalls | ||
}; | ||
}); | ||
return promise; | ||
} | ||
restoreOutputs(); | ||
return { | ||
returnValue, | ||
capturedStdout, | ||
capturedStderr, | ||
mockConsoleCalls | ||
}; | ||
} | ||
class FakeOut { | ||
captured = []; | ||
write(output, encodingOrCallback, callback) { | ||
let err; | ||
const refinedCallback = typeof encodingOrCallback === `function` ? encodingOrCallback : callback; | ||
try { | ||
if (typeof output === `string`) { | ||
this.captured.push(output); | ||
} else { | ||
const encoding = typeof encodingOrCallback === `string` ? encodingOrCallback : undefined; | ||
const encoded = Buffer.from(output).toString(encoding); | ||
this.captured.push(encoded); | ||
} | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
err = error; | ||
} | ||
} | ||
refinedCallback?.(err); | ||
return true; | ||
} | ||
} | ||
// src/tree.ts | ||
@@ -130,3 +247,5 @@ function required(arg) { | ||
}, logger = { | ||
error: (...args) => console.error(...args) | ||
error: (...args) => { | ||
console.error(...args); | ||
} | ||
}) { | ||
@@ -179,5 +298,5 @@ return (passed = process.argv) => { | ||
suppliedOptions, | ||
writeJsonSchema: (path2) => { | ||
writeJsonSchema: (filepath) => { | ||
const jsonSchema = zodToJsonSchema(optionsSchema); | ||
fs.writeFileSync(path2, JSON.stringify(jsonSchema, null, `\t`)); | ||
fs.writeFileSync(filepath, JSON.stringify(jsonSchema, null, `\t`)); | ||
} | ||
@@ -193,3 +312,4 @@ }; | ||
optional, | ||
encapsulate, | ||
cli | ||
}; |
{ | ||
"name": "comline", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"license": "MIT", | ||
@@ -35,3 +35,4 @@ "author": { | ||
"lint:eslint": "eslint .", | ||
"lint": "bun run lint:biome && bun run lint:eslint", | ||
"lint:types": "tsc --noEmit", | ||
"lint": "bun run lint:biome && bun run lint:eslint && bun run lint:types", | ||
"test": "vitest", | ||
@@ -38,0 +39,0 @@ "test:once": "vitest run", |
@@ -12,2 +12,3 @@ import * as fs from "node:fs" | ||
export * from "./option-parsers" | ||
export * from "./encapsulate" | ||
export * from "./tree" | ||
@@ -28,5 +29,9 @@ export * from "./flag" | ||
} | ||
: { | ||
parse: (arg: string) => T | ||
}) & { | ||
: T extends boolean | ||
? { | ||
parse: (arg: string) => boolean | ||
} | ||
: { | ||
parse: (arg: string) => T | ||
}) & { | ||
flag?: Flag | ||
@@ -83,3 +88,5 @@ required: T extends undefined ? false : true | ||
logger = { | ||
error: (...args: any[]) => console.error(...args), | ||
error: (...args: any[]) => { | ||
console.error(...args) | ||
}, | ||
}, | ||
@@ -161,5 +168,5 @@ ): (args: string[]) => { | ||
suppliedOptions, | ||
writeJsonSchema: (path) => { | ||
writeJsonSchema: (filepath) => { | ||
const jsonSchema = zodToJsonSchema(optionsSchema) | ||
fs.writeFileSync(path, JSON.stringify(jsonSchema, null, `\t`)) | ||
fs.writeFileSync(filepath, JSON.stringify(jsonSchema, null, `\t`)) | ||
}, | ||
@@ -166,0 +173,0 @@ } |
@@ -155,6 +155,6 @@ import type * as net from "node:net" | ||
if (returnValue instanceof Promise) { | ||
const promise = returnValue.then((returnValue) => { | ||
const promise = returnValue.then((awaited) => { | ||
restoreOutputs() | ||
return { | ||
returnValue, | ||
returnValue: awaited, | ||
capturedStdout, | ||
@@ -161,0 +161,0 @@ capturedStderr, |
@@ -15,3 +15,3 @@ import type { Tree, TreePath } from "./tree" | ||
const validPositionalArgs: string[] = [] | ||
let treePointer: Object = positionalArgTree | ||
let treePointer: object = positionalArgTree | ||
let argumentIndex = -1 | ||
@@ -18,0 +18,0 @@ if (positionalArgs === undefined || positionalArgs.length === 0) { |
27777
810