-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andy Haynes
committed
Aug 8, 2024
1 parent
6864f8f
commit 889db8e
Showing
47 changed files
with
419 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "tsconfig/cjs.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/commonjs", | ||
"lib": ["es2022", "dom"] | ||
}, | ||
"files": [ | ||
"src/index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "tsconfig/cjs.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/commonjs", | ||
"lib": ["es2022", "dom"] | ||
}, | ||
"files": [ | ||
"src/index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { lstat, opendir, readFile, rename, writeFile } from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
async function enumerateContents(contentPath) { | ||
const dir = await opendir(contentPath); | ||
let files = []; | ||
for await (let entry of dir) { | ||
if (entry.name === 'node_modules') { | ||
continue; | ||
} | ||
|
||
const entryPath = path.join(contentPath, entry.name); | ||
if (entry.isDirectory()) { | ||
files = [...files, ...(await enumerateContents(entryPath))]; | ||
} else if (entry.name.endsWith('.js')) { | ||
files.push(entryPath); | ||
} | ||
} | ||
|
||
return files; | ||
} | ||
|
||
async function cjsIfy() { | ||
const [,, inputPath] = process.argv; | ||
const basePath = path.resolve(process.cwd(), inputPath); | ||
|
||
for (let projectFilePath of await enumerateContents(basePath)) { | ||
let contents = (await readFile(projectFilePath)).toString(); | ||
const relativeImports = [...contents.matchAll(/require\("(\.\.?\/+[^"]+)"\)/ig)]; | ||
for (let localImport of relativeImports) { | ||
const [matchedText, relativePath] = [...localImport]; | ||
if (relativePath.endsWith('.json')) { | ||
continue; | ||
} | ||
|
||
const absolutePath = path.resolve(projectFilePath.split('/').slice(0, -1).join('/'), relativePath); | ||
let isDirectory = false; | ||
try { | ||
isDirectory = (await lstat(absolutePath)).isDirectory(); | ||
} catch { /* lstat has failed because `absolutePath` points to a JS file but is missing the .js extension */ } | ||
|
||
const replacementPath = isDirectory | ||
? `${relativePath}/index.cjs` | ||
: `${relativePath}.cjs`; | ||
contents = contents.replaceAll(matchedText, `require("${replacementPath}")`); | ||
} | ||
|
||
if (relativeImports.length) { | ||
await writeFile(projectFilePath, contents); | ||
} | ||
|
||
await rename(projectFilePath, [...projectFilePath.split('.').slice(0, -1), 'cjs'].join('.')); | ||
} | ||
} | ||
|
||
(async function() { | ||
await cjsIfy(); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "build", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"private": true, | ||
"bin": { | ||
"cjsify": "./cjsify.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "tsconfig/cjs.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/commonjs", | ||
"lib": ["es2022", "dom"] | ||
}, | ||
"files": [ | ||
"src/index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "tsconfig/cjs.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/commonjs", | ||
"lib": ["es2022", "dom"] | ||
}, | ||
"files": [ | ||
"src/index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "tsconfig/cjs.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/commonjs", | ||
"lib": ["es2022", "dom"] | ||
}, | ||
"files": [ | ||
"src/index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "tsconfig/cjs.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/commonjs", | ||
"lib": ["es2022", "dom"] | ||
}, | ||
"files": [ | ||
"src/index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "tsconfig/cjs.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/commonjs", | ||
"lib": ["es2022", "dom"] | ||
}, | ||
"files": [ | ||
"src/index.ts" | ||
] | ||
} |
Oops, something went wrong.