This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module finder now loads from local bundled templates
- Loading branch information
1 parent
d4d1942
commit 36791ef
Showing
8 changed files
with
273 additions
and
222 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,4 @@ | ||
export default class ModuleFinderConfig { | ||
public packageRoot: string; | ||
public templatesPath: string; | ||
} |
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,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; | ||
} | ||
|
||
} |
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,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> | ||
} |
Oops, something went wrong.