Commit 022606a

Anton Golub <antongolub@antongolub.com>
2025-01-07 18:59:38
chore(deno): minor polyfill refactoring (#1062)
1 parent 704bf34
Changed files (2)
scripts/deno.polyfill.js
@@ -1,15 +1,11 @@
 import { createRequire } from 'node:module'
 import * as process from 'node:process'
 
-const require = createRequire(import.meta.url)
-const __filename = new URL(import.meta.url).pathname
-const __dirname = new URL('.', import.meta.url).pathname
-
 // prettier-ignore
 if (globalThis.Deno) {
-  globalThis.require = require
-  globalThis.__filename = __filename
-  globalThis.__dirname = __dirname
+  globalThis.require = createRequire(import.meta.url)
+  globalThis.__filename = new URL(import.meta.url).pathname
+  globalThis.__dirname = new URL('.', import.meta.url).pathname
   globalThis.module = new Proxy({}, { set() { return true } })
 
   const p = globalThis.process = globalThis.process || process
@@ -18,5 +14,3 @@ if (globalThis.Deno) {
   p.env || (p.env = globalThis.Deno.env.toObject())
   p.argv || (p.argv = [globalThis.Deno.execPath(), globalThis.Deno.mainModule.replace('file://', ''), ...globalThis.Deno.args])
 }
-
-export { require, __dirname, __filename }
test/cli.test.js
@@ -266,28 +266,25 @@ describe('cli', () => {
   test('executes a script from $PATH', async () => {
     const isWindows = process.platform === 'win32'
     const oldPath = process.env.PATH
-
-    const envPathSeparator = isWindows ? ';' : ':'
-    const dir = tmpdir()
-    process.env.PATH += envPathSeparator + dir
-
     const toPOSIXPath = (_path) => _path.split(path.sep).join(path.posix.sep)
 
     const zxPath = path.resolve('./build/cli.js')
     const zxLocation = isWindows ? toPOSIXPath(zxPath) : zxPath
-    const scriptName = 'script-from-path'
     const scriptCode = `#!/usr/bin/env ${zxLocation}\nconsole.log('The script from path runs.')`
-    const scriptPath = path.join(dir, scriptName)
+    const scriptName = 'script-from-path'
+    const scriptFile = tmpfile(scriptName, scriptCode)
+    const scriptDir = path.dirname(scriptFile)
+    fs.chmodSync(scriptFile, 0o744)
+
+    const envPathSeparator = isWindows ? ';' : ':'
+    process.env.PATH += envPathSeparator + scriptDir
 
     try {
       await $`chmod +x ${zxLocation}`
-      await $`echo ${scriptCode}`.pipe(
-        fs.createWriteStream(scriptPath, { mode: 0o744 })
-      )
       await $`${scriptName}`
     } finally {
       process.env.PATH = oldPath
-      fs.rmSync(scriptPath)
+      await fs.rm(scriptFile)
     }
   })