-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·58 lines (40 loc) · 1.28 KB
/
index.js
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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var resolve = require('path').resolve;
var chalk = require('chalk');
var inquirer = require('inquirer');
var questions = require('./questions.js');
var convertToMeta = function ( answers ){
// Convert the plugins array to an object
var pluginsObj = {};
answers.plugins.forEach(function(name){
pluginsObj[name] = {};
});
// Search in answers for keys like [plugin-name]--[option]
// and merge it into the pluginsObj
for ( var key in answers ){
if ( key.indexOf('--') === -1 ) { continue; }
var pluginKeyName = key.split('--');
if (!!pluginsObj[pluginKeyName[0]]){
pluginsObj[pluginKeyName[0]][pluginKeyName[1]] = answers[key];
delete answers[key];
}
}
answers.plugins = pluginsObj;
// Add empty metadata
answers.metadata = {};
return answers;
};
var writeMeta = function( answers ){
var config = resolve(process.cwd(), 'metalsmith.json');
fs.writeFile(config, JSON.stringify(answers, null, ' '), function(err){
if ( err ){
throw err;
}
console.log(chalk.green('\n✓ Successfully written ') + chalk.cyan('metalsmith.json'));
});
};
inquirer.prompt ( questions , function ( answers ){
writeMeta ( convertToMeta ( answers ) );
});