-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·214 lines (208 loc) · 6.89 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env node
/**
* credit: https://www.sitepoint.com/javascript-command-line-interface-cli-node-js/
*/
const bfs = require('./lib/fs')
const vorpal = require('vorpal')()
const ora = require('ora')
const colors = require('colors')
const art = require('./lib/art')
/**
* Run initialization stuff
*/
console.log(art.green)
console.log('Please make sure your AEM instance is completely shutdown before using ANY of the CLI commands'.bold.yellow)
/**
* BACKUP
*/
vorpal
.command('backup [backupName]', 'Archive crx-quickstart in current directory and move archive to crx-quickstart.backups folder in current directory')
.alias('b')
.option('-f, --force', 'Force backup overwrite.')
.validate(function (args) {
if (args.backupName) return true
else return '*tsk tsk*, you need to specify a name for the backup'
})
.action(function (args, finishAction) {
const spinner = ora('').start().info('checking if backup already exists')
const name = args.backupName
const exists = bfs.backupExists(name)
const force = args.options.force
let backupSize = 0
// prepare a reusable function to be called when we need to backup
const backItUp = () => {
spinner
.start()
.succeed('One backup coming right up!'.green)
.start('back it up...')
return bfs
.createBackup(name, {
onEntry: (path, stat) => {
if(!spinner.isSpinning) spinner.start();
const size = stat.size ? stat.size : 0
backupSize += size
spinner.text = `[${bfs.formatBytes(backupSize)}]`.magenta + ` Backing up ${path}`.green
}
})
.then(() => {
spinner.succeed(`[${bfs.formatBytes(backupSize)}] All backed up! We have a lift off!`.green)
finishAction()
})
.catch(err => {
spinner.fail('womp womp... some error occured *rolls eyes*'.red)
throw err
})
}
if (exists) {
if (force) { // backup exists and force overwrite. back it up!
spinner
.start()
.info(`it does!, and you've chosen to force overwrite.`)
.start()
.info('Attempting to remove old backup')
bfs
.removeBackup(name)
.then(() => {
spinner
.start()
.succeed('Removed old backup')
})
.catch((err) => {
spinner
.start()
.fail('uh oh... that pesky backup refuses to be removed.'.red)
throw err
})
backItUp()
} else { // backup exists, no overwrite
spinner
.start()
.fail('it does... but we cant ovewrite it. You have to use -f to force overwrite'.red)
finishAction()
}
} else { // backup does not exists, back it up!
spinner
.start()
.succeed('it does not'.green)
backItUp()
}
})
/**
* LIST
*/
vorpal
.command('list', 'List all available backups')
.alias('l')
.action(function (args, actionFinished) {
const backups = bfs.listBackups()
if (backups && backups.length) {
backups.forEach(file => {
this.log(` - ${file.green.bold}`)
})
actionFinished()
} else {
this.log(`No backups found`.bold.red)
actionFinished()
}
})
/**
* DELETE
*/
vorpal
.command('delete', 'Delete a backup from available backups')
.alias('d')
.option('-f, --force', 'Force restore.')
.action(function (args, actionFinished) {
const backups = bfs.listBackups()
if (backups || backups.length > 0) {
this.prompt(
[{
type: 'checkbox',
name: 'backupNames',
default: false,
message: 'Please check the backups you want to delete'.bold.yellow,
choices: backups
}, {
type: 'confirm',
name: 'shouldDelete',
message: `Still wanna do it? just checkin'...`,
default: false,
when: () => !args.options.force
}]
).then(answers => {
if (!answers.shouldDelete) {
console.log(`Whew, nothing happened. That must feel good, eh?`.green)
actionFinished()
return
}
const spinner = ora(`Attempting to delete backups:${answers.backupNames.map(b => ` [${b}] `)}` ).start().info().start()
const deletePromises = answers.backupNames.map(backupNme => bfs.removeBackup(backupNme))
Promise
.all(deletePromises)
.then((items) => {
spinner.succeed(`deleted!`.green)
actionFinished()
}).catch(err => {
spinner.fail(`ummm, yeah.. this is awkward; we could't delete the backups for some reason`.red)
throw err
})
})
} else {
this.log('oh no! There are no backups available. try creating one :)'.bold.red)
}
})
/**
* RESTORE
*/
vorpal
.command('restore', 'Restore a backup from a list of available backups')
.alias('r')
.option('-f, --force', 'Force restore.')
.action(function (args, actionFinished) {
const backups = bfs.listBackups()
if (backups || backups.length > 0) {
this.prompt(
[{
type: 'list',
name: 'backupName',
default: false,
message: 'Please pick a backup to restore \n' + 'WARNING: this will replace existing crx-quickstart folder'.bold.yellow,
choices: backups
}, {
type: 'confirm',
name: 'shouldRestore',
message: 'This action will wipe your current crx-quickstart folder, are you sure you want to continue?',
default: false,
when: () => !args.options.force
}]
).then(answers => {
if (!answers.shouldRestore) {
console.log(`Whew, nothing happened. That must feel good, eh?`.green)
actionFinished()
return
}
const spinner = ora('Cleaning crx-quickstart').start().info().start()
let restoreSize = 0
bfs.restoreBackup(answers.backupName, {
cleanSuccess: () => spinner.start().succeed('crx-quickstart cleaned!').start(),
cleanFail: () => spinner.start().fail('crx-quickstart could not be cleaned :( please make sure your AEM instance is shutdown'.red),
restoreSuccess: () => {
spinner.start().succeed('Restored!')
actionFinished()
},
restoreFail: () => spinner.start().fail(`whoops.. something went wrong while restoring... maybe check if the galaxy's are aligned? or the error log :)`.red),
onEntry: (entry) => {
if(!spinner.isSpinning) spinner.start();
const size = entry.size ? entry.size : 0
restoreSize += size
spinner.text = `[${bfs.formatBytes(restoreSize)}]`.magenta + `Restoring ${entry.path}`.green
}
})
})
} else {
this.log('oh no! There are no backups available. try creating one :)'.bold.red)
}
})
vorpal
.delimiter('aemb$')
.show()