2015-02-26 15:47:56 +00:00
( function ( ) {
2015-04-20 12:56:10 +00:00
var _ , async , examplesData , fs , mkdirp , path , resin , vcs , visuals ;
mkdirp = require ( 'mkdirp' ) ;
2015-02-26 15:47:56 +00:00
async = require ( 'async' ) ;
fs = require ( 'fs' ) ;
path = require ( 'path' ) ;
_ = require ( 'lodash' ) ;
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-03-12 15:24:36 +00:00
vcs = require ( 'resin-vcs' ) ;
2015-03-11 13:03:13 +00:00
2015-02-26 15:47:56 +00:00
examplesData = require ( '../data/examples.json' ) ;
exports . list = {
signature : 'examples' ,
description : 'list all example applications' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to list available example applications from resin.io\n\nExample:\n\n $ resin examples' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( ) {
examplesData = _ . map ( examplesData , function ( example , index ) {
example . id = index + 1 ;
return example ;
} ) ;
examplesData = _ . map ( examplesData , function ( example ) {
if ( example . author == null ) {
example . author = 'Unknown' ;
}
return example ;
} ) ;
2015-05-21 15:58:15 +00:00
return console . log ( visuals . widgets . table . horizontal ( examplesData , [ 'id' , 'name' , 'display_name' , 'repository' , 'author' ] ) ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . info = {
2015-05-21 15:58:15 +00:00
signature : 'example <name>' ,
2015-02-26 15:47:56 +00:00
description : 'list a single example application' ,
2015-05-21 15:58:15 +00:00
help : 'Use this command to show information of a single example application\n\nExample:\n\n $ resin example cimon' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-05-21 15:58:15 +00:00
var example ;
example = _ . findWhere ( examplesData , {
name : params . name
} ) ;
2015-02-26 15:47:56 +00:00
if ( example == null ) {
2015-05-21 15:58:15 +00:00
return done ( new Error ( "Unknown example: " + params . name ) ) ;
2015-02-26 15:47:56 +00:00
}
if ( example . author == null ) {
example . author = 'Unknown' ;
}
2015-05-21 15:58:15 +00:00
console . log ( visuals . widgets . table . vertical ( example , [ 'name' , 'display_name' , 'description' , 'author' , 'repository' ] ) ) ;
2015-02-26 15:47:56 +00:00
return done ( ) ;
}
} ;
exports . clone = {
2015-05-21 15:58:15 +00:00
signature : 'example clone <name>' ,
2015-02-26 15:47:56 +00:00
description : 'clone an example application' ,
2015-05-21 15:58:15 +00:00
help : 'Use this command to clone an example application to the current directory\n\nThis command outputs information about the cloning process.\nUse `--quiet` to remove that output.\n\nExample:\n\n $ resin example clone cimon' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-04-20 12:56:10 +00:00
var currentDirectory , destination , example ;
2015-05-21 15:58:15 +00:00
example = _ . findWhere ( examplesData , {
name : params . name
} ) ;
2015-02-26 15:47:56 +00:00
if ( example == null ) {
2015-05-21 15:58:15 +00:00
return done ( new Error ( "Unknown example: " + params . name ) ) ;
2015-02-26 15:47:56 +00:00
}
2015-03-12 15:24:36 +00:00
currentDirectory = process . cwd ( ) ;
2015-04-20 12:56:10 +00:00
destination = path . join ( currentDirectory , example . name ) ;
return mkdirp ( destination , function ( error ) {
if ( error != null ) {
return done ( error ) ;
}
console . info ( "Cloning " + example . display _name + " to " + destination ) ;
vcs . clone ( example . repository , destination , done ) ;
return done ( ) ;
} ) ;
2015-02-26 15:47:56 +00:00
}
} ;
} ) . call ( this ) ;