Skip to content

Commit

Permalink
t2 run/push record an entry point for reuse by t2 restart. Fixes gh-675
Browse files Browse the repository at this point in the history
… (#677)

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Apr 16, 2016
1 parent 4d10285 commit 5589828
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 66 deletions.
44 changes: 28 additions & 16 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#!/usr/bin/env node

// System Objects
var path = require('path');
//...

// Third Party Dependencies
var parser = require('nomnom').script('t2');
const updateNotifier = require('update-notifier');

// Check for updates
const pkg = require('../package.json');
updateNotifier({
pkg
}).notify();

// Internal
var controller = require('../lib/controller');
var CrashReporter = require('../lib/crash_reporter');
var init = require('../lib/init');
var logs = require('../lib/logs');
var drivers = require('./tessel-install-drivers');
var Preferences = require('../lib/preferences');

const CLI_ENTRYPOINT = 'cli.entrypoint';

// Check for updates
const pkg = require('../package.json');
updateNotifier({
pkg
}).notify();

function makeCommand(commandName) {
return parser.command(commandName)
Expand Down Expand Up @@ -125,21 +128,27 @@ parser.command('provision')

makeCommand('restart')
.callback(function(opts) {
var packageJson;

// 1. Check that the type is a valid type
if (opts.type !== 'ram' && opts.type !== 'flash') {
return module.exports.closeFailedCommand('--type Invalid ');
}

// 2. If an entry point file wasn't specified, get the last
// known entry point file name and use that.
if (opts.entryPoint === undefined) {
packageJson = require(path.resolve(process.cwd(), 'package.json'));

if (packageJson && packageJson.main) {
opts.entryPoint = packageJson.main;
}
Preferences.read(CLI_ENTRYPOINT, undefined).then(entryPoint => {
if (entryPoint) {
opts.entryPoint = entryPoint;
} else {
// 3. However, if that doesn't exist either,
// there is nothing further to do.
return module.exports.closeFailedCommand('Cannot determine entry point file name');
}
callControllerWith('restartScript', opts);
});
} else {
callControllerWith('restartScript', opts);
}

callControllerWith('restartScript', opts);
})
.option('entryPoint', {
position: 1,
Expand Down Expand Up @@ -421,6 +430,9 @@ makeCommand('root')


module.exports = function(args) {
// Clear the spec from one call to the next. This is
// only necessary for testing the CLI (each call must be "fresh")
parser.specs = {};
parser.parse(args);
};

Expand Down
1 change: 1 addition & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ controller.deployScript = function(opts) {

controller.restartScript = function(opts) {
opts.authorized = true;

return controller.standardTesselCommand(opts, function(tessel) {
// Tell Tessel to restart an existing script
return tessel.restartScript(opts);
Expand Down
35 changes: 24 additions & 11 deletions lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ var urljoin = require('url-join');
var commands = require('./commands');
var deployLists = require('./deploy-lists');
var logs = require('../logs');
var Preferences = require('../preferences');
// Necessary to ensure that the next line has had the LOCAL_AUTH_PATH descriptor added.
var provision = require('./provision'); // jshint ignore:line
var Tessel = require('./tessel');

var PUSH_START_SCRIPT_NAME = 'start';
var binaryModulesUsed = new Map();
var isWindows = process.platform.startsWith('win');

// Used to store local functionality and allow
// exporting those definitions for testing.
var actions = {};

var rMemoryRow = /(.*):(?:\s+)([0-9]{1,9})/;
var replacements = {
'(anon)': '_anon',
Expand All @@ -48,9 +49,9 @@ function transformKey(value) {

const BINARY_SERVER_ROOT = 'http://packages.tessel.io/npm/';
const BINARY_CACHE_PATH = path.join(Tessel.LOCAL_AUTH_PATH, 'binaries');
const PUSH_START_SCRIPT_NAME = 'start';
const CLI_ENTRYPOINT = 'cli.entrypoint';

var binaryModulesUsed = new Map();
var isWindows = /^win/.test(process.platform);

/*
Get the results of `cat /proc/meminfo` and create an object with the data.
Expand Down Expand Up @@ -130,6 +131,7 @@ Tessel.prototype.memoryInfo = function() {
Tessel.prototype.deployScript = function(opts) {
// Only an _explicit_ `true` will set push mode
var isPush = opts.push === true;
var entryPoint = opts.entryPoint;

return new Promise((resolve, reject) => {
// Stop running any existing scripts
Expand Down Expand Up @@ -166,13 +168,16 @@ Tessel.prototype.deployScript = function(opts) {
return actions.sendBundle(this, opts);
})
.then((script) => {
if (isPush) {
// Push the script into flash
return actions.pushScript(this, script, opts).then(resolve);
} else {
// Push the code into ram
return actions.runScript(this, script, opts).then(resolve);
}
//
return Preferences.write(CLI_ENTRYPOINT, entryPoint).then(() => {
if (isPush) {
// Push the script into flash
return actions.pushScript(this, script, opts).then(resolve);
} else {
// Push the code into ram
return actions.runScript(this, script, opts).then(resolve);
}
});
});
})
.catch(reject);
Expand Down Expand Up @@ -990,6 +995,10 @@ actions.tarBundle = function(opts) {
};

actions.runScript = function(t, filepath, opts) {
if (opts.resolvedEntryPoint === undefined) {
opts.resolvedEntryPoint = opts.entryPoint;
}

logs.info('Running %s...', opts.resolvedEntryPoint);

return new Promise(function(resolve, reject) {
Expand All @@ -1012,6 +1021,10 @@ actions.runScript = function(t, filepath, opts) {

actions.pushScript = function(t, script, opts) {
// Write the node start file
if (opts.resolvedEntryPoint === undefined) {
opts.resolvedEntryPoint = opts.entryPoint;
}

return actions.writeToFile(t, opts.resolvedEntryPoint)
.then(function start() {
// Then start the script
Expand Down
63 changes: 32 additions & 31 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,57 @@
"esnext": true,
"unused": true,
"globals": {
"exports": true,
"document": true,
"Promise": true,
"copy": true,
"IS_TEST_ENV": true,
"cp": true,
"events": true,
"path": true,
"stream": true,
"util": true,
"Emitter": true,
"Duplex": true,
"_": true,
"acorn": true,
"async": true,
"bindings": true,
"Project": true,
"cli": true,
"commands": true,
"controller": true,
"copy": true,
"cp": true,
"Daemon": true,
"deploy": true,
"deployLists": true,
"discover": true,
"document": true,
"Duplex": true,
"Emitter": true,
"events": true,
"exports": true,
"fs": true,
"fsTemp": true,
"Ignore": true,
"inquirer": true,
"IS_TEST_ENV": true,
"lan": true,
"LAN": true,
"logs": true,
"mdns": true,
"mkdirp": true,
"NodeRSA": true,
"osenv": true,
"path": true,
"Preferences": true,
"Project": true,
"Promise": true,
"provision": true,
"RemoteProcessSimulator": true,
"request": true,
"sinon": true,
"sshpk": true,
"ssh": true,
"sshpk": true,
"stream": true,
"tags": true,
"tar": true,
"uglify": true,
"Tessel": true,
"commands": true,
"deploy": true,
"deployLists": true,
"provision": true,
"controller": true,
"discover": true,
"logs": true,
"TesselSeeker": true,
"TesselSimulator": true,
"uglify": true,
"updates": true,
"lan": true,
"usb": true,
"Daemon": true,
"TesselSimulator": true,
"RemoteProcessSimulator": true,
"cli": true,
"LAN": true,
"TesselSeeker": true,
"USB": true,
"tags": true,
"request": true,
"util": true,
"zlib": true
}
}

0 comments on commit 5589828

Please sign in to comment.