-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathscript-utils.ts
573 lines (517 loc) · 19.3 KB
/
script-utils.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Copyright (c) 2022, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import os from 'os'
import path from 'path'
import archiver, {EntryData} from 'archiver'
import {default as _fetch, Response} from 'node-fetch'
import {URL} from 'url'
import {
createWriteStream,
existsSync,
readFile,
readJson,
stat,
mkdtemp,
rm,
readdir,
Stats,
Dirent
} from 'fs-extra'
import {Minimatch} from 'minimatch'
import git from 'git-rev-sync'
import validator from 'validator'
import {execSync} from 'child_process'
import semver from 'semver'
export const DEFAULT_CLOUD_ORIGIN = 'https://cloud.mobify.com'
export const DEFAULT_DOCS_URL =
'https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/pushing-and-deploying-bundles.html'
interface Credentials {
username: string
api_key: string
}
interface CloudAPIClientOpts {
credentials: Credentials
origin?: string
// _fetch is a function and a namespace, we want to use the function here
fetch?: typeof _fetch
}
interface StringMap {
[key: string]: string
}
interface Bundle {
message: string
encoding: string
data: string
ssr_parameters: object
ssr_only: string[]
ssr_shared: string[]
}
interface Pkg {
name: string
version: string
ccExtensibility?: {extends: string; overridesDir: string}
dependencies?: {[key: string]: string}
devDependencies?: {[key: string]: string}
}
/**
* Get the package info for pwa-kit-dev.
*/
export const getPkgJSON = async (): Promise<Pkg> => {
const candidates = [
path.join(__dirname, '..', 'package.json'),
path.join(__dirname, '..', '..', 'package.json')
]
for (const candidate of candidates) {
try {
const data = await readJson(candidate)
return data as Pkg
} catch {
// Keep looking
}
}
return {name: '@salesforce/pwa-kit-dev', version: 'unknown'}
}
/**
* Get the package info for the current project.
*/
export const getProjectPkg = async (): Promise<Pkg> => {
const p = path.join(process.cwd(), 'package.json')
try {
const data = await readJson(p)
return data as Pkg
} catch {
throw new Error(`Could not read project package at "${p}"`)
}
}
/**
* Get the set of file paths within a specific directory
* @param dir Directory to walk
* @returns Set of file paths within the directory
*/
export const walkDir = async (
dir: string,
baseDir: string,
fileSet?: Set<string>
): Promise<Set<string>> => {
fileSet = fileSet || new Set<string>()
const entries: Dirent[] = await readdir(dir, {withFileTypes: true})
await Promise.all(
entries.map(async (entry) => {
const entryPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
await walkDir(entryPath, baseDir, fileSet)
} else {
fileSet?.add(entryPath.replace(baseDir + path.sep, ''))
}
})
)
return fileSet
}
interface DependencyTree {
version: string
name?: string
dependencies?: Record<string, DependencyTree>
resolved?: string
overridden?: boolean
}
/**
* Returns a DependencyTree that includes the versions of all packages
* including their dependencies within the project.
*
* @returns A DependencyTree with the versions of all dependencies
*/
export const getProjectDependencyTree = async (): Promise<DependencyTree | null> => {
// When executing this inside template-retail-react-app, the output of `npm ls` exceeds the
// max buffer size that child_process can handle, so we can't use that directly. The max string
// size is much larger, so writing/reading a temp file is a functional workaround.
const tmpDir = await mkdtemp(path.join(os.tmpdir(), 'pwa-kit-dev-'))
const destination = path.join(tmpDir, 'npm-ls.json')
try {
execSync(`npm ls --all --json > ${destination}`)
return await readJson(destination, 'utf8')
} catch (_) {
// Don't prevent bundles from being pushed if this step fails
return null
} finally {
// Remove temp file asynchronously after returning; ignore failures
void rm(destination).catch(() => {})
}
}
/**
* Returns the lowest version of a package installed.
*
* @param packageName - The name of the package to get the lowest version for
* @param dependencyTree - The dependency tree including all package versions
* @returns The lowest version of the given package that is installed
*/
export const getLowestPackageVersion = (
packageName: string,
dependencyTree: DependencyTree
): string => {
let lowestVersion: string | null = null
function search(tree: DependencyTree) {
for (const key in tree.dependencies) {
const dependency = tree.dependencies[key]
if (key === packageName) {
const version = dependency.version
if (!lowestVersion || semver.lt(version, lowestVersion)) {
lowestVersion = version
}
}
if (dependency.dependencies) {
search(dependency)
}
}
}
search(dependencyTree)
return lowestVersion ?? 'unknown'
}
/**
* Returns the versions of all PWA Kit dependencies of a project.
* This will search the dependency tree for the lowest version of each PWA Kit package.
*
* @param dependencyTree - The dependency tree including all package versions
* @returns The versions of all dependencies of the project.
*/
export const getPwaKitDependencies = (dependencyTree: DependencyTree): {[key: string]: string} => {
const pwaKitDependencies = [
'@salesforce/pwa-kit-react-sdk',
'@salesforce/pwa-kit-runtime',
'@salesforce/pwa-kit-dev'
]
// pwa-kit package versions are not always listed as direct dependencies
// in the package.json such as when a bundle is using template extensibility
const nestedPwaKitDependencies: {[key: string]: string} = {}
pwaKitDependencies.forEach((packageName) => {
nestedPwaKitDependencies[packageName] = getLowestPackageVersion(packageName, dependencyTree)
})
return nestedPwaKitDependencies
}
export class CloudAPIClient {
private opts: Required<CloudAPIClientOpts>
constructor(params: CloudAPIClientOpts) {
this.opts = {
origin: params.origin || DEFAULT_CLOUD_ORIGIN,
fetch: params.fetch || _fetch,
credentials: params.credentials
}
}
private getAuthHeader(): StringMap {
const {username, api_key} = this.opts.credentials
const encoded = Buffer.from(`${username}:${api_key}`, 'binary').toString('base64')
return {Authorization: `Basic ${encoded}`}
}
private async getHeaders(): Promise<StringMap> {
const pkg = await getPkgJSON()
return {
'User-Agent': `${pkg.name}@${pkg.version}`,
...this.getAuthHeader()
}
}
private async throwForStatus(res: Response) {
if (res.status < 400) {
return
}
const body = await res.text()
let error: {message?: string; docs_url?: string}
try {
error = JSON.parse(body)
} catch {
error = {} // Cloud doesn't always return JSON
}
if (res.status === 403) {
error.docs_url =
'https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/mrt-overview.html#users,-abilities,-and-roles'
}
throw new Error(
[
`HTTP ${res.status}`,
error.message || body,
`For more information visit ${error.docs_url || DEFAULT_DOCS_URL}`
].join('\n')
)
}
async push(bundle: Bundle, projectSlug: string, target?: string) {
const base = `api/projects/${projectSlug}/builds/`
const pathname = target ? base + `${target}/` : base
const url = new URL(this.opts.origin)
url.pathname = pathname
const body = Buffer.from(JSON.stringify(bundle))
const headers = {
...(await this.getHeaders()),
'Content-Length': body.length.toString()
}
const res = await this.opts.fetch(url.toString(), {
body,
method: 'POST',
headers
})
await this.throwForStatus(res)
return await res.json()
}
async createLoggingToken(project: string, environment: string): Promise<string> {
const url = new URL(this.opts.origin)
url.pathname = `/api/projects/${project}/target/${environment}/jwt/`
const headers = {
...(await this.getHeaders()),
// Annoyingly, the new logging endpoint only accepts an
// Authorization header that is inconsistent with our older APIs!
Authorization: `Bearer ${this.opts.credentials.api_key}`
}
const res = await this.opts.fetch(url.toString(), {
method: 'POST',
headers
})
await this.throwForStatus(res)
const data = await res.json()
return data['token']
}
/** Polls MRT for deployment status every 30 seconds. */
async waitForDeploy(project: string, environment: string): Promise<void> {
return new Promise((resolve, reject) => {
/** Milliseconds to wait between checks. */
const delay = 30_000
/** Check the deployment status to see whether it has finished. */
const check = async (): Promise<void> => {
const url = new URL(
`/api/projects/${project}/target/${environment}`,
this.opts.origin
)
const res = await this.opts.fetch(url, {headers: await this.getHeaders()})
if (!res.ok) {
const text = await res.text()
let json
try {
if (text) json = JSON.parse(text)
} catch (_) {} // eslint-disable-line no-empty
const message = json?.detail ?? text
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const detail = message ? `: ${message}` : ''
throw new Error(`${res.status} ${res.statusText}${detail}`)
}
const data: {state: string} = await res.json()
if (typeof data.state !== 'string') {
return reject(
new Error('An unknown state occurred when polling the deployment.')
)
}
switch (data.state) {
case 'CREATE_IN_PROGRESS':
case 'PUBLISH_IN_PROGRESS':
// In progress - check again after the next delay
// `check` is async, so we need to use .catch to properly handle errors
setTimeout(() => void check().catch(reject), delay)
return
case 'CREATE_FAILED':
case 'PUBLISH_FAILED':
// Failed - reject with failure
return reject(new Error('Deployment failed.'))
case 'ACTIVE':
// Success!
return resolve()
default:
// Unknown - reject with confusion
return reject(new Error(`Unknown deployment state "${data.state}".`))
}
}
// Start checking after the first delay!
setTimeout(() => void check().catch(reject), delay)
})
}
}
export const defaultMessage = (gitInstance: typeof git = git): string => {
try {
return `${gitInstance.branch()}: ${gitInstance.short()}`
} catch (err: any) {
if (err?.code === 'ENOENT') {
console.log(
'Using default bundle message as no message was provided and not in a Git repo.'
)
}
return 'PWA Kit Bundle'
}
}
interface CreateBundleArgs {
message: string | null | undefined
ssr_parameters: any
ssr_only: string[]
ssr_shared: string[]
buildDirectory: string
projectSlug: string
}
export const createBundle = async ({
message,
ssr_parameters,
ssr_only,
ssr_shared,
buildDirectory,
projectSlug
}: CreateBundleArgs): Promise<Bundle | any> => {
message = message || defaultMessage()
const tmpDir = await mkdtemp(path.join(os.tmpdir(), 'pwa-kit-dev-'))
const destination = path.join(tmpDir, 'build.tar')
const filesInArchive: string[] = []
let bundle_metadata: {[key: string]: any} = {}
if (ssr_only.length === 0 || ssr_shared.length === 0) {
throw new Error('no ssrOnly or ssrShared files are defined')
}
return (
Promise.resolve()
.then(() => stat(buildDirectory))
.catch(() => {
const fullPath = path.join(process.cwd(), buildDirectory)
throw new Error(
`Build directory at path "${fullPath}" not found.\n` +
'Run `pwa-kit-dev build` first!'
)
})
.then(
() =>
new Promise((resolve, reject) => {
const output = createWriteStream(destination)
const archive = archiver('tar')
archive.pipe(output)
// See https://web.archive.org/web/20160712064705/http://archiverjs.com/docs/global.html#TarEntryData
const newRoot = path.join(projectSlug, 'bld', '')
// WARNING: There are a lot of type assertions here because we use a very old
// version of archiver, and the types provided don't match the docs. :\
archive.directory(buildDirectory, '', ((entry: EntryData) => {
const stats = entry.stats as unknown as Stats | undefined
if (stats?.isFile() && entry.name) {
filesInArchive.push(entry.name)
}
entry.prefix = newRoot
return entry
}) as unknown as EntryData)
archive.on('error', reject)
output.on('finish', resolve)
archive.finalize()
})
)
.then(async () => {
const {
dependencies = {},
devDependencies = {},
ccExtensibility = {extends: '', overridesDir: ''}
} = await getProjectPkg()
const extendsTemplate = 'node_modules/' + ccExtensibility.extends
let cc_overrides: string[] = []
if (ccExtensibility.overridesDir) {
const overrides_files = await walkDir(
ccExtensibility.overridesDir,
ccExtensibility.overridesDir
)
cc_overrides = Array.from(overrides_files).filter((item) =>
existsSync(path.join(extendsTemplate, item))
)
}
const dependencyTree = await getProjectDependencyTree()
// If we can't load the dependency tree, pretend that it's empty.
// TODO: Should we report an error?
const pwaKitDeps = dependencyTree ? getPwaKitDependencies(dependencyTree) : {}
bundle_metadata = {
dependencies: {
...dependencies,
...devDependencies,
...(pwaKitDeps ?? {})
},
cc_overrides: cc_overrides
}
})
.then(() => readFile(destination))
.then((data) => {
const encoding = 'base64'
return {
message,
encoding,
data: data.toString(encoding),
ssr_parameters,
ssr_only: filesInArchive.filter(glob(ssr_only)),
ssr_shared: filesInArchive.filter(glob(ssr_shared)),
bundle_metadata
}
})
// This is a false positive. The promise returned by `.finally()` won't resolve until
// the `rm()` completes!
// eslint-disable-next-line @typescript-eslint/no-misused-promises
.finally(() => rm(tmpDir, {recursive: true}))
)
}
export const glob = (patterns?: string[]): ((str: string) => boolean) => {
// The patterns can include negations, so matching is done against all
// the patterns. A match is true if a given path matches any pattern and
// does not match any negating patterns.
const allPatterns = (patterns || [])
.map((pattern) => new Minimatch(pattern, {nocomment: true}))
.filter((pattern) => !pattern.empty)
const positivePatterns = allPatterns.filter((pattern) => !pattern.negate)
const negativePatterns = allPatterns.filter((pattern) => pattern.negate)
return (path: string) => {
if (path) {
const positive = positivePatterns.some((pattern) => pattern.match(path))
const negative = negativePatterns.some((pattern) => !pattern.match(path))
return positive && !negative
}
return false
}
}
export const getCredentialsFile = (cloudOrigin: string, credentialsFile?: string): string => {
if (credentialsFile) {
return credentialsFile
} else {
const url = new URL(cloudOrigin)
const host = url.host
const suffix = host === 'cloud.mobify.com' ? '' : `--${host}`
return path.join(os.homedir(), `.mobify${suffix}`)
}
}
export const readCredentials = async (filepath: string): Promise<Credentials> => {
try {
const data = await readJson(filepath)
return {
username: data.username,
api_key: data.api_key
}
} catch (e) {
throw new Error(
`Credentials file "${filepath}" not found.\n` +
'Visit https://runtime.commercecloud.com/account/settings for ' +
'steps on authorizing your computer to push bundles.'
)
}
}
interface LogRecord {
level: string | undefined
message: string
shortRequestId?: string
}
export const parseLog = (log: string): LogRecord => {
const parts = log.trim().split('\t')
let requestId, shortRequestId, level
if (
parts.length >= 3 &&
validator.isISO8601(parts[0]) &&
validator.isUUID(parts[1]) &&
validator.isAlpha(parts[2])
) {
// An application log
parts.shift()
requestId = parts.shift()
level = parts.shift()
} else {
// A platform log
const words = parts[0].split(' ')
level = words.shift()
parts[0] = words.join(' ')
}
const message = parts.join('\t')
const match = /(?<id>[a-f\d]{8})/.exec(requestId || message)
if (match) {
shortRequestId = match.groups?.id
}
return {level, message, shortRequestId}
}