From d34a520fafc231fb9b316c222030fd48c450fe40 Mon Sep 17 00:00:00 2001 From: CodeTker <925639912@qq.com> Date: Thu, 17 Feb 2022 19:57:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20update.json=20?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#393)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复 update.json 可能不存在的问题 * refactor(lock-file): 优化函数命名 * ci(test.yml): 所有 push 和 pr 都触发 Test action Co-authored-by: alexqxxu --- .github/workflows/test.yml | 11 +---------- packages/feflow-cli/src/shared/lock-file.ts | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b4523c0c..78de90af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,15 +1,6 @@ name: Test -on: - push: - branches: - - master - - release - - feat_* - - feat/* - - fix_* - - fix/* - workflow_call: null +on: [push, pull_request, workflow_call] jobs: test: diff --git a/packages/feflow-cli/src/shared/lock-file.ts b/packages/feflow-cli/src/shared/lock-file.ts index b2266293..4fff389f 100644 --- a/packages/feflow-cli/src/shared/lock-file.ts +++ b/packages/feflow-cli/src/shared/lock-file.ts @@ -17,10 +17,16 @@ export default class LockFile { this.tryMax = 50; this.tryGap = 100; this.tryCount = 0; + + // 初始化的时候如果不存在也创建 + this.ensureFileExists(); } public read(key?: string) { return new Promise((resolve, reject) => { + // 文件不存在则创建文件后读取 + this.ensureFileExists(); + fs.stat(this.filePath, (err, stats) => { if (err) { this.logger.error(err); @@ -76,10 +82,9 @@ export default class LockFile { public update(key: string, value: JSONValue): Promise { return new Promise((resolve, reject) => { try { - if (!fs.existsSync(this.filePath)) { - // 文件不存在则创建文件后插入 - this.resetFile(); - } + // 文件不存在则创建文件后插入 + this.ensureFileExists(); + this.lock((err) => { if (err) { this.unlock(); @@ -170,4 +175,10 @@ export default class LockFile { err && this.logger.error(`unlock ${this.lockKey} error: `, err); }); } + + private ensureFileExists(): void { + if (!fs.existsSync(this.filePath)) { + this.resetFile(); + } + } }