Skip to content

Commit

Permalink
chore: remove util/index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 29, 2023
1 parent 5bc68f3 commit 491e6e8
Show file tree
Hide file tree
Showing 33 changed files with 287 additions and 284 deletions.
3 changes: 2 additions & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Arg, ArgDefinition} from './interfaces/parser'
import {dirExists, fileExists, isNotFalsy} from './util/index'
import {dirExists, fileExists} from './util/fs'
import {Command} from './command'
import {URL} from 'node:url'
import {isNotFalsy} from './util/util'

/**
* Create a custom arg.
Expand Down
2 changes: 1 addition & 1 deletion src/cli-ux/action/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {stderr, stdout} from '../stream'
import {Options} from './types'
import {castArray} from '../../util'
import {castArray} from '../../util/util'
import {inspect} from 'node:util'

export interface ITask {
Expand Down
2 changes: 1 addition & 1 deletion src/cli-ux/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ActionBase} from './action/base'
import {PJSON} from '../interfaces/pjson'
import {requireJson} from '../util/index'
import {requireJson} from '../util/fs'
import simple from './action/simple'
import spinner from './action/spinner'

Expand Down
2 changes: 1 addition & 1 deletion src/cli-ux/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {maxBy} from '../util/index'
import {maxBy} from '../util/util'
import {stdtermwidth} from '../screen'
const wordwrap = require('wordwrap')

Expand Down
2 changes: 1 addition & 1 deletion src/cli-ux/styled/table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as F from '../../flags'
import * as Interfaces from '../../interfaces'
import {capitalize, sumBy} from '../../util'
import {capitalize, sumBy} from '../../util/util'
import chalk from 'chalk'
import {inspect} from 'node:util'
import {orderBy} from 'natural-orderby'
Expand Down
3 changes: 2 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from './interfaces/parser'
import {format, inspect} from 'node:util'
import {formatCommandDeprecationWarning, formatFlagDeprecationWarning, normalizeArgv, toConfiguredId} from './help/util'
import {requireJson, uniq} from './util/index'
import {stderr, stdout} from './cli-ux/stream'
import {CommandError} from './interfaces/errors'
import {Config} from './config'
Expand All @@ -27,6 +26,8 @@ import {PrettyPrintableError} from './errors'
import {aggregateFlags} from './util/aggregate-flags'
import chalk from 'chalk'
import {fileURLToPath} from 'node:url'
import {requireJson} from './util/fs'
import {uniq} from './util/util'
import {ux} from './cli-ux'

const pjson = requireJson<PJSON>(__dirname, '..', 'package.json')
Expand Down
4 changes: 3 additions & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {Hook, Hooks, PJSON, Topic} from '../interfaces'
import {Plugin as IPlugin, Options} from '../interfaces/plugin'
import {URL, fileURLToPath} from 'node:url'
import {arch, userInfo as osUserInfo, release, tmpdir, type} from 'node:os'
import {compact, getHomeDir, getPlatform, isProd, requireJson} from '../util/index'
import {compact, isProd} from '../util/util'
import {getHomeDir, getPlatform} from '../util/os'
import {join, sep} from 'node:path'
import {Command} from '../command'
import {Performance} from '../performance'
Expand All @@ -15,6 +16,7 @@ import WSL from 'is-wsl'
import {format} from 'node:util'
import {getHelpFlagAdditions} from '../help/util'
import {loadWithData} from '../module-loader'
import {requireJson} from '../util/fs'
import {settings} from '../settings'
import {stdout} from '../cli-ux/stream'

Expand Down
3 changes: 2 additions & 1 deletion src/config/plugin-loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as Plugin from './plugin'
import {Plugin as IPlugin, Options} from '../interfaces/plugin'
import {isProd, readJson} from '../util/index'
import {Debug} from './util'
import {PJSON} from '../interfaces'
import {Performance} from '../performance'
import {isProd} from '../util/util'
import {join} from 'node:path'
import {readJson} from '../util/fs'

// eslint-disable-next-line new-cap
const debug = Debug()
Expand Down
3 changes: 2 additions & 1 deletion src/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {CLIError, error} from '../errors'
import {Debug, getCommandIdPermutations, resolvePackage} from './util'
import {Plugin as IPlugin, PluginOptions} from '../interfaces/plugin'
import {compact, exists, isProd, mapValues, readJson, requireJson} from '../util/index'
import {compact, isProd, mapValues} from '../util/util'
import {dirname, join, parse, relative, sep} from 'node:path'
import {exists, readJson, requireJson} from '../util/fs'
import {loadWithData, loadWithDataFromManifest} from '../module-loader'
import {Command} from '../command'
import {Manifest} from '../interfaces/manifest'
Expand Down
3 changes: 2 additions & 1 deletion src/config/ts-node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as TSNode from 'ts-node'
import {Plugin, TSConfig} from '../interfaces'
import {isProd, readJsonSync} from '../util/index'
import {join, relative as pathRelative} from 'node:path'
import {Debug} from './util'
import {existsSync} from 'node:fs'
import {isProd} from '../util/util'
import {memoizedWarn} from '../errors'
import {readJsonSync} from '../util/fs'
import {settings} from '../settings'

// eslint-disable-next-line new-cap
Expand Down
2 changes: 1 addition & 1 deletion src/flags.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable valid-jsdoc */
import {BooleanFlag, CustomOptions, FlagDefinition, OptionFlag} from './interfaces'
import {dirExists, fileExists} from './util/index'
import {dirExists, fileExists} from './util/fs'
import {CLIError} from './errors'
import {URL} from 'node:url'
import {loadHelpClass} from './help'
Expand Down
3 changes: 2 additions & 1 deletion src/help/command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as Interfaces from '../interfaces'
import {HelpFormatter, HelpSection, HelpSectionRenderer} from './formatter'
import {castArray, compact, ensureArgObject, sortBy} from '../util'
import {castArray, compact, sortBy} from '../util/util'
import {Command} from '../command'
import {DocOpts} from './docopts'
import chalk from 'chalk'
import {ensureArgObject} from '../util/ensure-arg-object'
import stripAnsi from 'strip-ansi'

