Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add extend property #1115

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/lib/utils/options/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as _ from 'lodash';
import * as Util from 'util';
import * as ts from 'typescript';
import * as fs from 'fs-extra';

import { Event } from '../events';
import { Component, AbstractComponent, ChildableComponent } from '../component';
Expand Down Expand Up @@ -63,11 +64,11 @@ export class DiscoverEvent extends Event {
* Responsible for loading options via child components that read options
* from various sources.
*/
@Component({name: 'options', internal: true, childClass: OptionsComponent})
@Component({ name: 'options', internal: true, childClass: OptionsComponent })
export class Options extends ChildableComponent<Application, OptionsComponent> {
private declarations!: {[name: string]: OptionDeclaration};
private declarations!: { [name: string]: OptionDeclaration };

private values!: {[name: string]: any};
private values!: { [name: string]: any };

private compilerOptions!: ts.CompilerOptions;

Expand All @@ -81,7 +82,27 @@ export class Options extends ChildableComponent<Application, OptionsComponent> {
}

read(data: any = {}, mode: OptionsReadMode = OptionsReadMode.Fetch): OptionsReadResult {
const event = new DiscoverEvent(DiscoverEvent.DISCOVER, mode);
const loadedDirectories = new Set()

function loadData(filename: string): void {
if (loadedDirectories.has(filename)) throw new Error("Recursion is not allowed.")
loadedDirectories.add(filename)
let res = fs.readJSONSync(filename);
if (_.isString(res.extends)) {
res = _.merge(res, loadData(res.extends));
} else if (_.isArray(res.extends)) {
res.extends.map((val: string) => res = _.merge(res, loadData(val)));
}
return _.merge(data, res);
}

if (_.isString(data.extends)) {
data = loadData(data.extends);
} else if (_.isArray(data.extends)) {
data.extends.map((val: string) => { data = loadData(val); });
}

const event = new DiscoverEvent(DiscoverEvent.DISCOVER, mode);
event.data = data;

this.trigger(event);
Expand Down Expand Up @@ -162,7 +183,7 @@ export class Options extends ChildableComponent<Application, OptionsComponent> {
}
}

addDeclaration(declaration: OptionDeclaration|DeclarationOption) {
addDeclaration(declaration: OptionDeclaration | DeclarationOption) {
const decl = declaration instanceof OptionDeclaration
? declaration
: new OptionDeclaration(declaration);
Expand All @@ -178,7 +199,7 @@ export class Options extends ChildableComponent<Application, OptionsComponent> {
}
}

addDeclarations(declarations: (OptionDeclaration|DeclarationOption)[]) {
addDeclarations(declarations: (OptionDeclaration | DeclarationOption)[]) {
for (let declaration of declarations) {
this.addDeclaration(declaration);
}
Expand Down