Skip to content

Commit e368229

Browse files
authored
breaking: change default verbs + cleanup (#43)
* Replace `lint` with `check` which checks both format and lint errors. * Remove unnecessary `build` script. * Add prepare, pretest and posttest scripts to target. * Terminate with proper exit code on failures * Get rid of trivial fix.ts * README improvements. Fixes: #35
1 parent 86359e5 commit e368229

File tree

6 files changed

+53
-67
lines changed

6 files changed

+53
-67
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ Still on an older version of npm? We got ya! In a directory with your `package.j
2121

2222
```sh
2323
npm install --save-dev google-ts-style [email protected] [email protected] [email protected]
24-
$(npm bin)/gts init
24+
$(npm bin)/google-ts-style init
2525
```
2626

2727
# How this works
28-
- Adds a `tsconfig.json` file to your project that inherits from the Google TS Style.
29-
- Adds the necessary devDependencies to your package.json.
30-
- Adds the `compile`, `format`, and `lint` scripts to your package.json.
28+
- Adds a `tsconfig.json` file to your project that uses the Google TS Style.
29+
- Adds the necessary devDependencies to your `package.json`.
30+
- Adds scripts to your `package.json`:
31+
- `check`: Lints and checks for formatting problems.
32+
- `fix`: Automatically fixes formatting and linting problems (if possible).
33+
- `clean`: Removes output files.
34+
- `compile`: Compiles the source code using TypeScript compiler.
35+
- `pretest`, `posttest` and `prepare`: convenience integrations.
3136

3237
# License
3338
See [LICENSE.md](LICENSE.md)

src/clean.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,25 @@ import {getTSConfig, rimrafp} from './util';
2121
/**
2222
* Remove files generated by the build.
2323
*/
24-
export async function clean(options: Options): Promise<void> {
24+
export async function clean(options: Options): Promise<boolean> {
2525
const tsconfig = (await getTSConfig(options.targetRootDir));
2626
if (tsconfig.compilerOptions && tsconfig.compilerOptions.outDir) {
2727
const outDir = tsconfig.compilerOptions.outDir;
2828
if (outDir === '.') {
2929
console.error(
3030
`${chalk.red('ERROR:')} ${chalk.gray('compilerOptions.outDir')} ` +
3131
`cannot use the value '.'. That would delete all of our sources.`);
32-
return;
32+
return false;
3333
}
3434
const message = `${chalk.red('Removing')} ${outDir} ...`;
3535
console.log(message);
3636
await rimrafp(outDir);
37+
return true;
3738
} else {
3839
console.error(
3940
`${chalk.red('ERROR:')} The ${chalk.gray('clean')} command` +
4041
` requires ${chalk.gray('compilerOptions.outDir')} to be defined in ` +
4142
`tsconfig.json.`);
43+
return false;
4244
}
4345
}

src/cli.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as meow from 'meow';
1818
import * as updateNotifier from 'update-notifier';
1919
import {init} from './init';
2020
import {lint} from './lint';
21-
import {fix} from './fix';
21+
import {format} from './format';
2222
import {clean} from './clean';
2323

2424
export interface Options {
@@ -34,8 +34,8 @@ const cli = meow(`
3434
3535
Verb can be:
3636
init Adds default npm scripts to your package.json.
37-
lint Runs the lint tool against all *.ts files in the cwd.
38-
fix Runs the tslint and clang fix tools for all *.ts files.
37+
check Checks code for formatting and lint issues.
38+
fix Fixes formatting and linting issues (if possible).
3939
clean Removes all files generated by the build.
4040
4141
Options
@@ -44,7 +44,7 @@ const cli = meow(`
4444
--dry-run Don't make any acutal changes.
4545
4646
Examples
47-
$ gts init
47+
$ gts init -y
4848
$ gts lint
4949
$ gts fix
5050
$ gts clean
@@ -57,34 +57,36 @@ function usage(msg?: string): void {
5757
cli.showHelp(1);
5858
}
5959

60+
async function run(verb: string): Promise<boolean> {
61+
const options: Options = {
62+
dryRun: cli.flags.dryRun || false,
63+
gtsRootDir: `${process.cwd()}/node_modules/google-ts-style`,
64+
targetRootDir: process.cwd(),
65+
yes: cli.flags.yes || cli.flags.y || false
66+
};
67+
switch (verb) {
68+
case 'init':
69+
return await init(options);
70+
case 'check':
71+
return (await lint(options) && await format(options));
72+
case 'fix':
73+
return (await lint(options, true) && await format(options, true));
74+
case 'clean':
75+
return await clean(options);
76+
default:
77+
usage(`Unknown verb: ${verb}`);
78+
return false;
79+
}
80+
}
81+
6082
updateNotifier({pkg: cli.pkg}).notify();
6183

6284
if (cli.input.length !== 1) {
6385
usage();
6486
}
6587

66-
const verb = cli.input[0];
67-
const options: Options = {
68-
dryRun: cli.flags.dryRun || false,
69-
gtsRootDir: `${process.cwd()}/node_modules/google-ts-style`,
70-
targetRootDir: process.cwd(),
71-
yes: cli.flags.yes || cli.flags.y || false
72-
};
73-
74-
switch (verb) {
75-
case 'init':
76-
init(options);
77-
break;
78-
case 'lint':
79-
lint(options);
80-
break;
81-
case 'fix':
82-
fix(options);
83-
break;
84-
case 'clean':
85-
clean(options);
86-
break;
87-
default:
88-
usage(`Unknown verb: ${verb}`);
89-
break;
90-
}
88+
run(cli.input[0]).then(success => {
89+
if (!success) {
90+
process.exit(1);
91+
}
92+
});

src/fix.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/format.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ const baseArgs =
2323

2424
/**
2525
* Run tslint fix and clang fix with the default configuration
26-
* @param fix whether to automatically fix the format
2726
* @param options
27+
* @param fix whether to automatically fix the format
2828
*/
29-
export function format(fix: boolean, options: Options): Promise<boolean> {
29+
export function format(options: Options, fix = false): Promise<boolean> {
3030
const program = createProgram(options);
3131
const srcFiles = program.getSourceFiles()
3232
.map(sourceFile => sourceFile.fileName)

src/init.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ async function addScripts(
4949
packageJson: any, options: Options): Promise<boolean> {
5050
let edits = false;
5151
const scripts: Bag<string> = {
52-
build: 'npm run compile',
52+
check: `gts check`,
5353
clean: 'gts clean',
5454
compile: `tsc -p .`,
5555
fix: `gts fix`,
56-
lint: `gts lint`
56+
prepare: `npm run compile`,
57+
pretest: `npm run compile`,
58+
posttest: `npm run check`
5759
};
5860

5961
if (!packageJson.scripts) {
@@ -180,7 +182,7 @@ async function generateTsConfig(options: Options): Promise<void> {
180182
}
181183
}
182184

183-
export async function init(options: Options): Promise<void> {
185+
export async function init(options: Options): Promise<boolean> {
184186
let packageJson;
185187
try {
186188
packageJson = await readJson('./package.json', noop, true /*strict*/);
@@ -194,7 +196,7 @@ export async function init(options: Options): Promise<void> {
194196

195197
if (!generate) {
196198
console.log('Please run from a directory with your package.json.');
197-
return;
199+
return false;
198200
}
199201

200202
try {
@@ -214,4 +216,5 @@ export async function init(options: Options): Promise<void> {
214216
console.log('No edits needed in package.json.');
215217
}
216218
await generateTsConfig(options);
219+
return true;
217220
}

0 commit comments

Comments
 (0)