Skip to content

Commit efb0ab1

Browse files
committed
Use stripIndents to help format error messages and template strings
1 parent bf17b18 commit efb0ab1

28 files changed

+279
-163
lines changed

.dep-stats.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"browserslist": "4.24.4",
1717
"chalk-table": "^1.0.2",
1818
"cmd-shim": "^7.0.0",
19+
"common-tags": "^1.8.2",
1920
"hpagent": "^1.2.0",
2021
"ignore": "^7.0.3",
2122
"meow": "^13.2.0",
@@ -77,6 +78,7 @@
7778
"browserslist": "4.24.4",
7879
"chalk-table": "^1.0.2",
7980
"cmd-shim": "^7.0.0",
81+
"common-tags": "^1.8.2",
8082
"fast-content-type-parse": "^2.0.1",
8183
"has-flag": "4.0.0",
8284
"hpagent": "^1.2.0",

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"browserslist": "4.24.4",
8585
"chalk-table": "^1.0.2",
8686
"cmd-shim": "^7.0.0",
87+
"common-tags": "^1.8.2",
8788
"fast-content-type-parse": "^2.0.1",
8889
"has-flag": "4.0.0",
8990
"hpagent": "^1.2.0",
@@ -124,6 +125,7 @@
124125
"@rollup/pluginutils": "^5.1.4",
125126
"@types/blessed": "^0.1.25",
126127
"@types/cmd-shim": "^5.0.2",
128+
"@types/common-tags": "^1.8.4",
127129
"@types/micromatch": "^4.0.9",
128130
"@types/mocha": "^10.0.10",
129131
"@types/mock-fs": "^4.13.4",

src/commands/analytics/cmd-analytics.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { stripIndents } from 'common-tags'
12
import colors from 'yoctocolors-cjs'
23

