99
1010import * as path from 'path' ;
1111import * as os from 'os' ;
12+ import * as fs from 'fs' ;
1213import * as fg from 'fast-glob' ;
1314import { exec , find , mv , rm , which } from 'shelljs' ;
1415import { TestSession , execCmd } from '@salesforce/cli-plugins-testkit' ;
15- import { AsyncCreatable , Env , set } from '@salesforce/kit' ;
16+ import { AsyncCreatable , Env , set , parseJsonMap } from '@salesforce/kit' ;
1617import { AnyJson , Dictionary , ensureString , get , JsonMap , Nullable } from '@salesforce/ts-types' ;
17- import { AuthInfo , Config , Connection , fs , NamedPackageDir , SfdxProject } from '@salesforce/core' ;
18+ import { AuthInfo , SfdxPropertyKeys , Connection , NamedPackageDir , SfdxProject } from '@salesforce/core' ;
1819import { debug , Debugger } from 'debug' ;
1920import { MetadataResolver } from '@salesforce/source-deploy-retrieve' ;
2021import { Commands , Result , StatusResult } from './types' ;
@@ -74,7 +75,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
7475
7576 public constructor ( options : SourceTestkit . Options ) {
7677 super ( options ) ;
77- this . executable = options . executable || which ( 'sfdx' ) . stdout ;
78+ this . executable = options . executable ?? which ( 'sfdx' ) ? .stdout ?? 'sfdx' ;
7879 this . repository = options . repository ;
7980 this . orgless = ! ! options . orgless ;
8081 this . setupCommands = options . setupCommands || [ ] ;
@@ -209,7 +210,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
209210 const files = await this . doGlob ( globs ) ;
210211 const returnValue : Dictionary < string > = { } ;
211212 for ( const file of files ) {
212- returnValue [ file ] = await fs . readFile ( file , 'UTF-8 ' ) ;
213+ returnValue [ file ] = await fs . promises . readFile ( file , 'utf8 ' ) ;
213214 }
214215 return returnValue ;
215216 }
@@ -219,30 +220,30 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
219220 */
220221 public async readSourcePathInfos ( ) : Promise < AnyJson > {
221222 const sourcePathInfosPath = path . join ( this . projectDir , '.sfdx' , 'orgs' , this . username , 'sourcePathInfos.json' ) ;
222- return fs . readJson ( sourcePathInfosPath ) ;
223+ return JSON . parse ( await fs . promises . readFile ( sourcePathInfosPath , 'utf8' ) ) as AnyJson ;
223224 }
224225
225226 /**
226227 * Read the org's maxRevision.json
227228 */
228229 public async readMaxRevision ( ) : Promise < { sourceMembers : JsonMap } > {
229230 const maxRevisionPath = path . join ( this . projectDir , '.sfdx' , 'orgs' , this . username , 'maxRevision.json' ) ;
230- return fs . readJson ( maxRevisionPath ) as unknown as { sourceMembers : JsonMap } ;
231+ return JSON . parse ( await fs . promises . readFile ( maxRevisionPath , 'utf8' ) ) as { sourceMembers : JsonMap } ;
231232 }
232233
233234 /**
234235 * Write the org's maxRevision.json
235236 */
236237 public async writeMaxRevision ( contents : JsonMap ) : Promise < void > {
237238 const maxRevisionPath = path . join ( this . projectDir , '.sfdx' , 'orgs' , this . username , 'maxRevision.json' ) ;
238- return fs . writeJson ( maxRevisionPath , contents ) ;
239+ return fs . promises . writeFile ( maxRevisionPath , JSON . stringify ( contents ) ) ;
239240 }
240241
241242 /**
242243 * Write file
243244 */
244245 public async writeFile ( filename : string , contents : string ) : Promise < void > {
245- return fs . writeFile ( filename , contents ) ;
246+ return fs . promises . writeFile ( filename , contents ) ;
246247 }
247248
248249 /**
@@ -256,7 +257,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
256257</Package>
257258 ` ;
258259 const packageXmlPath = path . join ( this . projectDir , 'package.xml' ) ;
259- await fs . writeFile ( packageXmlPath , packageXml ) ;
260+ await fs . promises . writeFile ( packageXmlPath , packageXml ) ;
260261 return packageXmlPath ;
261262 }
262263
@@ -265,15 +266,15 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
265266 */
266267 public async deleteSourcePathInfos ( ) : Promise < void > {
267268 const sourcePathInfosPath = path . join ( this . projectDir , '.sfdx' , 'orgs' , this . username , 'sourcePathInfos.json' ) ;
268- return fs . unlink ( sourcePathInfosPath ) ;
269+ return fs . promises . unlink ( sourcePathInfosPath ) ;
269270 }
270271
271272 /**
272273 * Delete the org's maxRevision.json
273274 */
274275 public async deleteMaxRevision ( ) : Promise < void > {
275276 const maxRevisionPath = path . join ( this . projectDir , '.sfdx' , 'orgs' , this . username , 'maxRevision.json' ) ;
276- return fs . unlink ( maxRevisionPath ) ;
277+ return fs . promises . unlink ( maxRevisionPath ) ;
277278 }
278279
279280 /**
@@ -282,7 +283,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
282283 public async deleteGlobs ( globs : string [ ] ) : Promise < void > {
283284 const files = await this . doGlob ( globs ) ;
284285 for ( const file of files ) {
285- await fs . unlink ( file ) ;
286+ await fs . promises . unlink ( file ) ;
286287 }
287288 }
288289
@@ -291,8 +292,8 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
291292 */
292293 public async deleteAllSourceFiles ( ) : Promise < void > {
293294 for ( const pkg of this . packagePaths ) {
294- await fs . rmdir ( pkg , { recursive : true } ) ;
295- await fs . mkdirp ( pkg ) ;
295+ await fs . promises . rm ( pkg , { recursive : true } ) ;
296+ await fs . promises . mkdir ( pkg , { recursive : true } ) ;
296297 }
297298 }
298299
@@ -312,9 +313,9 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
312313 */
313314 public async modifyLocalFile ( file : string , append = os . EOL ) : Promise < void > {
314315 const fullPath = file . startsWith ( this . projectDir ) ? file : path . join ( this . projectDir , file ) ;
315- let contents = await fs . readFile ( fullPath , 'UTF -8' ) ;
316+ let contents = await fs . promises . readFile ( fullPath , 'utf -8' ) ;
316317 contents += append ;
317- await fs . writeFile ( fullPath , contents ) ;
318+ await fs . promises . writeFile ( fullPath , contents ) ;
318319 await this . fileTracker . update ( fullPath , 'modified file' ) ;
319320 }
320321
@@ -330,7 +331,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
330331 . sobject ( 'QuickActionDefinition' )
331332 . find ( { DeveloperName : 'NutAction' } , [ 'ID' ] ) ) ! ;
332333 const updateRequest = {
333- Id : result [ 0 ] . Id ,
334+ Id : result [ 0 ] . Id as string ,
334335 Description : 'updated description' ,
335336 } ;
336337 await this . connection ?. tooling . sobject ( 'QuickActionDefinition' ) . update ( updateRequest ) ;
@@ -348,7 +349,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
348349 try {
349350 fs . copyFileSync ( src , dest ) ;
350351 } catch {
351- await fs . mkdirp ( path . dirname ( dest ) ) ;
352+ await fs . promises . mkdir ( path . dirname ( dest ) , { recursive : true } ) ;
352353 fs . copyFileSync ( src , dest ) ;
353354 }
354355 }
@@ -488,7 +489,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
488489 . replace ( SourceTestkit . LocalProdPath , '' )
489490 . replace ( SourceTestkit . LocalDevPath , '' ) ;
490491 const pkgJsonPath = path . join ( npmPackagePath , 'package.json' ) ;
491- const pkgJson = ( await fs . readJsonMap ( pkgJsonPath ) ) as { oclif : { bin : string } } ;
492+ const pkgJson = parseJsonMap < { oclif : { bin : string } } > ( await fs . promises . readFile ( pkgJsonPath , 'utf8' ) ) ;
492493 return pkgJson . oclif . bin as Executable ;
493494 } else {
494495 return path . basename ( this . executable ) as Executable ;
@@ -536,7 +537,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
536537 }
537538
538539 private async getDefaultUsername ( ) : Promise < string > {
539- const configVar = this . executableName === Executable . SF ? 'target-org' : Config . DEFAULT_USERNAME ;
540+ const configVar = this . executableName === Executable . SF ? 'target-org' : SfdxPropertyKeys . DEFAULT_USERNAME ;
540541 const configResult = execCmd ( `config:get ${ configVar } --json` ) . jsonOutput ! ;
541542 const results = get ( configResult , 'result' , configResult ) as Array < { key ?: string ; name ?: string ; value : string } > ;
542543 const username = results . find ( ( r ) => r . key === configVar || r . name === configVar ) ! . value ;
0 commit comments