I am building a app where Grunt compiles a file that is returned back to the user as a download.
If I already had a static file, the I could just do:
app.post('/', function(req, res){
var pkgFile = __dirname + '/myfile.js';
var filestream = fs.createReadStream(pkgFile);
filestream.pipe(res);
});
The problem is if the file is compiled by Grunt, called from inside app.post() and when the Grunt task is done it exits my express server. I get a messade "Done, without errors" and with that the process exits.
I am running my Grunt task using Grunt.cli and passing a callback to my packager since my Grunt task returns data, no files written.
grunt.cli({
gruntfile: __dirname + "/../gruntfile.js",
modules: modules,
strip: !compat,
releaseVersion: version,
project: project,
noCoreDependencies: !addCoreDependencies,
callback: stream
});
function stream (data) {
var filename = ['MooTools-', project, '-', version, (compat ? '-compat' : '') + (minified ? '-compressed' : ''), '.js'].join('');
if (minified) data = uglify(data);
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.write(data, 'binary');
res.end();
}
This way everything works one time, then I have to re-start the server...
Is there a way I can run Grunt.cli as a child process, or in a way that it exits without killing my server, and being able to have that callback as a parameter into grunt options?
Or is there a better way to design this?
What I am doing is compiling different files from a library into one .js file > uglifying it > providing as download to user.
"use strict";
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
var cb = grunt.option('callback'),
strip = grunt.option('strip'),
version = grunt.option('releaseVersion'),
modules = grunt.option('modules'),
project = grunt.option('project'),
noCoreDependencies = grunt.option('noCoreDependencies');
function projectPath(project_, version_){
var versions = require('./package.json')._projects[project_].versions;
if (!~versions.indexOf(version_)) version_ = versions.filter(function(ver){
return ver.slice(0, -2) <= version_.slice(0, -2);
})[0];
return 'cache/' + project_.toLowerCase() + '/docs/' + project_.toLowerCase() + '-' + version + '/Source/';
}
var packagerOptions = {
options: {
name: {
Core: projectPath('core', version),
More: projectPath('more', version)
},
callback: cb,
noOutput: true,
ignoreYAMLheader: noCoreDependencies
},
customBuild: {
src: [projectPath('core', version) + '**/*.js', projectPath('more', version) + '**/*.js'],
dest: 'mootools-rocks!.js'
}
}
if (modules.length) packagerOptions.options.only = modules;
if (strip) packagerOptions.options.strip = ['.*compat'];
grunt.initConfig({
'packager': packagerOptions
});
grunt.registerTask('default', 'build file and don\'t exit', function(){
grunt.task.run(['packager:customBuild']);
// how to prevent process.exit() ?
});
};