Skip to content

Commit

Permalink
add cli.js types
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Oct 4, 2019
1 parent 7796bf0 commit a8dc55f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 171 deletions.
278 changes: 107 additions & 171 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,155 +17,73 @@ const writeOutputFile = require('./writeOutputFile');

const EXIT_CODE_ERROR = 2;

/*:: type meowOptionsType = {
argv?: string[],
autoHelp: boolean,
autoVersion: boolean,
help: string,
flags: {
cache: {
type: string
},
"cache-location": {
type: string
},
config: {
default: boolean,
type: string
},
"config-basedir": {
type: string
},
"print-config": {
type: string
},
color: {
type: string
},
"custom-formatter": {
type: string
},
"custom-syntax": {
type: string
},
"disable-default-ignores": {
alias: string,
type: string
},
fix: {
type: string
},
formatter: {
alias: string,
default: "string",
type: string
},
help: {
alias: string,
type: string
},
"ignore-disables": {
alias: string,
type: string
},
"ignore-path": {
alias: string
},
"ignore-pattern": {
alias: string
},
"no-color": {
type: string
},
"output-file" : {
type: string
},
"report-needless-disables": {
alias: string
},
"max-warnings": {
alias: string
},
"stdin-filename": {
type: string
},
quiet: {
alias: string,
type: string,
default: boolean
},
syntax: {
alias: string
},
version: {
alias: string,
type: string
}
},
pkg: string,
} */

/*:: type cliType = {
flags: {
cache: any,
cacheLocation: any,
config: any,
configBasedir: any,
printConfig: boolean,
customFormatter: any,
customSyntax: any,
disableDefaultIgnores: boolean,
fix: any,
formatter: any,
h: boolean,
help: boolean,
ignoreDisables: boolean,
ignorePath: string,
ignorePattern: string,
maxWarnings: number,
outputFile: string,
quiet: any,
reportNeedlessDisables: boolean,
reportInvalidScopeDisables: boolean,
stdinFilename: any,
syntax: any,
v: string,
version: string,
allowEmptyInput: boolean
},
input: any,
help: any,
pkg: any,
showHelp: Function,
showVersion: Function
}*/

/*:: type optionBaseType = {
formater?: any,
cache?: boolean,
cacheLocation?: any,
codeFilename?: any,
configBasedir?: any,
configFile?: any,
configOverrides: {
quiet?: any,
},
printConfig?: any,
customSyntax?: any,
fix?: any,
ignoreDisables?: any,
ignorePath?: any,
outputFile?: string,
reportNeedlessDisables?: boolean,
reportInvalidScopeDisables?: boolean,
maxWarnings?: any,
syntax?: any,
disableDefaultIgnores?: any,
ignorePattern?: any,
allowEmptyInput?: boolean
}*/

