mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-21 14:37:47 +00:00
Automate resin init command
This commit is contained in:
parent
af08a2f306
commit
6ba97cd961
@ -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 <name>',
|
||||
description: 'create an application',
|
||||
@ -98,28 +102,58 @@
|
||||
}
|
||||
};
|
||||
|
||||
exports.init = {
|
||||
signature: 'init <id>',
|
||||
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 <id>',
|
||||
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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -77,6 +77,8 @@
|
||||
|
||||
capitano.command(actions.app.restart);
|
||||
|
||||
capitano.command(actions.app.associate);
|
||||
|
||||
capitano.command(actions.app.init);
|
||||
|
||||
capitano.command(actions.device.list);
|
||||
|
11
build/git-example.js
Normal file
11
build/git-example.js
Normal 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
18
build/git.js
Normal 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);
|
@ -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 <name>'
|
||||
@ -130,38 +132,74 @@ exports.remove =
|
||||
resin.models.application.remove(params.id, callback)
|
||||
, done
|
||||
|
||||
exports.init =
|
||||
signature: 'init <id>'
|
||||
description: 'init an application'
|
||||
exports.associate =
|
||||
signature: 'app associate <id>'
|
||||
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)
|
||||
|
@ -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)
|
||||
|
@ -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 ----------
|
||||
|
8
lib/git.coffee
Normal file
8
lib/git.coffee
Normal 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)
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user