Skip to content

Add Quasar Framework generator. #134

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 9 commits into from
Jun 9, 2019
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
3 changes: 3 additions & 0 deletions src/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactGenerator from "./generators/ReactGenerator";
import ReactNativeGenerator from "./generators/ReactNativeGenerator";
import TypescriptInterfaceGenerator from "./generators/TypescriptInterfaceGenerator";
import VueGenerator from "./generators/VueGenerator";
import QuasarGenerator from "./generators/QuasarGenerator";

function wrap(cl) {
return ({ hydraPrefix, templateDirectory }) =>
Expand All @@ -21,5 +22,7 @@ export default function generators(generator = "react") {
return wrap(TypescriptInterfaceGenerator);
case "vue":
return wrap(VueGenerator);
case "quasar":
return wrap(QuasarGenerator);
}
}
224 changes: 224 additions & 0 deletions src/generators/QuasarGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import chalk from "chalk";
import BaseGenerator from "./BaseGenerator";
import handlebars from "handlebars";
import hbh_comparison from "handlebars-helpers/lib/comparison";

export default class extends BaseGenerator {
constructor(params) {
super(params);

this.registerTemplates(`quasar/`, [
// modules
"store/modules/foo/index.js",
"store/modules/foo/create/actions.js",
"store/modules/foo/create/getters.js",
"store/modules/foo/create/index.js",
"store/modules/foo/create/mutation_types.js",
"store/modules/foo/create/mutations.js",
"store/modules/foo/delete/actions.js",
"store/modules/foo/delete/getters.js",
"store/modules/foo/delete/index.js",
"store/modules/foo/delete/mutation_types.js",
"store/modules/foo/delete/mutations.js",
"store/modules/foo/list/actions.js",
"store/modules/foo/list/getters.js",
"store/modules/foo/list/index.js",
"store/modules/foo/list/mutation_types.js",
"store/modules/foo/list/mutations.js",
"store/modules/foo/show/actions.js",
"store/modules/foo/show/getters.js",
"store/modules/foo/show/index.js",
"store/modules/foo/show/mutation_types.js",
"store/modules/foo/show/mutations.js",
"store/modules/foo/update/actions.js",
"store/modules/foo/update/getters.js",
"store/modules/foo/update/index.js",
"store/modules/foo/update/mutation_types.js",
"store/modules/foo/update/mutations.js",

// components
"components/foo/Create.vue",
"components/foo/Form.vue",
"components/foo/List.vue",
"components/foo/Update.vue",
"components/foo/Show.vue",

// routes
"router/foo.js",

// error
"error/SubmissionError.js",

// utils
"utils/fetch.js"
]);

handlebars.registerHelper("compare", hbh_comparison.compare);
}

help(resource) {
const titleLc = resource.title.toLowerCase();

console.log(
'Code for the "%s" resource type has been generated!',
resource.title
);
console.log(
"Paste the following definitions in your application configuration:"
);
console.log(
chalk.green(`
//Add to quasar.conf
// 1. in 'framework.components'
'QTable',
'QTh',
'QTr',
'QTd',
'QAjaxBar',
'QBreadcrumbs',
'QBreadcrumbsEl',
'QSpace',
'QInput',
'QForm',
'QSelect',
'QMarkupTable',
'QDate',
'QTime',
'QCheckbox',
'QPopupProxy',

// 2. in 'framework.config', customize later
notify: {
position: 'top',
multiLine: true,
timeout: 0,
},

// 3. in 'framework.directives
'ClosePopup'

// 4. in 'framework.plugins
'Notify'

//import routes
import ${titleLc}Routes from './router/${titleLc}';

// Add routes to VueRouter
const router = new VueRouter({
// ...
routes: [
...${titleLc}Routes,
]
});

// Add the modules in the store
import ${titleLc} from './store/modules/${titleLc}/';

export const store = new Vuex.Store({
// ...
modules: {
${titleLc}
}
});
`)
);
}

generate(api, resource, dir) {
const lc = resource.title.toLowerCase();
const titleUcFirst =
resource.title.charAt(0).toUpperCase() + resource.title.slice(1);

const context = {
title: resource.title,
name: resource.name,
lc,
uc: resource.title.toUpperCase(),
fields: resource.readableFields,
formFields: this.buildFields(resource.writableFields),
hydraPrefix: this.hydraPrefix,
titleUcFirst
};

// Create directories
// These directories may already exist
for (let dir of [
`${dir}/config`,
`${dir}/error`,
`${dir}/router`,
`${dir}/utils`
]) {
this.createDir(dir, false);
}

for (let dir of [
`${dir}/store/modules/${lc}`,
`${dir}/store/modules/${lc}/create`,
`${dir}/store/modules/${lc}/delete`,
`${dir}/store/modules/${lc}/list`,
`${dir}/store/modules/${lc}/show`,
`${dir}/store/modules/${lc}/update`,
`${dir}/components/${lc}`
]) {
this.createDir(dir);
}

for (let pattern of [
// modules
"store/modules/%s/index.js",
"store/modules/%s/create/actions.js",
"store/modules/%s/create/getters.js",
"store/modules/%s/create/index.js",
"store/modules/%s/create/mutation_types.js",
"store/modules/%s/create/mutations.js",
"store/modules/%s/delete/actions.js",
"store/modules/%s/delete/getters.js",
"store/modules/%s/delete/index.js",
"store/modules/%s/delete/mutation_types.js",
"store/modules/%s/delete/mutations.js",
"store/modules/%s/list/actions.js",
"store/modules/%s/list/getters.js",
"store/modules/%s/list/index.js",
"store/modules/%s/list/mutation_types.js",
"store/modules/%s/list/mutations.js",
"store/modules/%s/show/actions.js",
"store/modules/%s/show/getters.js",
"store/modules/%s/show/index.js",
"store/modules/%s/show/mutation_types.js",
"store/modules/%s/show/mutations.js",
"store/modules/%s/update/actions.js",
"store/modules/%s/update/getters.js",
"store/modules/%s/update/index.js",
"store/modules/%s/update/mutation_types.js",
"store/modules/%s/update/mutations.js",

// components
"components/%s/Create.vue",
"components/%s/Form.vue",
"components/%s/List.vue",
"components/%s/Update.vue",
"components/%s/Show.vue",

// routes
"router/%s.js"
]) {
this.createFileFromPattern(pattern, dir, lc, context);
}

// error
this.createFile(
"error/SubmissionError.js",
`${dir}/error/SubmissionError.js`,
context,
false
);

this.createEntrypoint(api.entrypoint, `${dir}/config/entrypoint.js`);
this.createFile(
"utils/fetch.js",
`${dir}/utils/fetch.js`,
{ hydraPrefix: this.hydraPrefix },
false
);
}
}
74 changes: 74 additions & 0 deletions src/generators/QuasarGenerator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Api from "@api-platform/api-doc-parser/lib/Api";
import Resource from "@api-platform/api-doc-parser/lib/Resource";
import Field from "@api-platform/api-doc-parser/lib/Field";
import fs from "fs";
import tmp from "tmp";
import QuasarGenerator from "./QuasarGenerator";

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

