Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix js2ts script #4802

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions private/js2ts/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { opendir, readFile, open, writeFile, rm } from 'node:fs/promises'
import { argv } from 'node:process'
import { extname } from 'node:path'
import { extname, resolve } from 'node:path'
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
import { existsSync } from 'node:fs'

const packageRoot = new URL(`../../packages/${argv[2]}/`, import.meta.url)
Expand Down Expand Up @@ -58,12 +58,13 @@ try {

for await (const dirent of dir) {
if (!dirent.isDirectory()) {
const { path: filepath } = dirent
const ext = extname(filepath)
const { name } = dirent
const ext = extname(name)
if (ext !== '.js' && ext !== '.jsx') continue // eslint-disable-line no-continue
const filePath = resolve(dirent.path, name)
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
await writeFile(
filepath.slice(0, -ext.length) + ext.replace('js', 'ts'),
(await readFile(filepath, 'utf-8'))
`${filePath.slice(0, -ext.length)}${ext.replace('js', 'ts')}`,
(await readFile(filePath, 'utf-8'))
.replace(
// The following regex aims to capture all imports and reexports of local .js(x) files to replace it to .ts(x)
// It's far from perfect and will have false positives and false negatives.
Expand All @@ -78,7 +79,7 @@ for await (const dirent of dir) {
`// @ts-ignore We don't want TS to generate types for the package.json${originalImport}`,
),
)
await rm(filepath)
await rm(filePath)
}
}

Expand Down