11import type { LocalImage } from '../images'
22import type { LocalImageImpl } from '../images/local-image'
33import type { DeployedDevModel , DeployedImageOptions , FullDeployedImageOptions , ProductNameable } from './list'
4+ import { DeployError } from '../errors'
45
56export interface Device {
67 setUuid ( uuid : `${string } -${string } -${string } -${string } -${string } `) : this
@@ -239,31 +240,55 @@ class ImageDeployerImpl implements Device {
239240 . join ( '\n' ) } \n`
240241 }
241242
242- private writeLists ( config : FullDeployedImageOptions ) : void | Error {
243+ private async writeToListWithCheck ( config : FullDeployedImageOptions ) : Promise < void | DeployError > {
243244 const fs = this . image . getImageManager ( ) . getOptions ( ) . fs
244245 const path = this . image . getImageManager ( ) . getOptions ( ) . path
245246 const listsPath = path . resolve ( this . image . getImageManager ( ) . getOptions ( ) . deployedPath , 'lists.json' )
246- if ( ! fs . existsSync ( listsPath ) ) {
247- fs . writeFileSync ( listsPath , JSON . stringify ( [ config ] , null , 2 ) )
247+
248+ try {
249+ if ( ! fs . existsSync ( listsPath ) ) {
250+ fs . writeFileSync ( listsPath , JSON . stringify ( [ config ] , null , 2 ) )
251+ }
252+ else {
253+ const lists : FullDeployedImageOptions [ ] = JSON . parse ( fs . readFileSync ( listsPath , 'utf-8' ) ) ?? [ ]
254+ if ( ! Array . isArray ( lists ) )
255+ return new DeployError ( DeployError . Code . LIST_JSON_NOT_AN_ARRAY , 'Lists is not an array' )
256+ if ( lists . find ( item => item . name === config . name ) )
257+ return
258+ lists . push ( config )
259+ fs . writeFileSync ( listsPath , JSON . stringify ( lists , null , 2 ) )
260+ }
248261 }
249- else {
250- const lists : FullDeployedImageOptions [ ] = JSON . parse ( fs . readFileSync ( listsPath , 'utf-8' ) ) ?? [ ]
251- if ( ! Array . isArray ( lists ) )
252- return new Error ( 'Lists is not an array' )
253- if ( lists . find ( item => item . name === config . name ) )
254- return
255- lists . push ( config )
256- fs . writeFileSync ( listsPath , JSON . stringify ( lists , null , 2 ) )
262+ catch ( err ) {
263+ return err instanceof Error ? new DeployError ( DeployError . Code . CATCHED_ERROR , String ( err ) ) : new DeployError ( DeployError . Code . CATCHED_ERROR , String ( err ) )
257264 }
265+
266+ return new Promise ( ( resolve ) => {
267+ const timer = setTimeout ( ( ) => {
268+ const content : FullDeployedImageOptions [ ] = JSON . parse ( fs . readFileSync ( listsPath , 'utf-8' ) ) ?? [ ]
269+
270+ if ( ! Array . isArray ( content ) ) {
271+ resolve ( new DeployError ( DeployError . Code . LIST_JSON_NOT_AN_ARRAY , 'List rechecker is not an array!' ) )
272+ }
273+ else if ( ! content . find ( item => item . name === config . name ) ) {
274+ resolve ( new DeployError ( DeployError . Code . MAYBE_OPENED_DEVICE_MANAGER_IN_DEVECO_STUDIO , `Device ${ config . name } not found in lists.json! Maybe not deployed yet?` ) )
275+ }
276+ else {
277+ resolve ( )
278+ }
279+
280+ clearTimeout ( timer )
281+ } , 1000 )
282+ } )
258283 }
259284
260- async deploy ( symlinkImage : boolean = true ) : Promise < void | Error > {
285+ async deploy ( symlinkImage : boolean = true ) : Promise < void | DeployError > {
261286 const { fs, path, imageBasePath, sdkPath } = this . image . getImageManager ( ) . getOptions ( )
262287 const config = await this . buildList ( )
263288 if ( fs . existsSync ( config . path ) )
264- return new Error ( `Image ${ config . name } already deployed` )
289+ return new DeployError ( DeployError . Code . DEVICE_ALREADY_DEPLOYED , `Image ${ config . name } already deployed` )
265290
266- const error = this . writeLists ( config )
291+ const error = await this . writeToListWithCheck ( config )
267292 if ( error )
268293 return error
269294
@@ -279,7 +304,7 @@ class ImageDeployerImpl implements Device {
279304 if ( stat . isSymbolicLink ( ) )
280305 fs . unlinkSync ( symlinkSdkPath )
281306 else
282- return undefined
307+ return new DeployError ( DeployError . Code . SYMLINK_SDK_PATH_EXISTS , 'Symlink SDK path already exists' )
283308 }
284309 catch {
285310 // 路径不存在,继续创建
@@ -294,10 +319,9 @@ class ImageDeployerImpl implements Device {
294319 fs . symlinkSync ( target , linkPath )
295320 }
296321 catch ( err ) {
297- return err instanceof Error ? err : new Error ( String ( err ) )
322+ return err instanceof Error ? new DeployError ( DeployError . Code . CATCHED_ERROR , String ( err ) ) : new DeployError ( DeployError . Code . CATCHED_ERROR , String ( err ) )
298323 }
299324 }
300- return undefined
301325 }
302326
303327 async isDeployed ( ) : Promise < boolean > {
0 commit comments