-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathhooks.tsx
162 lines (148 loc) · 5.32 KB
/
hooks.tsx
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { Context, ContextFileNotFoundError, ContextNotFoundError, MissingCurrentContextError } from './models';
import { ContextService } from './contextService';
import { container } from 'tsyringe';
import { SpecificationFile } from '../validation';
import * as messages from '../../messages';
export type Result = {
response?: any,
error?: Error
}
export const useContextFile = (): any => {
const contextService: ContextService = container.resolve(ContextService);
return {
list: (): Result => {
try {
const ctx: Context = contextService.loadContextFile();
const response = Object.keys(ctx.store).map(c => ({ key: c, path: ctx.store[String(c)] }));
return { response };
} catch (error) {
return { error };
}
},
current: (): Result => {
try {
const ctx: Context = contextService.loadContextFile();
if (!ctx.current) { throw new MissingCurrentContextError(); }
const response = { key: ctx.current, path: ctx.store[ctx.current] };
return { response };
} catch (error) {
return { error };
}
},
addContext: (key: string, specFile: SpecificationFile): Result => {
try {
const ctx = contextService.loadContextFile();
const updatedContext = contextService.addContext(ctx, key, specFile);
contextService.save(updatedContext);
const response = messages.NEW_CONTEXT_ADDED(key);
return { response };
} catch (error) {
if (error instanceof ContextFileNotFoundError) {
const context: Context = { current: '', store: {} };
try {
const newContext = contextService.addContext(context, key, specFile);
contextService.save(contextService.updateCurrent(newContext, key));
const response = messages.NEW_CONTEXT_ADDED(key);
return { response };
} catch (error) {
return { error };
}
}
return { error };
}
},
setCurrent: (key: string): Result => {
try {
const ctx = contextService.loadContextFile();
const updateCurrent = contextService.updateCurrent(ctx, key);
contextService.save(updateCurrent);
const response = { key: updateCurrent.current, path: updateCurrent.store[updateCurrent.current] };
return { response };
} catch (error) {
return { error };
}
},
deleteContext: (key: string): Result => {
try {
const ctx = contextService.loadContextFile();
if (Object.keys(ctx.store).length === 1) {
contextService.deleteContextFile();
return { response: messages.CONTEXT_DELETED };
}
const updatedContext = contextService.deleteContext(ctx, key);
contextService.save(updatedContext);
const response = messages.CONTEXT_DELETED;
return { response };
} catch (error) {
return { error };
}
},
loadSpecFile: (): Result => {
try {
let response: SpecificationFile;
const autoDetectedSpecFile = contextService.autoDetectSpecFile();
if (autoDetectedSpecFile) {
response = new SpecificationFile(autoDetectedSpecFile);
return { response };
}
const context = contextService.loadContextFile();
const currentCtx = context.store[context.current];
if (!currentCtx) {
throw new MissingCurrentContextError();
}
response = new SpecificationFile(currentCtx);
return { response };
} catch (error) {
return { error };
}
},
getContext: (key: string): Result => {
try {
const ctx = contextService.loadContextFile();
const ctxValue = ctx.store[String(key)];
if (!ctxValue) { throw new ContextNotFoundError(key); }
const response = new SpecificationFile(ctxValue);
return { response };
} catch (error) {
return { error };
}
}
};
};
export interface useSpecFileInput {
file?: string,
context?: string
}
export interface useSpecFileOutput {
specFile?: SpecificationFile,
error?: Error
}
export const useSpecfile = (flags: useSpecFileInput): useSpecFileOutput => {
const contextService: ContextService = container.resolve(ContextService);
try {
if (flags.file) {
const specFile: SpecificationFile = new SpecificationFile(flags.file);
if (specFile.isNotValid()) { throw new Error('Invalid spec path'); }
return { specFile };
}
const ctx: Context = contextService.loadContextFile();
if (flags.context) {
const ctxFile = ctx.store[flags.context];
if (!ctxFile) { throw new ContextNotFoundError(flags.context); }
const specFile = new SpecificationFile(ctxFile);
return { specFile };
}
if (ctx.current) {
const currentFile = ctx.store[ctx.current];
if (!currentFile) { throw new MissingCurrentContextError(); }
const specFile = new SpecificationFile(currentFile);
return { specFile };
}
const autoDetectedSpecPath = contextService.autoDetectSpecFile();
if (typeof autoDetectedSpecPath === 'undefined') { throw new Error('No spec path found in your working directory, please use flags or store a context'); }
const specFile = new SpecificationFile(autoDetectedSpecPath);
return { specFile };
} catch (error) {
return { error };
}
};