1+ import { isWindows } from '../../../../common/utils/platformUtils' ;
2+
13export function hasStartupCode ( content : string , start : string , end : string , keys : string [ ] ) : boolean {
2- // Normalize line endings to \n
34 const normalizedContent = content . replace ( / \r \n / g, '\n' ) ;
45 const startIndex = normalizedContent . indexOf ( start ) ;
56 const endIndex = normalizedContent . indexOf ( end ) ;
@@ -10,45 +11,46 @@ export function hasStartupCode(content: string, start: string, end: string, keys
1011 return contentBetween . length > 0 && keys . every ( ( key ) => contentBetween . includes ( key ) ) ;
1112}
1213
13- export function insertStartupCode ( content : string , start : string , end : string , code : string ) : string {
14- // Detect line ending style from content (default to \n if cannot determine)
15- const lineEnding = content . includes ( '\r\n' ) ? '\r\n' : '\n' ;
14+ function getLineEndings ( content : string ) : string {
15+ if ( content . includes ( '\r\n' ) ) {
16+ return '\r\n' ;
17+ } else if ( content . includes ( '\n' ) ) {
18+ return '\n' ;
19+ }
20+ return isWindows ( ) ? '\r\n' : '\n' ;
21+ }
1622
17- // Normalize line endings to \n for processing
23+ export function insertStartupCode ( content : string , start : string , end : string , code : string ) : string {
24+ let lineEnding = getLineEndings ( content ) ;
1825 const normalizedContent = content . replace ( / \r \n / g, '\n' ) ;
26+
1927 const startIndex = normalizedContent . indexOf ( start ) ;
2028 const endIndex = normalizedContent . indexOf ( end ) ;
2129
2230 let result : string ;
2331 if ( startIndex !== - 1 && endIndex !== - 1 && startIndex < endIndex ) {
24- // Both markers exist in correct order
2532 result =
2633 normalizedContent . substring ( 0 , startIndex + start . length ) +
2734 '\n' +
2835 code +
2936 '\n' +
3037 normalizedContent . substring ( endIndex ) ;
3138 } else if ( startIndex !== - 1 ) {
32- // Only start marker exists - truncate everything after the start marker
3339 result = normalizedContent . substring ( 0 , startIndex + start . length ) + '\n' + code + '\n' + end + '\n' ;
3440 } else {
35- // No markers or only end marker exists
3641 result = normalizedContent + '\n' + start + '\n' + code + '\n' + end + '\n' ;
3742 }
3843
39- // Restore original line ending style
4044 if ( lineEnding === '\r\n' ) {
4145 result = result . replace ( / \n / g, '\r\n' ) ;
4246 }
4347 return result ;
4448}
4549
4650export function removeStartupCode ( content : string , start : string , end : string ) : string {
47- // Detect line ending style from content (default to \n if cannot determine)
48- const lineEnding = content . includes ( '\r\n' ) ? '\r\n' : '\n' ;
49-
50- // Normalize line endings to \n for processing
51+ let lineEnding = getLineEndings ( content ) ;
5152 const normalizedContent = content . replace ( / \r \n / g, '\n' ) ;
53+
5254 const startIndex = normalizedContent . indexOf ( start ) ;
5355 const endIndex = normalizedContent . indexOf ( end ) ;
5456
@@ -67,7 +69,6 @@ export function removeStartupCode(content: string, start: string, end: string):
6769 result = before + after ;
6870 }
6971
70- // Restore original line ending style
7172 if ( lineEnding === '\r\n' ) {
7273 result = result . replace ( / \n / g, '\r\n' ) ;
7374 }
0 commit comments