]>
Commit | Line | Data |
---|---|---|
1 | import { readFile, writeFile } from 'fs/promises'; | |
2 | import { rmIfExists } from './utils.js'; | |
3 | ||
4 | import GitStrategy from './remotes/git.js'; | |
5 | ||
6 | const internals = { | |
7 | strings: { | |
8 | configurationNotFound: 'Remote configuration not set, consult help for more info.' | |
9 | }, | |
10 | strategies: [ | |
11 | GitStrategy | |
12 | ] | |
13 | }; | |
14 | ||
15 | export default { | |
16 | async add(remoteConfig, remote) { | |
17 | ||
18 | await writeFile(remoteConfig, remote); | |
19 | }, | |
20 | ||
21 | async remove(remoteConfig) { | |
22 | ||
23 | await rmIfExists(remoteConfig); | |
24 | }, | |
25 | ||
26 | async syncUp(remoteConfig, blogDirectory) { | |
27 | ||
28 | await this._executeMethodOnStrategy(remoteConfig, 'syncUp', blogDirectory); | |
29 | }, | |
30 | ||
31 | async syncDown(remoteConfig, blogDirectory) { | |
32 | ||
33 | await this._executeMethodOnStrategy(remoteConfig, 'syncDown', blogDirectory); | |
34 | }, | |
35 | ||
36 | async _executeMethodOnStrategy(remoteConfig, method, blogDirectory) { | |
37 | ||
38 | const remote = await this._ensureConfiguration(remoteConfig); | |
39 | ||
40 | for (const strategy of internals.strategies) { | |
41 | if (strategy.canHandle(remote)) { | |
42 | await strategy[method](remote, blogDirectory); | |
43 | } | |
44 | } | |
45 | }, | |
46 | ||
47 | async _ensureConfiguration(remoteConfig) { | |
48 | ||
49 | try { | |
50 | const configuration = await readFile(remoteConfig, { encoding: 'utf8' }); | |
51 | return configuration; | |
52 | } | |
53 | catch { | |
54 | throw new Error(internals.strings.configurationNotFound); | |
55 | } | |
56 | } | |
57 | }; |