Skip to content

Commit

Permalink
warn on invalid or missing bowerJson file
Browse files Browse the repository at this point in the history
Logs appropriate message based on error

Fixes #197
  • Loading branch information
eddiemonge committed Mar 17, 2016
1 parent b649193 commit dc88ab0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
14 changes: 14 additions & 0 deletions test/fixture/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "wiredep-test"
"version": "0.0.0",
"dependencies": {
"codecode": "0.0.3",
"bootstrap": "3.0.0",
"modular-scale": "2.0.3",
"3l": "1.4.4",
"karma": "0.2.1"
},
"devDependencies": {
"things": "0.1.1"
}
}
32 changes: 32 additions & 0 deletions test/wiredep_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ describe('wiredep-cli', function () {
}

describe('replace functionality', function () {
var stub;
before(function () {
fs.copySync('test/fixture', '.tmp');
process.chdir('.tmp');
stub = sinon.stub(console, 'warn');
});

after(function () {
process.chdir('..');
fs.removeSync('.tmp');
stub.restore();
});

function runCli (arg) {
Expand Down Expand Up @@ -75,6 +78,35 @@ describe('wiredep-cli', function () {
runLog(['-b', 'bower.json'], msg, 'error');
runLog(['--bowerJson', 'bower.json'], msg, 'error');
});

it('should message when the requested file not found', function () {
var stub = sinon.stub(console, 'error');
var warnMsg = '> Could not find `no.json`';
runLog(['-s', 'foo', '-b', 'no.json'], warnMsg, 'warn');
runLog(['-s', 'foo', '--bowerJson', 'no.json'], warnMsg, 'warn');
stub.restore();

stub = sinon.stub(console, 'warn');
var errMsg = /bower.json not found/;
runLog(['-s', 'foo', '-b', 'no.json'], errMsg, 'error');
runLog(['-s', 'foo', '--bowerJson', 'no.json'], errMsg, 'error');
stub.restore();
});

it('should message when the requested file is invalid', function () {
var file = resolve('test/fixture/invalid.json');
var stub = sinon.stub(console, 'error');
var warnMsg = '> Invalid';
runLog(['-s', 'foo', '-b', file], warnMsg, 'warn');
runLog(['-s', 'foo', '--bowerJson', file], warnMsg, 'warn');
stub.restore();

stub = sinon.stub(console, 'warn');
var errMsg = /bower.json not found/;
runLog(['-s', 'foo', '-b', file], errMsg, 'error');
runLog(['-s', 'foo', '--bowerJson', file], errMsg, 'error');
stub.restore();
});
});
});

Expand Down
38 changes: 24 additions & 14 deletions wiredep-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,34 @@ if (!argv.src) {
return;
}

try {
argv.bowerJson = JSON.parse(fs.readFileSync(argv.bowerJson));
} catch (e) {
delete argv.bowerJson;
if (argv.bowerJson) {
try {
argv.bowerJson = require(argv.bowerJson);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
console.warn(chalk.bold.red('> Could not find `' + argv.bowerJson + '`'));
}

if (/SyntaxError/.test(e)) {
console.warn(chalk.bold.red('> Invalid `' + argv.bowerJson + '`'));
}

delete argv.bowerJson;
}
}

try {
if (!argv.bowerJson) {
if (!argv.bowerJson) {
try {
fs.statSync(path.normalize('./bower.json'));
} catch (e) {
console.error(
chalk.bold.red('> bower.json not found.') + EOL +
'Please run `wiredep` from the directory where your `bower.json` file' +
' is located.' + EOL +
'Alternatively, pass a `--bowerJson path/to/bower.json`.'
);
return;
}
} catch (e) {
console.error(
chalk.bold.red('> bower.json not found.') + EOL +
'Please run `wiredep` from the directory where your `bower.json` file' +
' is located.' + EOL +
'Alternatively, pass a `--bowerJson path/to/bower.json`.'
);
return;
}

// Replace the arguments short form with their long form names
Expand Down

0 comments on commit dc88ab0

Please sign in to comment.