11'use strict' ;
22import * as fs from 'fs' ;
3+ import { copyFile } from 'fs/promises' ;
34import * as jsonc from 'jsonc-parser' ;
45import * as path from 'path' ;
56import * as vscode from 'vscode' ;
67import { ICommandAPI , ICommandCreator , IPreferencesAPI } from '../api' ;
78import { logger } from '../logger' ;
8- import { getClassName , ncpAsync } from '../utilities' ;
9+ import { getClassName } from '../utilities' ;
10+ import * as fileUtils from '../shared/fileUtils' ;
911
1012export interface ICppJsonLayout {
1113 name : string ;
@@ -25,158 +27,38 @@ async function performCopy(
2527 includeRoot : vscode . Uri ,
2628 replaceName : string
2729) : Promise < boolean > {
28- const commandFolder = path . join ( commandRoot , command . foldername ) ;
29- const copiedSrcFiles : string [ ] = [ ] ;
30- const copiedHeaderFiles : string [ ] = [ ] ;
31- await ncpAsync ( commandFolder , folderSrc . fsPath , {
32- filter : ( cf : string ) : boolean => {
33- if ( ! fs . lstatSync ( cf ) . isFile ( ) ) {
34- return true ;
35- }
36- const bn = path . basename ( cf ) ;
37- if ( command . source . indexOf ( bn ) > - 1 ) {
38- copiedSrcFiles . push ( path . relative ( commandFolder , cf ) ) ;
39- return true ;
40- } else {
41- return false ;
42- }
43- } ,
44- } ) ;
45-
46- await ncpAsync ( commandFolder , folderHeader . fsPath , {
47- filter : ( cf : string ) : boolean => {
48- if ( ! fs . lstatSync ( cf ) . isFile ( ) ) {
49- return true ;
50- }
51- const bn = path . basename ( cf ) ;
52- if ( command . headers . indexOf ( bn ) > - 1 ) {
53- copiedHeaderFiles . push ( path . relative ( commandFolder , cf ) ) ;
54- return true ;
55- } else {
56- return false ;
57- }
58- } ,
59- } ) ;
60-
61- let promiseArray : Promise < void > [ ] = [ ] ;
62-
63- for ( const f of copiedHeaderFiles ) {
64- const file = path . join ( folderHeader . fsPath , f ) ;
65- promiseArray . push (
66- new Promise < void > ( ( resolve , reject ) => {
67- fs . readFile ( file , 'utf8' , ( err , dataIn ) => {
68- if ( err ) {
69- reject ( err ) ;
70- } else {
71- const dataOut = dataIn . replace ( new RegExp ( command . replacename , 'g' ) , replaceName ) ;
72- fs . writeFile ( file , dataOut , 'utf8' , ( err1 ) => {
73- if ( err1 ) {
74- reject ( err ) ;
75- } else {
76- resolve ( ) ;
77- }
78- } ) ;
79- }
80- } ) ;
81- } )
30+ try {
31+ const commandFolder = path . join ( commandRoot , command . foldername ) ;
32+
33+ const renamedHeader = path . join ( folderHeader . fsPath , `${ replaceName } .h` ) ;
34+ const renamedSource = path . join ( folderSrc . fsPath , `${ replaceName } .cpp` ) ;
35+ // The source and header arrays are always one item long
36+ await copyFile ( path . join ( commandFolder , command . headers [ 0 ] ) , renamedHeader ) ;
37+ await copyFile ( path . join ( commandFolder , command . source [ 0 ] ) , renamedSource ) ;
38+
39+ // Process header files
40+ const headerReplacements = new Map < RegExp , string > ( ) ;
41+ headerReplacements . set ( new RegExp ( command . replacename , 'g' ) , replaceName ) ;
42+
43+ await fileUtils . processFile ( renamedHeader , headerReplacements ) ;
44+ // Process source files with more complex replacements
45+ const sourceReplacements = new Map < RegExp , string > ( ) ;
46+ const joinedName = path
47+ . join ( path . relative ( includeRoot . path , folderHeader . path ) , replaceName )
48+ . replace ( / \\ / g, '/' ) ;
49+
50+ sourceReplacements . set (
51+ new RegExp ( `#include "${ command . replacename } .h"` , 'g' ) ,
52+ `#include "${ joinedName } .h"`
8253 ) ;
83- }
84-
85- await Promise . all ( promiseArray ) ;
86-
87- promiseArray = [ ] ;
88-
89- for ( const f of copiedSrcFiles ) {
90- const file = path . join ( folderSrc . fsPath , f ) ;
91- promiseArray . push (
92- new Promise < void > ( ( resolve , reject ) => {
93- fs . readFile ( file , 'utf8' , ( err , dataIn ) => {
94- if ( err ) {
95- reject ( err ) ;
96- } else {
97- const joinedName = path
98- . join ( path . relative ( includeRoot . path , folderHeader . path ) , replaceName )
99- . replace ( / \\ / g, '/' ) ;
54+ sourceReplacements . set ( new RegExp ( command . replacename , 'g' ) , replaceName ) ;
10055
101- const dataOut = dataIn
102- . replace (
103- new RegExp ( `#include "${ command . replacename } .h"` , 'g' ) ,
104- `#include "${ joinedName } .h"`
105- )
106- . replace ( new RegExp ( command . replacename , 'g' ) , replaceName ) ;
107-
108- fs . writeFile ( file , dataOut , 'utf8' , ( err1 ) => {
109- if ( err1 ) {
110- reject ( err ) ;
111- } else {
112- resolve ( ) ;
113- }
114- } ) ;
115- }
116- } ) ;
117- } )
118- ) ;
56+ await fileUtils . processFile ( renamedSource , sourceReplacements ) ;
57+ return true ;
58+ } catch ( error ) {
59+ logger . error ( 'Error performing copy operation:' , error ) ;
60+ return false ;
11961 }
120-
121- await Promise . all ( promiseArray ) ;
122-
123- let movePromiseArray : Promise < void > [ ] = [ ] ;
124- for ( const f of copiedSrcFiles ) {
125- const file = path . join ( folderSrc . fsPath , f ) ;
126- const bname = path . basename ( file ) ;
127- const dirname = path . dirname ( file ) ;
128- if ( path . basename ( file ) . indexOf ( command . replacename ) > - 1 ) {
129- const newname = path . join (
130- dirname ,
131- bname . replace ( new RegExp ( command . replacename , 'g' ) , replaceName )
132- ) ;
133- movePromiseArray . push (
134- new Promise < void > ( ( resolve , reject ) => {
135- fs . rename ( file , newname , ( err ) => {
136- if ( err ) {
137- reject ( err ) ;
138- } else {
139- resolve ( ) ;
140- }
141- } ) ;
142- } )
143- ) ;
144- }
145- }
146-
147- if ( movePromiseArray . length > 0 ) {
148- await Promise . all ( movePromiseArray ) ;
149- }
150-
151- movePromiseArray = [ ] ;
152- for ( const f of copiedHeaderFiles ) {
153- const file = path . join ( folderHeader . fsPath , f ) ;
154- const bname = path . basename ( file ) ;
155- const dirname = path . dirname ( file ) ;
156- if ( path . basename ( file ) . indexOf ( command . replacename ) > - 1 ) {
157- const newname = path . join (
158- dirname ,
159- bname . replace ( new RegExp ( command . replacename , 'g' ) , replaceName )
160- ) ;
161- movePromiseArray . push (
162- new Promise < void > ( ( resolve , reject ) => {
163- fs . rename ( file , newname , ( err ) => {
164- if ( err ) {
165- reject ( err ) ;
166- } else {
167- resolve ( ) ;
168- }
169- } ) ;
170- } )
171- ) ;
172- }
173- }
174-
175- if ( movePromiseArray . length > 0 ) {
176- await Promise . all ( movePromiseArray ) ;
177- }
178-
179- return true ;
18062}
18163
18264export class Commands {
0 commit comments