Commit 5e0165d

Anton Medvedev <anton@medv.io>
2022-05-30 00:45:22
Simplify globals.ts
1 parent 20960b7
src/cli.ts
@@ -20,18 +20,11 @@ import { tmpdir } from 'node:os'
 import { basename, dirname, extname, join, resolve } from 'node:path'
 import url from 'node:url'
 
-import {
-  $,
-  argv,
-  fetch,
-  ProcessOutput,
-  registerGlobals,
-  chalk,
-} from './index.js'
+import { $, argv, fetch, ProcessOutput, chalk } from './index.js'
 import { randomId } from './util.js'
+import './globals.js'
 
 await (async function main() {
-  registerGlobals()
   $.verbose = !argv.quiet
   if (typeof argv.shell === 'string') {
     $.shell = argv.shell
src/globals.ts
@@ -1,3 +1,24 @@
-import { registerGlobals } from './index.js'
+import * as _ from './index.js'
 
-registerGlobals()
+Object.assign(global, _)
+
+declare global {
+  var $: typeof _.$
+  var argv: typeof _.argv
+  var cd: typeof _.cd
+  var chalk: typeof _.chalk
+  // @ts-ignore
+  // TODO: This not working for some reason.
+  // var fetch: typeof _.fetch
+  var fs: typeof _.fs
+  var glob: typeof _.glob
+  var globby: typeof _.globby
+  var nothrow: typeof _.nothrow
+  var os: typeof _.os
+  var path: typeof _.path
+  var question: typeof _.question
+  var quiet: typeof _.quiet
+  var sleep: typeof _.sleep
+  var which: typeof _.which
+  var YAML: typeof _.YAML
+}
src/index.ts
@@ -50,24 +50,3 @@ export {
   which,
   YAML,
 }
-
-export function registerGlobals() {
-  Object.assign(global, {
-    $,
-    argv,
-    cd,
-    chalk,
-    fetch,
-    fs,
-    glob,
-    globby,
-    nothrow,
-    os,
-    path,
-    question,
-    quiet,
-    sleep,
-    which,
-    YAML,
-  })
-}
test/index.test.js
@@ -219,8 +219,11 @@ test('globby available', async (t) => {
   t.deepEqual(await globby('*.md'), ['README.md'])
 })
 
-test('fetch', async t => {
-  t.regex(await fetch('https://medv.io').then(res => res.text()), /Anton Medvedev/)
+test('fetch', async (t) => {
+  t.regex(
+    await fetch('https://medv.io').then((res) => res.text()),
+    /Anton Medvedev/
+  )
 })
 
 test('Executes a script from $PATH', async (t) => {
package.json
@@ -4,7 +4,7 @@
   "description": "A tool for writing better scripts.",
   "type": "module",
   "main": "build/index.js",
-  "types": "src/index.d.ts",
+  "types": "build/index.d.ts",
   "exports": {
     ".": "./build/index.js",
     "./globals": "./build/globals.js",