-
Notifications
You must be signed in to change notification settings - Fork 964
/
page.js
90 lines (81 loc) · 2.2 KB
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Listr from 'listr'
import camelcase from 'camelcase'
import pascalcase from 'pascalcase'
import { writeFilesTask, addRoutesToRouterTask } from 'src/lib'
import c from 'src/lib/colors'
import { templateForComponentFile, pathName } from '../helpers'
const COMPONENT_SUFFIX = 'Page'
const REDWOOD_WEB_PATH_NAME = 'pages'
export const files = ({ name, ...rest }) => {
const pageFile = templateForComponentFile({
name,
suffix: COMPONENT_SUFFIX,
webPathSection: REDWOOD_WEB_PATH_NAME,
generator: 'page',
templatePath: 'page.js.template',
templateVars: rest,
})
const testFile = templateForComponentFile({
name,
suffix: COMPONENT_SUFFIX,
extension: '.test.js',
webPathSection: REDWOOD_WEB_PATH_NAME,
generator: 'page',
templatePath: 'test.js.template',
templateVars: rest,
})
// Returns
// {
// "path/to/fileA": "<<<template>>>",
// "path/to/fileB": "<<<template>>>",
// }
return [pageFile, testFile].reduce((acc, [outputPath, content]) => {
return {
[outputPath]: content,
...acc,
}
}, {})
}
export const routes = ({ name, path }) => {
return [
`<Route path="${path}" page={${pascalcase(name)}Page} name="${camelcase(
name
)}" />`,
]
}
export const command = 'page <name> [router-path]'
export const desc = 'Generates a page component.'
export const builder = {
'router-path': {
type: 'string',
required: false,
description:
'The path used to reference this page in the router: `/about-us`, `/home`, etc...',
},
force: { type: 'boolean', default: false },
}
export const handler = async ({ name, path, force }) => {
const tasks = new Listr(
[
{
title: 'Generating page files...',
task: async () => {
const f = await files({ name, path: pathName(path, name) })
return writeFilesTask(f, { overwriteExisting: force })
},
},
{
title: 'Updating routes file...',
task: async () => {
addRoutesToRouterTask(routes({ name, path: pathName(path, name) }))
},
},
].filter(Boolean),
{ collapse: false }
)
try {
await tasks.run()
} catch (e) {
console.log(c.error(e.message))
}
}