Skip to content

Commit f7fbc96

Browse files
author
John Dillick
committed
Fix arcli reactium empty.
1 parent cc990b6 commit f7fbc96

File tree

3 files changed

+96
-80
lines changed

3 files changed

+96
-80
lines changed

commands/reactium/empty/actions.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = spinner => {
1212
};
1313

1414
return {
15-
style: ({ action, params, props }) => {
15+
style: async ({ action, params, props }) => {
1616
const { style } = params;
1717

1818
if (style) {
@@ -43,7 +43,7 @@ module.exports = spinner => {
4343

4444
return Promise.resolve({ action, status: 200 });
4545
},
46-
manifest: ({ action, params, props }) => {
46+
manifest: async ({ action, params, props }) => {
4747
const { toolkit } = params;
4848

4949
if (toolkit) {
@@ -55,18 +55,20 @@ module.exports = spinner => {
5555
`${cwd}/src/app/toolkit/index.js`,
5656
);
5757

58-
let cont = fs.readFileSync(manifestFile);
59-
cont = String(cont).replace(
60-
/menu: {((.|\n|\r)*)},/,
61-
'menu: {},',
62-
);
58+
if (fs.existsSync(manifestFile)) {
59+
let cont = fs.readFileSync(manifestFile);
60+
cont = String(cont).replace(
61+
/menu: {((.|\n|\r)*)},/,
62+
'menu: {},',
63+
);
6364

64-
fs.writeFileSync(manifestFile, cont);
65+
fs.writeFileSync(manifestFile, cont);
66+
}
6567
}
6668

6769
return Promise.resolve({ action, status: 200 });
6870
},
69-
empty: ({ action, params, props }) => {
71+
empty: async ({ action, params, props }) => {
7072
const { cwd } = props;
7173
const { demo, font, images, toolkit } = params;
7274

@@ -106,18 +108,6 @@ module.exports = spinner => {
106108
].forEach(p => fs.removeSync(p));
107109
}
108110

109-
if (toolkit) {
110-
message(`Removing ${chalk.cyan('toolkit elements')}...`);
111-
112-
const toolkitPath = path.normalize(`${cwd}/src/app/toolkit`);
113-
const toolkitExclude = ['index.js', 'overview'];
114-
115-
fs.readdirSync(toolkitPath)
116-
.filter(file => Boolean(!toolkitExclude.includes(file)))
117-
.forEach(file =>
118-
fs.removeSync(path.normalize(`${toolkitPath}/${file}`)),
119-
);
120-
}
121111

122112
return Promise.resolve({ action, status: 200 });
123113
},
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
21
const ora = require('ora');
32
const ActionSequence = require('action-sequence');
43

5-
6-
const spinner = ora({
7-
spinner : 'dots',
8-
color : 'cyan'
9-
});
10-
11-
const actions = require('./actions')(spinner);
12-
134
module.exports = ({ params, props }) => {
5+
console.log('');
6+
const spinner = ora({
7+
spinner: 'dots',
8+
color: 'cyan',
9+
});
10+
1411
spinner.start();
1512

13+
const actions = require('./actions')(spinner);
14+
1615
return ActionSequence({
1716
actions,
18-
options: { params, props }
19-
}).then((success) => {
20-
spinner.succeed('complete!');
21-
return success;
22-
}).catch((error) => {
23-
spinner.fail('error!');
24-
return error;
25-
});
17+
options: { params, props },
18+
})
19+
.then(success => {
20+
spinner.succeed('complete!');
21+
console.log('');
22+
return success;
23+
})
24+
.catch(error => {
25+
spinner.fail('error!');
26+
console.error(error);
27+
return error;
28+
});
2629
};

commands/reactium/empty/index.js

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
*/
66

77
const chalk = require('chalk');
8-
const generator = require('./generator');
8+
const fs = require('fs-extra');
99
const prettier = require('prettier');
1010
const path = require('path');
11+
const op = require('object-path');
1112
const mod = path.dirname(require.main.filename);
1213
const { error, message } = require(`${mod}/lib/messenger`);
14+
const GENERATOR = fs.existsSync(
15+
path.normalize(path.join(__dirname, 'generator.js')),
16+
)
17+
? require('./generator')
18+
: require(`${mod}/lib/generator`);
1319

1420
/**
1521
* NAME String
@@ -43,6 +49,8 @@ const CANCELED = 'Reactium empty canceled!';
4349
*/
4450
const CONFIRM = ({ props, params }) => {
4551
const { prompt } = props;
52+
if (op.get(prompt, 'override.confirm', false) === true)
53+
return Promise.resolve(true);
4654

4755
return new Promise((resolve, reject) => {
4856
prompt.get(
@@ -141,6 +149,30 @@ const SCHEMA = ({ props }) => {
141149
};
142150
};
143151

152+
/**
153+
* FLAGS
154+
* @description Array of flags passed from the commander options.
155+
* @since 2.0.18
156+
*/
157+
const FLAGS = ['confirm', 'demo', 'font', 'images', 'style', 'toolkit'];
158+
159+
/**
160+
* FLAGS_TO_PARAMS Function
161+
* @description Create an object used by the prompt.override property.
162+
* @since 2.0.18
163+
*/
164+
const FLAGS_TO_PARAMS = ({ opt = {} }) =>
165+
FLAGS.reduce((obj, key) => {
166+
let val = opt[key];
167+
val = typeof val === 'function' ? undefined : val;
168+
169+
if (val) {
170+
obj[key] = val;
171+
}
172+
173+
return obj;
174+
}, {});
175+
144176
/**
145177
* ACTION Function
146178
* @description Function used as the commander.action() callback.
@@ -151,47 +183,37 @@ const SCHEMA = ({ props }) => {
151183
*/
152184
const ACTION = ({ opt, props }) => {
153185
const { cwd, prompt } = props;
154-
155186
const schema = SCHEMA({ props });
156-
157-
const ovr = ['demo', 'font', 'images', 'style', 'toolkit'].reduce(
158-
(obj, key) => {
159-
let val = opt[key];
160-
val = typeof val === 'function' ? null : val;
161-
if (val) {
162-
obj[key] = val;
163-
}
164-
return obj;
165-
},
166-
{},
167-
);
187+
const ovr = FLAGS_TO_PARAMS({ opt });
168188

169189
prompt.override = ovr;
170190
prompt.start();
171-
prompt.get(schema, (err, input) => {
172-
// Keep this conditional as the first line in this function.
173-
// Why? because you will get a js error if you try to set or use anything related to the input object.
174-
if (err) {
175-
prompt.stop();
176-
error(`${NAME} ${err.message}`);
177-
return;
178-
}
179191

180-
const params = { ...CONFORM({ input, props }), ...ovr };
181-
182-
CONFIRM({ props, params })
183-
.then(() => {
184-
console.log('');
185-
generator({ params, props }).then(success => {
186-
console.log('');
187-
});
188-
})
189-
.then(() => prompt.stop())
190-
.catch(err => {
192+
let params = {};
193+
194+
return new Promise((resolve, reject) => {
195+
prompt.get(schema, (err, input = {}) => {
196+
if (err) {
191197
prompt.stop();
192-
message(CANCELED);
193-
});
194-
});
198+
reject(`${NAME} ${err.message}`);
199+
return;
200+
}
201+
202+
input = { ...ovr, ...input };
203+
params = CONFORM({ input, props });
204+
resolve();
205+
});
206+
})
207+
.then(() => CONFIRM({ props, params }))
208+
.then(() => GENERATOR({ params, props }))
209+
.then(() => prompt.stop())
210+
.then(results => {
211+
console.log('');
212+
})
213+
.catch(err => {
214+
prompt.stop();
215+
message(op.get(err, 'message', CANCELED));
216+
});
195217
};
196218

197219
/**
@@ -202,24 +224,25 @@ const COMMAND = ({ program, props }) =>
202224
program
203225
.command(NAME)
204226
.description(DESC)
205-
.action(opt => ACTION({ opt, props }))
206-
.option('-D, --no-demo [demo]', 'Keep the demo site and components.')
227+
.action((commandName, opt) => ACTION({ opt, props }))
228+
.option('-D, --no-demo', 'Keep the demo site and components.')
207229
.option(
208-
'-T, --no-toolkit [toolkit]',
230+
'-T, --no-toolkit',
209231
'Keep the default toolkit elements.',
210232
)
211233
.option(
212-
'-S, --no-style [style]',
234+
'-S, --no-style',
213235
'Do not empty the ~/src/assets/style/style.scss file.',
214236
)
215237
.option(
216-
'-F, --no-font [font]',
238+
'-F, --no-font',
217239
'Do not empty the ~/src/assets/fonts directory.',
218240
)
219241
.option(
220-
'-I, --no-images [images]',
242+
'-I, --no-images',
221243
'Do not empty the ~/src/assets/images directory.',
222244
)
245+
.option('-y, --confirm', 'Skip confirmation.')
223246
.on('--help', HELP);
224247

225248
/**

0 commit comments

Comments
 (0)