UNPKG

3.38 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 config.background = true;
62 }
63
64 // never allow build to be in the background
65 if (mode === 'build') {
66 if (config.background) {
67 grunt.warn('You can\'t do a lumbar build in the background, forcing non-background execution...');
68 }
69 config.background = false;
70 }
71
72 // build up command string
73 var command = [
74 'node',
75 path.join(__dirname, '../bin/lumbar'),
76 mode
77 ];
78
79 if (config.package) {
80 command.push('--package ' + config.package);
81 }
82
83 if (config.config) {
84 command.push('--config');
85 command.push(config.config);
86 }
87
88 if (config.minimize) {
89 command.push('--minimize');
90 }
91
92 if (config.use) {
93 command.push('--use');
94 command.push(config.use);
95 }
96
97 if (config.withJson) {
98 command.push('--with');
99 command.push(JSON.stringify(config.withJson));
100 }
101
102 if (config.plugins) {
103 config.plugins.forEach(function(plugin) {
104 if (plugin.use) {
105 command.push('--use');
106 command.push(plugin.use);
107 }
108
109 if (plugin.withJson) {
110 command.push('--with');
111 command.push(JSON.stringify(plugin.withJson));
112 }
113 });
114 }
115
116 if (config.libraries) {
117 config.libraries.forEach(function(library) {
118 command.push('--library=' + library);
119 });
120 }
121
122 if (config.verbose) {
123 command.push('--verbose');
124 }
125
126 if (config.sourceMap) {
127 command.push('--sourceMap');
128 }
129
130 command.push(lumbarFile);
131 command.push(outputDir);
132
133 var lumbarProcess = require('child_process').spawn(command.shift(), command);
134
135 lumbarProcess.stdout.on('data', function(data) {
136 process.stdout.write(data.toString());
137 });
138 lumbarProcess.stderr.on('data', function(data) {
139 process.stdout.write(data.toString());
140 });
141
142 if (config.background) {
143 done(true);
144 } else {
145 lumbarProcess.on('exit', function() {
146 done(true);
147 });
148 }
149 });
150}