diff --git a/src/lib/builtins/index.ts b/src/lib/builtins/index.ts index beb924f..9cb62ef 100644 --- a/src/lib/builtins/index.ts +++ b/src/lib/builtins/index.ts @@ -1,6 +1,7 @@ import * as __dirname from "./__dirname"; import * as __filename from "./__filename"; import * as buffer from "./buffer"; +import * as processBuiltin from "./process"; /** * This is how we handle Node builtins @@ -10,6 +11,6 @@ import * as buffer from "./buffer"; * - modification: string[] the lines of code to prepend to the source code */ -const builtins = [__filename, __dirname, buffer]; +const builtins = [__filename, __dirname, buffer, processBuiltin]; export default builtins; diff --git a/src/lib/builtins/process.ts b/src/lib/builtins/process.ts new file mode 100644 index 0000000..94e7c37 --- /dev/null +++ b/src/lib/builtins/process.ts @@ -0,0 +1,5 @@ +const PROCESS_IS_USED = /process\./; + +export const test = (sourceCode: string) => PROCESS_IS_USED.test(sourceCode); + +export const modification = [`import process from "node:process";`]; diff --git a/src/lib/denoifySingleFile.ts b/src/lib/denoifySingleFile.ts index 27de0f8..0888381 100644 --- a/src/lib/denoifySingleFile.ts +++ b/src/lib/denoifySingleFile.ts @@ -75,16 +75,6 @@ export function denoifySingleFileFactory(params: {} & ReturnType\w+)|\[(?['"])(?\w+)\k\])/g, - `= Deno.env.get('$$')` - ); - modifiedSourceCode = modifiedSourceCode.replaceAll( - /process.env(?:\.(?\w+)|\[(?['"])(?\w+)\k\])\s+=\s+(?[^;]+)/g, - `Deno.env.set('$$', $)` - ); - for (const [hash, denoifiedImportExportStatement] of denoifiedImportExportStatementByHash) { modifiedSourceCode = modifiedSourceCode.replace(new RegExp(hash, "g"), denoifiedImportExportStatement); } diff --git a/test/denoifySingleFile.test.ts b/test/denoifySingleFile.test.ts index a801c02..ec53a2a 100644 --- a/test/denoifySingleFile.test.ts +++ b/test/denoifySingleFile.test.ts @@ -209,7 +209,7 @@ Buffer.from("hello"); expect(modifiedSourceCode).toBe(expected); }); - it("should denoify the source code by adding relevant import statement", async () => { + it("should denoify the source code by adding relevant import statement (Buffer.from)", async () => { const sourceCode = ` Buffer.from("hello"); `; @@ -248,7 +248,7 @@ Buffer.from("hello"); expect(modifiedSourceCode).toBe(expected); }); - it("should denoify the source code by adding relevant import statement", async () => { + it("should denoify the source code by adding relevant import statement (Buffer)", async () => { const sourceCode = ` Buffer`; @@ -324,56 +324,23 @@ Buffer_name expect(modifiedSourceCode).toBe(expected); }); - it("should access environment variables correctly", async () => { - const sourceCode = `const foo = process.env.FOO\nconst bar = process.env['BAR'];`; - const expected = `const foo = Deno.env.get('FOO')\nconst bar = Deno.env.get('BAR');`; + it("should import node:process if process is used", async () => { + const sourceCode = `const foo = process.env.FOO;\nconst bar = process.env['BAR'];`; - const { denoifySingleFile } = denoifySingleFileFactory({ - "denoifyImportExportStatement": () => { - tsafeAssert(false); - } - }); - - const modifiedSourceCode = await denoifySingleFile({ - sourceCode, - "dirPath": "" - }); - - expect(modifiedSourceCode).toBe(expected); - }); - it("should set environment variables correctly", async () => { - const sourceCode = `process.env.FOO = 'foo';\nprocess.env['BAR'] = 22;`; - - const expected = `Deno.env.set('FOO', 'foo');\nDeno.env.set('BAR', 22);`; + const expected = `import process from "node:process"; +const foo = process.env.FOO; +const bar = process.env['BAR'];`; const { denoifySingleFile } = denoifySingleFileFactory({ - "denoifyImportExportStatement": () => { - tsafeAssert(false); + "denoifyImportExportStatement": async ({ importExportStatement }) => { + return importExportStatement; } }); const modifiedSourceCode = await denoifySingleFile({ sourceCode, - "dirPath": "" - }); - - expect(modifiedSourceCode).toBe(expected); - }); - it("should update environment variables correctly", async () => { - const sourceCode = `process.env.FOO = process.env['BAR']+1;\nprocess.env['BAR'] = process.env.FOO + 'bar';`; - - const expected = `Deno.env.set('FOO', Deno.env.get('BAR')+1);\nDeno.env.set('BAR', Deno.env.get('FOO') + 'bar');`; - - const { denoifySingleFile } = denoifySingleFileFactory({ - "denoifyImportExportStatement": () => { - tsafeAssert(false); - } - }); - - const modifiedSourceCode = await denoifySingleFile({ - sourceCode, - "dirPath": "" + "dirPath": "whatever" }); expect(modifiedSourceCode).toBe(expected);