-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtype.mjs
More file actions
144 lines (125 loc) · 3.59 KB
/
type.mjs
File metadata and controls
144 lines (125 loc) · 3.59 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @fileoverview Monorepo-aware TypeScript type checker.
* Runs type checking across packages with pretty UI.
*/
import colors from 'yoctocolors-cjs'
import { isQuiet } from '@socketsecurity/lib/argv/flags'
import { parseArgs } from '@socketsecurity/lib/argv/parse'
import { WIN32 } from '@socketsecurity/lib/constants/platform'
import { getDefaultLogger } from '@socketsecurity/lib/logger'
import { spawn } from '@socketsecurity/lib/spawn'
import { printFooter, printHeader } from '@socketsecurity/lib/stdio/header'
import { getPackagesWithScript } from './utils/monorepo-helper.mjs'
const logger = getDefaultLogger()
/**
* Run type check on a specific package with pretty output.
*/
async function runPackageTypeCheck(pkg, quiet = false) {
const displayName = pkg.displayName || pkg.name
if (!quiet) {
logger.progress(`${displayName}: checking types`)
}
const result = await spawn('pnpm', ['--filter', pkg.name, 'run', 'type'], {
cwd: process.cwd(),
shell: WIN32,
stdio: 'pipe',
stdioString: true,
})
if (result.code !== 0) {
if (!quiet) {
logger.clearLine()
logger.log(`${colors.red('✗')} ${displayName}`)
}
if (result.stdout) {
logger.log(result.stdout)
}
if (result.stderr) {
logger.error(result.stderr)
}
return result.code
}
if (!quiet) {
logger.clearLine()
logger.log(`${colors.green('✓')} ${displayName}`)
}
return 0
}
async function main() {
try {
// Parse arguments.
const { values } = parseArgs({
options: {
help: { type: 'boolean', default: false },
quiet: { type: 'boolean', default: false },
silent: { type: 'boolean', default: false },
},
allowPositionals: false,
strict: false,
})
// Show help if requested.
if (values.help) {
logger.log('Monorepo Type Checker')
logger.log('\nUsage: pnpm type [options]')
logger.log('\nOptions:')
logger.log(' --help Show this help message')
logger.log(' --quiet, --silent Suppress progress messages')
logger.log('\nExamples:')
logger.log(' pnpm type # Type check all packages')
logger.log('\nNote: Type checking always runs on all packages due to')
logger.log(' cross-package TypeScript dependencies.')
process.exitCode = 0
return
}
const quiet = isQuiet(values)
if (!quiet) {
printHeader('Monorepo Type Checker')
logger.log('')
}
// Get all packages with type script.
const packages = getPackagesWithScript('type')
if (!packages.length) {
if (!quiet) {
logger.step('No packages with type checking found')
}
process.exitCode = 0
return
}
// Display what we're checking.
if (!quiet) {
logger.step(
`Type checking ${packages.length} package${packages.length > 1 ? 's' : ''}`,
)
// Blank line.
logger.error('')
}
// Run type check across all packages.
let exitCode = 0
for (const pkg of packages) {
const result = await runPackageTypeCheck(pkg, quiet)
if (result !== 0) {
exitCode = result
break
}
}
if (exitCode !== 0) {
if (!quiet) {
logger.error('')
logger.log('Type checking failed')
}
process.exitCode = exitCode
} else {
if (!quiet) {
logger.error('')
logger.success('All type checks passed!')
printFooter()
}
}
} catch (error) {
logger.error(`Type checker failed: ${error.message}`)
process.exitCode = 1
}
}
main().catch(e => {
logger.error(e)
process.exitCode = 1
})