-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', ''); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |