Skip to content

Commit

Permalink
feat: add process.cwd() in bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Nov 9, 2024
1 parent 4341c7e commit f9b06ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bundler/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ export const build = async (...files) => {
const cwd = process.cwd()
for (const re of [/readFileSync\('([^'\\]+)'[),]/gu, /readFileSync\("([^"\\]+)"[),]/gu]) {
for (const match of source.matchAll(re)) {
const file = match[1]
let file = match[1]
if (file && /^[a-z0-9@_./-]+$/iu.test(file)) {
if (!resolve(file).startsWith(`${cwd}/`)) continue
file = resolve(file)
if (!file.startsWith(`${cwd}/`)) continue
const data = readFileSync(file, 'base64')
if (fsFilesContents.has(file)) {
assert(fsFilesContents.get(file) === data)
Expand Down Expand Up @@ -246,6 +247,8 @@ export const build = async (...files) => {
'process.type': 'undefined',
'process.version': stringify('v22.5.1'), // shouldn't depend on currently used Node.js version
'process.versions.node': stringify('22.5.1'), // see line above
'process.cwd': 'EXODUS_TEST_PROCESS.cwd',
EXODUS_TEST_PROCESS_CWD: stringify(process.cwd()),
EXODUS_TEST_FILES: stringify(files.map((f) => [dirname(f), basename(f)])),
EXODUS_TEST_SNAPSHOTS: stringify(EXODUS_TEST_SNAPSHOTS),
EXODUS_TEST_RECORDINGS: stringify(EXODUS_TEST_RECORDINGS),
Expand Down
2 changes: 2 additions & 0 deletions bundler/modules/fs.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const constants = require('constants-browserify')
const { resolve } = require('path')
const { F_OK, R_OK, W_OK, X_OK } = constants

// promises, sync, callbacks
Expand Down Expand Up @@ -97,6 +98,7 @@ const readFileSync = (file, options) => {
}

if (typeof file !== 'string') throw new Error('file argument should be string')
file = resolve(process.cwd(), file)
if (fsFilesContents?.has(file)) {
const data = Buffer.from(fsFilesContents.get(file), 'base64')
if (encoding?.toLowerCase().replace('-', '') === 'utf8') return data.toString('utf8')
Expand Down
4 changes: 4 additions & 0 deletions bundler/modules/globals.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ if (typeof process === 'undefined') {
}, 0)
}
},
cwd: () => {
if (typeof EXODUS_TEST_PROCESS_CWD === 'string') return EXODUS_TEST_PROCESS_CWD
throw new Error('Can not determine cwd, no process available')
},
}

globalThis.EXODUS_TEST_PROCESS = process
Expand Down

0 comments on commit f9b06ef

Please sign in to comment.