Automate resin init command

This commit is contained in:
Juan Cruz Viotti 2015-03-09 09:14:39 -04:00
parent af08a2f306
commit 6ba97cd961
10 changed files with 144 additions and 32 deletions

View File

@ -1,5 +1,7 @@
(function() { (function() {
var _, async, commandOptions, resin, visuals; var _, async, commandOptions, git, path, resin, visuals;
path = require('path');
_ = require('lodash-contrib'); _ = require('lodash-contrib');
@ -11,6 +13,8 @@
commandOptions = require('./command-options'); commandOptions = require('./command-options');
git = require('../git');
exports.create = { exports.create = {
signature: 'app create <name>', signature: 'app create <name>',
description: 'create an application', description: 'create an application',
@ -98,28 +102,58 @@
} }
}; };
exports.init = { exports.associate = {
signature: 'init <id>', signature: 'app associate <id>',
description: 'init an application', description: 'associate a resin project',
help: 'Use this command to associate a local project to an existing resin.io application.\n\nThe application should be a git repository before issuing this command.\nNotice this command adds a `resin` git remote to your application.\n\nExamples:\n\n $ cd myApp && resin init 91', help: 'Use this command to associate a project directory with a resin application.\n\nThis command adds a \'resin\' git remote to the directory and runs git init if necessary.\n\nExamples:\n\n $ resin app associate 91\n $ resin app associate 91 --project my/app/directory',
permission: 'user', permission: 'user',
action: function(params, options, done) { action: function(params, options, done) {
var currentDirectory; var currentDirectory;
currentDirectory = process.cwd(); currentDirectory = process.cwd();
return async.waterfall([ return async.waterfall([
function(callback) { function(callback) {
return resin.vcs.isResinProject(currentDirectory, callback); return git.isGitDirectory(currentDirectory, callback);
}, function(isResinProject, callback) { }, function(isGitDirectory, callback) {
var error; if (isGitDirectory) {
if (isResinProject) { return callback();
error = new Error('Project is already a resin application.');
return callback(error);
} }
return callback(); return git.execute('init', currentDirectory, _.unary(callback));
}, function(callback) { }, function(callback) {
return resin.models.application.get(params.id, callback); return resin.models.application.get(params.id, callback);
}, function(application, callback) { }, function(application, callback) {
return resin.vcs.initProjectWithApplication(application, currentDirectory, callback); return git.execute("remote add resin " + application.git_repository, currentDirectory, function(error) {
if (error != null) {
return callback(error);
}
console.info("git repository added: " + application.git_repository);
return callback(null, application.git_repository);
});
}
], done);
}
};
exports.init = {
signature: 'init',
description: 'init an application',
help: 'Use this command to initialise a directory as a resin application.\n\nThis command performs the following steps:\n - Create a resin.io application.\n - Initialize the current directory as a git repository.\n - Add the corresponding git remote to the application.\n\nExamples:\n\n $ resin init\n $ resin init --project my/app/directory',
permission: 'user',
action: function(params, options, done) {
var currentDirectory;
currentDirectory = process.cwd();
return async.waterfall([
function(callback) {
var currentDirectoryBasename;
currentDirectoryBasename = path.basename(currentDirectory);
return visuals.widgets.ask('What is the name of your application?', currentDirectoryBasename, callback);
}, function(applicationName, callback) {
return exports.create.action({
name: applicationName
}, options, callback);
}, function(applicationId, callback) {
return exports.associate.action({
id: applicationId
}, options, callback);
} }
], done); ], done);
} }

View File

@ -82,7 +82,7 @@
if (!_.isEmpty(params.name)) { if (!_.isEmpty(params.name)) {
return callback(null, params.name); return callback(null, params.name);
} }
return visuals.widgets.ask('How do you want to name this device?', callback); return visuals.widgets.ask('How do you want to name this device?', null, callback);
}, function(name, callback) { }, function(name, callback) {
return resin.models.device.rename(params.id, name, callback); return resin.models.device.rename(params.id, name, callback);
} }

View File

@ -77,6 +77,8 @@
capitano.command(actions.app.restart); capitano.command(actions.app.restart);
capitano.command(actions.app.associate);
capitano.command(actions.app.init); capitano.command(actions.app.init);
capitano.command(actions.device.list); capitano.command(actions.device.list);

11
build/git-example.js Normal file
View File

@ -0,0 +1,11 @@
(function() {
var git;
git = require('./git');
git.execute('status', process.cwd(), function(error, stdout, stderr) {
console.log(arguments);
return console.log(stdout);
});
}).call(this);

18
build/git.js Normal file
View File

@ -0,0 +1,18 @@
(function() {
var child_process;
child_process = require('child_process');
exports.isGitDirectory = function(directory, callback) {
return exports.execute('status', directory, function(error, stdout, stderr) {
return callback(null, error == null);
});
};
exports.execute = function(command, cwd, callback) {
return child_process.exec("git " + command, {
cwd: cwd
}, callback);
};
}).call(this);

View File

@ -1,8 +1,10 @@
path = require('path')
_ = require('lodash-contrib') _ = require('lodash-contrib')
async = require('async') async = require('async')
resin = require('resin-sdk') resin = require('resin-sdk')
visuals = require('resin-cli-visuals') visuals = require('resin-cli-visuals')
commandOptions = require('./command-options') commandOptions = require('./command-options')
git = require('../git')
exports.create = exports.create =
signature: 'app create <name>' signature: 'app create <name>'
@ -130,38 +132,74 @@ exports.remove =
resin.models.application.remove(params.id, callback) resin.models.application.remove(params.id, callback)
, done , done
exports.init = exports.associate =
signature: 'init <id>' signature: 'app associate <id>'
description: 'init an application' description: 'associate a resin project'
help: ''' help: '''
Use this command to associate a local project to an existing resin.io application. Use this command to associate a project directory with a resin application.
The application should be a git repository before issuing this command. This command adds a 'resin' git remote to the directory and runs git init if necessary.
Notice this command adds a `resin` git remote to your application.
Examples: Examples:
$ cd myApp && resin init 91 $ resin app associate 91
$ resin app associate 91 --project my/app/directory
''' '''
permission: 'user' permission: 'user'
action: (params, options, done) -> action: (params, options, done) ->
currentDirectory = process.cwd() currentDirectory = process.cwd()
async.waterfall [ async.waterfall([
(callback) -> (callback) ->
resin.vcs.isResinProject(currentDirectory, callback) git.isGitDirectory(currentDirectory, callback)
(isResinProject, callback) -> (isGitDirectory, callback) ->
if isResinProject return callback() if isGitDirectory
error = new Error('Project is already a resin application.') git.execute('init', currentDirectory, _.unary(callback))
return callback(error)
return callback()
(callback) -> (callback) ->
resin.models.application.get(params.id, callback) resin.models.application.get(params.id, callback)
(application, callback) -> (application, callback) ->
resin.vcs.initProjectWithApplication(application, currentDirectory, callback) git.execute "remote add resin #{application.git_repository}", currentDirectory, (error) ->
return callback(error) if error?
console.info("git repository added: #{application.git_repository}")
return callback(null, application.git_repository)
], done ], done)
exports.init =
signature: 'init'
description: 'init an application'
help: '''
Use this command to initialise a directory as a resin application.
This command performs the following steps:
- Create a resin.io application.
- Initialize the current directory as a git repository.
- Add the corresponding git remote to the application.
Examples:
$ resin init
$ resin init --project my/app/directory
'''
permission: 'user'
action: (params, options, done) ->
currentDirectory = process.cwd()
async.waterfall([
(callback) ->
currentDirectoryBasename = path.basename(currentDirectory)
visuals.widgets.ask('What is the name of your application?', currentDirectoryBasename, callback)
(applicationName, callback) ->
exports.create.action(name: applicationName, options, callback)
(applicationId, callback) ->
exports.associate.action(id: applicationId, options, callback)
], done)

View File

@ -122,7 +122,7 @@ exports.rename =
(callback) -> (callback) ->
if not _.isEmpty(params.name) if not _.isEmpty(params.name)
return callback(null, params.name) return callback(null, params.name)
visuals.widgets.ask('How do you want to name this device?', callback) visuals.widgets.ask('How do you want to name this device?', null, callback)
(name, callback) -> (name, callback) ->
resin.models.device.rename(params.id, name, callback) resin.models.device.rename(params.id, name, callback)

View File

@ -57,6 +57,7 @@ capitano.command(actions.app.list)
capitano.command(actions.app.info) capitano.command(actions.app.info)
capitano.command(actions.app.remove) capitano.command(actions.app.remove)
capitano.command(actions.app.restart) capitano.command(actions.app.restart)
capitano.command(actions.app.associate)
capitano.command(actions.app.init) capitano.command(actions.app.init)
# ---------- Device Module ---------- # ---------- Device Module ----------

8
lib/git.coffee Normal file
View File

@ -0,0 +1,8 @@
child_process = require('child_process')
exports.isGitDirectory = (directory, callback) ->
exports.execute 'status', directory, (error, stdout, stderr) ->
return callback(null, not error?)
exports.execute = (command, cwd, callback) ->
child_process.exec("git #{command}", { cwd }, callback)

View File

@ -61,7 +61,7 @@
"npm": "^2.6.1", "npm": "^2.6.1",
"open": "0.0.5", "open": "0.0.5",
"progress-stream": "^0.5.0", "progress-stream": "^0.5.0",
"resin-cli-visuals": "^0.0.6", "resin-cli-visuals": "^0.0.7",
"resin-sdk": "^0.0.1", "resin-sdk": "^0.0.1",
"underscore.string": "~2.4.0", "underscore.string": "~2.4.0",
"update-notifier": "^0.3.1" "update-notifier": "^0.3.1"