-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·107 lines (92 loc) · 2.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
var Rsync = require('rsync')
var git = require('git-rev-sync')
var yesno = require('yesno')
var readPkgUp = require('read-pkg-up')
;(async () => {
var config = await readPkgUp();
// Get options from package.json
var options = config.packageJson.rploy;
if(options) {
console.log('Loading rploy options from package.json');
}
// Try to get options from rploy.config.js if not defined in package.json
if (!options) {
try {
var configPath = path.resolve('./rploy.config.js');
if (fs.existsSync(configPath)) {
options = require(configPath);
}
} catch (e) {
console.log(e);
}
}
// Bail when no options
if (typeof options === 'undefined' || options.constructor === Object && Object.entries(options).length === 0) {
console.error('⚠️ No `rploy` options found in either package.json or rploy.config.js')
return
}
// Handle branches option
if ('branches' in options && typeof options.branches === 'object') {
try {
var branch = git.branch(process.cwd())
var destination = options.branches[branch]
if (destination) {
if (typeof destination === 'object') {
options = Object.assign(options, destination)
} else {
options.destination = destination
}
delete options.branches
console.log(`⚙️ Deploy branch "${branch}" to ${destination}`)
} else {
console.error(`⚠️ No remote path defined for the current branch "${branch}"`)
return
}
} catch (e) {
console.error('⚠️ `rploy` is configured with the `branches` option, but this is not a git repository.')
return
}
}
// Merge options with defaults
options = Object.assign({
flags: 'avC',
shell: 'ssh',
delete: true
}, options)
// Dry-run
console.log('📤 Review outgoing changes:')
var dryrun = await new Promise((resolve, reject) => {
Rsync
.build(options)
.dry()
.execute((error, code, cmd) => {
resolve()
}, data => {
console.log(data.toString('utf-8').trim())
}, data => {
console.log(data.toString('utf-8').trim())
})
})
// Yes/No
var proceed = await yesno({
question: '🚚 Deploy? (y/n)'
})
// Deploy
if (proceed) {
var deploy = await new Promise((resolve, reject) => {
Rsync
.build(options)
.execute((error, code, cmd) => {
resolve()
}, data => {
console.log(data.toString('utf-8').trim())
}, data => {
console.log(data.toString('utf-8').trim())
})
})
}
return
})()