This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
component.ts
69 lines (57 loc) · 2.29 KB
/
component.ts
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
import { GluegunToolbox } from "gluegun"
export const description = "Generates a component and a storybook test."
export const run = async function(toolbox: GluegunToolbox) {
// grab some features
const { parameters, strings, print, ignite, patching, filesystem, prompt } = toolbox
const { camelCase, isBlank, kebabCase, pascalCase } = strings
// validation
if (isBlank(parameters.first)) {
print.info("A name is required.")
print.info(`ignite generate component <name>\n`)
return
}
let observer = parameters.options.observer
if (parameters.options.observer === undefined) {
observer = await prompt.confirm(
`Should this component be _observed_ by Mobx?\n${print.colors.gray(`
If you'll be passing any mobx-state-tree objects in this component's props
and dereferencing them within this component, you'll want the component wrapped
in \`observer\` so that the component rerenders when properties of the object change.
If all props will be simple values or objects that don't come from a mobx store,
you don't need the component to be wrapped in \`observer\`.
`)}`,
)
}
const name = parameters.first
const pascalName = pascalCase(name)
const camelCaseName = camelCase(name)
const kebabCaseName = kebabCase(name)
const props = { camelCaseName, kebabCaseName, name, observer, pascalName }
const jobs = [
{
template: "component.story.tsx.ejs",
target: `app/components/${kebabCaseName}/${kebabCaseName}.story.tsx`,
},
{
template: "component.tsx.ejs",
target: `app/components/${kebabCaseName}/${kebabCaseName}.tsx`,
},
]
await ignite.copyBatch(toolbox, jobs, props)
// patch the barrel export file
const barrelExportPath = `${process.cwd()}/app/components/index.ts`
const exportToAdd = `export * from "./${kebabCaseName}/${kebabCaseName}"\n`
if (!filesystem.exists(barrelExportPath)) {
const msg =
`No '${barrelExportPath}' file found. Can't export component.` +
`Export your new component manually.`
print.warning(msg)
process.exit(1)
}
await patching.append(barrelExportPath, exportToAdd)
// wire up example
await patching.prepend(
"./storybook/storybook-registry.ts",
`require("../app/components/${kebabCaseName}/${kebabCaseName}.story")\n`,
)
}