-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
145 lines (141 loc) · 4.24 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { Plugin } from '@merryjs/cli/lib/plugin'
import path from 'path'
import { FlutterAction, FlutterOptions } from './action'
import model from './model'
import mobx from './mobx'
import i18n from './i18n'
/**
* FlutterAnswers
*/
export default (api: Plugin) => {
api
.command('flutter [action]')
.option('-n, --name [value]', 'name of your widget/model/page')
.option('-s, --stateful', 'stateful widget')
.option('-c, --clean_stores', 'clean stores')
.option('-a, --auto', 'auto translation')
.option('-o, --src [value]', 'source path of quick type')
.option('-d, --dist [value]', 'dist folder for widget')
.option('-t, --tpl [value]', 'template for generate mobx store')
.option(
'-k, --skip [value]',
"skip some key if you don't need it (mobx only) "
)
.action(
async (
action: FlutterAction,
options: FlutterOptions = {
name: '',
}
) => {
const availableActions = Object.keys(FlutterAction).filter(
f => !(parseInt(f, 10) >= 0)
)
if (action === undefined || !availableActions.includes(action)) {
api.log(
`action required, supported actions: ${availableActions
.map(f => f)
.join(' ')}`
)
return
}
if (
FlutterAction.fastlane !== action &&
FlutterAction.mobx !== action &&
FlutterAction.i18n !== action &&
FlutterAction.init !== action &&
(!options.name || typeof options.name === 'function')
) {
api.log('name option required')
return
}
switch (action) {
case FlutterAction.init:
await api.fs.writeFile(
'.merryrc',
JSON.stringify(
{
dist: 'lib',
},
null,
2
)
)
const settings = '.vscode/settings.json'
if (!api.fs.existsSync(settings)) {
api.fs.ensureFileSync(settings)
await api.fs.writeFile(
settings,
JSON.stringify(
{
'editor.formatOnSave': true,
},
null,
2
)
)
}
break
case FlutterAction.i18n:
i18n(api, options)
break
case FlutterAction.fastlane:
await api.fs.copy(
path.join(__dirname, './fastlane'),
path.join(api.conf.dist, '../fastlane'),
{
overwrite: true,
}
)
await api.fs.copy(
path.join(__dirname, './Gemfile'),
path.join(api.conf.dist, '../Gemfile'),
{
overwrite: true,
}
)
api.log(
'fastlane init succeeded. You may need edit:\n- fastlane/.env\n- fastlane/.env.dev\n- fastlane/.env.dev.secret\nand fastlane/.env.prod files for production'
)
break
case FlutterAction.model:
options.dist = options.dist || 'models'
await model(api, options)
break
case FlutterAction.mobx:
options.dist = options.dist || 'stores'
await mobx(api, options)
break
case FlutterAction.page:
const page = options.stateful ? './page-stateful.hbs' : './page.hbs'
await api.tmpl(
page,
path.join(
api.conf.dist,
options.dist || 'pages',
`{{snakecase name}}.dart`
),
options
)
break
case FlutterAction.widget:
const tpl = options.stateful
? './widget-stateful.hbs'
: './widget.hbs'
await api.tmpl(
tpl,
path.join(
api.conf.dist,
options.dist || 'widgets',
`{{snakecase name}}.dart`
),
options
)
break
default:
api.log('action not found.')
break
}
}
)
}