Skip to content

Commit 3ac91da

Browse files
refactor(cli): improve error handling for local script reading
1 parent 11f1c8e commit 3ac91da

File tree

14 files changed

+57
-14
lines changed

14 files changed

+57
-14
lines changed

.size-limit.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"build/globals.js",
3434
"build/deno.js"
3535
],
36-
"limit": "849.50 kB",
36+
"limit": "850.00 kB",
3737
"brotli": false,
3838
"gzip": false
3939
},
@@ -66,8 +66,8 @@
6666
"README.md",
6767
"LICENSE"
6868
],
69-
"limit": "911.20 kB",
69+
"limit": "912.00 kB",
7070
"brotli": false,
7171
"gzip": false
7272
}
73-
]
73+
]

build/cli.cjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,13 @@ function readScript() {
300300
script = yield readScriptFromHttp(firstArg);
301301
tempPath = getFilepath(import_index.$.cwd, name, ext2);
302302
} else {
303-
script = yield import_index.fs.readFile(firstArg, "utf8");
303+
try {
304+
script = yield import_index.fs.readFile(firstArg, "utf8");
305+
} catch (err) {
306+
console.error(`Error: Can't read ${firstArg}`);
307+
import_node_process2.default.exitCode = 1;
308+
throw new import_index.Fail(`Failed to read local script: ${firstArg} (${err.message})`);
309+
}
304310
scriptPath = firstArg.startsWith("file:") ? import_node_url.default.fileURLToPath(firstArg) : import_index.path.resolve(firstArg);
305311
}
306312
const { ext, base, dir } = import_index.path.parse(tempPath || scriptPath);

build/cli.js

100644100755
File mode changed.

scripts/build-clean.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
import { fileURLToPath } from 'node:url'
18+
import path from 'node:path'
1719
import fs from 'node:fs'
1820
import glob from 'fast-glob'
1921

22+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
23+
2024
const redundants = await glob(
2125
['build/{repl,globals-jsr}.d.ts', 'build/{deps,internals,util,vendor*}.js'],
2226
{

scripts/build-dts.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
import { fileURLToPath } from 'node:url'
18+
import path from 'node:path'
19+
20+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
21+
1722
import fs from 'node:fs/promises'
1823
import { generateDtsBundle } from 'dts-bundle-generator'
1924
import glob from 'fast-glob'

scripts/build-js.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import esbuildResolvePlugin from 'esbuild-plugin-resolve'
2727
import minimist from 'minimist'
2828
import glob from 'fast-glob'
2929

30-
const __dirname = path.dirname(new URL(import.meta.url).pathname)
30+
import { fileURLToPath } from 'node:url'
31+
32+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
3133

3234
const argv = minimist(process.argv.slice(2), {
3335
default: {

scripts/build-jsr.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
import fs from 'node:fs'
1616
import path from 'node:path'
17-
const __dirname = path.dirname(new URL(import.meta.url).pathname)
17+
import { fileURLToPath } from 'node:url'
18+
19+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1820
const root = path.resolve(__dirname, '..')
1921
const pkgJson = JSON.parse(
2022
fs.readFileSync(path.resolve(root, 'package.json'), 'utf-8')

scripts/build-pkgjson-lite.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import fs from 'node:fs'
1818
import path from 'node:path'
1919
import { depseekSync } from 'depseek'
2020

21-
const __dirname = path.dirname(new URL(import.meta.url).pathname)
21+
import { fileURLToPath } from 'node:url'
22+
23+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2224
const root = path.resolve(__dirname, '..')
2325
const source = 'package.json'
2426
const dest = 'package-lite.json'

scripts/build-pkgjson-main.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import fs from 'node:fs'
1818
import path from 'node:path'
1919

20-
const __dirname = path.dirname(new URL(import.meta.url).pathname)
20+
import { fileURLToPath } from 'node:url'
21+
22+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2123
const root = path.resolve(__dirname, '..')
2224
const source = 'package.json'
2325
const dest = 'package-main.json'

scripts/build-tests.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ const modules = [
2626
['cli', cli],
2727
['index', index],
2828
]
29-
const root = path.resolve(new URL(import.meta.url).pathname, '../..')
29+
import { fileURLToPath } from 'node:url'
30+
31+
const root = path.resolve(fileURLToPath(import.meta.url), '../..')
3032
const filePath = path.resolve(root, `test/export.test.js`)
3133

32-
const copyright = await fs.readFileSync(
34+
const copyright = fs.readFileSync(
3335
path.resolve(root, 'test/fixtures/copyright.txt'),
3436
'utf8'
3537
)

0 commit comments

Comments
 (0)