-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: 🎸 handle environment variable access
- Loading branch information
Showing
7 changed files
with
180 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const DIRNAME_IS_USED = /(?:^|[\s\(\);=><{}\[\]\/:?,])__dirname(?:$|[^a-zA-Z0-9$_-])/; | ||
|
||
export const test = (sourceCode: string) => DIRNAME_IS_USED.test(sourceCode); | ||
|
||
export const modification = [ | ||
`const __dirname = (() => {`, | ||
` const { url: urlStr } = import.meta;`, | ||
` const url = new URL(urlStr);`, | ||
` const __filename = (url.protocol === "file:" ? url.pathname : urlStr)`, | ||
` .replace(/[/][^/]*$/, '');`, | ||
``, | ||
` const isWindows = (() => {`, | ||
``, | ||
` let NATIVE_OS: typeof Deno.build.os = "linux";`, | ||
` // eslint-disable-next-line @typescript-eslint/no-explicit-any`, | ||
` const navigator = (globalThis as any).navigator;`, | ||
` if (globalThis.Deno != null) {`, | ||
` NATIVE_OS = Deno.build.os;`, | ||
` } else if (navigator?.appVersion?.includes?.("Win") ?? false) {`, | ||
` NATIVE_OS = "windows";`, | ||
` }`, | ||
``, | ||
` return NATIVE_OS == "windows";`, | ||
``, | ||
` })();`, | ||
``, | ||
` return isWindows ?`, | ||
` __filename.split("/").join("\\\\").substring(1) :`, | ||
` __filename;`, | ||
`})();`, | ||
`` | ||
]; |
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,31 @@ | ||
const FILENAME_IS_USED = /(?:^|[\s\(\);=><{}\[\]\/:?,])__filename(?:$|[^a-zA-Z0-9$_-])/; | ||
|
||
export const test = (sourceCode: string) => FILENAME_IS_USED.test(sourceCode); | ||
|
||
export const modification = [ | ||
`const __filename = (() => {`, | ||
` const { url: urlStr } = import.meta;`, | ||
` const url = new URL(urlStr);`, | ||
` const __filename = (url.protocol === "file:" ? url.pathname : urlStr);`, | ||
``, | ||
` const isWindows = (() => {`, | ||
``, | ||
` let NATIVE_OS: typeof Deno.build.os = "linux";`, | ||
` // eslint-disable-next-line @typescript-eslint/no-explicit-any`, | ||
` const navigator = (globalThis as any).navigator;`, | ||
` if (globalThis.Deno != null) {`, | ||
` NATIVE_OS = Deno.build.os;`, | ||
` } else if (navigator?.appVersion?.includes?.("Win") ?? false) {`, | ||
` NATIVE_OS = "windows";`, | ||
` }`, | ||
``, | ||
` return NATIVE_OS == "windows";`, | ||
``, | ||
` })();`, | ||
``, | ||
` return isWindows ?`, | ||
` __filename.split("/").join("\\\\").substring(1) :`, | ||
` __filename;`, | ||
`})();`, | ||
`` | ||
]; |
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,6 @@ | ||
const BUFFER_IS_USED = /(?:^|[\s\(\);=><{}\[\]\/:?,])Buffer(?:$|[^a-zA-Z0-9$_-])/; | ||
const BUFFER_IS_IMPORTED = /import\s*{[^}]*Buffer[^}]*}\s*from\s*["'][^"']+["']/; | ||
|
||
export const test = (sourceCode: string) => BUFFER_IS_USED.test(sourceCode) && !BUFFER_IS_IMPORTED.test(sourceCode); | ||
|
||
export const modification = [`import { Buffer } from "buffer";`]; |
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,15 @@ | ||
import * as __dirname from "./__dirname"; | ||
import * as __filename from "./__filename"; | ||
import * as buffer from "./buffer"; | ||
|
||
/** | ||
* This is how we handle Node builtins | ||
* | ||
* Each module in this directory should export two functions: | ||
* - 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 = [__filename, __dirname, buffer]; | ||
|
||
export default builtins; |
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