Commit 6cb712f
Changed files (3)
.github/scripts/build.mjs
@@ -1,34 +1,43 @@
#!/usr/bin/env zx
-import path from 'path'
-
$.verbose = false
-
-let inputFile = path.join(__dirname, '../../index.mjs')
-let outputDir = path.join(path.dirname(inputFile), 'dist')
-
console.log(chalk.black.bgYellowBright` BUILD `)
-console.log('- inputFile:', inputFile)
-console.log('- outputDir:', outputDir)
-
let i = 0,
spin = () => process.stdout.write(` ${'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[i++ % 10]}\r`),
stop = (id => () => clearInterval(id))(setInterval(spin, 100))
-let {stdout: bundle} = await $`rollup --format cjs --plugin commonjs,node-resolve ${inputFile}`
-bundle = bundle
- .replace(/node:(\w+)/g, '$1')
- .replace(`Object.defineProperty(exports, '__esModule', { value: true });`, ``)
-
+let indexFile = path.join(__dirname, '../../index.mjs')
+let outputDir = path.join(path.dirname(indexFile), 'dist')
await fs.ensureDir(outputDir)
-await fs.writeFile(path.join(outputDir, 'bundle.cjs'), bundle)
-const exportNames = Object.keys(await import(inputFile))
-await fs.writeFile(path.join(outputDir, 'index.cjs'),
- `const {${exportNames.join(', ')}} = require('./bundle.cjs')
+{
+ let outputFile = 'bundle.cjs'
+ console.log('>', outputFile)
+ let {stdout: bundle} = await $`rollup --format cjs --plugin commonjs,node-resolve ${indexFile}`
+ bundle = bundle
+ .replace(/node:(\w+)/g, '$1')
+ await fs.writeFile(path.join(outputDir, outputFile), bundle)
+}
+
+{
+ let outputFile = 'globals.cjs'
+ console.log('>', outputFile)
+ await fs.writeFile(path.join(outputDir, outputFile),
+ `const {registerGlobals} = require('./index.cjs')
+
+registerGlobals()
+`)
+}
+
+{
+ let outputFile = 'index.cjs'
+ console.log('>', outputFile)
+ const exportNames = Object.keys(await import(indexFile))
+ await fs.writeFile(path.join(outputDir, outputFile),
+ `const {${exportNames.join(', ')}} = require('./bundle.cjs')
module.exports = {${exportNames.join(', ')}}
`)
+}
stop()
-
console.log(chalk.black.bgGreenBright` DONE `)
package.json
@@ -2,10 +2,15 @@
"name": "zx",
"version": "4.1.1",
"description": "A tool for writing better scripts",
- "main": "dist/index.cjs",
"exports": {
- "import": "./index.mjs",
- "require": "./dist/index.cjs"
+ ".": {
+ "import": "./index.mjs",
+ "require": "./dist/index.cjs"
+ },
+ "./globals": {
+ "import": "./globals.mjs",
+ "require": "./dist/globals.cjs"
+ }
},
"types": "index.d.ts",
"bin": {
README.md
@@ -372,21 +372,22 @@ zx docs/markdown.md
```
#### TypeScript scripts
-
-The `zx` can compile `.ts` scripts to `.mjs` and execute them.
-
-```bash
-zx script.ts
-```
-
-Example:
+
```ts
import {$} from 'zx'
+// Or
+import 'zx/globals'
void async function () {
await $`ls -la`
}()
-```
+```
+
+Compile the TypeScript to JS and run it. Or use something like ts-node.
+
+```bash
+ts-node script.ts
+```
#### Executing remote scripts