diff --git a/package.json b/package.json index 9145f23a..0a7db16d 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "eslint-check": "eslint-config-prettier src/index.js", "build": "babel src -d lib --ignore '*.test.js'", "watch": "babel --watch src -d lib --ignore '*.test.js'", - "test-gen": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com ./tmp/react -g react && ./lib/index.js https://demo.api-platform.com ./tmp/react-native -g react-native && ./lib/index.js https://demo.api-platform.com ./tmp/vue -g vue", + "test-gen": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com ./tmp/react -g react && ./lib/index.js https://demo.api-platform.com ./tmp/react-native -g react-native && ./lib/index.js https://demo.api-platform.com ./tmp/next -g next && ./lib/index.js https://demo.api-platform.com ./tmp/vue -g vue", "test-gen-custom": "rm -rf ./tmp && yarn build && babel src/generators/ReactGenerator.js src/generators/BaseGenerator.js -d ./tmp/gens && cp -r ./templates/react ./templates/react-common ./templates/entrypoint.js ./tmp/gens && ./lib/index.js https://demo.api-platform.com ./tmp/react-custom -g \"$(pwd)/tmp/gens/ReactGenerator.js\" -t ./tmp/gens", "test-gen-swagger": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react-native -g react-native -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/vue -g vue -f swagger", "test-gen-env": "rm -rf ./tmp && yarn build && API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT=https://demo.api-platform.com API_PLATFORM_CLIENT_GENERATOR_OUTPUT=./tmp ./lib/index.js" diff --git a/src/generators/BaseGenerator.js b/src/generators/BaseGenerator.js index 7409ca0f..c621fc26 100644 --- a/src/generators/BaseGenerator.js +++ b/src/generators/BaseGenerator.js @@ -37,10 +37,10 @@ export default class { } } - createFileFromPattern(pattern, dir, lc, context) { + createFileFromPattern(pattern, dir, value, context, templateValue = "foo") { this.createFile( - sprintf(pattern, "foo"), - sprintf(`${dir}/${pattern}`, lc), + sprintf(pattern, templateValue), + sprintf(`${dir}/${pattern}`, value), context ); } diff --git a/src/generators/NextGenerator.js b/src/generators/NextGenerator.js index 5909c447..d0893521 100644 --- a/src/generators/NextGenerator.js +++ b/src/generators/NextGenerator.js @@ -1,4 +1,6 @@ import chalk from "chalk"; +import handlebars from "handlebars"; +import hbh_comparison from "handlebars-helpers/lib/comparison"; import BaseGenerator from "./BaseGenerator"; export default class NextGenerator extends BaseGenerator { @@ -15,8 +17,9 @@ export default class NextGenerator extends BaseGenerator { "components/foo/Form.tsx", // types - "types/Collection.ts", + "types/collection.ts", "types/foo.ts", + "types/item.ts", // pages "pages/foos/[id]/index.tsx", @@ -28,6 +31,8 @@ export default class NextGenerator extends BaseGenerator { "utils/dataAccess.ts", "utils/mercure.ts", ]); + + handlebars.registerHelper("compare", hbh_comparison.compare); } help(resource) { @@ -66,22 +71,25 @@ export default class NextGenerator extends BaseGenerator { // Copy with patterned name this.createDir(`${dir}/components/${context.lc}`); - this.createDir(`${dir}/pages/${context.lc}s`); - this.createDir(`${dir}/pages/${context.lc}s/[id]`); + this.createDir(`${dir}/pages/${context.name}`); + this.createDir(`${dir}/pages/${context.name}/[id]`); [ // components "components/%s/List.tsx", "components/%s/Show.tsx", "components/%s/Form.tsx", - - // pages - "pages/%ss/[id]/index.tsx", - "pages/%ss/[id]/edit.tsx", - "pages/%ss/index.tsx", - "pages/%ss/create.tsx", ].forEach((pattern) => this.createFileFromPattern(pattern, dir, context.lc, context) ); + [ + // pages + "pages/%s/[id]/index.tsx", + "pages/%s/[id]/edit.tsx", + "pages/%s/index.tsx", + "pages/%s/create.tsx", + ].forEach((pattern) => + this.createFileFromPattern(pattern, dir, context.name, context, "foos") + ); // interface pattern should be camel cased this.createFile("types/foo.ts", `${dir}/types/${context.ucf}.ts`, context); @@ -93,7 +101,8 @@ export default class NextGenerator extends BaseGenerator { "components/common/ReferenceLinks.tsx", // types - "types/Collection.ts", + "types/collection.ts", + "types/item.ts", // utils "utils/dataAccess.ts", diff --git a/src/generators/NextGenerator.test.js b/src/generators/NextGenerator.test.js index 292582c6..d5cb08f3 100644 --- a/src/generators/NextGenerator.test.js +++ b/src/generators/NextGenerator.test.js @@ -25,7 +25,7 @@ describe("generate", () => { description: "An URL", }), ]; - const resource = new Resource("abc", "http://example.com/foos", { + const resource = new Resource("abcs", "http://example.com/foos", { id: "abc", title: "abc", readableFields: fields, @@ -46,7 +46,8 @@ describe("generate", () => { "/components/common/ReferenceLinks.tsx", "/components/common/Pagination.tsx", "/types/Abc.ts", - "/types/Collection.ts", + "/types/collection.ts", + "/types/item.ts", "/pages/abcs/[id]/index.tsx", "/pages/abcs/[id]/edit.tsx", "/pages/abcs/index.tsx", diff --git a/templates/next/components/common/Pagination.tsx b/templates/next/components/common/Pagination.tsx index 17dac045..dd316809 100644 --- a/templates/next/components/common/Pagination.tsx +++ b/templates/next/components/common/Pagination.tsx @@ -1,5 +1,5 @@ import Link from "next/link"; -import { PagedCollection } from "../../types/Collection"; +import { PagedCollection } from "../../types/collection"; interface Props { collection: PagedCollection; diff --git a/templates/next/components/foo/Form.tsx b/templates/next/components/foo/Form.tsx index 83472f45..6625ac11 100644 --- a/templates/next/components/foo/Form.tsx +++ b/templates/next/components/foo/Form.tsx @@ -74,13 +74,18 @@ export const Form: FunctionComponent = ({ {{{lc}}} }) => { diff --git a/templates/next/components/foo/Show.tsx b/templates/next/components/foo/Show.tsx index ef89523b..6aa0d5db 100644 --- a/templates/next/components/foo/Show.tsx +++ b/templates/next/components/foo/Show.tsx @@ -1,16 +1,17 @@ -import { FunctionComponent, useState } from 'react'; -import Link from 'next/link'; +import { FunctionComponent, useState } from "react"; +import Link from "next/link"; import { useRouter } from "next/router"; +import Head from "next/head"; import { fetch } from "../../utils/dataAccess"; -import ReferenceLinks from '../common/ReferenceLinks'; -import { {{{ucf}}} } from '../../types/{{{ucf}}}'; -import Head from 'next/head' +import ReferenceLinks from "../common/ReferenceLinks"; +import { {{{ucf}}} } from "../../types/{{{ucf}}}"; interface Props { {{{lc}}}: {{{ucf}}}; + text: string; } -export const Show: FunctionComponent = ({ {{{lc}}} }) => { +export const Show: FunctionComponent = ({ {{{lc}}}, text }) => { const [error, setError] = useState(null); const router = useRouter(); @@ -30,11 +31,7 @@ export const Show: FunctionComponent = ({ {{{lc}}} }) => {
{`Show {{{ucf}}} ${ {{~lc}}['@id']}`} - +