You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
657 B

4 years ago
  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