Sha256: 46e30a0f4b8833c6343e68959ec7cb407584e7cc4c7f251d91949a567031227d

Contents?: true

Size: 983 Bytes

Versions: 46

Compression:

Stored size: 983 Bytes

Contents

'use strict'

const u = require('universalify').fromCallback
const fs = require('fs')
const path = require('path')
const mkdir = require('../mkdirs')
const remove = require('../remove')

const emptyDir = u(function emptyDir (dir, callback) {
  callback = callback || function () {}
  fs.readdir(dir, (err, items) => {
    if (err) return mkdir.mkdirs(dir, callback)

    items = items.map(item => path.join(dir, item))

    deleteItem()

    function deleteItem () {
      const item = items.pop()
      if (!item) return callback()
      remove.remove(item, err => {
        if (err) return callback(err)
        deleteItem()
      })
    }
  })
})

function emptyDirSync (dir) {
  let items
  try {
    items = fs.readdirSync(dir)
  } catch (err) {
    return mkdir.mkdirsSync(dir)
  }

  items.forEach(item => {
    item = path.join(dir, item)
    remove.removeSync(item)
  })
}

module.exports = {
  emptyDirSync,
  emptydirSync: emptyDirSync,
  emptyDir,
  emptydir: emptyDir
}

Version data entries

46 entries across 46 versions & 2 rubygems

Version Path
govuk_publishing_components-16.21.0 node_modules/fs-extra/lib/empty/index.js
govuk_publishing_components-16.20.1 node_modules/fs-extra/lib/empty/index.js
govuk_publishing_components-16.20.0 node_modules/fs-extra/lib/empty/index.js
govuk_publishing_components-16.19.0 node_modules/fs-extra/lib/empty/index.js
govuk_publishing_components-16.18.0 node_modules/fs-extra/lib/empty/index.js
govuk_publishing_components-16.17.0 node_modules/fs-extra/lib/empty/index.js