1- #!/usr/bin/env node
2- import yargs from "yargs" ;
3- import { hideBin } from "yargs/helpers" ;
1+ /*---------------------------------------------------------------------------------------------
2+ * Copyright (c) Equinor ASA
3+ * Licensed under the MIT License. See LICENSE in the project root for license information.
4+ *--------------------------------------------------------------------------------------------*/
5+
6+ import yargs , { CommandModule } from "yargs" ;
47import { TextDocument } from "vscode-languageserver-textdocument" ;
58import { SepticCnfg } from "../cnfg" ;
69import {
@@ -9,64 +12,12 @@ import {
912 SepticDiagnosticLevel ,
1013} from "../diagnostics" ;
1114import { SepticMetaInfoProvider } from "../metaInfoProvider" ;
12- import { validateFileExists , createDocumentFromFile , runCli } from "./utils" ;
15+ import { validateFileExists , createDocumentFromFile } from "./utils" ;
1316
1417interface LintOptions {
1518 file : string ;
1619 ignore ?: string [ ] ;
17- version ?: string ;
18- }
19-
20- function parseArguments ( ) : LintOptions {
21- const availableVersions = SepticMetaInfoProvider . getAvailableVersions ( ) ;
22- const versionList = availableVersions . join ( ", " ) ;
23-
24- const argv = yargs ( hideBin ( process . argv ) )
25- . command ( "$0 <file>" , "Lint a Septic config file" , ( yargs ) => {
26- return yargs . positional ( "file" , {
27- type : "string" ,
28- description : "Path to Septic config file to lint" ,
29- } ) ;
30- } )
31- . option ( "ignore" , {
32- alias : "i" ,
33- type : "array" ,
34- description : "Diagnostic codes to ignore (e.g., W101 E202)" ,
35- string : true ,
36- } )
37- . option ( "septicversion" , {
38- alias : "s" ,
39- type : "string" ,
40- description : `Septic version to use for linting (available: ${ versionList } )` ,
41- default : "latest" ,
42- } )
43- . example ( "$0 config.cnfg" , "Lint a config file" )
44- . example (
45- "$0 config.cnfg --ignore W101 W203" ,
46- "Lint and ignore specific diagnostic codes" ,
47- )
48- . example (
49- "$0 config.cnfg --septicversion v3.5" ,
50- "Lint using Septic version 3.5" ,
51- )
52- . help ( )
53- . parseSync ( ) ;
54-
55- if ( ! argv . file ) {
56- console . error ( "No file specified for linting." ) ;
57- process . exit ( 1 ) ;
58- }
59-
60- const options : LintOptions = {
61- file : argv . file as string ,
62- version : argv . septicversion as string ,
63- } ;
64-
65- if ( argv . ignore ) {
66- options . ignore = argv . ignore as string [ ] ;
67- }
68-
69- return options ;
20+ septicversion ?: string ;
7021}
7122
7223async function lintSepticConfig (
@@ -114,13 +65,12 @@ function printDiagnostics(diagnostics: SepticDiagnostic[], uri: string): void {
11465 }
11566}
11667
117- async function main ( ) : Promise < void > {
118- const options = parseArguments ( ) ;
68+ async function handler ( options : LintOptions ) : Promise < void > {
11969 validateFileExists ( options . file ) ;
12070
12171 // Set the version to use for linting
122- if ( options . version ) {
123- SepticMetaInfoProvider . setVersion ( options . version ) ;
72+ if ( options . septicversion ) {
73+ SepticMetaInfoProvider . setVersion ( options . septicversion ) ;
12474 }
12575
12676 const document = createDocumentFromFile ( options . file ) ;
@@ -142,4 +92,45 @@ async function main(): Promise<void> {
14292 process . exit ( exitCode ) ;
14393}
14494
145- runCli ( main ) ;
95+ export const lintCommand : CommandModule < object , LintOptions > = {
96+ command : "lint <file>" ,
97+ describe : "Lint a Septic config file" ,
98+ builder : ( yargs ) => {
99+ const availableVersions = SepticMetaInfoProvider . getAvailableVersions ( ) ;
100+ const versionList = availableVersions . join ( ", " ) ;
101+
102+ return yargs
103+ . positional ( "file" , {
104+ type : "string" ,
105+ description : "Path to Septic config file to lint" ,
106+ demandOption : true ,
107+ } )
108+ . option ( "ignore" , {
109+ alias : "i" ,
110+ type : "array" ,
111+ description : "Diagnostic codes to ignore (e.g., W101 E202)" ,
112+ string : true ,
113+ } )
114+ . option ( "septicversion" , {
115+ alias : "s" ,
116+ type : "string" ,
117+ description : `Septic version to use for linting (available: ${ versionList } )` ,
118+ default : "latest" ,
119+ } )
120+ . example ( "$0 lint config.cnfg" , "Lint a config file" )
121+ . example (
122+ "$0 lint config.cnfg --ignore W101 W203" ,
123+ "Lint and ignore specific diagnostic codes" ,
124+ )
125+ . example (
126+ "$0 lint config.cnfg --septicversion v3.5" ,
127+ "Lint using Septic version 3.5" ,
128+ ) as unknown as yargs . Argv < LintOptions > ;
129+ } ,
130+ handler : ( argv ) => {
131+ handler ( argv ) . catch ( ( error ) => {
132+ console . error ( "Unexpected error:" , error ) ;
133+ process . exit ( 1 ) ;
134+ } ) ;
135+ } ,
136+ } ;
0 commit comments