]>
Commit | Line | Data |
---|---|---|
1 | const { readFile, writeFile } = require('fs/promises'); | |
2 | const { rmIfExists } = require('./utils'); | |
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) { | |
15 | ||
16 | await writeFile(remoteConfig, remote); | |
17 | }, | |
18 | ||
19 | async remove(remoteConfig) { | |
20 | ||
21 | await rmIfExists(remoteConfig); | |
22 | }, | |
23 | ||
24 | async syncUp(remoteConfig, blogDirectory) { | |
25 | ||
26 | await this._executeMethodOnStrategy(remoteConfig, 'syncUp', blogDirectory); | |
27 | }, | |
28 | ||
29 | async syncDown(remoteConfig, blogDirectory) { | |
30 | ||
31 | await this._executeMethodOnStrategy(remoteConfig, 'syncDown', blogDirectory); | |
32 | }, | |
33 | ||
34 | async _executeMethodOnStrategy(remoteConfig, method, blogDirectory) { | |
35 | ||
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) { | |
46 | ||
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 | } |