Ohm-Management - Projektarbeit B-ME
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.

index.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*!
  2. * write <https://github.com/jonschlinkert/write>
  3. *
  4. * Copyright (c) 2014-2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. 'use strict';
  8. var fs = require('fs');
  9. var path = require('path');
  10. var mkdir = require('mkdirp');
  11. /**
  12. * Asynchronously write a file to disk. Creates any intermediate
  13. * directories if they don't already exist.
  14. *
  15. * ```js
  16. * var writeFile = require('write');
  17. * writeFile('foo.txt', 'This is content to write.', function(err) {
  18. * if (err) console.log(err);
  19. * });
  20. * ```
  21. *
  22. * @name writeFile
  23. * @param {String} `dest` Destination file path
  24. * @param {String} `str` String to write to disk.
  25. * @param {Function} `callback`
  26. * @api public
  27. */
  28. module.exports = function writeFile(dest, str, cb) {
  29. var dir = path.dirname(dest);
  30. fs.exists(dir, function (exists) {
  31. if (exists) {
  32. fs.writeFile(dest, str, cb);
  33. } else {
  34. mkdir(dir, function (err) {
  35. if (err) {
  36. return cb(err);
  37. } else {
  38. fs.writeFile(dest, str, cb);
  39. }
  40. });
  41. }
  42. });
  43. };
  44. /**
  45. * Synchronously write files to disk. Creates any intermediate
  46. * directories if they don't already exist.
  47. *
  48. * ```js
  49. * var writeFile = require('write');
  50. * writeFile.sync('foo.txt', 'This is content to write.');
  51. * ```
  52. *
  53. * @name writeFile.sync
  54. * @param {String} `dest` Destination file path
  55. * @param {String} `str` String to write to disk.
  56. * @api public
  57. */
  58. module.exports.sync = function writeFileSync(dest, str) {
  59. var dir = path.dirname(dest);
  60. if (!fs.existsSync(dir)) {
  61. mkdir.sync(dir);
  62. }
  63. fs.writeFileSync(dest, str);
  64. };
  65. /**
  66. * Uses `fs.createWriteStream`, but also creates any intermediate
  67. * directories if they don't already exist.
  68. *
  69. * ```js
  70. * var write = require('write');
  71. * write.stream('foo.txt');
  72. * ```
  73. *
  74. * @name writeFile.stream
  75. * @param {String} `dest` Destination file path
  76. * @return {Stream} Returns a write stream.
  77. * @api public
  78. */
  79. module.exports.stream = function writeFileStream(dest) {
  80. var dir = path.dirname(dest);
  81. if (!fs.existsSync(dir)) {
  82. mkdir.sync(dir);
  83. }
  84. return fs.createWriteStream(dest);
  85. };