Skip to content

Commit 022606a

Browse files
authored
chore(deno): minor polyfill refactoring (#1062)
1 parent 704bf34 commit 022606a

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

scripts/deno.polyfill.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { createRequire } from 'node:module'
22
import * as process from 'node:process'
33

4-
const require = createRequire(import.meta.url)
5-
const __filename = new URL(import.meta.url).pathname
6-
const __dirname = new URL('.', import.meta.url).pathname
7-
84
// prettier-ignore
95
if (globalThis.Deno) {
10-
globalThis.require = require
11-
globalThis.__filename = __filename
12-
globalThis.__dirname = __dirname
6+
globalThis.require = createRequire(import.meta.url)
7+
globalThis.__filename = new URL(import.meta.url).pathname
8+
globalThis.__dirname = new URL('.', import.meta.url).pathname
139
globalThis.module = new Proxy({}, { set() { return true } })
1410

1511
const p = globalThis.process = globalThis.process || process
@@ -18,5 +14,3 @@ if (globalThis.Deno) {
1814
p.env || (p.env = globalThis.Deno.env.toObject())
1915
p.argv || (p.argv = [globalThis.Deno.execPath(), globalThis.Deno.mainModule.replace('file://', ''), ...globalThis.Deno.args])
2016
}
21-
22-
export { require, __dirname, __filename }

test/cli.test.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -266,28 +266,25 @@ describe('cli', () => {
266266
test('executes a script from $PATH', async () => {
267267
const isWindows = process.platform === 'win32'
268268
const oldPath = process.env.PATH
269-
270-
const envPathSeparator = isWindows ? ';' : ':'
271-
const dir = tmpdir()
272-
process.env.PATH += envPathSeparator + dir
273-
274269
const toPOSIXPath = (_path) => _path.split(path.sep).join(path.posix.sep)
275270

276271
const zxPath = path.resolve('./build/cli.js')
277272
const zxLocation = isWindows ? toPOSIXPath(zxPath) : zxPath
278-
const scriptName = 'script-from-path'
279273
const scriptCode = `#!/usr/bin/env ${zxLocation}\nconsole.log('The script from path runs.')`
280-
const scriptPath = path.join(dir, scriptName)
274+
const scriptName = 'script-from-path'
275+
const scriptFile = tmpfile(scriptName, scriptCode)
276+
const scriptDir = path.dirname(scriptFile)
277+
fs.chmodSync(scriptFile, 0o744)
278+
279+
const envPathSeparator = isWindows ? ';' : ':'
280+
process.env.PATH += envPathSeparator + scriptDir
281281

282282
try {
283283
await $`chmod +x ${zxLocation}`
284-
await $`echo ${scriptCode}`.pipe(
285-
fs.createWriteStream(scriptPath, { mode: 0o744 })
286-
)
287284
await $`${scriptName}`
288285
} finally {
289286
process.env.PATH = oldPath
290-
fs.rmSync(scriptPath)
287+
await fs.rm(scriptFile)
291288
}
292289
})
293290

0 commit comments

Comments
 (0)