Commits

Damon Oehlman committed b0c36a8

Implementing zipping of project files

Comments (0)

Files changed (4)

lib/commands/build.js

+var tasks = require('../tasks');
+
 exports.desc = 'Build the current project';
 
 exports.args = {
 
 // export runner
 exports.run = function(opts, callback) {
+    tasks.run(['package'], process.cwd());
 };
     });
 };
 
+// load the local configuration settings
+exports.loadLocal = function(targetPath, callback) {
+    var localConfig = path.resolve(targetPath, 'config.json'),
+        cfg;
+
+    fs.readFile(localConfig, 'utf8', function(err, data) {
+        try {
+            cfg = JSON.parse(data || '{}');
+        }
+        catch (e) {
+            cfg = {};
+        }
+
+        callback(_.defaults(cfg, {
+            projectName: path.basename(targetPath)
+        }));
+    });
+};
+
 exports.save = function(data, callback) {
     var output;
 
+var async = require('async'),
+    fs = require('fs'),
+    path = require('path'),
+    mkdirp = require('mkdirp'),
+    fstream = require('fstream'),
+    task = require('taskify'),
+    out = require('out'),
+    spawn = require('child_process').spawn,
+    config = require('./config'),
+    reIgnoreFiles = /^(\.git|node_modules|Jakefile|\.DS_Store|template|output|lib|package.json)/,
+    ignoreExtensions = ['.DS_Store', '.bar', '.zip'],
+    projectFiles = [];
+
+// discover the nature of the project through 
+task('discovery', function(targetPath) {
+    var reader = fstream.Reader({
+            path: targetPath,
+            filter: function(entry) {
+                var testPath = entry.path.slice(targetPath.length + 1),
+                    shouldIgnore = reIgnoreFiles.test(testPath);
+
+                return ! (shouldIgnore || ignoreExtensions.indexOf(path.extname(entry.path)) >= 0);
+            }
+        });
+        
+    reader.on('child', function(entry) {
+        if (entry.type === 'File') {
+            projectFiles.push(entry.path);
+        }
+    });
+    
+    reader.on('end', this.async());
+    out('!{bold}analyzing project structure');
+});
+
+task('prepare', function(targetPath) {
+    mkdirp(path.resolve(targetPath, 'output'), this.async());
+});
+
+task('package', ['discovery', 'prepare'], function(targetPath) {
+    var done = this.async(),
+        basePath = path.resolve(targetPath),
+        relativeFiles, proc;
+
+    // load the local config
+    config.loadLocal(targetPath, function(cfg) {
+        var targetFile = path.join('output', cfg.projectName + '.zip'),
+            args = [targetFile];
+
+        relativeFiles = projectFiles.map(function(filename) {
+            return filename.slice(basePath.length + 1);
+        });
+
+        proc = spawn('zip', args.concat(relativeFiles), {
+            cwd: path.resolve(targetPath)
+        });
+
+        proc.stdout.on('data', function(buffer) {
+            console.log(buffer.toString());
+        });
+        proc.on('exit', function(code) {
+            out('!{bold}zip completed with exit code: ' + code);
+            done();
+        });
+
+        out('!{bold}creating archive');
+    });
+});
+
+exports.run = task.run;
     "node": ">= 0.8.x < 0.9.0"
   },
   "dependencies": {
+    "async": "0.2.x",
+    "fstream": "0.1.x",
+    "mkdirp": "0.3.x",
     "scaffolder": "0.7.x",
     "taskify": "0.5.x",
+    "out": "0.4.x",
     "underscore": "1.4.x"
   },
   "devDependencies": {},