Skip to content

Commit

Permalink
support running gts on individual files instead of the whole project
Browse files Browse the repository at this point in the history
  • Loading branch information
jinwoo committed Oct 13, 2017
1 parent c1555c6 commit 39fee93
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 30 deletions.
46 changes: 37 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export interface Options {
logger: Logger;
}

export type VerbFunction = (options: Options, fix?: boolean) =>
Promise<boolean>;
export type VerbFilesFunction =
(options: Options, files: string[], fix?: boolean) => Promise<boolean>;

const logger: Logger = console;

const cli = meow(`
Usage
$ gts <verb> [options]
$ gts <verb> [<file>...] [options]
Verb can be:
init Adds default npm scripts to your package.json.
Expand All @@ -57,6 +57,7 @@ const cli = meow(`
$ gts init -y
$ gts check
$ gts fix
$ gts fix src/file1.ts src/file2.ts
$ gts clean
`);

Expand All @@ -67,7 +68,7 @@ function usage(msg?: string): void {
cli.showHelp(1);
}

async function run(verb: string): Promise<boolean> {
async function run(verb: string, files: string[]): Promise<boolean> {
const options: Options = {
dryRun: cli.flags.dryRun || false,
gtsRootDir: `${process.cwd()}/node_modules/gts`,
Expand All @@ -81,13 +82,15 @@ async function run(verb: string): Promise<boolean> {
if (verb === 'init') {
return await init(options);
}
const lint: VerbFunction = require('./lint').lint;
const format: VerbFunction = require('./format').format;
const lint: VerbFilesFunction = require('./lint').lint;
const format: VerbFilesFunction = require('./format').format;
switch (verb) {
case 'check':
return (await lint(options) && await format(options));
return (await lint(options, files) && await format(options, files));
case 'fix':
return (await lint(options, true) && await format(options, true));
return (
await lint(options, files, true) &&
await format(options, files, true));
case 'clean':
return await clean(options);
default:
Expand All @@ -98,11 +101,11 @@ async function run(verb: string): Promise<boolean> {

updateNotifier({pkg: cli.pkg}).notify();

if (cli.input.length !== 1) {
if (cli.input.length < 1) {
usage();
}

run(cli.input[0]).then(success => {
run(cli.input[0], cli.input.slice(1)).then(success => {
if (!success) {
process.exit(1);
}
Expand Down
12 changes: 8 additions & 4 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ const baseArgs =
* Run tslint fix and clang fix with the default configuration
* @param options
* @param fix whether to automatically fix the format
* @param files files to format
*/
export function format(options: Options, fix = false): Promise<boolean> {
export function format(
options: Options, files: string[] = [], fix = false): Promise<boolean> {
const program = createProgram(options);
const srcFiles = program.getSourceFiles()
.map(sourceFile => sourceFile.fileName)
.filter(f => !f.endsWith('.d.ts'));
const srcFiles = files.length > 0 ?
files :
program.getSourceFiles()
.map(sourceFile => sourceFile.fileName)
.filter(f => !f.endsWith('.d.ts'));

return fix ? fixFormat(srcFiles) : checkFormat(srcFiles);
}
Expand Down
10 changes: 6 additions & 4 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ import {Options} from './cli';

/**
* Run tslint with the default configuration. Returns true on success.
* @param fix automatically fix linter errors
* @param options gts options
* @param files files to run linter on
* @param fix automatically fix linter errors
*/
export function lint(options: Options, fix = false): boolean {
export function lint(
options: Options, files: string[] = [], fix = false): boolean {
const tslintConfigPath = path.join(options.gtsRootDir, 'tslint.json');

const program = createProgram(options);
const configuration =
Configuration.findConfiguration(tslintConfigPath, '').results;
const linter = new Linter({fix: fix, formatter: 'codeFrame'}, program);
const files = Linter.getFileNames(program);
files.forEach(file => {
const srcFiles = files.length > 0 ? files : Linter.getFileNames(program);
srcFiles.forEach(file => {
const fileContents = program.getSourceFile(file).getFullText();
linter.lint(file, fileContents, configuration);
});
Expand Down
22 changes: 19 additions & 3 deletions test/test-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test.serial('lint should return false on bad code', async t => {
'a.ts': BAD_CODE,
},
async () => {
const okay = lint.lint(OPTIONS, false);
const okay = lint.lint(OPTIONS);
t.is(okay, false);
});
});
Expand Down Expand Up @@ -112,7 +112,7 @@ test.serial(
'b.ts': BAD_CODE
},
async () => {
const okay = lint.lint(OPTIONS, false);
const okay = lint.lint(OPTIONS);
t.is(okay, false);
});
});
Expand Down Expand Up @@ -140,9 +140,25 @@ test.serial('lint should lint globs listed in include', async t => {
dirb: {'b.ts': BAD_CODE}
},
async () => {
const okay = lint.lint(OPTIONS, false);
const okay = lint.lint(OPTIONS);
t.is(okay, false);
});
});

test.serial('lint should lint only specified files', async t => {
await withFixtures(
{
'tsconfig.json': JSON.stringify({}),
'tslint.json': JSON.stringify(TSLINT_CONFIG),
dira: {'a.ts': GOOD_CODE},
dirb: {'b.ts': BAD_CODE}
},
async () => {
const aOkay = lint.lint(OPTIONS, ['dira/a.ts']);
t.is(aOkay, true);
const bOkay = lint.lint(OPTIONS, ['dirb/b.ts']);
t.is(bOkay, false);
});
});

// TODO: test for when tsconfig.json is missing.

0 comments on commit 39fee93

Please sign in to comment.