// Don't use os.EOL because we need to ensure that a string
Expand Down
2 changes: 1 addition & 1 deletion src/help/docopts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Command} from '../command'
import {ensureArgObject} from '../util/index'
import {ensureArgObject} from '../util/ensure-arg-object'
/**
* DocOpts - See http://docopt.org/.
*
Expand Down
2 changes: 1 addition & 1 deletion src/help/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Interfaces from '../interfaces'
import {compact, sortBy, uniqBy} from '../util/index'
import {compact, sortBy, uniqBy} from '../util/util'
import {formatCommandDeprecationWarning, getHelpFlagAdditions, standardizeIDFromArgv, toConfiguredId} from './util'
import {Command} from '../command'
import {CommandHelp} from './command'
Expand Down
2 changes: 1 addition & 1 deletion src/help/root.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Interfaces from '../interfaces'
import {HelpFormatter} from './formatter'
import {compact} from '../util/index'
import {compact} from '../util/util'
import stripAnsi from 'strip-ansi'

export default class RootHelp extends HelpFormatter {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {CLIError} from '../errors'
import chalk from 'chalk'
import {flagUsages} from './help'
import {renderList} from '../cli-ux/list'
import {uniq} from '../util/index'
import {uniq} from '../util/util'

export {CLIError} from '../errors'

Expand Down
2 changes: 1 addition & 1 deletion src/parser/help.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Flag, FlagUsageOptions} from '../interfaces/parser'
import chalk from 'chalk'
import {sortBy} from '../util/index'
import {sortBy} from '../util/util'

export function flagUsage(flag: Flag<any>, options: FlagUsageOptions = {}): [string, string | undefined] {
const label = []
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
ParserOutput,
ParsingToken,
} from '../interfaces/parser'
import {isTruthy, last, pickBy} from '../util/index'
import {isTruthy, last, pickBy} from '../util/util'
import {createInterface} from 'node:readline'

let debug: any
Expand Down
2 changes: 1 addition & 1 deletion src/parser/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
UnexpectedArgsError,
Validation,
} from './errors'
import {uniq} from '../util/index'
import {uniq} from '../util/util'

export async function validate(parse: {input: ParserInput; output: ParserOutput}): Promise<void> {
let cachedResolvedFlags: Record<string, unknown> | undefined
Expand Down
3 changes: 2 additions & 1 deletion src/util/cache-command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {ArgInput, FlagInput} from '../interfaces/parser'
import {ensureArgObject, pickBy} from './index'
import {Command} from '../command'
import {Plugin as IPlugin} from '../interfaces/plugin'
import {aggregateFlags} from './aggregate-flags'
import {cacheDefaultValue} from './cache-default-value'
import {ensureArgObject} from './ensure-arg-object'
import {pickBy} from './util'

// In order to collect static properties up the inheritance chain, we need to recursively
// access the prototypes until there's nothing left. This allows us to combine baseFlags
Expand Down
15 changes: 15 additions & 0 deletions src/util/ensure-arg-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {ArgInput} from '../interfaces/parser'
import {Command} from '../command'

/**
* Ensure that the provided args are an object. This is for backwards compatibility with v1 commands which
* defined args as an array.
*
* @param args Either an array of args or an object of args
* @returns ArgInput
*/
export function ensureArgObject(args?: any[] | ArgInput | {[name: string]: Command.Arg.Cached}): ArgInput {
return (
Array.isArray(args) ? (args ?? []).reduce((x, y) => ({...x, [y.name]: y}), {} as ArgInput) : args ?? {}
) as ArgInput
}
57 changes: 57 additions & 0 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {access, readFile, stat} from 'node:fs/promises'
import {join} from 'node:path'
import {readFileSync} from 'node:fs'

const debug = require('debug')

export function requireJson<T>(...pathParts: string[]): T {
return JSON.parse(readFileSync(join(...pathParts), 'utf8'))
}

export async function exists(path: string): Promise<boolean> {
try {
await access(path)
return true
} catch {
return false
}
}

export const dirExists = async (input: string): Promise<string> => {
if (!(await exists(input))) {
throw new Error(`No directory found at ${input}`)
}

const fileStat = await stat(input)
if (!fileStat.isDirectory()) {
throw new Error(`${input} exists but is not a directory`)
}

return input
}

export const fileExists = async (input: string): Promise<string> => {
if (!(await exists(input))) {
throw new Error(`No file found at ${input}`)
}

const fileStat = await stat(input)
if (!fileStat.isFile()) {
throw new Error(`${input} exists but is not a file`)
}

return input
}

export async function readJson<T = unknown>(path: string): Promise<T> {
debug('config')('readJson %s', path)
const contents = await readFile(path, 'utf8')
return JSON.parse(contents) as T
}

export function readJsonSync(path: string, parse: false): string
export function readJsonSync<T = unknown>(path: string, parse?: true): T
export function readJsonSync<T = unknown>(path: string, parse = true): T | string {
const contents = readFileSync(path, 'utf8')
return parse ? (JSON.parse(contents) as T) : contents
}
Loading

0 comments on commit 491e6e8

Please sign in to comment.