-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework into new interface and structure
Try-out towards #16
- Loading branch information
1 parent
86324e4
commit 88dfcac
Showing
31 changed files
with
392 additions
and
342 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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { Bootstrap } from './utils/bootstrap' | ||
import { Analyzers } from './analyzers' | ||
import { Runner } from './runner' | ||
import { find } from './analyzers/Autoload' | ||
import { run } from './runner' | ||
|
||
const { exercise, options, solution, logger } = Bootstrap.call() | ||
const { exercise, options, input, logger } = Bootstrap.call() | ||
|
||
logger.log('=> DEBUG mode is on') | ||
logger.log(`=> exercise: ${exercise.slug}`) | ||
|
||
const AnalyzerClass = Analyzers.find(exercise) | ||
const analyzer = new AnalyzerClass(solution) | ||
const AnalyzerClass = find(exercise) | ||
const analyzer = new AnalyzerClass() | ||
|
||
Runner.call(analyzer, options) | ||
run(analyzer, input, options) | ||
.then(() => process.exit(0)) | ||
.catch((err) => logger.fatal(err.toString())) | ||
.catch((err: any) => logger.fatal(err.toString())) | ||
|
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,46 @@ | ||
import { Exercise } from '../exercise' | ||
|
||
import path from 'path' | ||
|
||
import { getProcessLogger } from '../utils/logger' | ||
|
||
type AnalyzerConstructor = new () => Analyzer | ||
|
||
/** | ||
* Find an analyzer for a specific exercise | ||
* | ||
* @param exercise The exericse | ||
* @returns the Analyzer constructor | ||
*/ | ||
export function find(exercise: Readonly<Exercise>): AnalyzerConstructor { | ||
const file = autoload(exercise) | ||
const key = Object.keys(file).find(key => file[key] instanceof Function) | ||
|
||
if (key === undefined) { | ||
throw new Error(`No Analyzer found in './${exercise.slug}`) | ||
} | ||
|
||
const analyzer = file[key] | ||
getProcessLogger().log(`=> analyzer: ${analyzer.name}`) | ||
return analyzer | ||
} | ||
|
||
function autoload(exercise: Readonly<Exercise>) { | ||
const modulePath = path.join(__dirname, exercise.slug, 'index') // explicit path (no extension) | ||
try { | ||
return require(modulePath) | ||
} catch(err) { | ||
const logger = getProcessLogger() | ||
logger.error(` | ||
Could not find the index.js analyzer in "${modulePath}" | ||
Make sure that: | ||
- the slug "${exercise.slug}" is valid (hint: use dashes, not underscores) | ||
- there is actually an analyzer written for that exercise | ||
Original error: | ||
`.trimLeft()) | ||
logger.fatal(JSON.stringify(err), -32) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,48 @@ | ||
import { readDir } from "../utils/fs"; | ||
import { FileInput } from "./FileInput"; | ||
|
||
import path from 'path' | ||
|
||
const EXTENSIONS = /\.(jsx?|tsx?|mjs)$/ | ||
const TEST_FILES = /\.spec|test\./ | ||
const CONFIGURATION_FILES = /(?:babel\.config\.js|jest\.config\.js|\.eslintrc\.js)$/ | ||
|
||
export class DirectoryInput implements Input { | ||
constructor(private readonly path: string, private readonly exerciseSlug: string) {} | ||
|
||
async read(n = 1): Promise<string[]> { | ||
const files = await readDir(this.path) | ||
|
||
const candidates = findCandidates(files, n, `${this.exerciseSlug}.js`) | ||
const fileSources = await Promise.all( | ||
candidates.map(candidate => new FileInput(path.join(this.path, candidate)).read().then(([source]) => source)) | ||
) | ||
|
||
return fileSources | ||
} | ||
} | ||
|
||
/** | ||
* Given a list of files, finds up to n files that are not test files and have | ||
* an extension that will probably work with the estree analyzer. | ||
* | ||
* @param files the file candidates | ||
* @param n the number of files it should return | ||
* @param preferredNames the names of the files it prefers | ||
*/ | ||
function findCandidates(files: string[], n: number, ...preferredNames: string[]) { | ||
const candidates = files | ||
.filter(file => EXTENSIONS.test(file)) | ||
.filter(file => !TEST_FILES.test(file)) | ||
.filter(file => !CONFIGURATION_FILES.test(file)) | ||
|
||
const preferredMatches = preferredNames | ||
? candidates.filter(file => preferredNames.includes(file)) | ||
: [] | ||
|
||
const allMatches = preferredMatches.length >= n | ||
? preferredMatches | ||
: preferredMatches.concat(candidates.filter(file => !preferredMatches.includes(file))) | ||
|
||
return allMatches.slice(0, n) | ||
} |
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,10 @@ | ||
import { readFile } from "../utils/fs"; | ||
|
||
export class FileInput implements Input { | ||
constructor(private readonly path: string) {} | ||
|
||
async read(n = 1): Promise<string[]> { | ||
const buffer = await readFile(this.path) | ||
return [buffer.toString("utf8")] | ||
} | ||
} |
Oops, something went wrong.