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