Commit 73cd163

Anton Medvedev <anton@medv.io>
2021-07-04 22:54:13
Use fs-extra
1 parent 9f0d0bc
index.d.ts
@@ -14,7 +14,7 @@
 
 import {ChildProcess} from 'child_process'
 import {Readable, Writable} from 'stream'
-import {createReadStream, createWriteStream, promises as fsPromises, constants as fsConstants} from 'fs'
+import * as _fs from 'fs-extra'
 import * as _os from 'os'
 import * as _chalk from 'chalk'
 import _fetch from 'node-fetch'
@@ -50,11 +50,6 @@ export class ProcessOutput {
 export type QuestionOptions = { choices: string[] }
 
 type cd = (path: string) => void
-type fs = typeof fsPromises & {
-  constants: typeof fsConstants
-  createWriteStream: typeof createWriteStream
-  createReadStream: typeof createReadStream
-}
 type nothrow = (p: ProcessPromise<ProcessOutput>) => ProcessPromise<ProcessOutput>
 type question = (query?: string, options?: QuestionOptions) => Promise<string>
 type sleep = (ms: number) => Promise<void>
@@ -63,7 +58,7 @@ export const $: $
 export const cd: cd
 export const chalk: typeof _chalk
 export const fetch: typeof _fetch
-export const fs: fs
+export const fs: typeof _fs
 export const nothrow: nothrow
 export const os: typeof _os
 export const question: question
@@ -75,7 +70,7 @@ declare global {
   var chalk: typeof _chalk
   // @ts-ignore
   var fetch: typeof _fetch
-  var fs: fs
+  var fs: typeof _fs
   var nothrow: nothrow
   var os: typeof _os
   var question: question
index.mjs
@@ -12,13 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-import {
-  createReadStream,
-  createWriteStream,
-  existsSync,
-  promises as fsPromises,
-  constants as fsConstants,
-} from 'fs'
+import fs from 'fs-extra'
 import os from 'os'
 import {promisify, inspect} from 'util'
 import {spawn} from 'child_process'
@@ -95,7 +89,7 @@ $.cwd = undefined
 
 export function cd(path) {
   if ($.verbose) console.log('$', colorize(`cd ${path}`))
-  if (!existsSync(path)) {
+  if (!fs.existsSync(path)) {
     let __from = (new Error().stack.split('at ')[2]).trim()
     console.error(`cd: ${path}: No such directory`)
     console.error(`    at ${__from}`)
@@ -104,8 +98,6 @@ export function cd(path) {
   $.cwd = path
 }
 
-export const fs = {...fsPromises, constants: fsConstants, createWriteStream, createReadStream}
-
 export async function question(query, options) {
   let completer = undefined
   if (Array.isArray(options?.choices)) {
@@ -265,4 +257,4 @@ Object.assign(global, {
   sleep,
 })
 
-export {chalk}
+export {chalk, fs}
package.json
@@ -14,9 +14,11 @@
     "test": "node zx.mjs test.mjs"
   },
   "dependencies": {
+    "@types/fs-extra": "^9.0.11",
     "@types/node": "^16.0",
     "@types/node-fetch": "^2.5.10",
     "chalk": "^4.1.1",
+    "fs-extra": "^10.0.0",
     "node-fetch": "^2.6.1",
     "which": "^2.0.2"
   },
zx.mjs
@@ -16,7 +16,7 @@
 
 import {basename, dirname, extname, join, parse, resolve} from 'path'
 import {tmpdir} from 'os'
-import {promises as fs} from 'fs'
+import fs from 'fs-extra'
 import {createRequire} from 'module'
 import url from 'url'
 import {$, fetch, ProcessOutput} from './index.mjs'