index.js 657 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict'
  2. const u = require('universalify').fromPromise
  3. const fs = require('../fs')
  4. const path = require('path')
  5. const mkdir = require('../mkdirs')
  6. const pathExists = require('../path-exists').pathExists
  7. async function outputFile (file, data, encoding = 'utf-8') {
  8. const dir = path.dirname(file)
  9. if (!(await pathExists(dir))) {
  10. await mkdir.mkdirs(dir)
  11. }
  12. return fs.writeFile(file, data, encoding)
  13. }
  14. function outputFileSync (file, ...args) {
  15. const dir = path.dirname(file)
  16. if (!fs.existsSync(dir)) {
  17. mkdir.mkdirsSync(dir)
  18. }
  19. fs.writeFileSync(file, ...args)
  20. }
  21. module.exports = {
  22. outputFile: u(outputFile),
  23. outputFileSync
  24. }