Skip to content

Commit

Permalink
parseCliFlags() util
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Feb 7, 2021
1 parent 864ff38 commit b3db31f
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fs from 'fs/promises';
import chalk from 'chalk';
import displayHelpOutput from './lib/commands/help.js';
import parseCliFlags from './lib/parse-cli-flags.js';

process.title = 'qunitx';

Expand All @@ -12,6 +13,8 @@ process.title = 'qunitx';
return await displayHelpOutput();
}

let config = await parseCliFlags();

const fileOrFolder = process.argv[2]; // then turn this to array of remaining args
try {
const entry = await fs.stat(fileOrFolder);
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/find-project-root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import searchInParentDirectories from './search-in-parent-directories.js';

export default async function() {
return (await searchInParentDirectories('.', 'package.json')).replace('/package.json', '');
}

38 changes: 38 additions & 0 deletions lib/utils/parse-cli-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from 'fs/promises';
import findProjectRoot from './find-project-root.js';

export default async function() {
const projectRoot = await findProjectRoot();
const packageJSON = await fs.readFile(`${projectRoot}/package.json`);
const projectConfig = JSON.parse(packageJSON.toString()).qunitx;
const defaultValues = {...projectConfig, projectRoot };
const providedFlags = process.argv.slice(3).reduce((result, arg) => {
if (arg.startsWith('--browser')) {
return Object.assign(result, { browser: parseBoolean(arg.split('=')[1]) });
} else if (args.startsWith('--debug')) {
return Object.assign(result, { debug: parseBoolean(arg.split('=')[1]) });
} else if (args.startsWith('--watch')) {
return Object.assign(result, { watch: parseBoolean(arg.split('=')[1]) });
} else if (args.startsWith('--failfast') || args.startsWith('--failFast')) {
return Object.assign(result, { failFast: parseBoolean(arg.split('=')[1]) });
} else if (args.startsWith('--reporter') {
return Object.assign(result, { reporter: arg.split('=')[1] || 'spec' });
} else if (args.startsWith('--coverage')) {
return Object.assign(result, { coverageDist: arg.split('=')[1] || './dist' });
}

return result;
}, {});

return Object.assign(defaultValues, projectConfig, providedFlags);
}

function parseBoolean(result, defaultValue=true) {
if (result === 'true') {
return true;
} else if (result === 'false') {
return false;
}

return defaultValue;
}
11 changes: 11 additions & 0 deletions lib/utils/path-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fs from 'fs/promises';

export default async function pathExists(path) {
try {
await fs.access(path);

return true;
} catch {
return false;
}
}
40 changes: 40 additions & 0 deletions lib/utils/recursive-file-lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// TODO: rewrite in pure node.js
import klaw from 'klaw';
import through2 from 'through2';

export default function(directory, extensions, options={}) {
return new Promise((resolve) => {
const items = []; // NOTE: files, directories, symlinks, etc

klaw(directory, { depthLimit: options.depthLimit || -1 })
.pipe(through2.obj(function(item, enc, next) {
if (!item.stats.isDirectory() && shouldIncludeItem(item, extensions) && pipeFilter(options, item)) {
this.push(item);
}

next();
}))
.on('data', (item) => items.push(item.path))
.on('end', () => resolve(items));
});
}

function shouldIncludeItem(item, extensions) {
if (!extensions) {
return item;
} else if (Array.isArray(extensions) && extensions.some((ext) => item.path.endsWith(ext))) {
return item;
} else if (item.path.endsWith(extensions)) {
return item;
}
}

function pipeFilter(options, item) {
if (!options.filter) {
return true;
} else if (Array.isArray(options.filter)) {
return options.filter.every((filterFunc) => filterFunc(item));
}

return options.filter(item);
}
15 changes: 15 additions & 0 deletions lib/utils/search-in-parent-directories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pathExists from './path-exists.js';

async function searchInParentDirectories(directory, targetEntry) {
directory = directory === '.' ? process.cwd() : directory;

if (await pathExists(`${directory}/${targetEntry}`)) {
return `${directory}/${targetEntry}`;
} else if (directory === '') {
return;
}

return await searchInParentDirectories(directory.slice(0, directory.lastIndexOf('/')), targetEntry);
}

export default searchInParentDirectories;

0 comments on commit b3db31f

Please sign in to comment.