1+ import chalk from "chalk" ;
2+ import { Convert , Location , Region , Result , Level } from "./typings/secret-lint-types" ;
3+
4+ interface SecretLintEngineResult {
5+ ok : boolean ;
6+ output : string ;
7+ }
8+
9+ interface SecretLintResult {
10+ ok : boolean ;
11+ results : Result [ ] ;
12+ }
13+
14+ const lintConfig = {
15+ rules : [
16+ {
17+ id : "@secretlint/secretlint-rule-preset-recommend" ,
18+ rules : [
19+ {
20+ "id" : "@secretlint/secretlint-rule-basicauth" ,
21+ "allowMessageIds" : [ "BasicAuth" ]
22+ }
23+ ]
24+ } , {
25+ id : "@secretlint/secretlint-rule-no-dotenv"
26+ }
27+
28+ ]
29+ } ;
30+
31+ const lintOptions = {
32+ configFileJSON : lintConfig ,
33+ formatter : "@secretlint/secretlint-formatter-sarif" , // checkstyle, compact, jslint-xml, junit, pretty-error, stylish, tap, unix, json, mask-result, table
34+ color : true ,
35+ maskSecrets : false
36+ } ;
37+
38+ // Helper function to dynamically import the createEngine function
39+ async function getEngine ( ) {
40+ // Use a raw dynamic import that will not be transformed
41+ // This is necessary because @secretlint /node is an ESM module
42+ const secretlintModule = await eval ( 'import("@secretlint/node")' ) ;
43+ const engine = await secretlintModule . createEngine ( lintOptions ) ;
44+ return engine ;
45+ }
46+
47+ export async function lintFiles (
48+ filePaths : string [ ]
49+ ) : Promise < SecretLintResult > {
50+ const engine = await getEngine ( ) ;
51+
52+ const engineResult = await engine . executeOnFiles ( {
53+ filePathList : filePaths
54+ } ) ;
55+ return parseResult ( engineResult ) ;
56+ }
57+
58+ export async function lintText (
59+ content : string ,
60+ fileName : string
61+ ) : Promise < SecretLintResult > {
62+ const engine = await getEngine ( ) ;
63+
64+ const engineResult = await engine . executeOnContent ( {
65+ content,
66+ filePath : fileName
67+ } ) ;
68+ return parseResult ( engineResult ) ;
69+ }
70+
71+ function parseResult ( result : SecretLintEngineResult ) : SecretLintResult {
72+ const output = Convert . toSecretLintOutput ( result . output ) ;
73+ const results = output . runs . at ( 0 ) ?. results ?? [ ] ;
74+ return { ok : result . ok , results } ;
75+ }
76+
77+ export function prettyPrintLintResult ( result : Result ) : string {
78+ if ( ! result . message . text ) {
79+ return JSON . stringify ( result ) ;
80+ }
81+
82+ const text = result . message . text ;
83+ const titleColor = result . level === undefined || result . level === Level . Error ? chalk . bold . red : chalk . bold . yellow ;
84+ const title = text . length > 54 ? text . slice ( 0 , 50 ) + '...' : text ;
85+ let output = `\t${ titleColor ( title ) } \n` ;
86+
87+ if ( result . locations ) {
88+ result . locations . forEach ( location => {
89+ output += `\t${ prettyPrintLocation ( location ) } \n` ;
90+ } ) ;
91+ }
92+ return output ;
93+ }
94+
95+ function prettyPrintLocation ( location : Location ) : string {
96+ if ( ! location . physicalLocation ) { return JSON . stringify ( location ) ; }
97+
98+ const uri = location . physicalLocation . artifactLocation ?. uri ;
99+ if ( ! uri ) { return JSON . stringify ( location ) ; }
100+
101+ let output = uri ;
102+
103+ const region = location . physicalLocation . region ;
104+ const regionStringified = region ? prettyPrintRegion ( region ) : undefined ;
105+ if ( regionStringified ) {
106+ output += `#${ regionStringified } ` ;
107+ }
108+
109+ return output ;
110+ }
111+
112+ function prettyPrintRegion ( region : Region ) : string | undefined {
113+ const startPosition = prettyPrintPosition ( region . startLine , region . startColumn ) ;
114+ const endPosition = prettyPrintPosition ( region . endLine , region . endColumn ) ;
115+
116+ if ( ! startPosition ) {
117+ return undefined ;
118+ }
119+
120+ let output = startPosition ;
121+ if ( endPosition && startPosition !== endPosition ) {
122+ output += `-${ endPosition } ` ;
123+ }
124+
125+ return output ;
126+ }
127+
128+ function prettyPrintPosition ( line : number | undefined , column : number | undefined ) : string | undefined {
129+ if ( line === undefined ) {
130+ return undefined ;
131+ }
132+ let output : string = line . toString ( ) ;
133+ if ( column !== undefined ) {
134+ output += `:${ column } ` ;
135+ }
136+
137+ return output ;
138+ }
0 commit comments