Monday, June 6, 2016

Gulp from command line with error handling


First step is installing the gulp.
Install gulp
Install gulp modules

There will be node-modules directory in your present working directory (pwd).

Create a gulpfile.js in your current working directory which would include the JS functions and gulp tasks.
------------------------------------
Sample gulpfile.js
------------------------------------
function handleError(err) {
    console.log("Error: ", err.toString());
    process.exit(-1);
}

var gulp = require('gulp');
var minifyCss = require('gulp-minify-css');

gulp.task('minify-css', function() {
  return gulp.src('./*.css')
    .pipe(minifyCss({compatibility: 'ie8'}))
    // handle error
    .on("error", handleError)
    .pipe(gulp.dest('CSS'));
});

var uglify = require('gulp-uglify');

gulp.task('minify-js', function() {
  return gulp.src('./*.js')
    .pipe(uglify())
    // handle error
    .on("error", handleError)
    .pipe(gulp.dest('JS'))
    ;
});



Open terminal in the directory where gulpfile.js is located.
Run the following command.
gulp <task-name>
Ex. gulp minify-js


Error handling

Error code returned from the gulp process will be handled in the shell script that invoked the gulp command.

On error, there will be an exit code of -1 from gulp handleError function which can be used to take corresponding action in the shell script.

----------------------------------------------
Sample shell script (powershell)
----------------------------------------------
gulp minify-js
if ($LASTEXITCODE -ne 0) {
    throw "Gulp Failed. Refer gulp log files."
    exit
}
echo ":) Gulp tasks finished successfully."

Export all the stored procedures to a file in SQL Server


Recently, I just wanted to get all the stored procedures, full text, on a file. This will give me power to do lot of things.
So, I came across of material. These are 3 approaches I followed.

Approach 1

Right Click on the database name -> Tasks -> Generate Scripts...



In the Choose Objects step,
Check the Stored Procedures.

Next step choose a destination file. Next. Finish.

Approach 2

This can be achieved by using a query as well, but be warned that there is a pitfall.
SQL Server truncates all the cell contents to 4000 characters. Tested on 2014.

    SELECT
        r.ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES r

You could use the columns ROUTINE_CATALOG, r.ROUTINE_SCHEMA, r.ROUTINE_NAME to get extra details.

Caution: SELECT query truncates cells to 4000 chars.
I have not found a direct and simple solution to this problem. (Please leave a comment if you have one).

Settings: You can access 
Tools -> Query Results -> Results to Grid 
to modify the settings. However, maximum length of Non XML data is 65535 only. Unable to go beyond that.

Approach 3

    SELECT DEFINITION FROM SYS.SQL_MODULES