Skip to content

Commit

Permalink
fix: 修复 update.json 可能不存在的问题 (#393)
Browse files Browse the repository at this point in the history
* fix: 修复 update.json 可能不存在的问题

* refactor(lock-file): 优化函数命名

* ci(test.yml): 所有 push 和 pr 都触发 Test action

Co-authored-by: alexqxxu <alexqxxu@tencent.com>
  • Loading branch information
codetker and alexqxxu authored Feb 17, 2022
1 parent f8d71da commit d34a520
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
19 changes: 15 additions & 4 deletions packages/feflow-cli/src/shared/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSONValue>((resolve, reject) => {
// 文件不存在则创建文件后读取
this.ensureFileExists();

fs.stat(this.filePath, (err, stats) => {
if (err) {
this.logger.error(err);
Expand Down Expand Up @@ -76,10 +82,9 @@ export default class LockFile {
public update(key: string, value: JSONValue): Promise<object | undefined> {
return new Promise((resolve, reject) => {
try {
if (!fs.existsSync(this.filePath)) {
// 文件不存在则创建文件后插入
this.resetFile();
}
// 文件不存在则创建文件后插入
this.ensureFileExists();

this.lock((err) => {
if (err) {
this.unlock();
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit d34a520

Please sign in to comment.