Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 update.json 可能不存在的问题 #393

Merged
merged 3 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}