Skip to content

Commit

Permalink
feat(command): Add defaultconfig command
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Feb 18, 2016
1 parent 3ff3f55 commit e247110
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
61 changes: 61 additions & 0 deletions commands/defaultconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

var util = require('../lib/util');
var os = require('os');
var chalk = require('chalk');
var semver = require('semver');
var fs = require('fs');
var path = require('path');
var defaults = require('json-schema-defaults');

module.exports = function initCommand(program) {
program
.command('defaultconfig [bundle]')
.description('Generate default config from configschema.json')
.action(action);
};

function action(bundleName) {
var cwd = process.cwd();
var nodecgPath = util.getNodeCGPath();

if (!bundleName) {
if (util.isBundleFolder(cwd)) {
bundleName = bundleName || path.basename(cwd);
}
else {
console.error(chalk.red('Error:') + ' No bundle found in the current directory!');
return;
}
}

var bundlePath = path.join(nodecgPath, 'bundles/', bundleName);
var schemaPath = path.join(nodecgPath, 'bundles/', bundleName, '/configschema.json');
var cfgPath = path.join(nodecgPath, 'cfg/');

if (!fs.existsSync(bundlePath)) {
console.error(chalk.red('Error:') + ' Bundle %s does not exist', bundleName);
return;
}
if (!fs.existsSync(schemaPath)) {
console.error(chalk.red('Error:') + ' Bundle %s does not have a configschema.json', bundleName);
return;
}
if (!fs.existsSync(cfgPath)) {
fs.mkdirSync(cfgPath);
}

var schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
var configPath = path.join(nodecgPath, 'cfg/', bundleName + '.json');
if (!fs.existsSync(configPath)) {
try {
fs.writeFileSync(configPath, JSON.stringify(defaults(schema), null, ' '));
console.log(chalk.green('Success:') + ' Created %s\'s default config from schema\n\n' + JSON.stringify(defaults(schema), null, ' '), bundleName);
}
catch (e) {
console.error(chalk.red('Error: ') + e);
}
} else {
console.error(chalk.red('Error:') + ' Bundle %s already has a config file', bundleName);
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"chalk": "^0.5.1",
"commander": "^2.6.0",
"inquirer": "^0.11.4",
"json-schema-defaults": "^0.1.1",
"npm-package-arg": "^3.0.0",
"request": "^2.51.0",
"rimraf": "^2.2.8",
Expand Down
102 changes: 102 additions & 0 deletions test/commands/4_defaultconfig.mspec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

var path = require('path');
var childProcess = require('child_process');
var mkdirp = require('mkdirp');
var assert = require('chai').assert;
var sinon = require('sinon');
var MockProgram = require('../mocks/program');
var DefaultConfigCommand = require('../../commands/defaultconfig');
var fs = require('fs');

describe('defaultconfig command', function () {
var defaultConfigCommand, program;

beforeEach(function() {
program = new MockProgram();
defaultConfigCommand = new DefaultConfigCommand(program);
if (fs.existsSync('./cfg/lfg-streamtip.json'))
fs.unlinkSync('./cfg/lfg-streamtip.json');
});

context('when run with a bundle argument', function() {
it('should successfully create a bundle config file when bundle has configschema.json', function () {
this.timeout(25000);
fs.writeFileSync('./bundles/lfg-streamtip/configschema.json', JSON.stringify({
type: 'object',
properties: {
username: {
type: 'string',
default: 'user'
},
value: {
type: 'integer',
default: 5
},
nodefault: {
type: 'string'
}
}
})
);
sinon.spy(childProcess, 'execSync');
program.runWith('defaultconfig lfg-streamtip');
assert.equal(fs.existsSync('./cfg/lfg-streamtip.json'), true);
var config = JSON.parse(fs.readFileSync('./cfg/lfg-streamtip.json'));
assert.equal('user', config.username);
assert.equal(5, config.value);
assert.isUndefined(config.nodefault);
childProcess.execSync.restore();
});

it('should print an error when the target bundle does not have a configschema.json', function () {
this.timeout(25000);
sinon.spy(console, 'error');
mkdirp.sync('./bundles/missing-schema-bundle');
program.runWith('defaultconfig missing-schema-bundle');
assert.equal('\u001b[31mError:\u001b[39m Bundle %s does not have a configschema.json',
console.error.getCall(0).args[0]);
console.error.restore();
});

it('should print an error when the target bundle does not exist', function () {
this.timeout(25000);
sinon.spy(console, 'error');
program.runWith('defaultconfig not-installed');
assert.equal('\u001b[31mError:\u001b[39m Bundle %s does not exist',
console.error.getCall(0).args[0]);
console.error.restore();
});

it('should print an error when the target bundle already has a config', function () {
this.timeout(25000);
sinon.spy(console, 'error');
fs.writeFileSync('./cfg/lfg-streamtip.json', JSON.stringify({"fake":"data"}));
program.runWith('defaultconfig lfg-streamtip');
assert.equal('\u001b[31mError:\u001b[39m Bundle %s already has a config file',
console.error.getCall(0).args[0]);
console.error.restore();
});
});

context('when run with no arguments', function () {
it('should successfully create a bundle config file when run from inside bundle directory', function () {
this.timeout(25000);
process.chdir('./bundles/lfg-streamtip');
program.runWith('defaultconfig');
assert.equal(fs.existsSync('../../cfg/lfg-streamtip.json'), true);
});

it('should print an error when in a folder with no package.json', function () {
this.timeout(25000);
mkdirp.sync('./bundles/not-a-bundle');
process.chdir('./bundles/not-a-bundle');

sinon.spy(console, 'error');
program.runWith('defaultconfig');
assert.equal('\u001b[31mError:\u001b[39m No bundle found in the current directory!',
console.error.getCall(0).args[0]);
console.error.restore();
});
});
});
File renamed without changes.

0 comments on commit e247110

Please sign in to comment.