-
Notifications
You must be signed in to change notification settings - Fork 22
/
link.ts
338 lines (302 loc) · 11.1 KB
/
link.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import * as retry from 'async-retry'
import * as bluebird from 'bluebird'
import chalk from 'chalk'
import * as chokidar from 'chokidar'
import * as debounce from 'debounce'
import { readFileSync } from 'fs'
import * as moment from 'moment'
import { join, resolve as resolvePath, sep } from 'path'
import { compose, concat, intersection, isEmpty, keys, map, not, pipe, prop, toPairs } from 'ramda'
import { createInterface } from 'readline'
import { createClients } from '../../clients'
import { getAccount, getEnvironment, getWorkspace } from '../../conf'
import { CommandError } from '../../errors'
import { toAppLocator } from '../../locator'
import log from '../../logger'
import { getAppRoot, getManifest, writeManifestSchema } from '../../manifest'
import { listenBuild } from '../build'
import { default as setup } from '../setup'
import { fixPinnedDependencies, formatNano, runYarnIfPathExists } from '../utils'
import startDebuggerTunnel from './debugger'
import { createLinkConfig, getIgnoredPaths, getLinkedDepsDirs, getLinkedFiles, listLocalFiles } from './file'
import { ChangeToSend, ProjectSizeLimitError, ProjectUploader, ChangeSizeLimitError } from './ProjectUploader'
import { checkBuilderHubMessage, pathToFileObject, showBuilderHubMessage, validateAppAction } from './utils'
import logger from '../../logger'
const root = getAppRoot()
const DELETE_SIGN = chalk.red('D')
const UPDATE_SIGN = chalk.blue('U')
const stabilityThreshold = process.platform === 'darwin' ? 100 : 200
const buildersToStartDebugger = ['node']
const buildersToRunLocalYarn = ['react', 'node']
const RETRY_OPTS_INITIAL_LINK = {
retries: 2,
minTimeout: 1000,
factor: 2,
}
const RETRY_OPTS_DEBUGGER = {
retries: 2,
minTimeout: 1000,
factor: 2,
}
const shouldStartDebugger = (manifest: Manifest) =>
compose<Manifest, any, string[], string[], boolean, boolean>(
not,
isEmpty,
intersection(buildersToStartDebugger),
keys,
prop('builders')
)(manifest)
const warnAndLinkFromStart = (
projectUploader: ProjectUploader,
unsafe: boolean,
extraData: { linkConfig: LinkConfig } = { linkConfig: null }
) => {
log.warn('Initial link requested by builder')
performInitialLink(projectUploader, extraData, unsafe)
return null
}
const watchAndSendChanges = async (
projectUploader: ProjectUploader,
extraData: { linkConfig: LinkConfig },
unsafe: boolean
): Promise<any> => {
const changeQueue: ChangeToSend[] = []
const onInitialLinkRequired = e => {
const data = e.response && e.response.data
if (data && data.code && data.code === 'initial_link_required') {
return warnAndLinkFromStart(projectUploader, unsafe, extraData)
}
throw e
}
const defaultPatterns = ['*/**', 'manifest.json', 'policies.json']
const linkedDepsPatterns = map(path => join(path, '**'), getLinkedDepsDirs(extraData.linkConfig))
const queueChange = (path: string, remove?: boolean) => {
console.log(`${chalk.gray(moment().format('HH:mm:ss:SSS'))} - ${remove ? DELETE_SIGN : UPDATE_SIGN} ${path}`)
changeQueue.push(pathToChange(path, remove))
sendChanges()
}
const sendChanges = debounce(async () => {
try {
return await projectUploader.sendToRelink(changeQueue.splice(0, changeQueue.length), {
tsErrorsAsWarnings: unsafe,
})
} catch (err) {
if (err instanceof ChangeSizeLimitError) {
logger.error(err.message)
process.exit(1)
}
onInitialLinkRequired(err)
}
}, 1000)
const pathToChange = (path: string, remove?: boolean): ChangeToSend => {
const content = remove ? null : readFileSync(resolvePath(root, path)).toString('base64')
const byteSize = remove ? 0 : Buffer.byteLength(content)
return {
content,
byteSize,
path: pathModifier(path),
}
}
const moduleAndMetadata = toPairs(extraData.linkConfig.metadata)
const mapLocalToBuiderPath = path => {
const abs = resolvePath(root, path)
for (const [module, modulePath] of moduleAndMetadata as any) {
if (abs.startsWith(modulePath)) {
return abs.replace(modulePath, join('.linked_deps', module))
}
}
return path
}
const pathModifier = pipe(mapLocalToBuiderPath, path => path.split(sep).join('/'))
const addIgnoreNodeModulesRule = (paths: Array<string | ((path: string) => boolean)>) =>
paths.concat((path: string) => path.includes('node_modules'))
const watcher = chokidar.watch([...defaultPatterns, ...linkedDepsPatterns], {
atomic: stabilityThreshold,
awaitWriteFinish: {
stabilityThreshold,
},
cwd: root,
ignoreInitial: true,
ignored: addIgnoreNodeModulesRule(getIgnoredPaths(root)),
persistent: true,
usePolling: process.platform === 'win32',
})
return new Promise((resolve, reject) => {
watcher
.on('add', file => queueChange(file))
.on('change', file => queueChange(file))
.on('unlink', file => queueChange(file, true))
.on('error', reject)
.on('ready', resolve)
})
}
const performInitialLink = async (
projectUploader: ProjectUploader,
extraData: { linkConfig: LinkConfig },
unsafe: boolean
): Promise<void> => {
const linkConfig = await createLinkConfig(root)
extraData.linkConfig = linkConfig
const usedDeps = toPairs(linkConfig.metadata)
if (usedDeps.length) {
const plural = usedDeps.length > 1
log.info(`The following local dependenc${plural ? 'ies are' : 'y is'} linked to your app:`)
usedDeps.forEach(([dep, path]) => log.info(`${dep} (from: ${path})`))
log.info(
`If you don\'t want ${plural ? 'them' : 'it'} to be used by your vtex app, please unlink ${
plural ? 'them' : 'it'
}`
)
}
const linkApp = async (bail: any, tryCount: number) => {
// wrapper for builder.linkApp to be used with the retry function below.
const [localFiles, linkedFiles] = await Promise.all([
listLocalFiles(root).then(paths => map(pathToFileObject(root), paths)),
getLinkedFiles(linkConfig),
])
const filesWithContent = concat(localFiles, linkedFiles) as BatchStream[]
if (tryCount === 1) {
const linkedFilesInfo = linkedFiles.length ? `(${linkedFiles.length} from linked node modules)` : ''
log.info(`Sending ${filesWithContent.length} file${filesWithContent.length > 1 ? 's' : ''} ${linkedFilesInfo}`)
log.debug('Sending files')
filesWithContent.forEach(p => log.debug(p.path))
}
if (tryCount > 1) {
log.info(`Retrying...${tryCount - 1}`)
}
try {
const { code } = await projectUploader.sendToLink(filesWithContent, { tsErrorsAsWarnings: unsafe })
if (code !== 'build.accepted') {
bail(new Error('Please, update your builder-hub to the latest version!'))
}
} catch (err) {
if (err instanceof ProjectSizeLimitError) {
log.error(err.message)
process.exit(1)
}
if (err.status) {
const response = err.response
const status = response.status
const data = response && response.data
const message = data.message
const statusMessage = status ? `: Status ${status}` : ''
log.error(`Error linking app${statusMessage} (try: ${tryCount})`)
if (message) {
log.error(`Message: ${message}`)
}
if (status && status < 500) {
return
}
}
throw err
}
}
await retry(linkApp, RETRY_OPTS_INITIAL_LINK)
}
export default async options => {
await validateAppAction('link')
const unsafe = !!(options.unsafe || options.u)
const manifest = await getManifest()
try {
await writeManifestSchema()
} catch (e) {
log.debug('Failed to write schema on manifest.')
}
const builderHubMessage = await checkBuilderHubMessage('link')
if (!isEmpty(builderHubMessage)) {
await showBuilderHubMessage(builderHubMessage.message, builderHubMessage.prompt, manifest)
}
const appId = toAppLocator(manifest)
const context = { account: getAccount(), workspace: getWorkspace(), environment: getEnvironment() }
const { builder } = createClients(context, { timeout: 60000 })
const projectUploader = ProjectUploader.getProjectUploader(appId, builder)
if (options.setup || options.s) {
await setup({ 'ignore-linked': false })
}
try {
const aux = await builder.getPinnedDependencies()
const pinnedDeps: Map<string, string> = new Map(Object.entries(aux))
await bluebird.map(buildersToRunLocalYarn, fixPinnedDependencies(pinnedDeps), { concurrency: 1 })
} catch (e) {
log.info('Failed to check for pinned dependencies')
}
// Always run yarn locally for some builders
map(runYarnIfPathExists, buildersToRunLocalYarn)
if (options.c || options.clean) {
log.info('Requesting to clean cache in builder.')
const { timeNano } = await builder.clean(appId)
log.info(`Cache cleaned successfully in ${formatNano(timeNano)}`)
}
const onError = {
build_failed: () => {
log.error(`App build failed. Waiting for changes...`)
},
initial_link_required: () => warnAndLinkFromStart(projectUploader, unsafe),
}
let debuggerStarted = false
const onBuild = async () => {
if (debuggerStarted) {
return
}
const startDebugger = async () => {
const port = await startDebuggerTunnel(manifest)
if (!port) {
throw new Error('Failed to start debugger.')
}
return port
}
if (shouldStartDebugger(manifest)) {
try {
const debuggerPort = await retry(startDebugger, RETRY_OPTS_DEBUGGER)
debuggerStarted = true
log.info(
`Debugger tunnel listening on ${chalk.green(`:${debuggerPort}`)}. Go to ${chalk.blue(
'chrome://inspect'
)} in Google Chrome to debug your running application.`
)
} catch (e) {
log.error(e.message)
}
}
}
log.info(`Linking app ${appId}`)
let unlistenBuild
const extraData = { linkConfig: null }
try {
const buildTrigger = performInitialLink.bind(this, projectUploader, extraData, unsafe)
const [subject] = appId.split('@')
if (options.watch === false) {
await listenBuild(subject, buildTrigger, { waitCompletion: true })
return
}
unlistenBuild = await listenBuild(subject, buildTrigger, { waitCompletion: false, onBuild, onError }).then(
prop('unlisten')
)
} catch (e) {
if (e.response) {
const { data } = e.response
if (data.code === 'routing_error' && /app_not_found.*vtex\.builder\-hub/.test(data.message)) {
return log.error(
'Please install vtex.builder-hub in your account to enable app linking (vtex install vtex.builder-hub)'
)
}
if (data.code === 'link_on_production') {
throw new CommandError(
`Please use a dev workspace to link apps. Create one with (${chalk.blue(
'vtex use <workspace> -rp'
)}) to be able to link apps`
)
}
}
throw e
}
createInterface({ input: process.stdin, output: process.stdout }).on('SIGINT', () => {
if (unlistenBuild) {
unlistenBuild()
}
log.info('Your app is still in development mode.')
log.info(`You can unlink it with: 'vtex unlink ${appId}'`)
process.exit()
})
await watchAndSendChanges(projectUploader, extraData, unsafe)
}