Skip to content

Commit 8c0c9b7

Browse files
Fixes error with pretty and adds cache to compilation (#28)
1 parent 70b6197 commit 8c0c9b7

File tree

9 files changed

+729
-677
lines changed

9 files changed

+729
-677
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
name: CI
22

33
on:
4-
[push]
4+
[pull_request]
55

66
jobs:
77
build:
88
runs-on: ${{ matrix.os }}
9-
strategy:
9+
strategy:
1010
matrix:
1111
os: [ubuntu-latest, windows-latest]
1212

1313
steps:
1414
- uses: actions/checkout@v2
1515
- name: Use Node.js 14.x
16-
uses: actions/setup-node@v1
16+
uses: actions/setup-node@v2
1717
with:
1818
node-version: '14.x'
1919
- run: npm i

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1111
- Strict by default without `@ts-strict` comment
1212
- Ignores file with `@ts-strict-ignore` comment
1313
- Migration tool `update-strict-comments` which updates comments in files which contain at least 1 strict error
14+
- Fixes error when `pretty: true` option was set in `tsconfig.json`
15+
- Adds a cache to compilation for `tsc-strict` process resulting in
16+
50% speedup
1417

1518
## [1.1.2] - 2021-06-15
1619

e2e/fixtures/default-config/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"baseUrl": "./",
77
"outDir": "./dist",
88
"strict": false,
9+
"pretty": true,
910
"esModuleInterop": true,
1011
"noImplicitAny": true,
1112
"plugins": [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-strict-plugin",
3-
"version": "2.0.0-beta.1",
3+
"version": "2.0.0-beta.2",
44
"description": "Typescript tools that help with migration to the strict mode",
55
"author": "Allegro",
66
"contributors": [

src/cli/findStrictFiles.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getPosixFilePath } from '../common/utils';
1+
import { getPosixFilePath, isFile } from '../common/utils';
22
import * as typescript from './typescript/typescript';
33
import { CliStrictFileChecker } from './CliStrictFileChecker';
44
import { getPluginConfig } from './getPluginConfig';
@@ -23,8 +23,8 @@ const filterOutNodeModulesFiles = (files: string[]): string[] => {
2323
};
2424

2525
async function getFilesCheckedByTs(): Promise<string[]> {
26-
const filesCheckedByTs = await typescript.listFilesOnly();
27-
const filePaths = filesCheckedByTs.split(/\r?\n/).map(getPosixFilePath);
26+
const filesCheckedByTs = await typescript.compile();
27+
const filePaths = filesCheckedByTs.split(/\r?\n/).filter(isFile).map(getPosixFilePath);
2828

2929
return filterOutNodeModulesFiles(filePaths);
3030
}

src/cli/typescript/compile.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolve } from 'path';
22
import * as typescript from './typescript';
3-
import { ExecaError } from 'execa';
3+
import { isFile } from '../../common/utils';
44

55
/**
66
* @param tscOutput
@@ -40,14 +40,9 @@ function getPathToErrorsMap(tscOutput: string[]): Map<string, string[]> {
4040
}
4141

4242
export async function compile(): Promise<Map<string, string[]>> {
43-
let tscOutput: string[] = [];
44-
try {
45-
await typescript.compile();
46-
} catch (error) {
47-
if (isExecaError(error) && error.all) {
48-
tscOutput = error.all.split(/\r?\n/);
49-
}
50-
}
43+
const tscOutput: string[] = (await typescript.compile())
44+
.split(/\r?\n/)
45+
.filter((it) => !isFile(it));
5146

5247
if (tscOutput.some((it) => it.startsWith('error'))) {
5348
console.log(`💥 Typescript did not compile due to some errors. Errors: `, tscOutput);
@@ -56,7 +51,3 @@ export async function compile(): Promise<Map<string, string[]>> {
5651

5752
return getPathToErrorsMap(tscOutput);
5853
}
59-
60-
function isExecaError(error: unknown): error is ExecaError {
61-
return typeof (error as ExecaError)?.all === 'string';
62-
}

src/cli/typescript/typescript.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
1-
import execa from 'execa';
1+
import execa, { ExecaError } from 'execa';
22

3-
export const listFilesOnly = async (): Promise<string> => {
4-
const output = await execa('tsc', ['--listFilesOnly'], {
3+
export const showConfig = async (): Promise<string> => {
4+
const output = await execa('tsc', ['--showConfig'], {
55
all: true,
66
preferLocal: true,
77
});
88

99
return output.stdout;
1010
};
1111

12-
export const showConfig = async (): Promise<string> => {
13-
const output = await execa('tsc', ['--showConfig'], {
14-
all: true,
15-
preferLocal: true,
16-
});
12+
let compilerOutputCache = '';
13+
export const compile = async (): Promise<string> => {
14+
if (compilerOutputCache) {
15+
return compilerOutputCache;
16+
}
1717

18-
return output.stdout;
18+
try {
19+
const compilerResult = await execa(
20+
'tsc',
21+
[...process.argv.slice(2), '--strict', '--noEmit', '--pretty', 'false', '--listFiles'],
22+
{
23+
all: true,
24+
preferLocal: true,
25+
},
26+
);
27+
28+
compilerOutputCache = compilerResult.stdout;
29+
} catch (error) {
30+
if (isExecaError(error) && error.all) {
31+
compilerOutputCache = error.all;
32+
}
33+
}
34+
35+
return compilerOutputCache;
1936
};
2037

21-
export const compile = () =>
22-
execa('tsc', ['--strict', '--noEmit', ...process.argv.slice(2)], {
23-
all: true,
24-
preferLocal: true,
25-
});
38+
function isExecaError(error: unknown): error is ExecaError {
39+
return typeof (error as ExecaError)?.all === 'string';
40+
}

src/common/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path';
2+
import fs from 'fs';
23

34
export function getPosixFilePath(filePath: string) {
45
return filePath.split(path.sep).join(path.posix.sep);
@@ -7,3 +8,13 @@ export function getPosixFilePath(filePath: string) {
78
export function pluralize(word: string, count: number) {
89
return count === 1 ? word : `${word}s`;
910
}
11+
12+
export function isFile(path: string) {
13+
try {
14+
const stats = fs.statSync(path);
15+
16+
return stats.isFile();
17+
} catch {
18+
return false;
19+
}
20+
}

0 commit comments

Comments
 (0)