Skip to content

Commit

Permalink
--slim by default. Fixes tesselgh-412, Fixes tesselgh-418
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Oct 30, 2015
1 parent c0e406a commit 4ab7d60
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 68 deletions.
28 changes: 21 additions & 7 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ makeCommand('restart')
})
.option('entryPoint', {
position: 1,
help: 'The entry point file to deploy to Tessel'
help: 'The program entry point file to deploy to Tessel.',
})
.option('type', {
default: 'ram',
Expand All @@ -85,7 +85,7 @@ makeCommand('run')
.option('entryPoint', {
position: 1,
required: true,
help: 'The entry point file to deploy to Tessel'
help: 'The program entry point file to deploy to Tessel.'
})
.option('single', {
flag: true,
Expand All @@ -99,10 +99,17 @@ makeCommand('run')
})
.option('slim', {
flag: true,
help: 'Bundle only the required modules'
default: true,
help: 'Deploy a single "bundle" file that contains that contains only the required files, excluding any files matched by non-negated rules in .tesselignore. Program is run from "slimPath" file.'
})
.option('slimPath', {
default: 'build.js'
default: '__tessel_program__.js',
help: 'Specify the name of the --slim bundle file.'
})
.option('full', {
flag: true,
default: false,
help: 'Deploy all files in project including those not used by the program, excluding any files matched by non-negated rules in .tesselignore. Program is run from specified "entryPoint" file.'
})
.help('Deploy a script to Tessel and run it with Node');

Expand All @@ -114,7 +121,7 @@ makeCommand('push')
.option('entryPoint', {
position: 1,
required: true,
help: 'The entry point file to deploy to Tessel'
help: 'The program entry point file to deploy to Tessel.'
})
.option('single', {
flag: true,
Expand All @@ -128,10 +135,17 @@ makeCommand('push')
})
.option('slim', {
flag: true,
help: 'Bundle only the required modules'
default: true,
help: 'Push a single "bundle" file that contains that contains only the required files, excluding any files matched by non-negated rules in .tesselignore. Program is run from "slimPath" file.'
})
.option('slimPath', {
default: 'build.js'
default: '__tessel_program__.js',
help: 'Specify the name of the --slim bundle file.'
})
.option('full', {
flag: true,
default: false,
help: 'Push all files in project including those not used by the program, excluding any files matched by non-negated rules in .tesselignore. Program is run from specified "entryPoint" file.'
})
.help('Pushes the file/dir to Flash memory to be run anytime the Tessel is powered, runs the file immediately once the file is copied over');

Expand Down
82 changes: 53 additions & 29 deletions lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var fs = require('fs');
var path = require('path');
var tar = require('tar');
var Ignore = require('fstream-ignore');
var Reader = require('fstream').Reader;
var fsTemp = require('fs-temp');
var browserify = require('browserify');
var uglify = require('uglify-js');
var glob = require('glob');
Expand Down Expand Up @@ -275,7 +277,7 @@ actions.sendBundle = function(tessel, filepath, opts) {
}

// Log write
logs.info('Writing %s to %s on %s (%d kB)...', project.entryPoint, memtype, tessel.name, bundle.length / 1000);
logs.info('Writing project to %s on %s (%d kB)...', memtype, tessel.name, bundle.length / 1000);

tessel.receive(remoteProcess).then(function() {
logs.info('Deployed.');
Expand All @@ -296,23 +298,31 @@ actions.sendBundle = function(tessel, filepath, opts) {

actions.tarBundle = function(opts) {
var target = opts.target || process.cwd();
var pack = tar.Pack();
var relative = path.relative(process.cwd(), target);
var packer = tar.Pack({
noProprietary: true
});
var buffers = [];

if (opts.full) {
opts.slim = false;
}

if (opts.slim) {
logs.info('Generating slim build.');
return new Promise(function(resolve, reject) {
actions.glob(target + '/**/*.tesselignore', {
dot: true
dot: true,
mark: true,
}, function(error, ignoreFiles) {
if (error) {
return reject(error);
}

var rules = ignoreFiles.reduce(function(rules, ignoreFile) {
var dirname = path.dirname(ignoreFile);
var patterns = fs.readFileSync(ignoreFile, 'utf8').trim().split('\n').map(function(pattern) {
return path.join(dirname, pattern).replace(process.cwd() + path.sep, '');
var patterns = fs.readFileSync(ignoreFile, 'utf8').trim().split(/\r?\n/).map(function(pattern) {
return path.relative(target, path.join(dirname, pattern));
});

return rules.concat(patterns);
Expand All @@ -329,50 +339,66 @@ actions.tarBundle = function(opts) {
return rule;
});

var exclusions = '{' + rules.join(',') + '}';

var b = actions.browserify(opts.resolvedEntryPoint, {
var fileCount = 0;
var entry = path.join(relative, opts.resolvedEntryPoint);
var bify = actions.browserify(entry, {
builtins: false,
commondir: false,
browserField: false,
detectGlobals: false,
ignoreMissing: true
});

actions.glob.sync(exclusions).forEach(function(file) {
b.exclude(file);
rules.forEach(function(rule) {
actions.glob.sync(rule, {
cwd: relative || target
}).forEach(function(file) {
bify.exclude(file);
});
});

b.bundle(function(error, results) {
bify.on('file', function() {
fileCount++;
});

bify.bundle(function(error, results) {
if (error) {
return reject(error);
} else {
fs.writeFileSync(opts.slimPath, actions.compress(results));

var fstream = new Ignore({
path: target,
});
// If there is only one file in this project, then there is
// no reason to use the code generated by browserify, because
// it will always have the module loading boilerplate included.
if (opts.single || fileCount === 1) {
results = fs.readFileSync(entry);
}

fstream.addIgnoreRules(['*', '!' + opts.slimPath]);
var projectDirectory = fsTemp.mkdirSync();
var projectBundle = path.join(projectDirectory, opts.slimPath);

fstream.basename = '';
pack._noProprietary = true;
fs.writeFileSync(projectBundle, actions.compress(results));

fstream.on('entry', function(entry) {
entry.root = {
path: entry.path
};
var fstream = new Reader({
path: projectDirectory,
type: 'Directory',
});

fstream.pipe(pack)
fstream
.on('entry', function(entry) {
entry.root = {
path: entry.path
};
})
.pipe(packer)
.on('data', function(chunk) {
buffers.push(chunk);
})
.on('error', function(data) {
reject(data);
})
.on('end', function() {
fs.unlinkSync(opts.slimPath);
fs.unlinkSync(projectBundle);
fs.rmdirSync(projectDirectory);
resolve(Buffer.concat(buffers));
});
}
Expand All @@ -383,6 +409,7 @@ actions.tarBundle = function(opts) {

return new Promise(function(resolve, reject) {
var fstream = new Ignore({
basename: '',
path: target,
ignoreFiles: ['.tesselignore']
});
Expand All @@ -391,9 +418,6 @@ actions.tarBundle = function(opts) {
fstream.addIgnoreRules(['*', '!' + opts.resolvedEntryPoint]);
}

fstream.basename = '';
pack._noProprietary = true;

// This ensures that the remote root directory
// is the same level as the directory containing
// our program entry-point files.
Expand All @@ -404,7 +428,7 @@ actions.tarBundle = function(opts) {
});

// Send the ignore-filtered file stream into the tar packer
fstream.pipe(pack)
fstream.pipe(packer)
.on('data', function(chunk) {
buffers.push(chunk);
})
Expand All @@ -419,7 +443,7 @@ actions.tarBundle = function(opts) {
};

actions.runScript = function(t, filepath, entryPoint, opts) {
logs.info('Running %s...', entryPoint);
logs.info('Running %s...', opts.slim ? 'bundled project' : entryPoint);
return new Promise(function(resolve) {
return t.connection.exec(commands.runScript(filepath, opts.slim ? opts.slimPath : entryPoint))
.then(function(remoteProcess) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"debug": "^2.2.0",
"envfile": "^1.0.0",
"fs-extra": "^0.18.0",
"fs-temp": "^1.0.0",
"fstream": "^1.0.8",
"fstream-ignore": "^1.0.2",
"glob": "^5.0.15",
"inquirer": "^0.8.5",
Expand All @@ -67,9 +69,7 @@
"ssh2": "^0.4.2",
"stream-to-buffer": "^0.1.0",
"tar": "^2.1.1",
"tar-stream": "^1.2.1",
"uglify-js": "^2.5.0",
"uglifyify": "^3.0.1",
"url-join": "0.0.1",
"usb": "1.1.0",
"usb-daemon-parser": "0.0.1"
Expand Down

0 comments on commit 4ab7d60

Please sign in to comment.