-
Notifications
You must be signed in to change notification settings - Fork 5
/
now-redirect
executable file
·52 lines (40 loc) · 1.62 KB
/
now-redirect
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
#!/usr/bin/env node
const spawn = require('child_process').spawn
const path = require('path')
const ncp = require('copy-paste')
const chalk = require('chalk')
if (process.argv.length !== 3 || process.argv[2] === '-h' || process.argv[2] === 'help') {
console.log('Usage: now-redirect <destination-domain>')
console.log('It will deploy app via zeit\'s now that redirects all requests to the given domain.')
process.exit(0)
}
const cmd = process.platform === 'win32' ? 'now.cmd' : 'now'
const dir = path.resolve(__dirname, '..', 'app')
let host = process.argv[2]
if (!/^https?:\/\//i.test(host)) {
host = 'http://' + host
}
const deploymentName = host.replace('http://', '')
// Have to force because otherwise susequent deployments with different envs and same code are ignored
const flagList = ['-f', '-e', "REDIRECT_HOST=" + host, dir, "--name=" + deploymentName]
const now = spawn(cmd, flagList, { stdio: [ 'inherit', 'pipe', 'inherit' ] })
now.on('error', err => console.error(err))
let buffer = ''
now.stdout.on('data', data => {
buffer += data.toString()
})
console.log(`> Deploying app that redirects all requests to ${host}...`)
now.on('close', code => {
if (code === 0) {
const url = buffer
ncp.copy(url, () => {
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} (copied to clipboard)`)
console.log(`> Requests to ${chalk.bold(url + '/*')} will be redirected to ${chalk.bold(host + '/*')}`)
console.log(`> If it works as expected, alias it with:`)
console.log(chalk.bold(` now alias ${url} domain-to-be-redirected`))
})
}
})
process.on('SIGINT', () => {
now.kill('SIGINT')
})