Skip to content

Commit 9863018

Browse files
committed
switch away from iife to support esnext
1 parent 50da7ae commit 9863018

File tree

9 files changed

+57
-27
lines changed

9 files changed

+57
-27
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ jobs:
110110
publish-gpr:
111111
name: 'Publish: GitHub Package Registry'
112112
needs: [test]
113+
permissions: write-all
113114
runs-on: ubuntu-latest
114115
steps:
115116
- name: Checkout

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# **`xtsz`**
22

3+
```sh
4+
# pnpm
5+
pnpm add --save-dev xtsz
6+
# yarn
7+
yarn add --dev xtsz
8+
# npm
9+
npm install --save-dev xtsz
10+
```
11+
312
```sh
413
xtsz is a TypeScript and JavaScript (Node.js) runner that supports https-imports.
514
It can run ESM and CJS: .ts, .js, .cjs, .mjs.
@@ -9,7 +18,7 @@ Usage:
918

1019

1120
Options:
12-
--file, -f The file to run. Can be passed as a flag or as an argument.
21+
--file The file to run. Can be passed as a flag or as an argument.
1322
--help, -h Show this help message.
1423

1524
Examples:

help.png

-78.5 KB
Loading

src/help.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ${ansi.bold}Usage:${ansi.reset}
1313
$${ansi.green} xtsz <filepath> ${ansi.reset}
1414
1515
${ansi.bold}Options:${ansi.reset}
16-
${ansi.green}--file, -f${ansi.reset}${ansi.brightPurple} The file to run. Can be passed as a flag or as an argument. ${ansi.reset}
16+
${ansi.green}--file ${ansi.reset}${ansi.brightPurple} The file to run. Can be passed as a flag or as an argument. ${ansi.reset}
1717
${ansi.green}--help, -h${ansi.reset}${ansi.brightPurple} Show this help message. ${ansi.reset}
1818
1919
${ansi.bold}Examples:${ansi.reset}

src/index.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
import meow from 'meow'
23
import { $, execa } from 'execa'
34
import * as esbuild from 'esbuild'
@@ -24,24 +25,33 @@ function woof() {
2425
return filePath
2526
}
2627

27-
//
28-
;(async () => {
29-
const process = await import('node:process')
30-
if (process.argv[2] === 'help') return process.stdout.write(helpMessage)
31-
const filePath = woof()
32-
const ENTRY_POINT = filePath
33-
const OUT_FILE = `/tmp/${Date.now()}-out.js`
28+
async function main() {
29+
try {
30+
const process = await import('node:process')
31+
if (process.argv[2] === 'help') return process.stdout.write(helpMessage)
32+
const filePath = woof()
33+
const ENTRY_POINT = filePath
34+
const OUT_FILE = `/tmp/${Date.now()}-out.mjs`
3435

35-
await esbuild.build({
36-
entryPoints: [ENTRY_POINT],
37-
bundle: true,
38-
outfile: OUT_FILE,
39-
plugins: [httpPlugin],
40-
})
36+
await esbuild.build({
37+
entryPoints: [ENTRY_POINT],
38+
bundle: true,
39+
outfile: OUT_FILE,
40+
target: 'esnext',
41+
format: 'esm',
42+
plugins: [httpPlugin],
43+
})
44+
45+
const { stdout } = await execa('tsx', [OUT_FILE])
46+
process.stdout.write(stdout)
47+
// lastly, remove the file
48+
await $`rm -rf ${OUT_FILE}`
49+
return stdout
50+
} catch (error) {
51+
console.error(error)
52+
console.info('\n\n\nPlease report this error to https://github.com/o-az/xtsz/issues. Really appreciate it!')
53+
process.exit(1)
54+
}
55+
}
4156

42-
const { stdout } = await execa('tsx', [OUT_FILE])
43-
process.stdout.write(stdout)
44-
// lastly, remove the file
45-
await $`rm -rf ${OUT_FILE}`
46-
return stdout
47-
})()
57+
main()

test/cases/index.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ const {
55
const result = example()
66

77
console.log({ result })
8+
;(async () => {
9+
let promise = await fetch('https://jsonplaceholder.typicode.com/todos/1')
10+
let data = await promise.json()
11+
12+
console.log(data)
13+
})()

test/cases/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import { example } from 'https://gist.githubusercontent.com/o-az/e8ed1e89fde52af306099ed28e297cae/raw/51a1d68f54ba0718c2ab72a52b97f13cf613563f/sample.js'
2-
const result = example()
3-
console.log({ result })
1+
let promise = await fetch('https://jsonplaceholder.typicode.com/todos/1')
2+
export let data = await promise.json()
3+
4+
console.log(data)

test/index.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@ import { execa } from 'execa'
22
import { it } from 'node:test'
33
import * as assert from 'node:assert'
44

5-
const result = `{ result: { foo: 'bar' } }`
6-
75
it('should run a TypeScript file', async () => {
6+
const result = `{ result: { foo: 'bar' } }`
87
const { stdout } = await execa('node_modules/.bin/xtsz', ['./test/cases/index.ts'])
98
assert.deepEqual(result, stdout)
109
})
1110

1211
it('should run a JavaScript file', async () => {
12+
const result = `{ userId: 1, id: 1, title: 'delectus aut autem', completed: false }`
1313
const { stdout } = await execa('node_modules/.bin/xtsz', ['./test/cases/index.js'])
1414
assert.equal(stdout, result)
1515
})
1616

1717
it('should run a CJS file', async () => {
18+
const result = `{ result: { foo: 'bar' } }
19+
{ userId: 1, id: 1, title: 'delectus aut autem', completed: false }`
1820
const { stdout } = await execa('node_modules/.bin/xtsz', ['./test/cases/index.cjs'])
1921
assert.equal(stdout, result)
2022
})
2123

2224
it('should run a file with --file flag', async () => {
25+
const result = `{ result: { foo: 'bar' } }`
2326
const { stdout } = await execa('node_modules/.bin/xtsz', ['--file', './test/cases/index.ts'])
2427
assert.equal(stdout, result)
2528
})

tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineConfig({
77
minify: true,
88
outDir: './dist',
99
platform: 'node',
10+
target: 'esnext',
1011
format: ['esm'],
1112
entry: ['./src/index.ts'],
12-
banner: _context => ({ js: `#!/usr/bin/env node` }),
1313
})

0 commit comments

Comments
 (0)