Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Module finder now loads from local bundled templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jhm-ciberman committed Jul 23, 2019
1 parent d4d1942 commit 36791ef
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 222 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"dependencies": {
"comment-parser": "^0.6.0",
"del": "^5.0.0",
"docs_gm-basic": "^3.1.0",
"fast-glob": "^3.0.4",
"fs-extra": "^8.1.0",
"get-installed-path": "^4.0.8",
Expand Down
28 changes: 19 additions & 9 deletions src/template/ModuleFinder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

import * as fse from "fs-extra";
import { inject, injectable } from "inversify";
import pkgDir from "pkg-dir";
import * as path from "path";
import { IGetInstalledPath } from "../npmmodules";
import { TYPES } from "../types";
import IModuleFinder from "./interfaces/IModuleFinder";
import ModuleFinderConfig from "./ModuleFinderConfig";

/**
* Finds a module installed locally or globally and returns his path
Expand All @@ -21,16 +23,15 @@ export default class ModuleFinder implements IModuleFinder {
* Gets the path of a template npm module from a global installation or local installation.
* @param templateName The name of the template to find
*/
public async find(moduleName: string): Promise<string> {
public async find(moduleName: string, moduleFinderConfig: ModuleFinderConfig): Promise<string> {
return this._findGlobalModule(moduleName)
.catch(() => this._findLocal(moduleName))
.catch(() => this._findLocal(moduleName, moduleFinderConfig))
.catch(() => Promise.reject(new Error(`Cannot find the module "${moduleName}"`)));
}

protected async _findLocal(moduleName: string) {
const packageRoot = await pkgDir() as string;
return this._findBundledModule(moduleName, packageRoot )
.catch(() => this._findLocalModule(moduleName, packageRoot));
protected async _findLocal(moduleName: string, moduleFinderConfig: ModuleFinderConfig) {
return this._findBundledModule(moduleName, moduleFinderConfig.templatesPath)
.catch(() => this._findLocalModule(moduleName, moduleFinderConfig.packageRoot));
}

protected async _findGlobalModule(moduleName: string): Promise<string> {
Expand All @@ -41,7 +42,16 @@ export default class ModuleFinder implements IModuleFinder {
return await this._getInstalledPath(moduleName, { local: true, cwd: packageRoot });
}

protected async _findBundledModule(moduleName: string, packageRoot: string): Promise<string> {
return await this._getInstalledPath(moduleName, { cwd: packageRoot + "/templates/" });
protected async _findBundledModule(moduleName: string, templatesPath: string): Promise<string> {
const templatePath = path.resolve(templatesPath, moduleName);
return new Promise((resolve, reject) => {
fse.access(path.resolve(templatePath, "package.json"), (err) => {
if (err) {
reject();
} else {
resolve(templatePath);
}
});
});
}
}
4 changes: 4 additions & 0 deletions src/template/ModuleFinderConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default class ModuleFinderConfig {
public packageRoot: string;
public templatesPath: string;
}
113 changes: 61 additions & 52 deletions src/template/TemplateLoader.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
import * as fse from "fs-extra";
import { inject, injectable } from "inversify";
import * as path from "path";
import IOutputConfig from "../config/interfaces/IOutputConfig";
import { TYPES } from "../types";
import IModuleFinder from "./interfaces/IModuleFinder";
import { ITemplate } from "./interfaces/ITemplate";
import ITemplateFactory from "./interfaces/ITemplateFactory";
import ITemplateLoader from "./interfaces/ITemplateLoader";
import { IRoot } from "./interfaces/TemplateJSON";
/**
* This class is used to load a Template from disk.
* It can be installed as an npm module or in a local folder.
*/
@injectable()
export default class TemplateLoader implements ITemplateLoader {

@inject(TYPES.ITemplateFactory)
private _templateFactory: ITemplateFactory;

@inject(TYPES.IModuleFinder)
private _moduleFinder: IModuleFinder;

/**
* Factory method to load the template from a folder
* @param folder The folder name
* @returns A promise
*/
public async loadFrom(folder: string): Promise<ITemplate> {
let data: IRoot;
const jsonPath = path.resolve(folder, "template.json");
try {
data = await fse.readJSON(jsonPath);
} catch (e) {
throw new Error(`Error loading Template from "${jsonPath}"`);
}
return this._templateFactory.create(folder, data);
}

/**
* Returns the folder of the template to load
* @param output The output config
*/
public async getFolder(output: IOutputConfig): Promise<string> {
if (output.templatesFolder !== "") {
return path.resolve(output.templatesFolder, output.template);
} else {
return await this._moduleFinder.find("docs_gm-" + output.template);
}
}

}
import * as fse from "fs-extra";
import { inject, injectable } from "inversify";
import * as path from "path";
import * as pkgDir from "pkg-dir";
import IOutputConfig from "../config/interfaces/IOutputConfig";
import { TYPES } from "../types";
import IModuleFinder from "./interfaces/IModuleFinder";
import { ITemplate } from "./interfaces/ITemplate";
import ITemplateFactory from "./interfaces/ITemplateFactory";
import ITemplateLoader from "./interfaces/ITemplateLoader";
import { IRoot } from "./interfaces/TemplateJSON";
import ModuleFinderConfig from "./ModuleFinderConfig";
/**
* This class is used to load a Template from disk.
* It can be installed as an npm module or in a local folder.
*/
@injectable()
export default class TemplateLoader implements ITemplateLoader {

@inject(TYPES.ITemplateFactory)
private _templateFactory: ITemplateFactory;

@inject(TYPES.IModuleFinder)
private _moduleFinder: IModuleFinder;

/**
* Factory method to load the template from a folder
* @param folder The folder name
* @returns A promise
*/
public async loadFrom(folder: string): Promise<ITemplate> {
let data: IRoot;
const jsonPath = path.resolve(folder, "template.json");
try {
data = await fse.readJSON(jsonPath);
} catch (e) {
throw new Error(`Error loading Template from "${jsonPath}"`);
}
return this._templateFactory.create(folder, data);
}

/**
* Returns the folder of the template to load
* @param output The output config
*/
public async getFolder(output: IOutputConfig): Promise<string> {
if (output.templatesFolder !== "") {
return path.resolve(output.templatesFolder, output.template);
} else {
return await this._moduleFinder.find("docs_gm-" + output.template, await this._createConfig());
}
}

protected async _createConfig() {
const config = new ModuleFinderConfig();
config.packageRoot = await pkgDir() as string;
config.templatesPath = config.packageRoot + "/templates/";
return config;
}

}
6 changes: 4 additions & 2 deletions src/template/interfaces/IModuleFinder.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default interface IModuleFinder {
find(moduleName: string): Promise<string>
import ModuleFinderConfig from "../ModuleFinderConfig";

export default interface IModuleFinder {
find(moduleName: string, moduleFinderConfig: ModuleFinderConfig): Promise<string>
}
Loading

0 comments on commit 36791ef

Please sign in to comment.