1+ #!/usr/bin/env node
2+
3+ var Rsync = require ( 'rsync' )
4+ var git = require ( 'git-rev-sync' )
5+ var yesno = require ( 'yesno' )
6+ var readPkgUp = require ( 'read-pkg-up' )
7+
8+ ; ( async ( ) => {
9+ var config = await readPkgUp ( )
10+
11+ // Extract rploy options
12+ try {
13+ var options = config . packageJson . rploy
14+ } catch ( e ) { }
15+
16+ // Bail when no options
17+ if ( typeof options !== 'object' ) {
18+ console . error ( '⚠️ No `rploy` options found in package.json' )
19+ return
20+ }
21+
22+ // Handle branches option
23+ if ( 'branches' in options && typeof options . branches === 'object' ) {
24+ try {
25+ var branch = git . branch ( process . cwd ( ) )
26+ var destination = options . branches [ branch ]
27+ if ( destination ) {
28+ if ( typeof destination === 'object' ) {
29+ options = Object . assign ( options , destination )
30+ } else {
31+ options . destination = destination
32+ }
33+ delete options . branches
34+ console . log ( `⚙️ Deploy branch "${ branch } " to ${ destination } ` )
35+ } else {
36+ console . error ( `⚠️ No remote path defined for the current branch "${ branch } "` )
37+ return
38+ }
39+ } catch ( e ) {
40+ console . error ( '⚠️ `rploy` is configured with the `branches` option, but this is not a git repository.' )
41+ return
42+ }
43+ }
44+
45+ // Merge options with defaults
46+ options = Object . assign ( {
47+ flags : 'avC' ,
48+ shell : 'ssh' ,
49+ delete : true
50+ } , options )
51+
52+ // Dry-run
53+ console . log ( '📤 Review outgoing changes:' )
54+ var dryrun = await new Promise ( ( resolve , reject ) => {
55+ Rsync
56+ . build ( options )
57+ . dry ( )
58+ . execute ( ( error , code , cmd ) => {
59+ resolve ( )
60+ } , data => {
61+ console . log ( data . toString ( 'utf-8' ) . trim ( ) )
62+ } , data => {
63+ console . log ( data . toString ( 'utf-8' ) . trim ( ) )
64+ } )
65+ } )
66+
67+ // Yes/No
68+ var proceed = await yesno ( {
69+ question : '🚚 Deploy? (y/n)'
70+ } )
71+
72+ // Deploy
73+ if ( proceed ) {
74+ var deploy = await new Promise ( ( resolve , reject ) => {
75+ Rsync
76+ . build ( options )
77+ . execute ( ( error , code , cmd ) => {
78+ resolve ( )
79+ } , data => {
80+ console . log ( data . toString ( 'utf-8' ) . trim ( ) )
81+ } , data => {
82+ console . log ( data . toString ( 'utf-8' ) . trim ( ) )
83+ } )
84+ } )
85+ }
86+
87+ return
88+
89+ } ) ( )
0 commit comments