diff --git a/CHANGELOG.md b/CHANGELOG.md index e1e850d..18c886b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### Upcoming + +* Add `tempFiles` helper + ### 4.2.0 * Use `consistent-path` package to determine `$PATH` correctly on OSX diff --git a/README.md b/README.md index 0e1af62..8b6e040 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ class Helpers{ static findAsync(directory: Strng, names: String | Array): Promise static findCachedAsync(directory: Strng, names: String | Array): Promise static tempFile(fileName:String, fileContents:String, Callback:Function):Promise + static tempFiles(filesNames:Array<{name: String, contents: String}>, callback:Function):Promise static createElement(tagName: string): HTMLElement } ``` diff --git a/lib/helpers.js b/lib/helpers.js index 604347b..71cc142 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -14,6 +14,7 @@ exports.findCachedAsync = findCachedAsync; exports.find = find; exports.findCached = findCached; exports.tempFile = tempFile; +exports.tempFiles = tempFiles; exports.parse = parse; var _atom = require('atom'); @@ -349,6 +350,58 @@ function tempFile(fileName, fileContents, callback) { }); } +function tempFiles(files, callback) { + if (!Array.isArray(files)) { + throw new Error('Invalid or no `files` provided'); + } else if (typeof callback !== 'function') { + throw new Error('Invalid or no `callback` provided'); + } + + return new Promise(function (resolve, reject) { + TMP.dir({ + prefix: 'atom-linter_' + }, function (error, directory, directoryCleanup) { + if (error) { + directoryCleanup(); + return reject(error); + } + let foundError = false; + let filePaths = null; + Promise.all(files.map(function (file) { + const fileName = file.name; + const fileContents = file.contents; + const filePath = Path.join(directory, fileName); + return new Promise(function (resolve, reject) { + FS.writeFile(filePath, fileContents, function (error) { + if (error) { + // Note: Intentionally not doing directoryCleanup 'cause it won't work + // Because we would've already wrote a few files and when even file + // exists in a directory, it can't be removed + reject(error); + } else resolve(filePath); + }); + }); + })).then(function (_filePaths) { + return callback(filePaths = _filePaths); + }).catch(function (result) { + foundError = true; + return result; + }).then(function (result) { + if (filePaths !== null) { + Promise.all(filePaths.map(function (filePath) { + return new Promise(function (resolve) { + FS.unlink(filePath, resolve); + }); + })).then(directoryCleanup); + } + if (foundError) { + throw result; + } else return result; + }); + }); + }); +} + function parse(data, regex) { let opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; diff --git a/src/helpers.js b/src/helpers.js index 75a769c..539c994 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -314,6 +314,58 @@ export function tempFile(fileName, fileContents, callback) { }) } +export function tempFiles(files, callback) { + if (!Array.isArray(files)) { + throw new Error('Invalid or no `files` provided') + } else if (typeof callback !== 'function') { + throw new Error('Invalid or no `callback` provided') + } + + return new Promise(function(resolve, reject) { + TMP.dir({ + prefix: 'atom-linter_' + }, function(error, directory, directoryCleanup) { + if (error) { + directoryCleanup() + return reject(error) + } + let foundError = false + let filePaths = null + Promise.all(files.map(function(file) { + const fileName = file.name + const fileContents = file.contents + const filePath = Path.join(directory, fileName) + return new Promise(function(resolve, reject) { + FS.writeFile(filePath, fileContents, function(error) { + if (error) { + // Note: Intentionally not doing directoryCleanup 'cause it won't work + // Because we would've already wrote a few files and when even file + // exists in a directory, it can't be removed + reject(error) + } else resolve(filePath) + }) + }) + })).then(function(_filePaths) { + return callback(filePaths = _filePaths) + }).catch(function(result) { + foundError = true + return result + }).then(function(result) { + if (filePaths !== null) { + Promise.all(filePaths.map(function(filePath) { + return new Promise(function(resolve) { + FS.unlink(filePath, resolve) + }) + })).then(directoryCleanup) + } + if (foundError) { + throw result + } else return result + }) + }) + }) +} + export function parse(data, regex, opts = {}) { if (typeof data !== 'string') { throw new Error('Invalid or no `data` provided')