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 41e29ce
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 45 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
47 changes: 36 additions & 11 deletions lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,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 +296,29 @@ actions.sendBundle = function(tessel, filepath, opts) {

actions.tarBundle = function(opts) {
var target = opts.target || process.cwd();
var relative = path.relative(process.cwd(), target);
var pack = tar.Pack();
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,28 +335,47 @@ actions.tarBundle = function(opts) {
return rule;
});

var exclusions = '{' + rules.join(',') + '}';
var fileCount = 0;
var entry = path.join(relative, opts.resolvedEntryPoint);

var b = actions.browserify(opts.resolvedEntryPoint, {
var b = 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) {
b.exclude(file);
});
});

b.on('file', function() {
fileCount++;
});

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

// 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);
}

var relativeTargetPath = path.join(relative, opts.slimPath);

fs.writeFileSync(relativeTargetPath, actions.compress(results));

var fstream = new Ignore({
path: target,
path: relative || target,
});

fstream.addIgnoreRules(['*', '!' + opts.slimPath]);
Expand All @@ -372,7 +397,7 @@ actions.tarBundle = function(opts) {
reject(data);
})
.on('end', function() {
fs.unlinkSync(opts.slimPath);
fs.unlinkSync(relativeTargetPath);
resolve(Buffer.concat(buffers));
});
}
Expand Down
Loading

0 comments on commit 41e29ce

Please sign in to comment.