UNPKG

3.39 kBJavaScriptView Raw
1/*
2 Grunt task, sample usage:
3
4 var port = 8000,
5 publicDir = './public',
6 lumbarFile = './lumbar.json';
7
8 grunt.loadNpmTasks('lumbar');
9
10 grunt.initConfig({
11 server: {
12 base: publicDir,
13 port: port
14 },
15 lumbar: {
16 // performs an initial build so when tests
17 // and initial open are run, code is built
18 init: {
19 build: lumbarFile,
20 outputDir: publicDir
21 },
22 // a long running process that will watch
23 // for updates, to include another long
24 // running task such as "watch", set
25 // background: true
26 watch: {
27 background: false,
28 watch: lumbarFile,
29 outputDir: publicDir
30 }
31 }
32 });
33
34 grunt.registerTask('default', 'lumbar:build server lumbar:watch');
35*/
36
37var path = require('path');
38
39module.exports = function(grunt) {
40 grunt.registerMultiTask('lumbar', 'Starts a lumbar process', function() {
41
42 var done = this.async();
43
44 var config = this.data,
45 lumbarFile = config.watch || config.build || './lumbar.json',
46 outputDir = config.output || './public',
47 mode;
48
49 if (config.build) {
50 mode = 'build';
51 }
52 if (config.watch) {
53 mode = 'watch';
54 }
55
56 if (mode !== 'watch' && mode !== 'build') {
57 grunt.fatal('Arguments to lumbar task must be watch: {}, or build: {}');
58 }
59
60 if (!('background' in config)) {
61 // Default to background for anything other than build operations
62 config.background = mode !== 'build';
63 }
64
65 // never allow build to be in the background
66 if (mode === 'build') {
67 if (config.background) {
68 grunt.warn('You can\'t do a lumbar build in the background, forcing non-background execution...');
69 }
70 config.background = false;
71 }
72
73 // build up command string
74 var command = [
75 'node',
76 path.join(__dirname, '../bin/lumbar'),
77 mode
78 ];
79
80 if (config.package) {
81 command.push('--package');
82 command.push(config.package);
83 }
84
85 if (config.config) {
86 command.push('--config');
87 command.push(config.config);
88 }
89
90 if (config.minimize) {
91 command.push('--minimize');
92 }
93
94 if (config.use) {
95 command.push('--use');
96 command.push(config.use);
97 }
98
99 if (config.withJson) {
100 command.push('--with');
101 command.push(JSON.stringify(config.withJson));
102 }
103
104 if (config.plugins) {
105 config.plugins.forEach(function(plugin) {
106 if (plugin.use) {
107 command.push('--use');
108 command.push(plugin.use);
109 }
110
111 if (plugin.withJson) {
112 command.push('--with');
113 command.push(JSON.stringify(plugin.withJson));
114 }
115 });
116 }
117
118 if (config.libraries) {
119 config.libraries.forEach(function(library) {
120 command.push('--library=' + library);
121 });
122 }
123
124 if (config.verbose) {
125 command.push('--verbose');
126 }
127
128 if (config.sourceMap) {
129 command.push('--sourceMap' + (config.sourceMap !== true ? '=' + config.sourceMap : ''));
130 }
131
132 command.push(lumbarFile);
133 command.push(outputDir);
134
135 var lumbarProcess = require('child_process').spawn(command.shift(), command, { stdio: 'inherit', cwd: config.cwd });
136 if (config.background) {
137 done(true);
138 } else {
139 lumbarProcess.on('exit', function() {
140 done(lumbarProcess.exitCode);
141 });
142 }
143 });
144}