-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathserve.ts
181 lines (155 loc) · 4.88 KB
/
serve.ts
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
import { Command, flags } from '@oclif/command'
import { ChildProcess, fork } from 'child_process'
import { autoPrompt } from '../lib/prompt'
import chalk from 'chalk'
import chokidar from 'chokidar'
import ora from 'ora'
import path from 'path'
import globby from 'globby'
import { WebSocketServer } from 'ws'
import open from 'open'
import execa from 'execa'
export default class Serve extends Command {
private spinner: ora.Ora = ora()
static description = `Starts a local development server to test your integration.`
static examples = [`$ ./bin/run serve`, `$ PORT=3001 ./bin/run serve`, `$ ./bin/run serve --destination=slack`]
// Needed to support passing Node flags to the server process
static strict = false
static args = []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static flags: flags.Input<any> = {
help: flags.help({ char: 'h' }),
destination: flags.string({
char: 'd',
description: 'destination to serve'
}),
directory: flags.string({
char: 'b',
description: 'destination actions directory',
default: './packages/destination-actions/src/destinations'
}),
noUI: flags.boolean({
char: 'n',
description: 'do not open actions tester UI in browser'
}),
browser: flags.boolean({
char: 'r',
description: 'serve browser destinations'
})
}
async run() {
const { argv, flags } = this.parse(Serve)
let destinationName = flags.destination
const isBrowser = !!flags.browser
if (!destinationName) {
const integrationsGlob = `${flags.directory}/*`
const integrationDirs = await globby(integrationsGlob, {
expandDirectories: false,
onlyDirectories: true,
gitignore: true,
ignore: ['node_modules']
})
const { selectedDestination } = await autoPrompt<{ selectedDestination: { name: string } }>(flags, {
type: 'autocomplete',
name: 'selectedDestination',
message: 'Which destination?',
choices: integrationDirs.map((integrationPath) => {
const [name] = integrationPath.split(path.sep).reverse()
return {
title: name,
value: { name: name }
}
})
})
if (selectedDestination) {
destinationName = selectedDestination.name
}
}
if (!destinationName) {
this.warn('You must select a destination. Exiting.')
this.exit()
}
const folderPath = path.join(process.cwd(), flags.directory, destinationName)
let child: ChildProcess | null | undefined = null
const watcher = chokidar.watch(folderPath, {
cwd: process.cwd()
})
const DEFAULT_PORT = 3000
const port = parseInt(process.env.PORT ?? '', 10) || DEFAULT_PORT
if (!flags.noUI) {
const wss = new WebSocketServer({ port: port + 1 })
wss.on('connection', function connection(ws) {
watcher.on('change', () => {
ws.send('change')
})
})
}
const start = () => {
child = fork(require.resolve('../lib/server.ts'), {
cwd: process.cwd(),
env: {
...process.env,
DESTINATION: destinationName,
DIRECTORY: flags.directory,
TS_NODE_PROJECT: require.resolve('../../tsconfig.json'),
ENTRY: isBrowser ? path.join('src', 'index.ts') : 'index.ts'
},
execArgv: [
'-r',
'ts-node/register/transpile-only',
'-r',
'tsconfig-paths/register',
'-r',
'dotenv/config',
...argv
]
})
child.once('exit', (code?: number) => {
// @ts-ignore custom property
if (!child.respawn) process.exit(code)
child?.removeAllListeners()
child = undefined
})
if (flags.browser) {
execa.command('yarn browser dev').stdout
}
}
watcher.on('change', (file) => {
this.log(chalk.greenBright`Restarting... ${file} has been modified`)
if (child) {
// Child is still running, restart upon exit
child.on('exit', start)
stop(child)
} else {
// Child is already stopped, probably due to a previous error
start()
}
})
watcher.on('error', (error) => {
this.error(`Error: ${error.message}`)
})
watcher.once('ready', () => {
this.log(chalk.greenBright`Watching required files for changes .. `)
if (!flags.noUI) {
this.log(
chalk.greenBright`Visit https://app.segment.com/dev-center/actions-tester to preview your integration.`
)
void open('https://app.segment.com/dev-center/actions-tester')
}
})
start()
}
async catch(error: unknown) {
if (this.spinner?.isSpinning) {
this.spinner.fail()
}
throw error
}
}
function stop(process?: ChildProcess) {
if (process) {
// @ts-ignore custom propertiy
process.respawn = true
process.kill('SIGTERM')
}
}