Skip to content

Commit

Permalink
t2 init: broad cleanup (#779)
Browse files Browse the repository at this point in the history
* t2 init: broad cleanup

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* t2 init: more tests for --lang=javascript

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* t2 init: per review: not necessary to explicitly coerce lang input to string

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* t2 init: init.rs.createSampleProgram use fs.copy

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* t2 init: special *Error classes are not exported.

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* t2 init: reuse rust resource paths

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* t2 init: cleanup all resource paths

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jun 25, 2016
1 parent ec9eee2 commit f6bb140
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 200 deletions.
42 changes: 3 additions & 39 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ const updateNotifier = require('update-notifier');
const isRoot = require('is-root');

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

const CLI_ENTRYPOINT = 'cli.entrypoint';
Expand Down Expand Up @@ -41,40 +39,6 @@ if (!isRoot()) {
}
}

controller.t2Init = init.initProject;

controller.crashReporter = function(options) {
var cr = Promise.resolve();

// t2 crash-reporter --on
if (options.on) {
cr = CrashReporter.on();
} else if (options.off) {
// t2 crash-reporter --off
cr = CrashReporter.off();
}

// t2 crash-reporter --test
if (options.test) {
// not handling failures, as we want to trigger a crash
cr.then(CrashReporter.test)
.then(module.exports.closeSuccessfulCommand);
} else {
cr.then(CrashReporter.status)
.then(
module.exports.closeSuccessfulCommand,
module.exports.closeFailedCommand
);
}

return cr;
};


controller.installDrivers = function() {
return drivers.install();
};

