Comparing version 0.1.3 to 0.1.4
@@ -67,6 +67,8 @@ import { Flat, ToPath, Tree, TreeMap, TreePath } from 'treetrunks'; | ||
routes?: Routes; | ||
debugOutput?: boolean; | ||
discoverConfigPath?: (positionalArgs: TreePath<Routes>) => string | undefined; | ||
}; | ||
type CliRoutes<CLI extends CommandLineInterface<any>> = CLI[`routes`]; | ||
declare function cli<CLI extends CommandLineInterface<Routes>, Routes extends Tree = Exclude<CLI[`routes`], undefined>>({ cliName, routes, routeOptions, discoverConfigPath, }: CLI, logger?: { | ||
declare function cli<CLI extends CommandLineInterface<Routes>, Routes extends Tree = Exclude<CLI[`routes`], undefined>>({ cliName, routes, routeOptions, debugOutput, discoverConfigPath, }: CLI, logger?: { | ||
info: ((...args: any[]) => void) | undefined; | ||
error: (...args: any[]) => void; | ||
@@ -73,0 +75,0 @@ }): (args: string[]) => { |
@@ -115,2 +115,24 @@ // src/cli.ts | ||
// src/encapsulate.ts | ||
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; | ||
} | ||
} | ||
function encapsulateConsole() { | ||
@@ -209,25 +231,2 @@ const createMockFn = () => { | ||
} | ||
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; | ||
} | ||
} | ||
// ../treetrunks/src/treetrunks.ts | ||
@@ -265,3 +264,2 @@ function required(arg) { | ||
} | ||
// src/cli.ts | ||
@@ -285,9 +283,14 @@ function retrieveArgValue(argument, flag) { | ||
routeOptions, | ||
debugOutput = false, | ||
discoverConfigPath = () => path.join(process.cwd(), `${cliName}.config.json`) | ||
}, logger = { | ||
info: debugOutput ? (...args) => { | ||
console.log(`[${cliName}]:`, ...args); | ||
} : undefined, | ||
error: (...args) => { | ||
console.error(...args); | ||
console.error(`[${cliName}]:`, ...args); | ||
} | ||
}) { | ||
return (passed = process.argv) => { | ||
logger.info?.(`passed args:`, passed); | ||
let failedValidation = false; | ||
@@ -305,3 +308,5 @@ let optionsFromConfig; | ||
if (configFilePath) { | ||
logger.info?.(`looking for config file at:`, configFilePath); | ||
if (fs.existsSync(configFilePath)) { | ||
logger.info?.(`config file was found`); | ||
const configText = fs.readFileSync(configFilePath, `utf-8`); | ||
@@ -313,2 +318,3 @@ const optionsFromConfigJson = JSON.parse(configText); | ||
} | ||
logger.info?.(`options from config:`, optionsFromConfig); | ||
const argumentEntries = Object.entries(options); | ||
@@ -342,3 +348,5 @@ const optionsFromCommandLineEntries = argumentEntries.map((entry) => { | ||
const suppliedOptionsUnparsed = Object.assign(optionsFromConfig ?? {}, optionsFromCommandLine); | ||
logger.info?.(`options from command line:`, optionsFromCommandLine); | ||
const suppliedOptions = optionsSchema.parse(suppliedOptionsUnparsed); | ||
logger.info?.(`final options parsed:`, suppliedOptions); | ||
return { | ||
@@ -345,0 +353,0 @@ inputs: { |
{ | ||
"name": "comline", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"license": "MIT", | ||
@@ -25,5 +25,5 @@ "author": { | ||
"devDependencies": { | ||
"@types/node": "22.7.5", | ||
"@types/node": "22.7.6", | ||
"@types/tmp": "0.2.6", | ||
"bun-types": "1.1.30", | ||
"bun-types": "1.1.31", | ||
"concurrently": "9.0.1", | ||
@@ -30,0 +30,0 @@ "tmp": "0.2.3", |
@@ -71,2 +71,3 @@ import * as fs from "node:fs" | ||
routes?: Routes | ||
debugOutput?: boolean | ||
discoverConfigPath?: (positionalArgs: TreePath<Routes>) => string | undefined | ||
@@ -104,2 +105,3 @@ } | ||
routeOptions, | ||
debugOutput = false, | ||
discoverConfigPath = () => | ||
@@ -109,4 +111,9 @@ path.join(process.cwd(), `${cliName}.config.json`), | ||
logger = { | ||
info: debugOutput | ||
? (...args: any[]) => { | ||
console.log(`[${cliName}]:`, ...args) | ||
} | ||
: undefined, | ||
error: (...args: any[]) => { | ||
console.error(...args) | ||
console.error(`[${cliName}]:`, ...args) | ||
}, | ||
@@ -119,2 +126,4 @@ }, | ||
return (passed = process.argv) => { | ||
logger.info?.(`passed args:`, passed) | ||
type Options = CLI[`routeOptions`][keyof CLI[`routeOptions`]] | ||
@@ -142,3 +151,5 @@ | ||
if (configFilePath) { | ||
logger.info?.(`looking for config file at:`, configFilePath) | ||
if (fs.existsSync(configFilePath)) { | ||
logger.info?.(`config file was found`) | ||
const configText = fs.readFileSync(configFilePath, `utf-8`) | ||
@@ -150,2 +161,3 @@ const optionsFromConfigJson = JSON.parse(configText) | ||
} | ||
logger.info?.(`options from config:`, optionsFromConfig) | ||
const argumentEntries = Object.entries(options) | ||
@@ -201,3 +213,5 @@ const optionsFromCommandLineEntries = argumentEntries | ||
) | ||
logger.info?.(`options from command line:`, optionsFromCommandLine) | ||
const suppliedOptions = optionsSchema.parse(suppliedOptionsUnparsed) | ||
logger.info?.(`final options parsed:`, suppliedOptions) | ||
return { | ||
@@ -204,0 +218,0 @@ inputs: { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35289
991