output-json.js 657 B

123456789101112131415161718192021222324252627
  1. 'use strict'
  2. const path = require('path')
  3. const mkdir = require('../mkdirs')
  4. const pathExists = require('../path-exists').pathExists
  5. const jsonFile = require('./jsonfile')
  6. function outputJson (file, data, options, callback) {
  7. if (typeof options === 'function') {
  8. callback = options
  9. options = {}
  10. }
  11. const dir = path.dirname(file)
  12. pathExists(dir, (err, itDoes) => {
  13. if (err) return callback(err)
  14. if (itDoes) return jsonFile.writeJson(file, data, options, callback)
  15. mkdir.mkdirs(dir, err => {
  16. if (err) return callback(err)
  17. jsonFile.writeJson(file, data, options, callback)
  18. })
  19. })
  20. }
  21. module.exports = outputJson