const meowOptions /*: meowOptionsType*/ = {
/**
* @typedef {object} CLIFlags
* @property {boolean} [cache]
* @property {string} [cacheLocation]
* @property {string | false} config
* @property {string} [configBasedir]
* @property {string} [customSyntax]
* @property {string} [printConfig]
* @property {string} [color]
* @property {string} [customFormatter]
* @property {boolean} [disableDefaultIgnores]
* @property {boolean} [fix]
* @property {string} [formatter="string"]
* @property {string} [help]
* @property {boolean} [ignoreDisables]
* @property {string} [ignorePath]
* @property {string} [ignorePattern]
* @property {string} [noColor]
* @property {string} [outputFile]
* @property {string} [stdinFilename]
* @property {boolean} [reportNeedlessDisables]
* @property {boolean} [reportInvalidScopeDisables]
* @property {boolean} [maxWarnings]
* @property {string | boolean} quiet
* @property {string} [syntax]
* @property {string} [version]
* @property {boolean} [allowEmptyInput]
*/

/**
* @typedef {object} CLIOptions
* @property {any} input
* @property {any} help
* @property {any} pkg
* @property {Function} showHelp
* @property {Function} showVersion
* @property {CLIFlags} flags
*/

/**
* @typedef {object} OptionBaseType
* @property {any} formatter
* @property {boolean} [cache]
* @property {string} [configFile]
* @property {string} [cacheLocation]
* @property {string} [customSyntax]
* @property {string} [codeFilename]
* @property {string} [configBasedir]
* @property {{ quiet?: any }} configOverrides
* @property {any} [printConfig]
* @property {any} [printConfig]
* @property {boolean} [fix]
* @property {boolean} [ignoreDisables]
* @property {any} [ignorePath]
* @property {string} [outputFile]
* @property {boolean} [reportNeedlessDisables]
* @property {boolean} [reportInvalidScopeDisables]
* @property {boolean} [disableDefaultIgnores]
* @property {boolean} [maxWarnings]
* @property {string} [syntax]
* @property {any} [ignorePattern]
* @property {boolean} [allowEmptyInput]
* @property {string} [files]
* @property {string} [code]
*/

const meowOptions = {
autoHelp: false,
autoVersion: false,
help: `
Expand Down Expand Up @@ -393,11 +311,17 @@ const meowOptions /*: meowOptionsType*/ = {
},
},
pkg: require('../package.json'),
argv: /** @type {string[]} */ ([]),
};

module.exports = (argv /*: string[]*/) /*: Promise<void>|void*/ => {
/**
* @param {string[]} argv
* @return {Promise<any>}
*/
module.exports = (argv) => {
meowOptions.argv = argv;
const cli /*: cliType*/ = meow(meowOptions);
/** @type {CLIOptions} */
const cli = meow(meowOptions);

const invalidOptionsMessage = checkInvalidCLIOptions(meowOptions.flags, cli.flags);

Expand All @@ -416,7 +340,8 @@ module.exports = (argv /*: string[]*/) /*: Promise<void>|void*/ => {
formatter = dynamicRequire(customFormatter);
}

const optionsBase /*: optionBaseType*/ = {
/** @type {OptionBaseType} */
const optionsBase = {
formatter,
configOverrides: {},
};
Expand Down Expand Up @@ -504,37 +429,44 @@ module.exports = (argv /*: string[]*/) /*: Promise<void>|void*/ => {
optionsBase.maxWarnings = maxWarnings;
}

if (cli.flags.help || cli.flags.h) {
if (cli.flags.help) {
cli.showHelp(0);

return;
return Promise.resolve();
}

if (cli.flags.version || cli.flags.v) {
if (cli.flags.version) {
cli.showVersion();

return;
return Promise.resolve();
}

if (cli.flags.allowEmptyInput) {
optionsBase.allowEmptyInput = cli.flags.allowEmptyInput;
}

return Promise.resolve()
.then(() => {
// Add input/code into options
if (cli.input.length) {
return Object.assign({}, optionsBase, {
files: cli.input,
});
}

return getStdin().then((stdin) =>
Object.assign({}, optionsBase, {
code: stdin,
}),
);
})
.then(
/**
* @return {Promise<OptionBaseType>}
*/
() => {
// Add input/code into options
if (cli.input.length) {
return Promise.resolve(
Object.assign({}, optionsBase, {
files: /** @type {string} */ (cli.input),
}),
);
}

return getStdin().then((stdin) =>
Object.assign({}, optionsBase, {
code: /** @type {string} */ (stdin),
}),
);
},
)
.then((options) => {
if (cli.flags.printConfig) {
return printConfig(options)
Expand Down Expand Up @@ -602,7 +534,11 @@ module.exports = (argv /*: string[]*/) /*: Promise<void>|void*/ => {
});
};

function handleError(err /*: { stack: any, code: any }*/) /*: void */ {
/**
* @param {{ stack: any, code: any }} err
* @returns {void}
*/
function handleError(err) {
console.log(err.stack); // eslint-disable-line no-console
const exitCode = typeof err.code === 'number' ? err.code : 1;

Expand Down
29 changes: 29 additions & 0 deletions types/meow/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface Flag {
alias?: string,
type?: string | boolean,
default?: any
}

interface Options {
argv?: string[],
autoHelp: boolean,
autoVersion: boolean,
help: string,
pkg: object,
flags: {[k: string]: Flag }
}

declare var meowFn: {
(options: Options): {
input: any,
help: any,
pkg: any,
showHelp: Function,
showVersion: Function,
flags: any // TODO TYPES
};
};

declare module 'meow' {
export = meowFn;
}

0 comments on commit a8dc55f

Please sign in to comment.