-
-
Notifications
You must be signed in to change notification settings - Fork 131
feat: add next.js generator #130
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,267 @@ | ||
import chalk from "chalk"; | ||
import fs from "fs"; | ||
import BaseGenerator from "./BaseGenerator"; | ||
import { parse, print, types } from "recast"; | ||
|
||
export default class NextGenerator extends BaseGenerator { | ||
constructor(params) { | ||
super(params); | ||
|
||
this.routeAddedtoServer = false; | ||
this.registerTemplates(`next/`, [ | ||
// components | ||
"components/common/ReferenceLinks.tsx", | ||
"components/foo/List.tsx", | ||
"components/foo/ListItem.tsx", | ||
"components/foo/Show.tsx", | ||
|
||
// interfaces | ||
"error/SubmissionError.ts", | ||
|
||
// interfaces | ||
"interfaces/Collection.ts", | ||
"interfaces/foo.ts", | ||
|
||
// pages | ||
"pages/foo.tsx", | ||
"pages/foos.tsx", | ||
|
||
// utils | ||
"utils/dataAccess.ts" | ||
]); | ||
} | ||
|
||
checkDependencies(dir, serverPath) { | ||
const dependencies = this.getTargetDependencies(dir); | ||
|
||
if (!dependencies.length) { | ||
return; | ||
} | ||
|
||
if (!dependencies.includes("@zeit/next-typescript")) { | ||
console.log( | ||
chalk.yellow( | ||
"It seems next-typescript is not installed but generator needs typescript to work efficiently." | ||
) | ||
); | ||
} | ||
|
||
if (!dependencies.includes("express")) { | ||
console.log( | ||
chalk.yellow( | ||
"It seems express is not installed but generator needs a custom express server to work efficiently." | ||
) | ||
); | ||
} | ||
|
||
if (serverPath) { | ||
if (!fs.existsSync(serverPath)) { | ||
console.log(chalk.red("Express server file doesn't exists.")); | ||
return; | ||
} | ||
|
||
const { mode } = fs.statSync(serverPath); | ||
if ("200" !== (mode & parseInt("200", 8)).toString(8)) { | ||
console.log(chalk.red("Express server file is not writable.")); | ||
} | ||
} | ||
} | ||
|
||
checkImports(directory, imports, extension = ".ts") { | ||
imports.forEach(({ file }) => { | ||
if (fs.existsSync(directory + file + extension)) { | ||
return; | ||
} | ||
|
||
console.log( | ||
chalk.yellow( | ||
'An import for the file "%s" has been generated but the file doesn\'t exists.' | ||
), | ||
file | ||
); | ||
}); | ||
} | ||
|
||
help(resource, dir) { | ||
console.log( | ||
chalk.green('Code for the "%s" resource type has been generated!'), | ||
resource.title | ||
); | ||
|
||
// missing import | ||
const { imports } = this.parseFields(resource); | ||
this.checkImports(`${dir}/interfaces/`, imports); | ||
|
||
// server route configuration | ||
if (!this.routeAddedtoServer) { | ||
const lc = resource.title.toLowerCase(); | ||
console.log( | ||
"Paste the following route to your server configuration file:" | ||
); | ||
console.log(chalk.green(this.getShowRoute(lc))); | ||
} | ||
} | ||
|
||
generate(api, resource, dir, serverPath) { | ||
const lc = resource.title.toLowerCase(); | ||
const ucf = this.ucFirst(resource.title); | ||
const { fields, imports } = this.parseFields(resource); | ||
|
||
const context = { | ||
name: resource.name, | ||
lc, | ||
uc: resource.title.toUpperCase(), | ||
ucf, | ||
fields, | ||
formFields: this.buildFields(resource.writableFields), | ||
imports, | ||
hydraPrefix: this.hydraPrefix, | ||
title: resource.title | ||
}; | ||
|
||
// Create directories | ||
// These directories may already exist | ||
[ | ||
`${dir}/components/common`, | ||
`${dir}/config`, | ||
`${dir}/error`, | ||
`${dir}/interfaces`, | ||
`${dir}/pages`, | ||
`${dir}/utils` | ||
].forEach(dir => this.createDir(dir, false)); | ||
|
||
// copy with patterned name | ||
this.createDir(`${dir}/components/${context.lc}`); | ||
[ | ||
// components | ||
"components/%s/List.tsx", | ||
"components/%s/ListItem.tsx", | ||
"components/%s/Show.tsx", | ||
|
||
// pages | ||
"pages/%s.tsx", | ||
"pages/%ss.tsx" | ||
].forEach(pattern => | ||
this.createFileFromPattern(pattern, dir, context.lc, context) | ||
); | ||
|
||
// interface pattern should be camel cased | ||
this.createFile( | ||
"interfaces/foo.ts", | ||
`${dir}/interfaces/${context.ucf}.ts`, | ||
context | ||
); | ||
|
||
// copy with regular name | ||
[ | ||
// components | ||
"components/common/ReferenceLinks.tsx", | ||
|
||
// error | ||
"error/SubmissionError.ts", | ||
|
||
// interfaces | ||
"interfaces/Collection.ts", | ||
|
||
// utils | ||
"utils/dataAccess.ts" | ||
].forEach(file => this.createFile(file, `${dir}/${file}`, context, false)); | ||
|
||
// API config | ||
this.createEntrypoint(api.entrypoint, `${dir}/config/entrypoint.ts`); | ||
|
||
if (serverPath) { | ||
this.createExpressRoute(serverPath, lc, this.getShowRoute(lc)); | ||
} | ||
} | ||
|
||
getShowRoute(name) { | ||
return `server.get('/${name}/:id', (req, res) => { | ||
return app.render(req, res, '/${name}', { id: req.params.id }) | ||
});`; | ||
} | ||
|
||
createExpressRoute(path, resourceName, toInsert) { | ||
const content = fs.readFileSync(path, "utf-8"); | ||
const code = parse(content); | ||
const { namedTypes } = types; | ||
|
||
types.visit(code, { | ||
visitExpressionStatement: function(path) { | ||
const args = path.value.expression.arguments; | ||
if ( | ||
2 === args.length && | ||
namedTypes.Literal.check(args[0]) && | ||
"*" === args[0].value && | ||
namedTypes.ArrowFunctionExpression.check(args[1]) | ||
) { | ||
// insert route before "*" route | ||
path.parent.value.body.splice(path.name, 0, toInsert); | ||
|
||
return false; | ||
} | ||
|
||
this.traverse(path); | ||
} | ||
}); | ||
|
||
fs.writeFileSync(path, print(code).code); | ||
console.log( | ||
chalk.green("'Show' route for %s has been added to your server"), | ||
resourceName | ||
); | ||
this.routeAddedtoServer = true; | ||
} | ||
|
||
getDescription(field) { | ||
return field.description ? field.description.replace(/"/g, "'") : ""; | ||
} | ||
|
||
parseFields(resource) { | ||
const fields = [ | ||
...resource.writableFields, | ||
...resource.readableFields | ||
].reduce((list, field) => { | ||
if (list[field.name]) { | ||
return list; | ||
} | ||
|
||
return { | ||
...list, | ||
[field.name]: { | ||
notrequired: !field.required, | ||
name: field.name, | ||
type: this.getType(field), | ||
description: this.getDescription(field), | ||
readonly: false, | ||
reference: field.reference | ||
} | ||
}; | ||
}, {}); | ||
|
||
// Parse fields to add relevant imports, required for Typescript | ||
const fieldsArray = Object.values(fields); | ||
const imports = Object.values(fields).reduce( | ||
(list, { reference, type }) => { | ||
if (!reference) { | ||
return list; | ||
} | ||
|
||
return { | ||
...list, | ||
[type]: { | ||
type, | ||
file: `./${type}` | ||
} | ||
}; | ||
}, | ||
{} | ||
); | ||
|
||
return { fields: fieldsArray, imports: Object.values(imports) }; | ||
} | ||
|
||
ucFirst(target) { | ||
return target.charAt(0).toUpperCase() + target.slice(1); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.