34
import { logger } from '@socketsecurity/registry/lib/logger'
@@ -95,17 +96,21 @@ async function run(
9596
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
9697
process.exitCode = 2
9798
logger.error(
98-
`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
99-
- Scope must be "repo" or "org" ${badScope ? colors.red('(bad!)') : colors.green('(ok)')}\n
100-
- The time filter must either be 7, 30 or 90 ${badTime ? colors.red('(bad!)') : colors.green('(ok)')}\n
101-
${scope === 'repo' ? `- Repository name using --repo when scope is "repo" ${badRepo ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}\n
102-
${badFlags ? `- The \`--json\` and \`--markdown\` flags can not be used at the same time ${badFlags ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}\n
103-
${badFile ? `- The \`--file\` flag is only valid when using \`--json\` or \`--markdown\` ${badFile ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}\n
99+
stripIndents`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
100+
101+
- Scope must be "repo" or "org" ${badScope ? colors.red('(bad!)') : colors.green('(ok)')}
102+
103+
- The time filter must either be 7, 30 or 90 ${badTime ? colors.red('(bad!)') : colors.green('(ok)')}
104+
105+
${scope === 'repo' ? `- Repository name using --repo when scope is "repo" ${badRepo ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}
106+
107+
${badFlags ? `- The \`--json\` and \`--markdown\` flags can not be used at the same time ${badFlags ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}
108+
109+
${badFile ? `- The \`--file\` flag is only valid when using \`--json\` or \`--markdown\` ${badFile ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}
104110
`
105-
.trim()
106111
.split('\n')
107112
.filter(s => !!s.trim())
108-
.join('\n') + '\n'
113+
.join('\n')
109114
)
110115
return
111116
}

src/commands/analytics/display-analytics.ts

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'node:fs/promises'
33
// @ts-ignore
44
import ScreenWidget from 'blessed/lib/widgets/screen'
55
import contrib from 'blessed-contrib'
6+
import { stripIndents } from 'common-tags'
67

78
import { logger } from '@socketsecurity/registry/lib/logger'
89

@@ -182,58 +183,59 @@ function renderMarkdown(
182183
days: number,
183184
repoSlug: string
184185
): string {
185-
return (
186-
`# Socket Alert Analytics
186+
return stripIndents`
187+
# Socket Alert Analytics
187188
188189
These are the Socket.dev stats are analytics for the ${repoSlug ? `${repoSlug} repo` : 'org'} of the past ${days} days
189190
190-
` +
191-
[
192-
[
193-
'Total critical alerts',
194-
mdTableStringNumber('Date', 'Counts', data['total_critical_alerts'])
195-
],
196-
[
197-
'Total high alerts',
198-
mdTableStringNumber('Date', 'Counts', data['total_high_alerts'])
199-
],
200-
[
201-
'Total critical alerts added to the main branch',
202-
mdTableStringNumber('Date', 'Counts', data['total_critical_added'])
203-
],
204-
[
205-
'Total high alerts added to the main branch',
206-
mdTableStringNumber('Date', 'Counts', data['total_high_added'])
207-
],
208-
[
209-
'Total critical alerts prevented from the main branch',
210-
mdTableStringNumber('Date', 'Counts', data['total_critical_prevented'])
211-
],
212-
[
213-
'Total high alerts prevented from the main branch',
214-
mdTableStringNumber('Date', 'Counts', data['total_high_prevented'])
215-
],
216-
[
217-
'Total medium alerts prevented from the main branch',
218-
mdTableStringNumber('Date', 'Counts', data['total_medium_prevented'])
219-
],
220-
[
221-
'Total low alerts prevented from the main branch',
222-
mdTableStringNumber('Date', 'Counts', data['total_low_prevented'])
223-
]
224-
]
225-
.map(([title, table]) => {
226-
return `
191+
${[
192+
[
193+
'Total critical alerts',
194+
mdTableStringNumber('Date', 'Counts', data['total_critical_alerts'])
195+
],
196+
[
197+
'Total high alerts',
198+
mdTableStringNumber('Date', 'Counts', data['total_high_alerts'])
199+
],
200+
[
201+
'Total critical alerts added to the main branch',
202+
mdTableStringNumber('Date', 'Counts', data['total_critical_added'])
203+
],
204+
[
205+
'Total high alerts added to the main branch',
206+
mdTableStringNumber('Date', 'Counts', data['total_high_added'])
207+
],
208+
[
209+
'Total critical alerts prevented from the main branch',
210+
mdTableStringNumber('Date', 'Counts', data['total_critical_prevented'])
211+
],
212+
[
213+
'Total high alerts prevented from the main branch',
214+
mdTableStringNumber('Date', 'Counts', data['total_high_prevented'])
215+
],
216+
[
217+
'Total medium alerts prevented from the main branch',
218+
mdTableStringNumber('Date', 'Counts', data['total_medium_prevented'])
219+
],
220+
[
221+
'Total low alerts prevented from the main branch',
222+
mdTableStringNumber('Date', 'Counts', data['total_low_prevented'])
223+
]
224+
]
225+
.map(
226+
([title, table]) =>
227+
stripIndents`
227228
## ${title}
228229
229230
${table}
230-
`.trim()
231-
})
232-
.join('\n\n') +
233-
'\n\n## Top 5 alert types\n\n' +
234-
mdTableStringNumber('Name', 'Counts', data['top_five_alert_types']) +
235-
'\n'
231+
`
236232
)
233+
.join('\n\n')}
234+
235+
## Top 5 alert types
236+
237+
${mdTableStringNumber('Name', 'Counts', data['top_five_alert_types'])}
238+
`
237239
}
238240

239241
function displayAnalyticsScreen(data: FormattedData): void {

src/commands/audit-log/cmd-audit-log.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { stripIndents } from 'common-tags'
12
import colors from 'yoctocolors-cjs'
23

34
import { logger } from '@socketsecurity/registry/lib/logger'
@@ -79,10 +80,10 @@ async function run(
7980
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
8081
process.exitCode = 2
8182
logger.error(
82-
`
83+
stripIndents`
8384
${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
84-
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n
85-
`.trim()
85+
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
86+
`
8687
)
8788
return
8889
}

src/commands/audit-log/get-audit-log.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { stripIndents } from 'common-tags'
2+
13
import { logger } from '@socketsecurity/registry/lib/logger'
24
import { Separator, select } from '@socketsecurity/registry/lib/prompts'
35
import { SocketSdkReturnType } from '@socketsecurity/sdk'
@@ -108,7 +110,6 @@ async function outputAsMarkdown(
108110
page: number,
109111
perPage: number
110112
): Promise<void> {
111-
let md
112113
try {
113114
const table = mdTable(auditLogs, [
114115
'event_id',
@@ -119,8 +120,8 @@ async function outputAsMarkdown(
119120
'user_agent'
120121
])
121122

122-
md =
123-
`
123+
logger.log(
124+
stripIndents`
124125
# Socket Audit Logs
125126
126127
These are the Socket.dev audit logs as per requested query.
@@ -131,7 +132,8 @@ These are the Socket.dev audit logs as per requested query.
131132
- generated: ${new Date().toISOString()}
132133
133134
${table}
134-
`.trim() + '\n'
135+
`
136+
)
135137
} catch (e) {
136138
logger.error(
137139
'There was a problem converting the logs to JSON, please try without the `--json` flag'
@@ -140,8 +142,6 @@ ${table}
140142
process.exitCode = 1
141143
return
142144
}
143-
144-
logger.log(md)
145145
}
146146

147147
function mdTable<

src/commands/cdxgen/cmd-cdxgen.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,11 @@ async function run(
146146
//
147147
//
148148
// if (cli.input.length)
149-
// logger.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
150-
// - Unexpected arguments\n
149+
// logger.error(
150+
// stripIndents`
151+
// ${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
152+
//
153+
// - Unexpected arguments
151154
// `)
152155
// config.help(parentName, config)
153156
// return

src/commands/manifest/cmd-manifest-gradle.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22

3+
import { stripIndents } from 'common-tags'
34
import colors from 'yoctocolors-cjs'
45

56
import { logger } from '@socketsecurity/registry/lib/logger'
@@ -127,11 +128,11 @@ async function run(
127128
// options or missing arguments.
128129
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
129130
process.exitCode = 2
130-
logger.error(
131-
`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
132-
- The DIR arg is required ${!target ? colors.red('(missing!)') : target === '-' ? colors.red('(stdin is not supported)') : colors.green('(ok)')}\n
133-
- Can only accept one DIR (make sure to escape spaces!) ${cli.input.length > 1 ? colors.red(`(received ${cli.input.length}!)`) : colors.green('(ok)')}\n`
134-
)
131+
logger.error(stripIndents`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
132+
133+
- The DIR arg is required ${!target ? colors.red('(missing!)') : target === '-' ? colors.red('(stdin is not supported)') : colors.green('(ok)')}
134+
135+
- Can only accept one DIR (make sure to escape spaces!) ${cli.input.length > 1 ? colors.red(`(received ${cli.input.length}!)`) : colors.green('(ok)')}`)
135136
return
136137
}
137138

src/commands/manifest/cmd-manifest-kotlin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22

3+
import { stripIndents } from 'common-tags'
34
import colors from 'yoctocolors-cjs'
45

56
import { logger } from '@socketsecurity/registry/lib/logger'
@@ -133,9 +134,11 @@ async function run(
133134
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
134135
process.exitCode = 2
135136
logger.error(
136-
`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
137-
- The DIR arg is required ${!target ? colors.red('(missing!)') : target === '-' ? colors.red('(stdin is not supported)') : colors.green('(ok)')}\n
138-
- Can only accept one DIR (make sure to escape spaces!) ${cli.input.length > 1 ? colors.red(`(received ${cli.input.length}!)`) : colors.green('(ok)')}\n`
137+
stripIndents`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
138+
139+
- The DIR arg is required ${!target ? colors.red('(missing!)') : target === '-' ? colors.red('(stdin is not supported)') : colors.green('(ok)')}
140+
141+
- Can only accept one DIR (make sure to escape spaces!) ${cli.input.length > 1 ? colors.red(`(received ${cli.input.length}!)`) : colors.green('(ok)')}`
139142
)
140143
return
141144
}

0 commit comments

Comments
 (0)