Skip to content

Commit

Permalink
🆕 Add tempFiles helper
Browse files Browse the repository at this point in the history
  • Loading branch information
steelbrain committed Jan 2, 2016
1 parent 7f38314 commit e1d7819
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Upcoming

* Add `tempFiles` helper

### 4.2.0

* Use `consistent-path` package to determine `$PATH` correctly on OSX
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Helpers{
static findAsync(directory: Strng, names: String | Array<string>): Promise<?String>
static findCachedAsync(directory: Strng, names: String | Array<string>): Promise<?String>
static tempFile<T>(fileName:String, fileContents:String, Callback:Function<T>):Promise<T>
static tempFiles<T>(filesNames:Array<{name: String, contents: String}>, callback:Function<T>):Promise<T>
static createElement(tagName: string): HTMLElement
}
```
Expand Down
53 changes: 53 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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];

Expand Down
52 changes: 52 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit e1d7819

Please sign in to comment.