Skip to content

Commit

Permalink
feat(cli): add cli env cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Clunt committed May 30, 2018
1 parent 7b607e5 commit 225f705
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
90 changes: 90 additions & 0 deletions cli/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"use strict";

const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const configYargs = require('./util/config-yargs');
const configParse = require('./util/config-parse');


function generateEnv(configPath) {
const configFile = configParse.file({ config: configPath });
const modules = Object.keys(configFile.module || {});

if (modules.length === 0) {
throw new Error(`At least one presupposition module is needed`);
}

const config = {
git: '',
module: []
};

return inquirer.prompt({
type: 'input',
name: 'gitType',
message: 'Which git tag or branch will be diff? [default: the previous tag]'
})
.then(gitTypeAnswers => {
config.git = gitTypeAnswers.gitType;
})
.then(() => {
return inquirer.prompt({
type: 'checkbox',
name: 'moduleType',
message: 'Choice modules',
choices: ['a', 'b', 'c']
})
})
.then(moduleTypeAnswers => {
config.module = moduleTypeAnswers.moduleType;
})
.then(() => {
return JSON.stringify(config.git + ':' + config.module.join(','));
});
}

exports.cmd = 'env [config]';
exports.desc = 'Generate env config string';
exports.builder = yargs => yargs
.option('shortcut', {
type: 'boolean',
describe: 'Generate config to shortcut like git:modules,...',
group: configYargs.BASIC_GROUP
})
.option('output', {
type: 'string',
describe: 'The output directory',
group: configYargs.BASIC_GROUP,
requiresArg: true
})
.strict();

exports.handler = argv => {
new Promise((resolve, reject) => {
if (argv.shortcut) {
resolve(generateEnv(argv.config));
} else {
if (!(argv.config || typeof argv.config === 'string')) {
reject('Required config path');
}

const configPath = path.join(process.cwd(), argv.config);
const configFile = fs.readFileSync(configPath);
const configObj = JSON.parse(configFile);
const configStr = JSON.stringify(configObj);
const configEnv = JSON.stringify(configStr);
resolve(configEnv);
}
})
.then(configEnv => {
console.log(configEnv)
if (argv.output) {
const outputPath = path.join(process.cwd(), argv.output);
fs.writeFileSync(outputPath, configEnv, 'utf8');
}
})
.catch(e => {
throw new Error(e);
});
};
1 change: 1 addition & 0 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const configParse = require('./util/config-parse');

exports.commands = [
require('./init'),
require('./test')
];

exports.builder = configYargs;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"fs-extra": "^6.0.1",
"glob": "^7.1.2",
"inquirer": "^5.2.0",
"minimatch": "^3.0.4",
"yargs": "^11.0.0",
"yeoman-environment": "^2.1.1",
Expand Down

0 comments on commit 225f705

Please sign in to comment.