Skip to content

Commit 4f62b4a

Browse files
committed
feat: add DeployError exception when deploy the image
1 parent 36cb383 commit 4f62b4a

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

.changeset/smart-dancers-stay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@arkts/image-manager": patch
3+
---
4+
5+
feat: add DeployError exception when deploy the image

src/deployer/image-deployer.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { LocalImage } from '../images'
22
import type { LocalImageImpl } from '../images/local-image'
33
import type { DeployedDevModel, DeployedImageOptions, FullDeployedImageOptions, ProductNameable } from './list'
4+
import { DeployError } from '../errors'
45

56
export 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> {

src/errors/deploy-error.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export class DeployError extends Error {
2+
constructor(
3+
public readonly code: DeployError.Code,
4+
public readonly message: string,
5+
public readonly cause?: Error,
6+
) {
7+
super(message, { cause })
8+
}
9+
}
10+
11+
export namespace DeployError {
12+
export enum Code {
13+
DEVICE_ALREADY_DEPLOYED = 'DEVICE_ALREADY_DEPLOYED',
14+
LIST_JSON_NOT_AN_ARRAY = 'LIST_JSON_NOT_AN_ARRAY',
15+
CATCHED_ERROR = 'CATCHED_ERROR',
16+
MAYBE_OPENED_DEVICE_MANAGER_IN_DEVECO_STUDIO = 'MAYBE_OPENED_DEVICE_MANAGER_IN_DEVECO_STUDIO',
17+
SYMLINK_SDK_PATH_EXISTS = 'SYMLINK_SDK_PATH_EXISTS',
18+
}
19+
}

src/errors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './deploy-error'
12
export * from './request-url-error'

0 commit comments

Comments
 (0)