Skip to content

Commit

Permalink
chore(typescript): add typings for shell-quote@1.7.2
Browse files Browse the repository at this point in the history
P010-32
  • Loading branch information
unicornware committed Sep 17, 2021
1 parent 6e72cd6 commit 70156cb
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/grease/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = {
...RULES_SPELLCHECKER[1],
skipWords: RULES_SPELLCHECKER[1].skipWords.concat([
'autogeneration',
'boop',
'cmd',
'depchecker',
'dtag',
'dto',
Expand All @@ -27,6 +29,7 @@ module.exports = {
'gitdir',
'infile',
'lifecycles',
'lookups',
'msg',
'namespace',
'nullish',
Expand Down Expand Up @@ -60,6 +63,17 @@ module.exports = {
rules: {
'prefer-spread': 0
}
},
{
files: ['src/typings/shell-quote/index.d.ts'],
rules: {
'jsdoc/no-undefined-types': [
1,
{
definedTypes: ['Env', 'ParseOptions', 'ParsedShellCommand']
}
]
}
}
])
}
86 changes: 86 additions & 0 deletions packages/grease/src/typings/shell-quote/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
declare module 'shell-quote' {
/**
* Environment variable lookup function.
*/
export type EnvFn = (key: string) => Record<string, string> | string

/**
* Object with environment variables.
*/
export type EnvObject = Record<string, string>

/**
* Typically, `env` is an object, but it can also be a lookup function.
*
* When `env(key)` returns a string, the output is identical to `env[key]`.
*
* If an object is returned, an object will be inserted into the resulting
* array (like an `OperatorObject`).
*/
export type Env = EnvFn | EnvObject

/**
* Object representing a parsed shell operator.
*/
export type OperatorObject = Record<'op', string>

/**
* Shell command parsing options.
*/
export type ParseOptions = {
escape?: string
}

/**
* Array containing parsed shell commands and/or operator objects.
*/
export type ParsedShellCommand = string[] | (OperatorObject | string)[]

/**
* Return an array of arguments from the quoted string `cmd`.
*
*
* Interpolate embedded bash-style `$VARNAME` and `${VARNAME}` variables with
* the `env` object which, like bash, will replace `undefined` variables with
* `""`.
*
* Typically, `env` is an object, but it can also be a lookup function.
*
* When `env(key)` returns a string, the output is identical to `env[key]`.
*
* If an object is returned, an object will be inserted into the resulting
* array (like an `OperatorObject`).
*
* When a bash operator is encountered, the element in the array with be an
* object with an `'op'` key set to the operator string.
*
* @example
* parse('a "b c" \\$def \'it\\\'s great\'')
* // ['a', 'b c', '\\$def', 'it\'s great']
* @example
* parse('beep --boop="$PWD"', { PWD: '/home/robot' })
* // ['beep', '--boop=/home/robot']
* @example
* parse('beep > boop # > kaboom')
* // ['beep', { op: '>' }, 'boop', { comment: '> kaboom' }]
*
* @param {string} cmd - Shell command to parse
* @param {Env} [env={}] - Environment variables object or lookup function
* @param {ParseOptions} [opts={escape:'\\'}] - Shell command parsing options
* @param {string} [opts.escape='\\'] - Custom escape character
* @return {ParsedShellCommand} Parsed shell commands and/or operator objects
*/
export const parse: (
cmd: string,
env?: Env,
opts?: ParseOptions
) => ParsedShellCommand

/**
* Converts `args` into a quoted string suitable for using in shell commands.
*
* @param {string[]} args - List of string arguments (shell command as array)
* @return {string} Quoted string suitable for using in shell commands
*/
export const quote: (args: string[]) => string
}

0 comments on commit 70156cb

Please sign in to comment.