function makeCommand(commandName) {
return parser.command(commandName)
.option('timeout', {
Expand Down Expand Up @@ -323,7 +287,7 @@ parser.command('init')
.callback(options => {
log.level(options.loglevel);

callControllerWith('t2Init', options);
callControllerWith('createNewProject', options);
})
.option('interactive', {
flag: true,
Expand All @@ -334,7 +298,7 @@ parser.command('init')
metavar: 'LANG',
abbr: 'l',
default: 'js',
help: 'The language to use <javascript|rust|js|rs>. Javascript by default'
help: 'The language to use <javascript|rust|js|rs>. JavaScript by default'
})
.help('Initialize repository for your Tessel project');

Expand Down
38 changes: 38 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ var colors = require('colors');
var semver = require('semver');

// Internal
var CrashReporter = require('./crash-reporter');
var discover = require('./discover');
var drivers = require('../bin/tessel-install-drivers');
var init = require('./init');
var log = require('./log');
var Menu = require('./menu');
var updates = require('./update-fetch');
Expand Down Expand Up @@ -980,6 +983,41 @@ controller.tesselEnvVersions = opts => {
});
};

controller.createNewProject = init.createNewProject;

controller.crashReporter = function(options) {
var cr = Promise.resolve();

// t2 crash-reporter --on
if (options.on) {
cr = CrashReporter.on();
} else if (options.off) {
// t2 crash-reporter --off
cr = CrashReporter.off();
}

// t2 crash-reporter --test
if (options.test) {
// not handling failures, as we want to trigger a crash
cr.then(CrashReporter.test)
.then(module.exports.closeSuccessfulCommand);
} else {
cr.then(CrashReporter.status)
.then(
module.exports.closeSuccessfulCommand,
module.exports.closeFailedCommand
);
}

return cr;
};

controller.installDrivers = function() {
return drivers.install();
};



module.exports = controller;

// Shared exports
Expand Down
32 changes: 18 additions & 14 deletions lib/init/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// System Objects
var path = require('path');

// Third Party Dependencies

// Internal
Expand All @@ -12,42 +13,45 @@ var languages = {
var exportables = {};

// Initialize the directory given the various options
exportables.initProject = (opts) => {

exportables.createNewProject = (options) => {
// Set a default directory if one was not provided
opts.directory = opts.directory || path.resolve('.');
options.directory = options.directory || path.resolve('.');

// Detect the requested language
var lang = exportables.resolveLanguage(opts);
var lang = exportables.resolveLanguage(options.lang);

// If a language could not be detected
if (lang === null) {
// Return an error
return Promise.reject(new Error('Unrecognized language selection.'));
} else {
// Otherwise generate a project in that language
return lang.generateProject(opts);
return lang.generateProject(options);
}
};

// Determine the langauge to initialize the project with
exportables.resolveLanguage = (opts) => {
exportables.resolveLanguage = (input) => {

// If somehow a language option wasn't provided
if (!opts.lang) {
// Return JS as default
return languages['js'];
if (!input) {
return languages.js;
}

input = input.toLowerCase();


// If the shorthand "extension" name was used...
if (languages[input]) {
return languages[input];
}

// Iterate through each of the langauges
// ...Otherwise look in the languages[ext].meta.keywords list.
for (var key in languages) {
// Pull out the language info
var lang = languages[key];

// Check if the language option is within the available language keywords
if (lang.meta.keywords &&
lang.meta.keywords.indexOf(opts.lang.toLowerCase()) > -1) {
// If it is, return that language
lang.meta.keywords.indexOf(input) !== -1) {
return lang;
}
}
Expand Down
67 changes: 33 additions & 34 deletions lib/init/javascript.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// System Objects
var path = require('path');
var fs = require('fs-extra');

// Third Party Dependencies
var fs = require('fs-extra');
var PZ = require('promzard').PromZard;
var NPM = require('npm');

// Internal
var log = require('../log');

var packageJson = path.resolve('./package.json');

var pkg, ctx, options;

var packageJson = path.resolve('./package.json');
var resources = path.resolve(__dirname, './../../', 'resources/javascript');
var exportables = {};

exportables.meta = {
Expand All @@ -21,8 +20,8 @@ exportables.meta = {

exportables.loadNpm = () => {
// You have to load npm in order to use it programatically
return new Promise(function(resolve, reject) {
NPM.load(function(error, npm) {
return new Promise((resolve, reject) => {
NPM.load((error, npm) => {
if (error) {
return reject(error);
}
Expand All @@ -33,28 +32,28 @@ exportables.loadNpm = () => {
};

// Resolve an npm cofig list, or nothing (existance is not needed)
exportables.getNpmConfig = (npm) => {
exportables.resolveNpmConfig = (npm) => {
// Always resolve, we don't care if there isn't an npm config.
return Promise.resolve(npm.config.list || {});
};

// Builds the package.json file and writes it to the directory
exportables.buildJSON = (npmConfig) => {
return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
// Path to promzard config file
var promzardConfig;
ctx.config = npmConfig;
// Default to auto config
promzardConfig = path.resolve(__dirname, './../../', 'resources/javascript/init-default.js');
promzardConfig = path.join(resources, 'init-default.js');
if (options.interactive) {
promzardConfig = path.resolve(__dirname, './../../', 'resources/javascript/init-config.js');
promzardConfig = path.join(resources, 'init-config.js');
}

// Init promozard with appropriate config.
var pz = new PZ(promzardConfig, ctx);

// On data resolve the promise with data
pz.on('data', function(data) {
pz.on('data', (data) => {
if (!pkg) {
pkg = {};
}
Expand All @@ -69,7 +68,7 @@ exportables.buildJSON = (npmConfig) => {
});

// On error, reject with error;
pz.on('error', function(error) {
pz.on('error', (error) => {
reject(error);
});
});
Expand Down Expand Up @@ -99,7 +98,7 @@ exportables.npmInstall = (dependencies) => {
// load npm to get the npm object
exportables.loadNpm()
.then((npm) => {
npm.commands.install(dependencies, function(error) {
npm.commands.install(dependencies, (error) => {
if (error) {
reject(error);
}
Expand All @@ -109,23 +108,22 @@ exportables.npmInstall = (dependencies) => {
});
};

// // Generates blinky for JavaScript
exportables.generateJavaScriptSample = () => {
// Generates blinky for JavaScript
exportables.createSampleProgram = () => {
return new Promise((resolve) => {
var filename = 'index.js';

// If an index.js already exists
fs.exists(filename, function(exists) {
fs.exists(filename, (exists) => {
// just return
if (exists) {
return resolve();
}

// Pipe the contents of the default file into a new file
fs.createReadStream(path.resolve(__dirname, './../../', 'resources/javascript', filename))
.pipe(fs.createWriteStream(filename));

log.info('Wrote "Hello World" to index.js');
return resolve();
fs.copy(path.join(resources, filename), filename, () => {
log.info('Wrote "Hello World" to index.js');
resolve();
});
});
});
};
Expand All @@ -137,17 +135,18 @@ exportables.createTesselinclude = () => {
if (exists) {
return resolve();
}
fs.copy(path.join(resources, tesselinclude), tesselinclude, () => {
log.info('Created .tesselinclude.');
resolve();

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

exportables.readPackageJson = () => {
return new Promise(function(resolve, reject) {
fs.readFile(packageJson, 'utf8', function(err, data) {
return new Promise((resolve, reject) => {
fs.readFile(packageJson, 'utf8', (err, data) => {
if (err) {
return reject(err);
}
Expand All @@ -158,8 +157,8 @@ exportables.readPackageJson = () => {
};

exportables.writePackageJson = (data) => {
return new Promise(function(resolve, reject) {
fs.writeFile(packageJson, data, function(err) {
return new Promise((resolve, reject) => {
fs.writeFile(packageJson, data, (err) => {
if (err) {
return reject(err);
}
Expand All @@ -179,7 +178,7 @@ exportables.generateProject = (opts) => {

log.info('Initializing new Tessel project for JavaScript...');
return exportables.readPackageJson()
.then(function(data) {
.then((data) => {

// Try to parse current package JSON
try {
Expand All @@ -197,15 +196,15 @@ exportables.generateProject = (opts) => {
}
return ctx;
})
.catch(function() {
.catch(() => {
ctx = {};
ctx.dirname = path.dirname(packageJson);
ctx.basename = path.basename(ctx.dirname);
ctx.version = undefined;
return ctx;
})
.then(exportables.loadNpm)
.then(exportables.getNpmConfig)
.then(exportables.resolveNpmConfig)
.then(exportables.buildJSON)
.then(exportables.prettyPrintJson)
.then(exportables.writePackageJson)
Expand All @@ -214,8 +213,8 @@ exportables.generateProject = (opts) => {
.then(exportables.getDependencies)
.then(exportables.npmInstall)
.then(exportables.createTesselinclude)
.then(exportables.generateJavaScriptSample)
.catch(function(error) {
.then(exportables.createSampleProgram)
.catch(error => {
log.error(error);
});
};
Expand Down

0 comments on commit f6bb140

Please sign in to comment.