Skip to content

Allow custom template directory fix #30 #32

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

Merged
merged 1 commit into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ReactCrudGenerator from './generators/ReactCrudGenerator';
import TypescriptInterfaceGenerator from './generators/TypescriptInterfaceGenerator';

function wrap (cl) {
return (prefix) => new cl(prefix)
return ({hydraPrefix, templateDirectory}) => new cl({hydraPrefix, templateDirectory})
}

function generators (generator = 'react') {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/ReactCrudGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import chalk from 'chalk';
export default class ReactCrudGenerator {
templates = {};

constructor(hydraPrefix) {
const templatePath = `${__dirname}/../../templates/react/`;
constructor({hydraPrefix, templateDirectory}) {
const templatePath = `${templateDirectory}/react/`;

this.hydraPrefix = hydraPrefix;

Expand Down
2 changes: 1 addition & 1 deletion src/generators/ReactCrudGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ReactCrudGenerator from './ReactCrudGenerator';


test('Generate a React app', () => {
const generator = new ReactCrudGenerator('hydra:');
const generator = new ReactCrudGenerator({hydraPrefix: 'hydra:', templateDirectory: `${__dirname}/../../templates`});
const tmpobj = tmp.dirSync({unsafeCleanup: true});

const fields = [new Field('bar', {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/TypescriptInterfaceGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import fs from 'fs';
export default class TypescriptInterfaceGenerator {
templates = {};

constructor() {
const templatePath = `${__dirname}/../../templates/typescript`;
constructor({templateDirectory}) {
const templatePath = `${templateDirectory}/typescript/`;
this.template = handlebars.compile(fs.readFileSync(`${templatePath}/interface.ts`).toString())
}

Expand Down
2 changes: 1 addition & 1 deletion src/generators/TypescriptInterfaceGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import TypescriptInterfaceGenerator from './TypescriptInterfaceGenerator';


test('Generate a typescript interface', () => {
const generator = new TypescriptInterfaceGenerator('hydra:');
const generator = new TypescriptInterfaceGenerator({templateDirectory: `${__dirname}/../../templates`});
const tmpobj = tmp.dirSync({unsafeCleanup: true});

const resource = new Resource('abc', 'http://example.com/foos', {
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ program
.option('-r, --resource [resourceName]', 'Generate CRUD for the given resource')
.option('-p, --hydra-prefix [hydraPrefix]', 'The hydra prefix used by the API', 'hydra:')
.option('-g, --generator [generator]', 'The generator to use, one of "react", "angular" etc.', 'react')
.option('-t, --template-directory [templateDirectory]', 'The templates directory base to use. Final directory will be ${templateDirectory}/${generator}', `${__dirname}/../templates/`)
.parse(process.argv);

if (2 !== program.args.length && (!process.env.API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT || !process.env.API_PLATFORM_CLIENT_GENERATOR_OUTPUT)) {
Expand All @@ -22,7 +23,10 @@ if (2 !== program.args.length && (!process.env.API_PLATFORM_CLIENT_GENERATOR_ENT
const entrypoint = program.args[0] || process.env.API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT;
const outputDirectory = program.args[1] || process.env.API_PLATFORM_CLIENT_GENERATOR_OUTPUT;

const generator = generators(program.generator)(program.hydraPrefix);
const generator = generators(program.generator)({
hydraPrefix: program.hydraPrefix,
templateDirectory: program.templateDirectory
});
const resourceToGenerate = program.resource ? program.resource.toLowerCase() : null;

parseHydraDocumentation(entrypoint).then(api => {
Expand Down