Skip to content

Commit d0aaaef

Browse files
committedMay 5, 2019
Scaffold tasks can now be done without interactive wizard
1 parent b237607 commit d0aaaef

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed
 

‎src/tasks/scaffold.js

+54-13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ For more information about service providers, visit:
8484
}
8585
};
8686

87+
const basicScaffoldDefaults = {
88+
options: {
89+
'--force': 'Force overwrite of existing target',
90+
'--type': 'Script type: client, server',
91+
'--filename': 'Specify name instead of using interactive wizard'
92+
}
93+
};
94+
95+
const forceBasicScaffold = args =>
96+
args.type && args.filename
97+
? {
98+
...args,
99+
target: path.join('src', args.type, args.filename),
100+
confirm: true
101+
}
102+
: false;
103+
104+
const forcePackageScaffold = args =>
105+
args.name
106+
? {
107+
...args,
108+
target: path.join('src', 'packages', args.name),
109+
confirm: true
110+
}
111+
: false;
112+
87113
const ask = (type, s) => inquirer.prompt([{
88114
name: 'type',
89115
message: `Select ${s.title} type`,
@@ -149,12 +175,14 @@ const scaffoldPackage = type => async ({logger, options, args}) => {
149175
.then(raw => raw.replace(/___NAME___/g, name))
150176
.then(contents => fs.writeFile(destination, contents))
151177
.then(() => {
152-
logger.success('Wrote', filename);
178+
logger.success('Wrote', destination.replace(options.root, ''));
153179
});
154180
});
155181
});
156182

157-
const choices = await inquirer.prompt([{
183+
const force = forcePackageScaffold(args);
184+
185+
const choices = force ? force : await inquirer.prompt([{
158186
name: 'name',
159187
message: 'Enter name of package ([A-z0-9_])',
160188
default: defaultName,
@@ -188,12 +216,11 @@ const scaffoldPackage = type => async ({logger, options, args}) => {
188216
return Promise.resolve(true);
189217
}
190218

191-
192219
const destination = path.resolve(options.root, choices.target);
193220
const exists = await fs.exists(destination);
194221
let replace = true;
195222

196-
if (exists) {
223+
if (exists && !args.force) {
197224
logger.warn('Target directory already exists');
198225

199226
const {overwrite} = await inquirer.prompt([{
@@ -217,15 +244,14 @@ const scaffoldPackage = type => async ({logger, options, args}) => {
217244
replace = a.replace;
218245
}
219246

220-
221247
await fs.ensureDir(destination);
222248

223249
return Promise.all(promises(choices.name, destination, replace))
224250
.then(() => logger.info('Running "npm install"'))
225-
.then(() => utils.spawnAsync(npmBinary, ['install'], {cwd: destination}))
251+
.then(() => args.dry ? false : utils.spawnAsync(npmBinary, ['install'], {cwd: destination}))
226252
.then(() => logger.success('...dependencies installed'))
227253
.then(() => logger.info('Running "npm run build"'))
228-
.then(() => utils.spawnAsync(npmBinary, ['run', 'build'], {cwd: destination}))
254+
.then(() => args.dry ? false : utils.spawnAsync(npmBinary, ['run', 'build'], {cwd: destination}))
229255
.then(() => logger.success('...build complete'))
230256
.then(() => {
231257
logger.info('Package was generated and built.');
@@ -246,7 +272,8 @@ const scaffoldBasic = type => async ({logger, options, args}) => {
246272
logger.info('Scaffolding', type);
247273

248274
const s = scaffolds[type];
249-
const choices = await ask(type, s);
275+
const forced = forceBasicScaffold(args);
276+
const choices = forced ? forced : await ask(type, s);
250277

251278
if (!choices.confirm) {
252279
logger.info('Scaffolding aborted...');
@@ -265,7 +292,7 @@ const scaffoldBasic = type => async ({logger, options, args}) => {
265292
);
266293

267294
const exists = await fs.exists(destination);
268-
if (exists) {
295+
if (exists && !args.force) {
269296
throw new Error('Destination already exists!');
270297
}
271298

@@ -282,26 +309,40 @@ const scaffoldBasic = type => async ({logger, options, args}) => {
282309
module.exports = {
283310
'make:auth': {
284311
description: 'Create Authentication adapter script',
285-
action: scaffoldBasic('auth')
312+
action: scaffoldBasic('auth'),
313+
...basicScaffoldDefaults
286314
},
287315
'make:settings': {
288316
description: 'Create Settings adapter script',
289-
action: scaffoldBasic('settings')
317+
action: scaffoldBasic('settings'),
318+
...basicScaffoldDefaults
290319
},
291320
'make:provider': {
292321
description: 'Create Service provider script',
293-
action: scaffoldBasic('providers')
322+
action: scaffoldBasic('providers'),
323+
...basicScaffoldDefaults
294324
},
295325
'make:vfs': {
296326
description: 'Create VFS adapter script',
297-
action: scaffoldBasic('vfs')
327+
action: scaffoldBasic('vfs'),
328+
...basicScaffoldDefaults
298329
},
299330
'make:application': {
300331
description: 'Create Application package',
332+
options: {
333+
'--force': 'Force overwrite of existing package',
334+
'--dry': 'Skip npm scripts',
335+
'--name': 'Specify name instead of using wizard'
336+
},
301337
action: scaffoldPackage('application')
302338
},
303339
'make:iframe-application': {
304340
description: 'Create IFrame Application package',
341+
options: {
342+
'--force': 'Force overwrite of existing package',
343+
'--dry': 'Skip npm scripts',
344+
'--name': 'Specify name instead of using interactive wizard'
345+
},
305346
action: scaffoldPackage('iframe-application')
306347
},
307348
'create:package': {

0 commit comments

Comments
 (0)
Please sign in to comment.