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