Skip to content

Commit

Permalink
Implement complete tesselinclude detect spec. Closes gh-638 (#639)
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Apr 8, 2016
1 parent 4d759b2 commit 4b3f72f
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = function(grunt) {
};

function changelog(rows) {
return tags.stripIndent`
return tags.stripIndent `
| Commit | Message/Description |
| ------ | ------------------- |
${rows}
Expand Down
23 changes: 20 additions & 3 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// System Objects
var path = require('path');
var fs = require('fs');
var fs = require('fs-extra');

// Third Party Dependencies
var PZ = require('promzard').PromZard;
Expand Down Expand Up @@ -124,7 +124,23 @@ function generateSample() {
fs.createReadStream(path.resolve(__dirname + './../resources/' + filename))
.pipe(fs.createWriteStream(filename));

console.log('Wrote \'Hello World\' to index.');
console.log('Wrote "Hello World" to index.js');
});
}

function createTesselinclude() {
var tesselinclude = '.tesselinclude';

return new Promise((resolve) => {
fs.exists(tesselinclude, (exists) => {
if (exists) {
return resolve();
}

fs.copySync(path.resolve(__dirname + './../resources/' + tesselinclude), tesselinclude);
console.log('Created .tesselinclude.');
resolve();
});
});
}

Expand Down Expand Up @@ -160,7 +176,7 @@ module.exports = function(opts) {
// Set interactive boolean off of CLI flags
options = opts;

console.log('Initializing tessel repository...');
console.log('Initializing Tessel 2 Project...');

readPackageJson()
.then(function(data) {
Expand Down Expand Up @@ -197,6 +213,7 @@ module.exports = function(opts) {
.then(JSON.parse)
.then(getDependencies)
.then(npmInstall)
.then(createTesselinclude)
.then(generateSample)
.catch(function(error) {
console.error(error);
Expand Down
53 changes: 27 additions & 26 deletions lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,26 +731,6 @@ actions.tarBundle = function(opts) {

var includeRules = actions.glob.rules(target, '.tesselinclude');

// assuming a programmer could miss to add .tesselinclude
// if there are project folders
try {
fs.statSync(globRoot+'/.tesselinclude');
} catch (e) {
var dirList = fs.readdirSync(globRoot);
var list = [];
dirList.forEach(function(element) {
if (element !== 'node_modules' && fs.lstatSync(globRoot+'/'+element).isDirectory()) {
// blame for directories because is seams the programmer
// forgot to create .tesselinclude and add it into it
list.push(element);
}
});
if (list.length > 0) {
logs.warn('Ignored folders due to missing .tesselinclude:');
list.forEach(list => console.log(list));
}
}

// Convert `deployLists.includes` into includeRules
deployLists.includes.forEach(include => includeRules.push(`node_modules/**/${include}`));

Expand Down Expand Up @@ -780,13 +760,32 @@ actions.tarBundle = function(opts) {
});
});

logs.info('Building project.');


// Both the --slim and --full paths will use a copy of the
// project to bundle. This allows us to be destructive
// with the files, but without directly tampering with the
// project files themselves.
var tempBundleDir = fsTemp.mkdirSync();
var common = ['node_modules', 'package.json', opts.resolvedEntryPoint];
var assets = fs.readdirSync(target).filter(entry => (common.indexOf(entry) === -1));

logs.info('Building project.');
if (assets.length) {
logs.warn(
tags.stripIndent `
The following items were found in your project directory, but not deployed.
${assets.join('\n')}
If you need to deploy them to your Tessel 2, you must have a .tesselinclude
file that lists the file and directory assets for your application. This can be
created manually or by typing 't2 init'.
For more information, visit: https://tessel.io/docs/cli#starting-projects
`
);
}

if (opts.slim) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -957,9 +956,10 @@ actions.tarBundle = function(opts) {
};

actions.runScript = function(t, filepath, opts) {
logs.info('Running %s...', opts.entryPoint);
logs.info('Running %s...', opts.resolvedEntryPoint);

return new Promise(function(resolve, reject) {
t.connection.exec(commands.runScript(Tessel.REMOTE_RUN_PATH, opts.entryPoint), {
t.connection.exec(commands.runScript(Tessel.REMOTE_RUN_PATH, opts.resolvedEntryPoint), {
pty: true
}, function(err, remoteProcess) {
if (err) {
Expand All @@ -978,10 +978,10 @@ actions.runScript = function(t, filepath, opts) {

actions.pushScript = function(t, script, opts) {
// Write the node start file
return actions.writeToFile(t, opts.entryPoint)
return actions.writeToFile(t, opts.resolvedEntryPoint)
.then(function start() {
// Then start the script
return actions.startPushedScript(t, opts.entryPoint);
return actions.startPushedScript(t, opts.resolvedEntryPoint);
});
};

Expand All @@ -1004,7 +1004,7 @@ actions.writeToFile = function(t, entryPoint) {
// When that process completes
remoteProcess.once('close', function() {
// Let the user know
logs.info('You may now disconnect from the Tessel. Your code will be run whenever Tessel boots up. To remove this code, use `tessel erase`.');
logs.info('You may now disconnect from the Tessel. Your code will be run whenever Tessel boots up. To remove this code, use `t2 erase`.');
return resolve();
});
});
Expand All @@ -1014,6 +1014,7 @@ actions.writeToFile = function(t, entryPoint) {
#!/bin/sh
exec node /app/remote-script/${entryPoint}
`;

remoteProcess.stdin.end(new Buffer(shellScript.trim()));
});
});
Expand Down
6 changes: 6 additions & 0 deletions resources/.tesselinclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file should list any files or directories you want included with the
# T2 bundling and deployment process. This is handy for including
# non-JavaScript assets, like HTML, CSS, and images, for use within your project.
# .tesselinclude works the same as .npminclude
# You DO NOT need to list node_modules or package.json
# For more information, visit: https://tessel.io/docs/cli#usage
63 changes: 63 additions & 0 deletions test/unit/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ exports['deploy.tarBundle'] = {
this.outputFileSync = sandbox.spy(fs, 'outputFileSync');
this.writeFileSync = sandbox.spy(fs, 'writeFileSync');
this.remove = sandbox.spy(fs, 'remove');
this.readdirSync = sandbox.spy(fs, 'readdirSync');

this.globSync = sandbox.spy(deploy.glob, 'sync');
this.exclude = sandbox.spy(Project.prototype, 'exclude');
Expand Down Expand Up @@ -1590,6 +1591,68 @@ exports['deploy.tarBundle'] = {
});
});
},

bothDetectAssetsWithoutInclude: function(test) {
test.expect(10);

var entryPoint = 'index.js';
var target = 'test/unit/fixtures/project-assets-without-include';

/*
project-assets-without-include
├── index.js
├── mock-foo.js
├── nested
│   └── another.js
├── node_modules
│   └── foo
│   ├── index.js
│   └── package.json
├── other.js
└── package.json
3 directories, 7 files
*/


Promise.all([
deploy.tarBundle({
target: path.normalize(target),
resolvedEntryPoint: entryPoint,
slim: true,
}),
deploy.tarBundle({
target: path.normalize(target),
resolvedEntryPoint: entryPoint,
}),
]).then(() => {

test.equal(this.readdirSync.callCount, 2);
test.equal(this.readdirSync.firstCall.args[0], target);
test.equal(this.readdirSync.lastCall.args[0], target);

// In both cases, logs.warn should've been called with the
// warning message and a list of the present, but unnaccounted for
// asset files and directories.

test.equal(this.logsWarn.callCount, 2);

[
'mock-foo.js',
'nested',
'other.js',
].forEach(asset => {
test.equal(this.logsWarn.firstCall.args[0].includes(asset), true);
test.equal(this.logsWarn.lastCall.args[0].includes(asset), true);
});

this.logsWarn.firstCall.args[0].includes('The following items were found in your project directory, but not deployed.');
this.logsWarn.firstCall.args[0].includes('If you need to deploy them to your Tessel 2, you must have a .tesselinclude\n file that lists the file and directory assets for your application. This can be\n created manually or by typing \'t2 init\'.');
this.logsWarn.firstCall.args[0].includes('For more information, visit: https://tessel.io/docs/cli#starting-projects');

test.done();
});
},
};

exports['Tessel.prototype.restartScript'] = {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/fixtures/project-assets-without-include/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var path = require('path');
var fs = require('fs');
var foo = true ? require('foo') : require('./mock-foo');

console.log(foo());

fs.readFile(path.normalize('node_modules/foo/package.json'), 'utf8', function(error, contents) {
if (error) {
process.exit(1);
}
console.log(contents);
process.exit(0);
});
3 changes: 3 additions & 0 deletions test/unit/fixtures/project-assets-without-include/mock-foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function() {
return '!!mock foo!! <-- this string must not appear in bundle.js';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('testing deploy');

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/unit/fixtures/project-assets-without-include/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('testing deploy');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "project-include-without-ignore",
"version": "0.0.1",
"description": "project-include-without-ignore",
"main": "index.js"
}

0 comments on commit 4b3f72f

Please sign in to comment.