-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): Add defaultconfig command
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.