From 6ba97cd9617b0ece022d16cb3c6c0ad13fe30b20 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 9 Mar 2015 09:14:39 -0400 Subject: [PATCH] Automate resin init command --- build/actions/app.js | 60 +++++++++++++++++++++++++-------- build/actions/device.js | 2 +- build/app.js | 2 ++ build/git-example.js | 11 ++++++ build/git.js | 18 ++++++++++ lib/actions/app.coffee | 70 ++++++++++++++++++++++++++++++--------- lib/actions/device.coffee | 2 +- lib/app.coffee | 1 + lib/git.coffee | 8 +++++ package.json | 2 +- 10 files changed, 144 insertions(+), 32 deletions(-) create mode 100644 build/git-example.js create mode 100644 build/git.js create mode 100644 lib/git.coffee diff --git a/build/actions/app.js b/build/actions/app.js index 35c9d2d9..eab96693 100644 --- a/build/actions/app.js +++ b/build/actions/app.js @@ -1,5 +1,7 @@ (function() { - var _, async, commandOptions, resin, visuals; + var _, async, commandOptions, git, path, resin, visuals; + + path = require('path'); _ = require('lodash-contrib'); @@ -11,6 +13,8 @@ commandOptions = require('./command-options'); + git = require('../git'); + exports.create = { signature: 'app create ', description: 'create an application', @@ -98,28 +102,58 @@ } }; - exports.init = { - signature: 'init ', - description: 'init an application', - 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', + exports.associate = { + signature: 'app associate ', + description: 'associate a resin project', + 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', action: function(params, options, done) { var currentDirectory; currentDirectory = process.cwd(); return async.waterfall([ function(callback) { - return resin.vcs.isResinProject(currentDirectory, callback); - }, function(isResinProject, callback) { - var error; - if (isResinProject) { - error = new Error('Project is already a resin application.'); - return callback(error); + return git.isGitDirectory(currentDirectory, callback); + }, function(isGitDirectory, callback) { + if (isGitDirectory) { + return callback(); } - return callback(); + return git.execute('init', currentDirectory, _.unary(callback)); }, function(callback) { return resin.models.application.get(params.id, 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); } diff --git a/build/actions/device.js b/build/actions/device.js index 730ed31d..f4ef099b 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -82,7 +82,7 @@ if (!_.isEmpty(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) { return resin.models.device.rename(params.id, name, callback); } diff --git a/build/app.js b/build/app.js index 330ef582..37999461 100644 --- a/build/app.js +++ b/build/app.js @@ -77,6 +77,8 @@ capitano.command(actions.app.restart); + capitano.command(actions.app.associate); + capitano.command(actions.app.init); capitano.command(actions.device.list); diff --git a/build/git-example.js b/build/git-example.js new file mode 100644 index 00000000..04afc31b --- /dev/null +++ b/build/git-example.js @@ -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); diff --git a/build/git.js b/build/git.js new file mode 100644 index 00000000..ace7e877 --- /dev/null +++ b/build/git.js @@ -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); diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index 747a34db..e27f1068 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -1,8 +1,10 @@ +path = require('path') _ = require('lodash-contrib') async = require('async') resin = require('resin-sdk') visuals = require('resin-cli-visuals') commandOptions = require('./command-options') +git = require('../git') exports.create = signature: 'app create ' @@ -130,38 +132,74 @@ exports.remove = resin.models.application.remove(params.id, callback) , done -exports.init = - signature: 'init ' - description: 'init an application' +exports.associate = + signature: 'app associate ' + description: 'associate a resin project' 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. - Notice this command adds a `resin` git remote to your application. + This command adds a 'resin' git remote to the directory and runs git init if necessary. Examples: - $ cd myApp && resin init 91 + $ resin app associate 91 + $ resin app associate 91 --project my/app/directory ''' permission: 'user' action: (params, options, done) -> currentDirectory = process.cwd() - async.waterfall [ + async.waterfall([ (callback) -> - resin.vcs.isResinProject(currentDirectory, callback) + git.isGitDirectory(currentDirectory, callback) - (isResinProject, callback) -> - if isResinProject - error = new Error('Project is already a resin application.') - return callback(error) - return callback() + (isGitDirectory, callback) -> + return callback() if isGitDirectory + git.execute('init', currentDirectory, _.unary(callback)) (callback) -> resin.models.application.get(params.id, 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) diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 2ba29cdf..dd7fbb27 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -122,7 +122,7 @@ exports.rename = (callback) -> if not _.isEmpty(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) -> resin.models.device.rename(params.id, name, callback) diff --git a/lib/app.coffee b/lib/app.coffee index c9e4b563..c6ce437a 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -57,6 +57,7 @@ capitano.command(actions.app.list) capitano.command(actions.app.info) capitano.command(actions.app.remove) capitano.command(actions.app.restart) +capitano.command(actions.app.associate) capitano.command(actions.app.init) # ---------- Device Module ---------- diff --git a/lib/git.coffee b/lib/git.coffee new file mode 100644 index 00000000..fb73fdad --- /dev/null +++ b/lib/git.coffee @@ -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) diff --git a/package.json b/package.json index 99ce08b4..c59a5cca 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "npm": "^2.6.1", "open": "0.0.5", "progress-stream": "^0.5.0", - "resin-cli-visuals": "^0.0.6", + "resin-cli-visuals": "^0.0.7", "resin-sdk": "^0.0.1", "underscore.string": "~2.4.0", "update-notifier": "^0.3.1"