Clean up the oopsie. Rebuild the blank exercises.

This commit is contained in:
Michael Frank
2021-05-09 00:39:21 +12:00
parent b984cb4c6f
commit d28d80c46f
1565 changed files with 27186 additions and 199003 deletions
+54 -13
View File
@@ -1,24 +1,65 @@
'use strict';
const path = require('path');
const pathExists = require('path-exists');
const fs = require('fs');
const {promisify} = require('util');
const pLocate = require('p-locate');
module.exports = (iterable, opts) => {
opts = Object.assign({
cwd: process.cwd()
}, opts);
const fsStat = promisify(fs.stat);
const fsLStat = promisify(fs.lstat);
return pLocate(iterable, el => pathExists(path.resolve(opts.cwd, el)), opts);
const typeMappings = {
directory: 'isDirectory',
file: 'isFile'
};
module.exports.sync = (iterable, opts) => {
opts = Object.assign({
cwd: process.cwd()
}, opts);
function checkType({type}) {
if (type in typeMappings) {
return;
}
for (const el of iterable) {
if (pathExists.sync(path.resolve(opts.cwd, el))) {
return el;
throw new Error(`Invalid type specified: ${type}`);
}
const matchType = (type, stat) => type === undefined || stat[typeMappings[type]]();
module.exports = async (paths, options) => {
options = {
cwd: process.cwd(),
type: 'file',
allowSymlinks: true,
...options
};
checkType(options);
const statFn = options.allowSymlinks ? fsStat : fsLStat;
return pLocate(paths, async path_ => {
try {
const stat = await statFn(path.resolve(options.cwd, path_));
return matchType(options.type, stat);
} catch (_) {
return false;
}
}, options);
};
module.exports.sync = (paths, options) => {
options = {
cwd: process.cwd(),
allowSymlinks: true,
type: 'file',
...options
};
checkType(options);
const statFn = options.allowSymlinks ? fs.statSync : fs.lstatSync;
for (const path_ of paths) {
try {
const stat = statFn(path.resolve(options.cwd, path_));
if (matchType(options.type, stat)) {
return path_;
}
} catch (_) {
}
}
};