1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | HandlerContext,
|
19 | logger,
|
20 | } from "@atomist/automation-client";
|
21 | import { PreferenceStoreFactory } from "@atomist/sdm";
|
22 | import * as fs from "fs-extra";
|
23 | import * as _ from "lodash";
|
24 | import * as os from "os";
|
25 | import * as path from "path";
|
26 | import {
|
27 | lock,
|
28 | unlock,
|
29 | } from "proper-lockfile";
|
30 | import {
|
31 | AbstractPreferenceStore,
|
32 | Preference,
|
33 | } from "./AbstractPreferenceStore";
|
34 |
|
35 | type PreferenceFile = Record<string, { name: string, value: string, ttl?: number }>;
|
36 |
|
37 | type WithPreferenceFile<V> = (p: PreferenceFile) => Promise<{ value?: V, save: boolean }>;
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | export const FilePreferenceStoreFactory: PreferenceStoreFactory = ctx => new FilePreferenceStore(ctx);
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export class FilePreferenceStore extends AbstractPreferenceStore {
|
50 |
|
51 | constructor(context: HandlerContext,
|
52 | private readonly filePath: string = path.join(os.homedir(), ".atomist", "prefs", "client.prefs.json")) {
|
53 | super(context);
|
54 | this.init();
|
55 | }
|
56 |
|
57 | protected async doGet(name: string, namespace: string): Promise<Preference | undefined> {
|
58 | const key = this.scopeKey(name, namespace);
|
59 | return this.doWithPreferenceFile<Preference | undefined>(async prefs => {
|
60 | if (!!prefs[key]) {
|
61 | return {
|
62 | save: false,
|
63 | value: {
|
64 | name,
|
65 | namespace,
|
66 | value: prefs[key].value,
|
67 | ttl: prefs[key].ttl,
|
68 | },
|
69 | };
|
70 | } else {
|
71 | return {
|
72 | save: false,
|
73 | value: undefined,
|
74 | };
|
75 | }
|
76 | });
|
77 | }
|
78 |
|
79 | protected async doPut(pref: Preference): Promise<void> {
|
80 | return this.doWithPreferenceFile<void>(async prefs => {
|
81 | const key = this.scopeKey(pref.name, pref.namespace);
|
82 | prefs[key] = {
|
83 | name: pref.name,
|
84 | value: pref.value,
|
85 | ttl: typeof pref.ttl === "number" ? Date.now() + pref.ttl : undefined,
|
86 | };
|
87 | return {
|
88 | save: true,
|
89 | };
|
90 | });
|
91 | }
|
92 |
|
93 | protected doList(namespace: string): Promise<Preference[]> {
|
94 | return this.doWithPreferenceFile<Preference[]>(async prefs => {
|
95 | const values: Preference[] = [];
|
96 | _.forEach(prefs, (v, k) => {
|
97 | if (!namespace || k.startsWith(`${namespace}_$_`)) {
|
98 | values.push(v as Preference);
|
99 | }
|
100 | });
|
101 | return {
|
102 | save: false,
|
103 | value: values,
|
104 | };
|
105 | });
|
106 | }
|
107 |
|
108 | protected doDelete(pref: string, namespace: string): Promise<void> {
|
109 | return this.doWithPreferenceFile<void>(async prefs => {
|
110 | const key = this.scopeKey(pref, namespace);
|
111 | delete prefs[key];
|
112 | return {
|
113 | save: true,
|
114 | };
|
115 | });
|
116 | }
|
117 |
|
118 | private async read(): Promise<PreferenceFile> {
|
119 | return (await fs.readJson(this.filePath)) as PreferenceFile;
|
120 | }
|
121 |
|
122 | private async doWithPreferenceFile<V>(withPreferenceFile: WithPreferenceFile<V>): Promise<V> {
|
123 | await lock(this.filePath, { retries: 5 });
|
124 | const prefs = await this.read();
|
125 | let result;
|
126 | try {
|
127 | result = await withPreferenceFile(prefs);
|
128 | if (result.save) {
|
129 | await fs.writeJson(this.filePath, prefs);
|
130 | }
|
131 | } catch (e) {
|
132 | logger.error(`Operation on preference file failed: ${e.message}`);
|
133 | }
|
134 | await unlock(this.filePath);
|
135 | return result.value as V;
|
136 | }
|
137 |
|
138 | private init(): void {
|
139 | fs.ensureDirSync(path.dirname(this.filePath));
|
140 | try {
|
141 | fs.readFileSync(this.filePath);
|
142 | } catch (e) {
|
143 | fs.writeJsonSync(this.filePath, {});
|
144 | }
|
145 | }
|
146 |
|
147 | }
|