Comparing version 5.5.0 to 6.0.0
44
files.js
/*files.js*/ | ||
"use strict"; | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const textFileContentPromiseFromPath = function (path) { | ||
const createNecessaryDirectories = function (filePath) { | ||
const directoryname = path.dirname(filePath); | ||
if (fs.existsSync(directoryname)) { | ||
return; | ||
} | ||
createNecessaryDirectories(directoryname); | ||
fs.mkdirSync(directoryname); | ||
}; | ||
const textFileContentPromiseFromPath = function (filePath) { | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(path, "utf-8", function (error, data) { | ||
fs.readFile(filePath, "utf-8", function (error, data) { | ||
if (error) { | ||
@@ -16,5 +26,6 @@ reject(error); | ||
const writeTextInFilePromiseFromPathAndString = function (path, string) { | ||
const writeTextInFilePromiseFromPathAndString = function (filePath, string) { | ||
return new Promise(function (resolve, reject) { | ||
fs.writeFile(path, string, "utf-8", function (error, notUsed) { | ||
createNecessaryDirectories(filePath); | ||
fs.writeFile(filePath, string, "utf-8", function (error, notUsed) { | ||
if (error) { | ||
@@ -28,24 +39,29 @@ reject(error); | ||
const concatenateFiles = function (files, destination, separator=``) { | ||
return Promise.all(files.map(textFileContentPromiseFromPath)).then( | ||
function (contents) { | ||
return writeTextInFilePromiseFromPathAndString(destination, contents.join(separator)) | ||
return writeTextInFilePromiseFromPathAndString( | ||
destination, | ||
contents.join(separator) | ||
); | ||
}); | ||
}; | ||
}; | ||
const copyFile = function (sourcePath, destination) { | ||
/* fs.copyFile exists in Node 9+ | ||
const copyFile = function (filePath, filePathDestination) { | ||
/* fs.copyFile exists in Node 9+ | ||
todo if dest cannot be reached created folders until it is*/ | ||
return new Promise(function (resolve, reject) { | ||
if (!fs.existsSync(sourcePath)) { | ||
reject(`${sourcePath} does not exist`); | ||
if (!fs.existsSync(filePath)) { | ||
reject(`${filePath} does not exist`); | ||
return; | ||
} | ||
fs.copyFile(sourcePath, destination, (err) => { | ||
if (err) { | ||
reject(err); | ||
createNecessaryDirectories(filePathDestination); | ||
fs.copyFile(filePath, filePathDestination, (error) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
resolve(`${sourcePath} was copied to ${destination}`); | ||
resolve(`${filePath} was copied to ${filePathDestination}`); | ||
}); | ||
@@ -52,0 +68,0 @@ }); |
{ | ||
"name": "utilsac", | ||
"version": "5.5.0", | ||
"description": "tools mostly", | ||
"version": "6.0.0", | ||
"description": "JavaScript General Purpose utility functions", | ||
"main": "files.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20537
10
377