1- import fs from 'node:fs'
1+ import { existsSync , readFileSync } from 'node:fs'
22import path from 'node:path'
33
44import { logger } from '@socketsecurity/registry/lib/logger'
5+ import { stripAnsi } from '@socketsecurity/registry/lib/strings'
56
67import type { CResult } from '../../types.mts'
78
9+ function prepareContent ( content : string ) : string {
10+ return stripAnsi ( content . trim ( ) )
11+ }
12+
813export async function convertCondaToRequirements (
914 filename : string ,
1015 cwd : string ,
1116 verbose : boolean ,
12- ) : Promise < CResult < { contents : string ; pip : string } > > {
13- let contents : string
17+ ) : Promise < CResult < { content : string ; pip : string } > > {
18+ let content : string
1419 if ( filename === '-' ) {
1520 if ( verbose ) {
1621 logger . info ( `[VERBOSE] reading input from stdin` )
1722 }
1823
19- const buf : string [ ] = [ ]
20- contents = await new Promise ( ( resolve , reject ) => {
24+ const strings : string [ ] = [ ]
25+ content = await new Promise ( ( resolve , reject ) => {
2126 process . stdin . on ( 'data' , chunk => {
2227 const input = chunk . toString ( )
23- buf . push ( input )
28+ strings . push ( input )
2429 } )
2530 process . stdin . on ( 'end' , ( ) => {
26- resolve ( buf . join ( '' ) )
31+ resolve ( prepareContent ( strings . join ( '' ) ) )
2732 } )
2833 process . stdin . on ( 'error' , e => {
2934 if ( verbose ) {
@@ -32,23 +37,23 @@ export async function convertCondaToRequirements(
3237 reject ( e )
3338 } )
3439 process . stdin . on ( 'close' , ( ) => {
35- if ( buf . length === 0 ) {
36- if ( verbose ) {
37- logger . error ( 'stdin closed explicitly without data received' )
38- }
39- reject ( new Error ( 'No data received from stdin' ) )
40- } else {
40+ if ( strings . length ) {
4141 if ( verbose ) {
4242 logger . error (
4343 'warning: stdin closed explicitly with some data received' ,
4444 )
4545 }
46- resolve ( buf . join ( '' ) )
46+ resolve ( prepareContent ( strings . join ( '' ) ) )
47+ } else {
48+ if ( verbose ) {
49+ logger . error ( 'stdin closed explicitly without data received' )
50+ }
51+ reject ( new Error ( 'No data received from stdin' ) )
4752 }
4853 } )
4954 } )
5055
51- if ( ! contents ) {
56+ if ( ! content ) {
5257 return {
5358 ok : false ,
5459 message : 'Manifest Generation Failed' ,
@@ -62,17 +67,17 @@ export async function convertCondaToRequirements(
6267 logger . info ( `[VERBOSE] target: ${ filepath } ` )
6368 }
6469
65- if ( ! fs . existsSync ( filepath ) ) {
70+ if ( ! existsSync ( filepath ) ) {
6671 return {
6772 ok : false ,
6873 message : 'Manifest Generation Failed' ,
6974 cause : `The file was not found at ${ filepath } ` ,
7075 }
7176 }
7277
73- contents = fs . readFileSync ( filepath , 'utf8' )
78+ content = readFileSync ( filepath , 'utf8' )
7479
75- if ( ! contents ) {
80+ if ( ! content ) {
7681 return {
7782 ok : false ,
7883 message : 'Manifest Generation Failed' ,
@@ -84,59 +89,58 @@ export async function convertCondaToRequirements(
8489 return {
8590 ok : true ,
8691 data : {
87- contents ,
88- pip : convertCondaToRequirementsFromInput ( contents ) ,
92+ content ,
93+ pip : convertCondaToRequirementsFromInput ( content ) ,
8994 } ,
9095 }
9196}
9297
9398// Just extract the first pip block, if one exists at all.
9499export function convertCondaToRequirementsFromInput ( input : string ) : string {
95- const keeping : string [ ] = [ ]
96100 let collecting = false
97101 let delim = '-'
98102 let indent = ''
99- input . split ( '\n' ) . some ( line => {
100- if ( ! line ) {
101- // Ignore empty lines
102- return
103+ const keeping : string [ ] = [ ]
104+ for ( const line of input . split ( '\n' ) ) {
105+ const trimmed = line . trim ( )
106+ if ( ! trimmed ) {
107+ // Ignore empty lines.
108+ continue
103109 }
104110 if ( collecting ) {
105111 if ( line . startsWith ( '#' ) ) {
106- // Ignore comment lines (keep?)
107- return
112+ // Ignore comment lines (keep?).
113+ continue
108114 }
109115 if ( line . startsWith ( delim ) ) {
110116 // In this case we have a line with the same indentation as the
111117 // `- pip:` line, so we have reached the end of the pip block.
112- return true // the end
113- } else {
114- if ( ! indent ) {
115- // Store the indentation of the block
116- if ( line . trim ( ) . startsWith ( '-' ) ) {
117- indent = line . split ( '-' ) [ 0 ] + '-'
118- if ( indent . length <= delim . length ) {
119- // The first line after the `pip:` line does not indent further
120- // than that so the block is empty?
121- return true
122- }
118+ break
119+ }
120+ if ( ! indent ) {
121+ // Store the indentation of the block.
122+ if ( trimmed . startsWith ( '-' ) ) {
123+ indent = line . split ( '-' ) [ 0 ] + '-'
124+ if ( indent . length <= delim . length ) {
125+ // The first line after the `pip:` line does not indent further
126+ // than that so the block is empty?
127+ break
123128 }
124129 }
125- if ( line . startsWith ( indent ) ) {
126- keeping . push ( line . slice ( indent . length ) . trim ( ) )
127- } else {
128- // Unexpected input. bail.
129- return true
130- }
131130 }
132- } else {
133- // Note: the line may end with a line comment so don't === it.
134- if ( line . trim ( ) . startsWith ( '- pip:' ) ) {
135- delim = line . split ( '-' ) [ 0 ] + '-'
136- collecting = true
131+ if ( line . startsWith ( indent ) ) {
132+ keeping . push ( line . slice ( indent . length ) . trim ( ) )
133+ } else {
134+ // Unexpected input. bail.
135+ break
137136 }
138137 }
139- } )
138+ // Note: the line may end with a line comment so don't === it.
139+ else if ( trimmed . startsWith ( '- pip:' ) ) {
140+ delim = line . split ( '-' ) [ 0 ] + '-'
141+ collecting = true
142+ }
143+ }
140144
141- return keeping . join ( '\n' )
145+ return prepareContent ( keeping . join ( '\n' ) )
142146}
0 commit comments