const fields = [
new Field("bar", {
id: "http://schema.org/url",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: true,
description: "An URL"
})
];
const resource = new Resource("abc", "http://example.com/foos", {
id: "foo",
title: "Foo",
readableFields: fields,
writableFields: fields
});
const api = new Api("http://example.com", {
entrypoint: "http://example.com:8080",
title: "My API",
resources: [resource]
});
generator.generate(api, resource, tmpobj.name);

expect(fs.existsSync(tmpobj.name + "/components/foo/Create.vue")).toBe(true);
expect(fs.existsSync(tmpobj.name + "/components/foo/Form.vue")).toBe(true);
expect(fs.existsSync(tmpobj.name + "/components/foo/List.vue")).toBe(true);
expect(fs.existsSync(tmpobj.name + "/components/foo/Show.vue")).toBe(true);
expect(fs.existsSync(tmpobj.name + "/components/foo/Update.vue")).toBe(true);

expect(fs.existsSync(tmpobj.name + "/config/entrypoint.js")).toBe(true);

expect(fs.existsSync(tmpobj.name + "/error/SubmissionError.js")).toBe(true);

expect(fs.existsSync(tmpobj.name + "/router/foo.js")).toBe(true);

expect(fs.existsSync(tmpobj.name + "/store/modules/foo/index.js")).toBe(true);

["create", "delete", "list", "show", "update"].forEach(action => {
expect(
fs.existsSync(`${tmpobj.name}/store/modules/foo/${action}/actions.js`)
).toBe(true);
expect(
fs.existsSync(`${tmpobj.name}/store/modules/foo/${action}/getters.js`)
).toBe(true);
expect(
fs.existsSync(`${tmpobj.name}/store/modules/foo/${action}/index.js`)
).toBe(true);
expect(
fs.existsSync(
`${tmpobj.name}/store/modules/foo/${action}/mutation_types.js`
)
).toBe(true);
expect(
fs.existsSync(`${tmpobj.name}/store/modules/foo/${action}/mutations.js`)
).toBe(true);
});

expect(fs.existsSync(tmpobj.name + "/utils/fetch.js")).toBe(true);

tmpobj.removeCallback();
});
Loading