UNPKG

2.79 kBJavaScriptView Raw
1var spawn = require('child_process').spawn;
2var archiver = require('archiver');
3var fs = require('fs');
4var pathResolve = require('path').resolve;
5var _ = require('underscore');
6var ProgressBar = require('progress');
7
8module.exports = buildApp;
9// 打包meteor应用
10function buildApp(appPath, buildLocaltion, buildOptions, callback) {
11 buildMeteorApp(appPath, buildLocaltion, buildOptions, function(code) {
12 if (code == 0) {
13 archiveIt(buildLocaltion, callback);
14 } else {
15 console.log("\n=> Build Error. Check the logs printed above.");
16 callback(new Error("build-error"));
17 }
18 });
19}
20
21function buildMeteorApp(appPath, buildLocaltion, buildOptions, callback) {
22 var executable = "meteor";
23 var args = [
24 "build", "--directory", buildLocaltion,
25 "--architecture", "os.linux.x86_64",
26 ];
27 // "--server", "http://localhost:3000",
28
29 if (buildOptions.serverOnly) {
30 args.push("--server-only");
31 }
32
33 if (buildOptions.debug) {
34 args.push("--debug");
35 }
36
37 if (buildOptions.mobileSettings) {
38 args.push('--mobile-settings');
39 args.push(JSON.stringify(buildOptions.mobileSettings));
40 }
41
42 console.info('[meteorup]'.magenta + ' - Meteor app path ' + appPath);
43 var options = { cwd: appPath };
44 // var meteor = spawn("mkdir", ["-p",buildLocaltion+"/bundle"], options);
45 var meteor = spawn(executable, args, options);
46 var stdout = "";
47 var stderr = "";
48
49 meteor.stdout.pipe(process.stdout, { end: false });
50 meteor.stderr.pipe(process.stderr, { end: false });
51
52 meteor.on('close', callback);
53}
54
55function archiveIt(buildLocaltion, callback) {
56 callback = _.once(callback);
57 var bundlePath = pathResolve(buildLocaltion, 'bundle.tar.gz');
58 var sourceDir = pathResolve(buildLocaltion, 'bundle');
59
60 // var states = fs.statSync(bundlePath);
61 var output = fs.createWriteStream(bundlePath);
62 var archive = archiver('tar', {
63 gzip: true,
64 gzipOptions: {
65 level: 6,
66 checkpoint: 100,
67 "checkpoint-action": "dot",
68 totals: true
69 }
70 });
71
72
73 archive.on('entry', function() {
74 process.stdout.cursorTo(0);
75 process.stdout.write('Compressing by ' + archive.pointer() + ' bytes');
76 process.stdout.clearLine(1);
77 });
78
79 archive.pipe(output);
80
81 output.once('close', function() {
82 process.stdout.clearLine();
83 process.stdout.cursorTo(0);
84 callback();
85 });
86
87 archive.once('error', function(err) {
88 console.log("=> Archiving failed:", err.message);
89 callback(err);
90 });
91 console.log('[meteorup]'.magenta + ' - Tar file path ' + sourceDir);
92 archive.directory(sourceDir, 'bundle').finalize();
93}