@@ -6,6 +6,8 @@ const cwd = require('./cwd')
6
6
7
7
let currentProject = null
8
8
let creator = null
9
+ let presets = [ ]
10
+ let features = [ ]
9
11
10
12
function list ( context ) {
11
13
return context . db . get ( 'projects' ) . value ( )
@@ -26,44 +28,119 @@ function generatePresetDescription (preset) {
26
28
}
27
29
28
30
function generateProjectCreation ( creator ) {
29
- const presets = creator . getPresets ( )
30
31
return {
31
- presets : [
32
- ...Object . keys ( presets ) . map (
33
- key => {
34
- const preset = presets [ key ]
35
- const features = getFeatures ( preset ) . map (
36
- f => toShortPluginId ( f )
37
- )
38
- const info = {
39
- id : key ,
40
- name : key === 'default' ? 'Default preset' : key ,
41
- features,
42
- raw : preset
43
- }
44
- info . description = generatePresetDescription ( info )
45
- return info
32
+ presets,
33
+ features
34
+ }
35
+ }
36
+
37
+ function initCreator ( ) {
38
+ const creator = new Creator ( '' , cwd . get ( ) , getPromptModules ( ) )
39
+
40
+ // Presets
41
+ const presetsData = creator . getPresets ( )
42
+ presets = [
43
+ ...Object . keys ( presetsData ) . map (
44
+ key => {
45
+ const preset = presetsData [ key ]
46
+ const features = getFeatures ( preset ) . map (
47
+ f => toShortPluginId ( f )
48
+ )
49
+ const info = {
50
+ id : key ,
51
+ name : key === 'default' ? 'Default preset' : key ,
52
+ features,
53
+ raw : preset
46
54
}
47
- ) ,
48
- {
49
- id : 'manual' ,
50
- name : 'No preset' ,
51
- description : 'No included features' ,
52
- features : [ ]
55
+ info . description = generatePresetDescription ( info )
56
+ return info
53
57
}
54
- ]
55
- }
58
+ ) ,
59
+ {
60
+ id : 'manual' ,
61
+ name : 'No preset' ,
62
+ description : 'No included features' ,
63
+ features : [ ]
64
+ }
65
+ ]
66
+
67
+ // Features
68
+ const featuresData = creator . featurePrompt . choices
69
+ features = [
70
+ ...featuresData . map (
71
+ data => ( {
72
+ id : data . value ,
73
+ name : data . name ,
74
+ description : data . description || null ,
75
+ link : data . link || null ,
76
+ plugins : data . plugins || null ,
77
+ enabled : false
78
+ } )
79
+ ) ,
80
+ {
81
+ id : 'use-config-files' ,
82
+ name : 'Use config files' ,
83
+ description : `Use specific configuration files (like '.babelrc') instead of using 'package.json'.` ,
84
+ link : null ,
85
+ plugins : null ,
86
+ enabled : false
87
+ }
88
+ ]
89
+
90
+ return creator
56
91
}
57
92
58
93
function getCreation ( context ) {
59
94
if ( ! creator ) {
60
- creator = new Creator ( '' , cwd . get ( ) , getPromptModules ( ) )
95
+ creator = initCreator ( )
61
96
}
62
97
return generateProjectCreation ( creator )
63
98
}
64
99
100
+ function setFeatureEnabled ( { id, enabled } , context ) {
101
+ const feature = features . find ( f => f . id === id )
102
+ if ( feature ) {
103
+ feature . enabled = enabled
104
+ } else {
105
+ console . warn ( `Feature '${ id } ' not found` )
106
+ }
107
+ return feature
108
+ }
109
+
110
+ function applyPreset ( id , context ) {
111
+ const preset = presets . find ( p => p . id === id )
112
+ if ( preset ) {
113
+ for ( const feature of features ) {
114
+ feature . enabled = ! ! (
115
+ preset . features . includes ( feature . id ) ||
116
+ ( feature . plugins && preset . features . some ( f => feature . plugins . includes ( f ) ) )
117
+ )
118
+ }
119
+ if ( preset . raw ) {
120
+ if ( preset . raw . router ) {
121
+ setFeatureEnabled ( { id : 'router' , enabled : true } , context )
122
+ }
123
+ if ( preset . raw . vuex ) {
124
+ setFeatureEnabled ( { id : 'vuex' , enabled : true } , context )
125
+ }
126
+ if ( preset . raw . cssPreprocessor ) {
127
+ setFeatureEnabled ( { id : 'css-preprocessor' , enabled : true } , context )
128
+ }
129
+ if ( preset . raw . useConfigFiles ) {
130
+ setFeatureEnabled ( { id : 'use-config-files' , enabled : true } , context )
131
+ }
132
+ }
133
+ } else {
134
+ console . warn ( `Preset '${ id } ' not found` )
135
+ }
136
+
137
+ return generateProjectCreation ( creator )
138
+ }
139
+
65
140
module . exports = {
66
141
list,
67
142
getCurrent,
68
- getCreation
143
+ getCreation,
144
+ applyPreset,
145
+ setFeatureEnabled
69
146
}
0 commit comments