Skip to content

Commit

Permalink
Merge pull request #109 from dancrumb/fix/#108
Browse files Browse the repository at this point in the history
Import `node:process` if `process` is used
  • Loading branch information
garronej authored Jun 15, 2023
2 parents 8f20f7e + d2219de commit 974e684
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 55 deletions.
5 changes: 3 additions & 2 deletions src/lib/builtins/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as __dirnameBuiltin from "./__dirname";
import * as __filenameBuiltin from "./__filename";
import * as bufferBuiltin from "./buffer";
import * as processBuiltin from "./process";


/**
* This is how we handle Node builtins
Expand All @@ -9,7 +11,6 @@ import * as bufferBuiltin from "./buffer";
* - test: (sourceCode: string) => boolean returns true if the source code needs to be modified because it refers to a Node builtin
* - modification: string[] the lines of code to prepend to the source code
*/

const builtins = [__filenameBuiltin, __dirnameBuiltin, bufferBuiltin];
const builtins = [__filenameBuiltin, __dirnameBuiltin, bufferBuiltin, processBuiltin];

export default builtins;
5 changes: 5 additions & 0 deletions src/lib/builtins/process.ts
Original file line number Diff line number Diff line change
@@ -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";`];
10 changes: 0 additions & 10 deletions src/lib/denoifySingleFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,6 @@ export function denoifySingleFileFactory(params: {} & ReturnType<typeof denoifyI
}
}

// Handle environment variable access
modifiedSourceCode = modifiedSourceCode.replaceAll(
/=\s+process.env(?:\.(?<varDot>\w+)|\[(?<q>['"])(?<varBracket>\w+)\k<q>\])/g,
`= Deno.env.get('$<varDot>$<varBracket>')`
);
modifiedSourceCode = modifiedSourceCode.replaceAll(
/process.env(?:\.(?<varDot>\w+)|\[(?<q>['"])(?<varBracket>\w+)\k<q>\])\s+=\s+(?<val>[^;]+)/g,
`Deno.env.set('$<varDot>$<varBracket>', $<val>)`
);

for (const [hash, denoifiedImportExportStatement] of denoifiedImportExportStatementByHash) {
modifiedSourceCode = modifiedSourceCode.replace(new RegExp(hash, "g"), denoifiedImportExportStatement);
}
Expand Down
53 changes: 10 additions & 43 deletions test/denoifySingleFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
`;
Expand Down Expand Up @@ -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`;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 974e684

Please sign in to comment.