Skip to content

Commit e22a639

Browse files
authored
feat: support running gts on individual files. (#61)
If files are explicitly given, only those files will be run instead of the whole project.
1 parent c1555c6 commit e22a639

File tree

5 files changed

+83
-30
lines changed

5 files changed

+83
-30
lines changed

package-lock.json

+37-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ export interface Options {
3333
logger: Logger;
3434
}
3535

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

3939
const logger: Logger = console;
4040

4141
const cli = meow(`
4242
Usage
43-
$ gts <verb> [options]
43+
$ gts <verb> [<file>...] [options]
4444
4545
Verb can be:
4646
init Adds default npm scripts to your package.json.
@@ -57,6 +57,7 @@ const cli = meow(`
5757
$ gts init -y
5858
$ gts check
5959
$ gts fix
60+
$ gts fix src/file1.ts src/file2.ts
6061
$ gts clean
6162
`);
6263

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

70-
async function run(verb: string): Promise<boolean> {
71+
async function run(verb: string, files: string[]): Promise<boolean> {
7172
const options: Options = {
7273
dryRun: cli.flags.dryRun || false,
7374
gtsRootDir: `${process.cwd()}/node_modules/gts`,
@@ -81,13 +82,15 @@ async function run(verb: string): Promise<boolean> {
8182
if (verb === 'init') {
8283
return await init(options);
8384
}
84-
const lint: VerbFunction = require('./lint').lint;
85-
const format: VerbFunction = require('./format').format;
85+
const lint: VerbFilesFunction = require('./lint').lint;
86+
const format: VerbFilesFunction = require('./format').format;
8687
switch (verb) {
8788
case 'check':
88-
return (await lint(options) && await format(options));
89+
return (await lint(options, files) && await format(options, files));
8990
case 'fix':
90-
return (await lint(options, true) && await format(options, true));
91+
return (
92+
await lint(options, files, true) &&
93+
await format(options, files, true));
9194
case 'clean':
9295
return await clean(options);
9396
default:
@@ -98,11 +101,11 @@ async function run(verb: string): Promise<boolean> {
98101

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

101-
if (cli.input.length !== 1) {
104+
if (cli.input.length < 1) {
102105
usage();
103106
}
104107

105-
run(cli.input[0]).then(success => {
108+
run(cli.input[0], cli.input.slice(1)).then(success => {
106109
if (!success) {
107110
process.exit(1);
108111
}

src/format.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ const baseArgs =
2525
* Run tslint fix and clang fix with the default configuration
2626
* @param options
2727
* @param fix whether to automatically fix the format
28+
* @param files files to format
2829
*/
29-
export function format(options: Options, fix = false): Promise<boolean> {
30+
export function format(
31+
options: Options, files: string[] = [], fix = false): Promise<boolean> {
3032
const program = createProgram(options);
31-
const srcFiles = program.getSourceFiles()
32-
.map(sourceFile => sourceFile.fileName)
33-
.filter(f => !f.endsWith('.d.ts'));
33+
const srcFiles = files.length > 0 ?
34+
files :
35+
program.getSourceFiles()
36+
.map(sourceFile => sourceFile.fileName)
37+
.filter(f => !f.endsWith('.d.ts'));
3438

3539
return fix ? fixFormat(srcFiles) : checkFormat(srcFiles);
3640
}

src/lint.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ import {Options} from './cli';
2121

2222
/**
2323
* Run tslint with the default configuration. Returns true on success.
24-
* @param fix automatically fix linter errors
2524
* @param options gts options
25+
* @param files files to run linter on
26+
* @param fix automatically fix linter errors
2627
*/
27-
export function lint(options: Options, fix = false): boolean {
28+
export function lint(
29+
options: Options, files: string[] = [], fix = false): boolean {
2830
const tslintConfigPath = path.join(options.gtsRootDir, 'tslint.json');
2931

3032
const program = createProgram(options);
3133
const configuration =
3234
Configuration.findConfiguration(tslintConfigPath, '').results;
3335
const linter = new Linter({fix: fix, formatter: 'codeFrame'}, program);
34-
const files = Linter.getFileNames(program);
35-
files.forEach(file => {
36+
const srcFiles = files.length > 0 ? files : Linter.getFileNames(program);
37+
srcFiles.forEach(file => {
3638
const fileContents = program.getSourceFile(file).getFullText();
3739
linter.lint(file, fileContents, configuration);
3840
});

test/test-lint.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ test.serial('lint should return false on bad code', async t => {
8282
'a.ts': BAD_CODE,
8383
},
8484
async () => {
85-
const okay = lint.lint(OPTIONS, false);
85+
const okay = lint.lint(OPTIONS);
8686
t.is(okay, false);
8787
});
8888
});
@@ -112,7 +112,7 @@ test.serial(
112112
'b.ts': BAD_CODE
113113
},
114114
async () => {
115-
const okay = lint.lint(OPTIONS, false);
115+
const okay = lint.lint(OPTIONS);
116116
t.is(okay, false);
117117
});
118118
});
@@ -140,9 +140,25 @@ test.serial('lint should lint globs listed in include', async t => {
140140
dirb: {'b.ts': BAD_CODE}
141141
},
142142
async () => {
143-
const okay = lint.lint(OPTIONS, false);
143+
const okay = lint.lint(OPTIONS);
144144
t.is(okay, false);
145145
});
146146
});
147147

148+
test.serial('lint should lint only specified files', async t => {
149+
await withFixtures(
150+
{
151+
'tsconfig.json': JSON.stringify({}),
152+
'tslint.json': JSON.stringify(TSLINT_CONFIG),
153+
dira: {'a.ts': GOOD_CODE},
154+
dirb: {'b.ts': BAD_CODE}
155+
},
156+
async () => {
157+
const aOkay = lint.lint(OPTIONS, ['dira/a.ts']);
158+
t.is(aOkay, true);
159+
const bOkay = lint.lint(OPTIONS, ['dirb/b.ts']);
160+
t.is(bOkay, false);
161+
});
162+
});
163+
148164
// TODO: test for when tsconfig.json is missing.

0 commit comments

Comments
 (0)