/
ts2js.ts
171 lines (156 loc) · 4.21 KB
/
ts2js.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import fs from 'fs'
import path from 'path'
import { transform } from '@babel/core'
import fg from 'fast-glob'
import { format } from 'prettier'
import { getPaths } from '@redwoodjs/project-config'
/**
* Converts all the TypeScript files in the `api` and `web` sides to JavaScript.
*
* @param {string} cwd - The base path to the project.
*/
export const convertTsProjectToJs = (cwd = getPaths().base) => {
const files = typeScriptSourceFiles(cwd)
convertTsFilesToJs(cwd, files)
}
/**
* Converts all the TypeScript files in the `api` and `web` sides to JavaScript.
*
* @param {string} cwd - The base path to the project.
*/
export const convertTsScriptsToJs = (cwd = getPaths().base) => {
const files = typeScriptSourceFiles(cwd, 'scripts/*.{ts,tsx}')
convertTsFilesToJs(cwd, files)
}
/**
* Converts TypeScript files to JavaScript.
*
* @param {string} cwd - Current directory
* @param {string[]} files - Collection of files to convert
*/
export const convertTsFilesToJs = async (cwd: string, files: string[]) => {
if (files.length === 0) {
console.log('No TypeScript files found to convert to JS in this project.')
}
for (const f of files) {
const code = await transformTSToJS(f)
if (code) {
fs.writeFileSync(
path.join(cwd, f.replace('.tsx', '.jsx').replace('.ts', '.js')),
code,
'utf8',
)
fs.unlinkSync(path.join(cwd, f))
}
}
if (fs.existsSync(path.join(cwd, 'api/tsconfig.json'))) {
fs.renameSync(
path.join(cwd, 'api/tsconfig.json'),
path.join(cwd, 'api/jsconfig.json'),
)
}
if (fs.existsSync(path.join(cwd, 'web/tsconfig.json'))) {
fs.renameSync(
path.join(cwd, 'web/tsconfig.json'),
path.join(cwd, 'web/jsconfig.json'),
)
}
if (fs.existsSync(path.join(cwd, 'scripts/tsconfig.json'))) {
fs.renameSync(
path.join(cwd, 'scripts/tsconfig.json'),
path.join(cwd, 'scripts/jsconfig.json'),
)
}
}
/**
* Get all the source code from a Redwood project
*/
export const typeScriptSourceFiles = (
cwd: string,
globPattern = '{api,web}/src/**/*.{ts,tsx}',
) => {
console.log(globPattern)
// TODO: When sides are expanded read the `api` and `web` string instead
// of hard-coding them.
return fg.sync(globPattern, {
cwd,
ignore: ['node_modules'],
})
}
/**
* Read the contents of a TypeScript file, transpile it to JavaScript,
* but leave the JSX intact and format via Prettier.
*
* @param {string} file - The path to the TypeScript file.
*/
export const transformTSToJS = (file: string) => {
const tsCode = fs.readFileSync(file, 'utf8')
const filename = path.basename(file)
const result = transform(tsCode, {
filename,
cwd: getPaths().base,
configFile: false,
plugins: [
[
'@babel/plugin-transform-typescript',
{
isTSX: true,
allExtensions: true,
},
],
],
retainLines: true,
})
if (!result?.code) {
return undefined
}
return prettify(result.code, filename.replace(/\.ts$/, '.js'))
}
export const getPrettierConfig = async () => {
try {
const { default: prettierConfig } = await import(
`file://${path.join(getPaths().base, 'prettier.config.js')}`
)
return prettierConfig
} catch (e) {
return undefined
}
}
/**
* Determine the prettier parser based off of the extension.
*
* See: https://prettier.io/docs/en/options.html#parser
* @param {string} filename
*/
const prettierParser = (filename: string) => {
switch (path.extname(filename.replace('.template', ''))) {
case '.css':
return 'css'
case '.js':
return 'babel'
case '.ts':
case '.tsx':
return 'babel-ts'
default:
return undefined
}
}
/**
* Prettify `code` according to the extension in `filename`.
* This will also read a user's `prettier.config.js` file if it exists.
*
* @param {string} code
* @param {string} filename
*/
export const prettify = async (code: string, filename: string) => {
const parser = prettierParser(filename)
// Return unformatted code if we could not determine the parser.
if (typeof parser === 'undefined') {
return code
}
const prettierConfig = await getPrettierConfig()
return format(code, {
...prettierConfig,
parser,
})
}