]> git.r.bdr.sh - rbdr/blog/blob - lib/remote.js
d8f73f9325daf2be886495e9efd952e50ed459c9
[rbdr/blog] / lib / remote.js
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) {
14 await writeFile(remoteConfig, remote);
15 },
16
17 async remove(remoteConfig) {
18 await rm(remoteConfig, { force: true })
19 },
20
21 async syncUp(remoteConfig, blogDirectory) {
22 this._executeMethodOnStrategy(remoteConfig, 'syncUp', blogDirectory);
23 },
24
25 async syncDown(remoteConfig, blogDirectory) {
26 this._executeMethodOnStrategy(remoteConfig, 'syncDown', blogDirectory);
27 },
28
29 async _executeMethodOnStrategy(remoteConfig, method, blogDirectory) {
30 const remote = await this._ensureConfiguration(remoteConfig);
31
32 for (const strategy of internals.strategies) {
33 if (strategy.canHandle(remote)) {
34 await strategy[method](remote, blogDirectory);
35 }
36 }
37 },
38
39 async _ensureConfiguration(remoteConfig) {
40 try {
41 const configuration = await readFile(remoteConfig, { encoding: 'utf8' });
42 return configuration;
43 }
44 catch {
45 throw new Error(internals.strings.configurationNotFound);
46 }
47 }
48 }