How to abort if command fails in Unix shell? - linux

I'm not very great with shell scripting and still couldn't find a solution to this. I want to run a command gulp tslint and if it returns errors, abort the script.
So far I tried gulp tslint || exit 1 with no success. I think it somehow is returning true, even though it has errors when i run it on command prompt e.g.
[12:30:14] Starting 'tslint'...
[12:30:14] Finished 'tslint' after 9.49 ms
[12:30:16] [gulp-tslint] error (quotemark) payment-types.ts[18, 53]: " should be '
How can i make it work?
Any help appreciated. Thanks!

This looks like a gulp question, rather than a shell question. As gulp completes successfully (i.e. reaches an exit without raising an error), it doesn't tell the shell that anything is wrong. The best way to address this is to reconfigure gulp to interpret lint failures as errors.
Could you post your gulpfile.js? Specifically your tslint task.
It probably looks like
gulp.task('tslint', function() {
gulp.src("**/*.ts")
.pipe(tslint.report('prose'))
});
You can instruct your reporter to fail on error by changing this to:
gulp.task('tslint', function() {
gulp.src("**/*.ts")
.pipe(tslint.report('prose', {emitError: true}))
});
edit: look at the gulp-tslint README for more details on emitError

Related

Yarn / node command "error Command failed with exit code 1." no error

I've got a weird issue going on. I have a node service being started with yarn .... The app seems to work fine for some random amount of time before I get hit with a...
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
There is no error logged. My service already has the following two handlers, but they never log anything when the service dies like this.
process.on('unhandledRejection', (err: any) => {
logger.fatal({ err: err }, 'Process failed. Unhandled Rejection');
});
process.on('uncaughtException', (list) => {
logger.fatal({ err: list }, 'unhandledExceptionThrown');
});
I've read some other SO question (error Command failed with exit code 1. when I try to run yarn) and tried to clear node modules / clean my yarn cache, but so far that has not helped the issue.
Is there something else I can try to track down what's causing the issue?
After lots of digging around, I found something that was able to help move me in the right direction. Im not sure why the handlers above weren't catching the issue, however... when I added --unhandled-rejections=strict to my node command to start the service, the app would blow up as expected and actually would output some useful information.

WebStorm debugging not stopping on breakpoints

My app is a node.js app which I can run through command line using: npm test inside the working directory. In WebStorm, I created a new configuration that looks like this:
Node interpreter: /usr/local/bin/npm
Node parameters: test
Working directory ~/dev/project
When I hit the run button, I get the correct output:
/usr/loca/bin/npm test
> app#1.0.0 test ~/dev/project
something else
Process finished with exit code 0
But since I have breakpoints set, it should have stopped on a breakpoint instead of getting to the Process finished part. I set my code to be pretty simple just so I can test breakpoints, so it looks like this:
"use strict";
let foo = false; (breakpoint)
let bar = true;
if (foo === bar) { (breakpoint)
console.log('something');
} else {
console.log('something else'); (breakpoint)
}
process.exit(1);
I also tried to make this work through command line. In my site settings, I set my Built-in server to port 12345. Can accept external connections, and allow unsigned requests.
when I run it through command line, I use:
npm --debug-brk=12345 test
I get the same result: it runs all the way to the exit point without stopping on the breakpoints.
Any ideas what I need to do to get this to work?
/usr/local/bin/npm is definitely not a Node interpreter, it's NPM package manager that is a Node.js application itself (i.e. it's run with Node.js interpreter). And test is a name of npm script, not a Node.js parameter.
If you like to debug your .js file that is run via npm test, you need to modify your npm script to include the debug options and then use NPM run configuration for debugging. Or, just create a Node.js configuration, set a valid path to Node.js execuitable there (/usr/bin/node or whatever it looks like on your system), then specify your .js file as JavaScript file:. See https://blog.jetbrains.com/webstorm/2017/09/debugging-node-js-apps-in-webstorm for more info

gulp-less task exits with zero on invalid styles

