forked from stackbit/stackbit-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
87 lines (79 loc) · 3.17 KB
/
Copy pathutils.ts
File metadata and controls
87 lines (79 loc) · 3.17 KB
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
import chalk from 'chalk';
import _ from 'lodash';
import { CMSMatchResult, ConfigLoaderResult, ContentLoaderResult, SiteAnalyzerResult, SSGMatchResult } from '@stackbit/sdk';
import { track, EVENTS, trackUncaughtException } from './telemetry';
let uncaughtExceptionMonitorSet = false;
export function setupUncaughtExceptionHandler() {
if (uncaughtExceptionMonitorSet) {
return;
}
uncaughtExceptionMonitorSet = true;
process.on('unhandledRejection', async (error: Error) => {
await handleUncaughtException(error);
});
process.on('uncaughtException', async (error: Error) => {
await handleUncaughtException(error);
});
}
async function handleUncaughtException(error: Error) {
await trackUncaughtException({
error: {
name: error.name,
message: error.message,
stack: error.stack
}
});
console.error('Uh-oh! Something went wrong');
console.error(error);
process.exit(1);
}
export function printSSGMatchResult(ssgMatchResult: SSGMatchResult | null) {
if (!ssgMatchResult) {
console.log(`Couldn't match SSG`);
return;
}
console.log(`Matched SSG: ${chalk.cyanBright(ssgMatchResult.ssgName)}`);
if (ssgMatchResult.ssgDir === undefined) {
const possibleDirs = ssgMatchResult.options?.ssgDirs ? ` Possible folders: ${ssgMatchResult.options?.ssgDirs.join(', ')}` : '';
console.log('Could not identify SSG folder.' + possibleDirs);
} else {
const ssgDir = ssgMatchResult.ssgDir === '' ? '.' : ssgMatchResult.ssgDir;
console.log(`SSG directory: ${chalk.cyanBright(`'${ssgDir}'`)}`);
console.log(`Repo is theme: ${chalk.cyanBright(!!ssgMatchResult.isTheme)}`);
if (ssgMatchResult.envVars) {
console.log(`Environment variables: ${chalk.cyanBright(`${ssgMatchResult.envVars.join(', ')}`)}`);
}
}
}
export function printCMSMatchResult(cmsMatchResult: CMSMatchResult | null) {
if (!cmsMatchResult) {
return;
}
console.log(`Matched CMS: ${chalk.cyanBright(cmsMatchResult.cmsName)}`);
if (cmsMatchResult.cmsData !== undefined) {
_.forEach(cmsMatchResult.cmsData, (value, prop) => {
console.log(`${prop}: ${chalk.cyanBright(`'${value}'`)}`);
});
}
}
export function trackInitResultStats(analyzeResult: SiteAnalyzerResult, inputDir: string) {
track(EVENTS.initResult, {
inputDir,
ssg_name: analyzeResult.config.ssgName,
is_theme: analyzeResult.ssgMatchResult?.isTheme,
cms_name: analyzeResult.config.cmsName,
model_count: analyzeResult.config.models.length
});
}
export function trackValidateResultStats(configResult: ConfigLoaderResult, contentResult: ContentLoaderResult | null, inputDir: string) {
track(EVENTS.validateResult, {
inputDir,
config_valid: configResult.valid,
config_error_count: configResult.errors.length,
ssg_name: configResult.config?.ssgName,
cms_name: configResult.config?.cmsName,
content_valid: contentResult?.valid,
content_item_count: contentResult?.contentItems.length,
content_error_count: contentResult?.errors.length
});
}