-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// System Objects | ||
var path = require('path'); | ||
// Third Party Dependencies | ||
|
||
// Internal | ||
var languages = { | ||
js: require('./javascript'), | ||
rs: require('./rust'), | ||
py: require('./python'), | ||
}; | ||
|
||
var exportables = {}; | ||
|
||
// Initialize the directory given the various options | ||
exportables.initProject = (opts) => { | ||
|
||
// Set a default directory if one was not provided | ||
opts.directory = opts.directory || path.resolve('.'); | ||
|
||
// Detect the requested language | ||
var lang = exportables.detectLanguage(opts); | ||
|
||
// 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); | ||
} | ||
}; | ||
|
||
// Determine the langauge to initialize the project with | ||
exportables.detectLanguage = (opts) => { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
// If somehow a language option wasn't provided | ||
if (!opts.lang) { | ||
// Return JS as default | ||
return languages['js']; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
// Iterate through each of the langauges | ||
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 | ||
return lang; | ||
} | ||
} | ||
|
||
// If not, someone has requested a language that is not supported | ||
return null; | ||
}; | ||
|
||
module.exports = exportables; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,19 @@ var PZ = require('promzard').PromZard; | |
var NPM = require('npm'); | ||
|
||
// Internal | ||
var log = require('./log'); | ||
|
||
var log = require('../log'); | ||
|
||
var packageJson = path.resolve('./package.json'); | ||
|
||
var pkg, ctx, options; | ||
|
||
function loadNpm() { | ||
var exportables = {}; | ||
|
||
exportables.meta = { | ||
keywords: ['javascript', 'js'] | ||
}; | ||
|
||
exportables.loadNpm = () => { | ||
// You have to load npm in order to use it programatically | ||
return new Promise(function(resolve, reject) { | ||
NPM.load(function(error, npm) { | ||
|
@@ -24,22 +30,24 @@ function loadNpm() { | |
resolve(npm); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function getNpmConfig(npm) { | ||
// Resolve an npm cofig list, or nothing (existance is not needed) | ||
exportables.getNpmConfig = (npm) => { | ||
// Always resolve, we don't care if there isn't an npm config. | ||
return Promise.resolve(npm.config.list || {}); | ||
} | ||
}; | ||
|
||
function buildJSON(npmConfig) { | ||
// Builds the package.json file and writes it to the directory | ||
exportables.buildJSON = (npmConfig) => { | ||
return new Promise(function(resolve, reject) { | ||
// Path to promzard config file | ||
var promzardConfig; | ||
ctx.config = npmConfig; | ||
// Default to auto config | ||
promzardConfig = path.resolve(__dirname + '/../', 'resources/init-default.js'); | ||
promzardConfig = path.resolve(__dirname, './../../', 'resources/javascript/init-default.js'); | ||
if (options.interactive) { | ||
promzardConfig = path.resolve(__dirname + '/../', 'resources/init-config.js'); | ||
promzardConfig = path.resolve(__dirname, './../../', 'resources/javascript/init-config.js'); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
// Init promozard with appropriate config. | ||
|
@@ -55,6 +63,7 @@ function buildJSON(npmConfig) { | |
pkg[k] = data[k]; | ||
} | ||
}); | ||
|
||
log.info('Created package.json.'); | ||
resolve(data); | ||
}); | ||
|
@@ -64,9 +73,10 @@ function buildJSON(npmConfig) { | |
reject(error); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function getDependencies(pkg) { | ||
// Returns the dependencies of the package.json file | ||
exportables.getDependencies = (pkg) => { | ||
if (!pkg.dependencies) { | ||
return []; | ||
} | ||
|
@@ -75,79 +85,67 @@ function getDependencies(pkg) { | |
dependencies.push(mod + '@' + pkg.dependencies[mod]); | ||
} | ||
return dependencies; | ||
} | ||
}; | ||
|
||
// Installs npm and dependencies | ||
exportables.npmInstall = (dependencies) => { | ||
return new Promise((resolve, reject) => { | ||
|
||
function npmInstall(dependencies) { | ||
return new Promise(function(resolve, reject) { | ||
// Install the dependencies | ||
// If there are no depencencies resolve | ||
if (!dependencies.length) { | ||
return resolve(); | ||
} | ||
|
||
// Load npm to get the npm object | ||
loadNpm() | ||
.then(function(npm) { | ||
// load npm to get the npm object | ||
exportables.loadNpm() | ||
.then((npm) => { | ||
npm.commands.install(dependencies, function(error) { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(); | ||
}); | ||
|
||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function generateSample() { | ||
return new Promise(resolve => { | ||
// // Generates blinky for JavaScript | ||
exportables.generateJavaScriptSample = () => { | ||
This comment has been minimized.
Sorry, something went wrong.
rwaldron
Contributor
|
||
return new Promise((resolve) => { | ||
var filename = 'index.js'; | ||
|
||
// If an index.js already exists | ||
fs.exists(filename, function(exists) { | ||
// just return | ||
if (exists) { | ||
return; | ||
} | ||
|
||
// If not and rust was requested | ||
if (options.rust) { | ||
// Place the rust example | ||
filename = 'index.rs'; | ||
} | ||
// If python was requested | ||
if (options.python) { | ||
// Place the python example | ||
filename = 'index.py'; | ||
return resolve(); | ||
} | ||
|
||
// Pipe the contents of the default file into a new file | ||
fs.createReadStream(path.resolve(__dirname + './../resources/' + filename)) | ||
fs.createReadStream(path.resolve(__dirname, './../../', 'resources/javascript', filename)) | ||
.pipe(fs.createWriteStream(filename)); | ||
|
||
log.info('Wrote "Hello World" to index.js'); | ||
resolve(); | ||
return resolve(); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function createTesselinclude() { | ||
exportables.createTesselinclude = () => { | ||
var tesselinclude = '.tesselinclude'; | ||
return new Promise((resolve) => { | ||
fs.exists(tesselinclude, (exists) => { | ||
if (exists) { | ||
return resolve(); | ||
} | ||
|
||
fs.copy(path.resolve(__dirname + './../resources/' + tesselinclude), tesselinclude, () => { | ||
log.info('Created .tesselinclude.'); | ||
resolve(); | ||
}); | ||
fs.copySync(path.resolve(__dirname, './../../', 'resources/javascript', tesselinclude), tesselinclude); | ||
log.info('Created .tesselinclude.'); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function readPackageJson() { | ||
exportables.readPackageJson = () => { | ||
return new Promise(function(resolve, reject) { | ||
fs.readFile(packageJson, 'utf8', function(err, data) { | ||
if (err) { | ||
|
@@ -157,9 +155,9 @@ function readPackageJson() { | |
resolve(data); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function writePackageJson(data) { | ||
exportables.writePackageJson = (data) => { | ||
return new Promise(function(resolve, reject) { | ||
fs.writeFile(packageJson, data, function(err) { | ||
if (err) { | ||
|
@@ -168,19 +166,19 @@ function writePackageJson(data) { | |
resolve(); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function prettyPrintJson(data) { | ||
exportables.prettyPrintJson = (data) => { | ||
return JSON.stringify(data, null, 2); | ||
} | ||
}; | ||
|
||
module.exports = function(opts) { | ||
// Set interactive boolean off of CLI flags | ||
options = opts; | ||
exportables.generateProject = (opts) => { | ||
|
||
log.info('Initializing Tessel 2 Project...'); | ||
// Make the options global | ||
options = opts; | ||
|
||
return readPackageJson() | ||
log.info('Initializing new Tessel project for JavaScript...'); | ||
return exportables.readPackageJson() | ||
.then(function(data) { | ||
|
||
// Try to parse current package JSON | ||
|
@@ -206,19 +204,20 @@ module.exports = function(opts) { | |
ctx.version = undefined; | ||
return ctx; | ||
}) | ||
.then(loadNpm) | ||
.then(getNpmConfig) | ||
.then(buildJSON) | ||
.then(prettyPrintJson) | ||
.then(writePackageJson) | ||
.then(readPackageJson) | ||
.then(exportables.loadNpm) | ||
.then(exportables.getNpmConfig) | ||
.then(exportables.buildJSON) | ||
.then(exportables.prettyPrintJson) | ||
.then(exportables.writePackageJson) | ||
.then(exportables.readPackageJson) | ||
.then(JSON.parse) | ||
.then(getDependencies) | ||
.then(npmInstall) | ||
.then(createTesselinclude) | ||
.then(generateSample) | ||
.then(() => {}) | ||
.catch((error) => { | ||
.then(exportables.getDependencies) | ||
.then(exportables.npmInstall) | ||
.then(exportables.createTesselinclude) | ||
.then(exportables.generateJavaScriptSample) | ||
.catch(function(error) { | ||
log.error(error); | ||
}); | ||
}; | ||
|
||
module.exports = exportables; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// System Objects | ||
|
||
// Third Party Dependencies | ||
|
||
// Internal | ||
var log = require('../log'); | ||
|
||
var exportables = {}; | ||
|
||
exportables.meta = { | ||
keywords: ['py', 'python'] | ||
}; | ||
|
||
exportables.generateProject = () => { | ||
log.info(`Sorry, Python project generation isn't implemented yet. Contributions welcome!`); | ||
}; | ||
|
||
module.exports = exportables; |
2 comments
on commit 6099746
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should've been through some kind of review.
I see that it was reviewed, but I don't think that review was sufficient considering the broad issues that I'm finding throughout this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this
detectLanguage
when there is already precedent forresolveLanguage
?