-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathpage.js
65 lines (60 loc) · 1.59 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
import camelcase from 'camelcase'
import { Listr } from 'listr2'
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'
import c from '../../../lib/colors.js'
import {
deleteFilesTask,
removeRoutesFromRouterTask,
} from '../../../lib/index.js'
import { pathName } from '../../generate/helpers.js'
import {
files as pageFiles,
paramVariants as templateVars,
} from '../../generate/page/page.js'
export const command = 'page <name> [path]'
export const description = 'Destroy a page and route component'
export const builder = (yargs) => {
yargs.positional('name', {
description: 'Name of the page',
type: 'string',
})
yargs.positional('path', {
description: 'URL path to the page. Defaults to name',
type: 'string',
})
}
export const tasks = ({ name, path }) =>
new Listr(
[
{
title: 'Destroying page files...',
task: async () => {
const p = pathName(path, name)
const f = pageFiles({
name,
path: p,
stories: true,
tests: true,
...templateVars(p),
})
return deleteFilesTask(f)
},
},
{
title: 'Cleaning up routes file...',
task: async () => removeRoutesFromRouterTask([camelcase(name)]),
},
],
{ rendererOptions: { collapseSubtasks: false }, exitOnError: true },
)
export const handler = async ({ name, path }) => {
recordTelemetryAttributes({
command: 'destroy page',
})
const t = tasks({ name, path })
try {
await t.run()
} catch (e) {
console.log(c.error(e.message))
}
}