This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·114 lines (104 loc) · 2.71 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
#!/usr/bin/env node
const { EventEmitter } = require('events')
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const Ora = require('ora')
const yargs = require('yargs')
const debounce = require('lodash.debounce')
const clear = require('clear')
const deploy = require('storeden-deploy')
const packageJson = require('./package.json')
const argv = yargs
.usage('Usage: $0 [options]')
.option('apiKey', {
alias: 'k',
describe: 'API Key associated with your Storeden Store',
demandOption: true
})
.option('apiExchange', {
alias: 'e',
describe: 'API Exchange associated with your Storeden Store',
demandOption: true
})
.option('sourcePath', {
alias: 'p',
describe: 'Path to template folder to deploy',
demandOption: true
})
.coerce('sourcePath', arg => {
const [source, exclude] = arg.split(':')
return { source: path.resolve(source), exclude }
})
.option('watch', {
alias: 'w',
describe: 'Enable watch mode to deploy automatically',
type: 'boolean',
demandOption: false
})
.demandCommand(0, 0)
.help()
.version().argv
const run = async argv => {
const spinner = new Ora()
const emitter = new EventEmitter()
emitter.on('zip', () => {
const exclude = argv.sourcePath.exclude
? ` (${argv.sourcePath.exclude})`
: ''
const output = argv.sourcePath.source + chalk.gray(exclude)
spinner.start(`Creating zip archive of ${chalk.bold(output)}`)
})
emitter.on('upload', file => {
spinner.succeed()
spinner.start(`Uploading ${chalk.bold(file)}`)
})
try {
const hrstart = process.hrtime()
await deploy({ ...argv, emitter })
const hrend = process.hrtime(hrstart)
const executionTime = ((hrend[0] * 1e9 + hrend[1]) / 1e9).toFixed(1)
spinner.succeed()
console.log(
`${chalk.cyan('Success!')} Deployment ready ` +
chalk.gray('[' + executionTime + 's]')
)
if (!argv.watch) {
process.exit(0)
}
} catch (e) {
spinner.fail(e.message)
console.log(chalk.red('Deploy failed'))
if (!argv.watch) {
process.exit(1)
}
}
}
const handleChanges = async (eventType, filename) => {
if (filename) {
console.log(
`\n🚚 Detected ${chalk.cyan(eventType)} to ${chalk.cyan(
filename
)}. Deploying...`
)
await run(argv)
}
}
;(async () => {
if (argv.watch) {
clear()
console.log(
chalk.bold(`Storeden Deploy CLI v${packageJson.version}\n\n`) +
`🔍 Watching for file changes on ${chalk.cyan(
argv.sourcePath.source
)}...`
)
fs.watch(
argv.sourcePath.source,
{ recursive: true },
debounce(handleChanges, 100)
)
} else {
await run(argv)
}
})()