Skip to content

Commit

Permalink
fix(builtins): import node:process if process is used
Browse files Browse the repository at this point in the history
  • Loading branch information
dancrumb committed Jun 14, 2023
1 parent cb11c7b commit e3a68ce
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 54 deletions.
3 changes: 2 additions & 1 deletion src/lib/builtins/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
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 e3a68ce

Please sign in to comment.