Skip to content

Commit

Permalink
meta: fix js2ts script on Node.js 20+ (#4802)
Browse files Browse the repository at this point in the history

Co-authored-by: Antoine du Hamel <[email protected]>
  • Loading branch information
Murderlon and aduh95 authored Dec 5, 2023
1 parent f55b24c commit 0bea173
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 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 { basename, extname, join } from 'node:path'
import { existsSync } from 'node:fs'

const packageRoot = new URL(`../../packages/${argv[2]}/`, import.meta.url)
Expand Down Expand Up @@ -58,12 +58,16 @@ 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 =
basename(dirent.path) === name
? dirent.path // Some versions of Node.js give the full path as dirent.path.
: join(dirent.path, name) // Others supply only the path to the parent.
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 +82,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

0 comments on commit 0bea173

Please sign in to comment.