-
Notifications
You must be signed in to change notification settings - Fork 19
/
PublishService.ts
232 lines (196 loc) · 7.4 KB
/
PublishService.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { container, InjectionToken, singleton } from 'tsyringe';
import { ref, InjectionKey, Ref, toRaw, reactive, computed } from 'vue';
import type EventEmitter from 'eventemitter3';
import { PluginDataRepository } from '../repository/PluginDataRepository';
import { JoplinDataRepository } from '../repository/JoplinDataRepository';
import {
Github,
GeneratingProgress,
PublishingProgress,
initialGeneratingProgress,
initialPublishProgress,
PublishResults,
PublishError,
DEFAULT_GITHUB,
} from '../model/Publishing';
import { AppService, FORBIDDEN } from './AppService';
import { isEmpty, noop, omit, pick, some } from 'lodash';
export enum GeneratorEvents {
PageGenerated = 'pageGenerated',
}
export enum GitEvents {
Progress = 'progress',
Message = 'message',
Terminated = 'terminated',
LocalRepoStatusChanged = 'localRepoStatusChanged',
}
export enum LocalRepoStatus {
Ready,
Fail,
Initializing,
MissingRepository,
}
export enum GithubClientEvents {
InfoChanged = 'infoChanged',
}
export interface GithubClient extends EventEmitter<GithubClientEvents> {
init(github: Github): void;
createRepository(): Promise<void>;
getRepositoryUrl(): string;
getRepositoryName(): string;
getDefaultRepositoryName(): string;
getGithubInfo(): Readonly<Github>;
}
const PUBLISH_RESULT_MESSAGE: Record<PublishResults, string> = {
[PublishResults.Terminated]: 'Publishing terminated.',
[PublishResults.Fail]:
'This is an unexpected error, you can retry, and report it as a Github issue',
[PublishResults.Success]: '',
};
export interface Git extends EventEmitter<GitEvents> {
init: (github: GithubClient, dir: string) => Promise<void>;
push: (files: string[], init: boolean) => Promise<void>;
terminate: () => void;
}
export interface Generator extends EventEmitter<GeneratorEvents> {
generateSite: () => Promise<string[]>;
getOutputDir: () => Promise<string>;
}
export const gitClientToken: InjectionToken<Git> = Symbol();
export const generatorToken: InjectionToken<Generator> = Symbol();
export const githubClientToken: InjectionToken<GithubClient> = Symbol();
export const token: InjectionKey<PublishService> = Symbol();
@singleton()
export class PublishService {
private readonly pluginDataRepository = new PluginDataRepository();
private readonly joplinDataRepository = new JoplinDataRepository();
private readonly appService = container.resolve(AppService);
private readonly generator = container.resolve(generatorToken);
private readonly git = container.resolve(gitClientToken);
private readonly github = container.resolve(githubClientToken);
private files: string[] = [];
private readonly localRepoStatus: Ref<LocalRepoStatus> = ref(LocalRepoStatus.Initializing);
readonly repositoryName = ref('');
readonly isRepositoryMissing = computed(
() => this.localRepoStatus.value === LocalRepoStatus.MissingRepository,
);
readonly isDefaultRepository = computed(
() => this.repositoryName.value === this.github.getDefaultRepositoryName(),
);
readonly githubInfo: Ref<Github | null> = ref(null);
readonly isGenerating = ref(false);
readonly isPublishing = ref(false);
readonly outputDir = ref('');
readonly generatingProgress: Required<GeneratingProgress> = reactive({
...initialGeneratingProgress,
});
readonly publishingProgress: PublishingProgress = reactive({
...initialPublishProgress,
});
constructor() {
this.init();
}
private async init() {
this.outputDir.value = await this.generator.getOutputDir();
this.git.init(this.github, this.outputDir.value).catch(noop);
this.git.on(GitEvents.Progress, this.refreshPublishingProgress.bind(this));
this.git.on(GitEvents.Message, (message) => this.refreshPublishingProgress({ message }));
this.git.on(GitEvents.LocalRepoStatusChanged, this.handleLocalRepoStatusChanged.bind(this));
this.generator.on(GeneratorEvents.PageGenerated, this.refreshGeneratingProgress.bind(this));
this.githubInfo.value = {
...DEFAULT_GITHUB,
token: (await this.joplinDataRepository.getGithubToken()) || '',
...(await this.pluginDataRepository.getGithubInfo()),
};
this.initGithubClient();
}
private async initGithubClient() {
if (!this.isGithubInfoValid.value || !this.githubInfo.value) {
return;
}
this.github.init(toRaw(this.githubInfo.value));
this.repositoryName.value = this.github.getRepositoryName();
}
private handleLocalRepoStatusChanged(status: LocalRepoStatus) {
this.localRepoStatus.value = status;
if (status === LocalRepoStatus.Initializing) {
this.refreshPublishingProgress({
phase: 'Local repository initializing...',
message: '',
});
}
}
isGithubInfoValid = computed(() => {
const requiredKeys: (keyof Github)[] = ['userName', 'email', 'token'];
const keyInfos = pick(this.githubInfo.value, requiredKeys);
return Object.keys(keyInfos).length === requiredKeys.length && !some(keyInfos, isEmpty);
});
async saveGithubInfo(githubInfo: Partial<Github>) {
const githubInfo_ = omit(githubInfo, ['token']);
Object.assign(this.githubInfo.value, githubInfo_);
await this.pluginDataRepository.saveGithubInfo(omit(toRaw(this.githubInfo.value), ['token']));
this.initGithubClient();
}
async generateSite() {
if (this.isGenerating.value || this.appService.getLatestWarning(FORBIDDEN.GENERATE)) {
throw new Error('generating!');
}
this.isGenerating.value = true;
this.refreshGeneratingProgress();
try {
const files = await this.generator.generateSite();
this.files = files;
this.generatingProgress.result = 'success';
this.generatingProgress.message = `${files.length} files in totals`;
} catch (error) {
this.generatingProgress.result = 'fail';
this.generatingProgress.message = (error as Error).message;
} finally {
this.isGenerating.value = false;
}
}
async refreshGeneratingProgress(progress: GeneratingProgress = initialGeneratingProgress) {
Object.assign(this.generatingProgress, progress);
}
async refreshPublishingProgress(progress: Partial<PublishingProgress> = initialPublishProgress) {
Object.assign(this.publishingProgress, progress);
}
stopPublishing() {
this.isPublishing.value = false;
this.git.terminate();
}
async publish(needToCreateRepo = false) {
if (this.isPublishing.value) {
return;
}
if (!this.isGithubInfoValid.value) {
throw new Error('invalid github info');
}
const needToInit =
this.publishingProgress.result === PublishResults.Fail ||
needToCreateRepo ||
this.localRepoStatus.value === LocalRepoStatus.Fail;
if (needToInit) {
this.refreshPublishingProgress();
}
this.isPublishing.value = true;
try {
if (needToCreateRepo && this.isRepositoryMissing.value) {
await this.github.createRepository();
}
await new Promise((resolve) => setTimeout(resolve, 3000)); // a 3s delay, so user can terminate
await this.git.push(this.files, needToInit);
this.publishingProgress.result = PublishResults.Success;
} catch (error) {
if (error instanceof PublishError) {
const message = `${error.message || ''} ${PUBLISH_RESULT_MESSAGE[error.type]}`.trim();
this.publishingProgress.result = error.type;
this.publishingProgress.message = message;
} else {
throw error;
}
} finally {
this.isPublishing.value = false;
}
}
}