Firstly, here is a repo with very simple example of my problem: https://github.com/nkoder/example-of-gulp-less-zero-exit-on-error
I want to define gulp build pipeline which fails on any error. This pipeline would be used in automated process (eg. Continuous Integration) so I need the build command to return non-zero exit code on failure. It works for me with invalid Jade templates or non-passing tests run with Karma.
The problem is when I use gulp-less to prepare CSS from LESS. Whenever I add any pipeline step after gulp-less execution, all errors are "consumed" and the whole task exits with 0. But it shouldn't.
The first question is: How to force wrong task to fail with non-zero exit code? (solved, see answer below)
The second question is: Why error event emitted in less() call followed by piped stream is not making a whole task to return non-zero exit code? Non-zero is returned in some other similar cases, eg. in Jade or ESLint failure. (not solved yet)
Versions:
node 5.11.0
npm 3.8.6
gulp 3.9.1
gulp-less 3.1.0
Listen for the error event and then exit the process explicitly:
gulp.task('wrong', function () {
return gulp
.src('invalid-styles.less')
.pipe(less().on('error', function(error) {
console.log(error.message);
process.exit(1);
}))
.pipe(gulp.dest('generated-styles.css'));
});

Grunt-contrib-coffee doesn't write to output file (silently fails without throwing an error)

I'm using grunt-contrib-coffee v.0.7.0 with grunt v0.4.1.
I've declared my coffeescript task in my Gruntfile.coffee like this:
coffee:
dev:
'asd.js': 'asd.coffee'
In the same directory I have asd.coffee which contains
hello = 'hi'
and asd.js which is empty.
When I run grunt coffee in my terminal, it prints:
Running "coffee:dev" (coffee) task
Done, without errors.
But nothing is ever written to asd.js (or anywhere). What's going wrong? How can I fix this?
Also to verify what GruntJs is actually doing than you can always try:
grunt coffee --verbose
And see what files it is processing (if any)
Change it to:
coffee:
dev:
files:
'asd.js': 'asd.coffee'

PhantomJS exited unexpectedly with exit code -1073741819

I run a bunch of Jasmine specs with PhantomJS (via Grunt) on a Windows 7 PC, and I happen to get the following error:
Testing jasmine specs via phantom
......
Running PhantomJS...ERROR
>> 0 [ '' ]
Warning: PhantomJS exited unexpectedly with exit code -1073741819. Use --force to continue.
Aborted due to warnings.
The error does not appear if I delete a bunch of tests; however I have no idea what causes the error.
What I also find strange, it that it only occurs now and then.
Any idea why this happens?
We are running AngularJS tests via jasmine and grunt, and had this very same problem. For us it turned out to be due to our custom $exceptionHandler function.
We were missing the throw exception line that is in the example handler. By not throwing the exception, the test would pass but it would randomly cause PhantomJS to crash.
This means that some of our tests were failing and we didn't know it!
Here's the sample handler from the $exceptionHandler documentation page:
angular.module('exceptionOverride', []).factory('$exceptionHandler', function () {
return function (exception, cause) {
exception.message += ' (caused by "' + cause + '")';
throw exception; // <--- This is important
};
});
So I'd look and see if you have custom exception handling too and are possibly missing the throw exception line.
I hope it's as simple as that for you too!
(Sadly, I don't have an answer. I would post as a comment, but I don't have the rep.)
I'm getting the same error trying to create html snapshots with grunt-html-snapshot.
Running PhantomJS...ERROR
>> 0 [ '' ]
Warning: PhantomJS exited unexpectedly with exit code -1073741819. Use --force to continue.
Aborted due to warnings.
I'm feeding grunt a list of URLs and I "randomly" get this crash somewhere in the list. I think I have traced it to the child process spawn call that launches phantom.exe with the bridge.js script. It doesn't seem to get as far as trying load the next page before the phantom.exe child process exits with the error. I haven't been able to find any documentation on the error code.
I received the same error message as Helge after renaming a folder and running jasmine in grunt.
The folder contained all the JavaScript files I wanted to minify into one file, and I renamed the folder to match the final file name. For example the folder, \src\modules\ was renamed \src\modules.js\.
Removing .js from the folder name solved the problem.
Using PhantomJS to generate snapshots for AngularJS.
The snapshots script goes through an array of urls of which it has to take snapshots.
Narrowed down to the possible cause and as it turned out there was a bug in my latest fix in AngularJS. This bug resulted in the constantly switching the home page and the page where the error occurred.
Code Visualization:
$state.go('home state');
...
$state.go('an other state');
...
$state.go('home state');
...
$state.go('an other state');
...
$state.go('home state');
...
...
=> Error: PhantomJS exited unexpectedly with exit code -1073741819
In short:
For me this error was caused by an infinite redirect loop.
The warning PhantomJS exited unexpectedly with exit code -1073741819 is also displayed when your local PhantomJS is incompatible or corrupt.
With npm list -g phantomjs you can check your PhantomJS version.
If you think that it is corrupt, just reinstall it with:
npm uninstall phantomjs -g
npm install phantomjs -g

Resources