-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathstate.ts
More file actions
37 lines (29 loc) · 1.25 KB
/
state.ts
File metadata and controls
37 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import fs from "fs-extra";
import * as yaml from "js-yaml";
import {withFileLock} from "./pid-file-lock.js";
const loadStateYML = async (stateFile: string): Promise<any> => {
if (!fs.existsSync(stateFile)) {
return {};
}
const stateFileContent = await fs.readFile(stateFile, "utf8");
return yaml.load(stateFileContent) || {};
};
const getPipelineIid = async (cwd: string, stateDir: string) => {
const stateFile = `${cwd}/${stateDir}/state.yml`;
const ymlData = await loadStateYML(stateFile);
return ymlData["pipelineIid"] ? ymlData["pipelineIid"] : 0;
};
const incrementPipelineIid = async (cwd: string, stateDir: string): Promise<number> => {
const stateFile = `${cwd}/${stateDir}/state.yml`;
const lockPath = `${cwd}/${stateDir}/state.lock`;
return withFileLock(lockPath, async () => {
const ymlData = await loadStateYML(stateFile);
const newIid = ymlData["pipelineIid"] != null ? ymlData["pipelineIid"] + 1 : 0;
ymlData["pipelineIid"] = newIid;
const tmpFile = `${stateFile}.tmp.${process.pid}`;
await fs.outputFile(tmpFile, `---\n${yaml.dump(ymlData)}`);
await fs.rename(tmpFile, stateFile);
return newIid;
});
};
export {getPipelineIid, incrementPipelineIid};