@@ -5,6 +5,8 @@ import { outputFixResult } from './output-fix-result.mts'
55import { pnpmFix } from './pnpm-fix.mts'
66import { CMD_NAME } from './shared.mts'
77import constants from '../../constants.mts'
8+ import { cmdFlagValueToArray } from '../../utils/cmd.mts'
9+ import { spawnCoana } from '../../utils/coana.mts'
810import { detectAndValidatePackageEnvironment } from '../../utils/package-environment.mts'
911
1012import type { OutputKind } from '../../types.mts'
@@ -15,6 +17,7 @@ const { NPM, PNPM } = constants
1517export async function handleFix ( {
1618 autoMerge,
1719 cwd,
20+ ghsas,
1821 limit,
1922 outputKind,
2023 purls,
@@ -24,28 +27,81 @@ export async function handleFix({
2427} : {
2528 autoMerge : boolean
2629 cwd : string
30+ ghsas : string [ ]
2731 limit : number
2832 outputKind : OutputKind
2933 purls : string [ ]
3034 rangeStyle : RangeStyle
3135 test : boolean
3236 testScript : string
3337} ) {
34- const pkgEnvResult = await detectAndValidatePackageEnvironment ( cwd , {
38+ let { length : ghsasCount } = ghsas
39+ if ( ghsasCount ) {
40+ // Lazily access constants.spinner.
41+ const { spinner } = constants
42+
43+ spinner . start ( )
44+
45+ if ( ghsasCount === 1 && ghsas [ 0 ] === 'auto' ) {
46+ const autoCResult = await spawnCoana (
47+ [ 'compute-fixes-and-upgrade-purls' , cwd ] ,
48+ { cwd, spinner } ,
49+ )
50+ if ( autoCResult . ok ) {
51+ console . log ( autoCResult . data )
52+ ghsas = cmdFlagValueToArray (
53+ / (?< = V u l n e r a b i l i t i e s f o u n d : ) [ ^ \n ] + / . exec (
54+ autoCResult . data as string ,
55+ ) ?. [ 0 ] ,
56+ )
57+ ghsasCount = ghsas . length
58+ } else {
59+ ghsas = [ ]
60+ ghsasCount = 0
61+ }
62+ }
63+
64+ spinner . stop ( )
65+
66+ if ( ghsasCount ) {
67+ spinner . start ( )
68+ await outputFixResult (
69+ await spawnCoana (
70+ [
71+ 'compute-fixes-and-upgrade-purls' ,
72+ cwd ,
73+ '--apply-fixes-to' ,
74+ ...ghsas ,
75+ ] ,
76+ { cwd, spinner } ,
77+ ) ,
78+ outputKind ,
79+ )
80+ spinner . stop ( )
81+ return
82+ }
83+ }
84+
85+ const pkgEnvCResult = await detectAndValidatePackageEnvironment ( cwd , {
3586 cmdName : CMD_NAME ,
3687 logger,
3788 } )
38- if ( ! pkgEnvResult . ok ) {
39- return pkgEnvResult
89+ if ( ! pkgEnvCResult . ok ) {
90+ await outputFixResult ( pkgEnvCResult , outputKind )
91+ return
4092 }
4193
42- const pkgEnvDetails = pkgEnvResult . data
94+ const { data : pkgEnvDetails } = pkgEnvCResult
4395 if ( ! pkgEnvDetails ) {
44- return {
45- ok : false ,
46- message : 'No package found' ,
47- cause : `No valid package environment was found in given cwd (${ cwd } )` ,
48- }
96+ await outputFixResult (
97+ {
98+ ok : false ,
99+ message : 'No package found' ,
100+ cause : `No valid package environment was found in given cwd (${ cwd } )` ,
101+ } ,
102+ outputKind ,
103+ )
104+ return
49105 }
50106
51107 logger . info (
@@ -54,27 +110,32 @@ export async function handleFix({
54110
55111 const { agent } = pkgEnvDetails
56112 if ( agent !== NPM && agent !== PNPM ) {
57- return {
58- ok : false ,
59- message : 'Not supported' ,
60- cause : `${ agent } is not supported by this command at the moment.` ,
61- }
113+ await outputFixResult (
114+ {
115+ ok : false ,
116+ message : 'Not supported' ,
117+ cause : `${ agent } is not supported by this command at the moment.` ,
118+ } ,
119+ outputKind ,
120+ )
121+ return
62122 }
63123
64124 // Lazily access spinner.
65125 const { spinner } = constants
66126 const fixer = agent === NPM ? npmFix : pnpmFix
67127
68- const result = await fixer ( pkgEnvDetails , {
69- autoMerge,
70- cwd,
71- limit,
72- purls,
73- rangeStyle,
74- spinner,
75- test,
76- testScript,
77- } )
78-
79- await outputFixResult ( result , outputKind )
128+ await outputFixResult (
129+ await fixer ( pkgEnvDetails , {
130+ autoMerge,
131+ cwd,
132+ limit,
133+ purls,
134+ rangeStyle,
135+ spinner,
136+ test,
137+ testScript,
138+ } ) ,
139+ outputKind ,
140+ )
80141}
0 commit comments