forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-ts.js
128 lines (109 loc) · 3.18 KB
/
check-ts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// This file runs the typescript checker against packages.
// you can run it against all:
// `node ./scripts/check-ts`
//
// or even scope it to specific packages.
// `node ./scripts/check-ts gatsby-cli`
"use strict"
const fs = require(`fs`)
const glob = require(`glob`)
const path = require(`path`)
const chalk = require(`chalk`)
const yargs = require(`yargs`)
const execa = require(`execa`)
console.log(`TS Check: Running...`)
const toAbsolutePath = relativePath => path.join(__dirname, `..`, relativePath)
const PACKAGES_DIR = toAbsolutePath(`/packages`)
const filterPackage = yargs.argv._[0]
const packages = fs
.readdirSync(PACKAGES_DIR)
.map(file => path.resolve(PACKAGES_DIR, file))
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory())
// We only want to typecheck against packages that have a tsconfig.json
// AND have some ts files in it's source code.
let packagesWithTs = packages
.filter(p => fs.existsSync(path.resolve(p, `tsconfig.json`)))
.filter(
project =>
glob.sync(`/**/*.ts`, {
root: project,
ignore: `**/node_modules/**`,
}).length
)
// TEMPORARILY NOT CHECKING GATSBY-ADMIN
// Gatsby admin is filled with type bugs, and i'm not sure the best way to solve it
// because they are coming from theme-ui.
.filter(path => !path.includes(`gatsby-admin`))
if (filterPackage) {
packagesWithTs = packagesWithTs.filter(project =>
project.endsWith(filterPackage)
)
if (packagesWithTs.length === 0) {
console.log()
console.error(chalk.red(`Error:`))
console.log(
chalk.red(
`A package matching "${filterPackage}" did not find a package with TypeScript enabled.`
)
)
console.log(
chalk.red(
`Make sure that package exists at "/packages/${filterPackage}" and has a tsconfig.json file`
)
)
console.log()
process.exit(1)
}
}
let totalTsFiles = 0
let totalJsFiles = 0
packagesWithTs.forEach(project => {
const tsFiles = glob.sync(
toAbsolutePath(
`./packages/${project.split(/.*packages[/\\]/)[1]}/src/**/*.ts`
)
).length
const jsFiles = glob.sync(
toAbsolutePath(
`./packages/${project.split(/.*packages[/\\]/)[1]}/src/**/*.js`
)
).length
totalTsFiles += tsFiles
totalJsFiles += jsFiles
const percentConverted = Number(
((tsFiles / (jsFiles + tsFiles)) * 100).toFixed(1)
)
console.log(
`TS Check: Checking ./packages/${project.split(/.*packages[/\\]/)[1]}`,
`\n - TS Files: ${tsFiles}`,
`\n - JS Files: ${jsFiles}`,
`\n - Percent Converted: ${percentConverted}%`
)
const args = [
`--max-old-space-size=4096`,
path.resolve(
require.resolve(`typescript/package.json`),
`..`,
require(`typescript/package.json`).bin.tsc
),
`-p`,
project,
`--noEmit`,
]
try {
execa.sync(`node`, args, { stdio: `inherit` })
} catch (e) {
process.exit(1)
}
})
console.log(`TS Check: Success`)
if (!filterPackage) {
const percentConverted = Number(
((totalTsFiles / (totalJsFiles + totalTsFiles)) * 100).toFixed(1)
)
console.log(
` - Total TS Files: ${totalJsFiles}`,
`\n - Total JS Files: ${totalJsFiles}`,
`\n - Percent Converted: ${percentConverted}%`
)
}