Skip to content

Commit

Permalink
[dev/run] typescriptify the dev/run helper (elastic#32705) (elastic#3…
Browse files Browse the repository at this point in the history
…2735)

Simple typescript conversion of the `src/dev/run` helpers.
  • Loading branch information
Spencer authored Mar 8, 2019
1 parent 9fbc458 commit 87170fd
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 46 deletions.
19 changes: 12 additions & 7 deletions src/dev/run/fail.js → src/dev/run/fail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ import { inspect } from 'util';

const FAIL_TAG = Symbol('fail error');

export function createFailError(reason, exitCode = 1) {
const error = new Error(reason);
error.exitCode = exitCode;
error[FAIL_TAG] = true;
return error;
interface FailError extends Error {
exitCode: number;
[FAIL_TAG]: true;
}

export function isFailError(error) {
export function createFailError(reason: string, exitCode = 1): FailError {
return Object.assign(new Error(reason), {
exitCode,
[FAIL_TAG]: true as true,
});
}

export function isFailError(error: any): error is FailError {
return Boolean(error && error[FAIL_TAG]);
}

export function combineErrors(errors) {
export function combineErrors(errors: Array<Error | FailError>) {
if (errors.length === 1) {
return errors[0];
}
Expand Down
35 changes: 27 additions & 8 deletions src/dev/run/flags.js → src/dev/run/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ import { relative } from 'path';

import getopts from 'getopts';

export function getFlags(argv) {
return getopts(argv, {
export interface Flags {
verbose: boolean;
quiet: boolean;
silent: boolean;
debug: boolean;
help: boolean;
_: string[];

[key: string]: undefined | boolean | string | string[];
}

export function getFlags(argv: string[]): Flags {
const { verbose, quiet, silent, debug, help, _, ...others } = getopts(argv, {
alias: {
v: 'verbose',
},
Expand All @@ -32,14 +43,23 @@ export function getFlags(argv) {
silent: false,
debug: false,
help: false,
}
},
});

return {
verbose,
quiet,
silent,
debug,
help,
_,
...others,
};
}

export function getHelp() {
return (
`
node ${relative(process.cwd(), process.argv[1], '.js')}
return `
node ${relative(process.cwd(), process.argv[1])}
Runs a dev task
Expand All @@ -50,6 +70,5 @@ export function getHelp() {
--silent Don't log anything
--help Show this message
`
);
`;
}
25 changes: 0 additions & 25 deletions src/dev/run/index.d.ts

This file was deleted.

File renamed without changes.
8 changes: 4 additions & 4 deletions src/dev/run/run.js → src/dev/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
* under the License.
*/

import { ToolingLog, pickLevelFromFlags } from '@kbn/dev-utils';
import { pickLevelFromFlags, ToolingLog } from '@kbn/dev-utils';
import { isFailError } from './fail';
import { getFlags, getHelp } from './flags';
import { Flags, getFlags, getHelp } from './flags';

export async function run(body) {
export async function run(body: (args: { log: ToolingLog; flags: Flags }) => Promise<void> | void) {
const flags = getFlags(process.argv.slice(2));

if (flags.help) {
Expand All @@ -31,7 +31,7 @@ export async function run(body) {

const log = new ToolingLog({
level: pickLevelFromFlags(flags),
writeTo: process.stdout
writeTo: process.stdout,
});

try {
Expand Down
10 changes: 10 additions & 0 deletions src/dev/run_i18n_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ run(
);
}

if (typeof path === 'boolean' || typeof includeConfig === 'boolean') {
throw createFailError(
`${chalk.white.bgRed(' I18N ERROR ')} --path and --include-config require a value`
);
}

if (typeof fix !== 'boolean') {
throw createFailError(`${chalk.white.bgRed(' I18N ERROR ')} --fix can't have a value`);
}

const config = await mergeConfigs(includeConfig);
const defaultMessages = await extractDefaultMessages({ path, config });

Expand Down
6 changes: 6 additions & 0 deletions src/dev/run_i18n_extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ run(
);
}

if (typeof path === 'boolean' || typeof includeConfig === 'boolean') {
throw createFailError(
`${chalk.white.bgRed(' I18N ERROR ')} --path and --include-config require a value`
);
}

const config = await mergeConfigs(includeConfig);
const defaultMessages = await extractDefaultMessages({ path, config });

Expand Down
25 changes: 23 additions & 2 deletions src/dev/run_i18n_integrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,30 @@ run(
);
}

if (Array.isArray(target)) {
if (typeof target === 'boolean' || Array.isArray(target)) {
throw createFailError(
`${chalk.white.bgRed(' I18N ERROR ')} --target should be specified only once.`
`${chalk.white.bgRed(
' I18N ERROR '
)} --target should be specified only once and must have a value.`
);
}

if (typeof path === 'boolean' || typeof includeConfig === 'boolean') {
throw createFailError(
`${chalk.white.bgRed(' I18N ERROR ')} --path and --include-config require a value`
);
}

if (
typeof ignoreIncompatible !== 'boolean' ||
typeof ignoreUnused !== 'boolean' ||
typeof ignoreMissing !== 'boolean' ||
typeof dryRun !== 'boolean'
) {
throw createFailError(
`${chalk.white.bgRed(
' I18N ERROR '
)} --ignore-incompatible, --ignore-unused, --ignore-missing, and --dry-run can't have values`
);
}

Expand Down

0 comments on commit 87170fd

Please sign in to comment.