-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
78 lines (71 loc) · 2.16 KB
/
cli.ts
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
#!/usr/bin/env node
import { NewLineKind, Project, QuoteKind } from 'ts-morph'
import { transform } from './src/transformations.js'
import { parseArgs } from 'node:util'
import type { StatisticsReport } from './src/statisticsReport.js'
import { setGlobalProject } from './src/utils.js'
const { values } = parseArgs({
options: {
configPath: { type: 'string' },
folderPath: {
type: 'string'
},
experimental: {
type: 'boolean'
},
target: {
type: 'string',
default: 'default' as const
}
}
})
const extensionIncludeList = ['.ts', '.js', '.tsx', '.jsx', '.vue']
const manipulationSettings = {
quoteKind: QuoteKind.Single,
newLineKind:
process.platform === 'win32' ? NewLineKind.CarriageReturnLineFeed : NewLineKind.LineFeed
}
let project: Project
// initialize
//TODO this does not work with composite tsconfigs
if (values.configPath) {
project = new Project({
tsConfigFilePath: values.configPath,
skipAddingFilesFromTsConfig: !!values.folderPath,
manipulationSettings: manipulationSettings
})
} else if (values.folderPath) {
project = new Project({
manipulationSettings: manipulationSettings
})
project.addSourceFilesAtPaths([
`${values.folderPath}/**`,
`!${values.folderPath}/**/node_modules/**`
])
} else {
throw new Error(
"No source provided, use the 'configPath' or 'folderPath' option to supply source files)"
)
}
setGlobalProject(project, true)
let statisticsReports: StatisticsReport[] = []
let fileModificationCount = 0
const sourceFiles = project.getSourceFiles()
for (const sourceFile of sourceFiles) {
if (sourceFile.getBaseName() === 'yfiles.d.ts') {
continue
}
if (!extensionIncludeList.includes(sourceFile.getExtension())) {
continue
}
console.log(`Working on ${sourceFile.getFilePath()}`)
const report = transform(sourceFile, values.experimental, values.target)
statisticsReports.push(report)
const changeCount = report.getTotalChanges()
if (changeCount > 0) {
fileModificationCount++
console.log(`Applied ${changeCount} changes`)
}
await sourceFile.save()
}
console.log(`Changed ${fileModificationCount} of ${project.getSourceFiles().length} files`)