]> git.r.bdr.sh - rbdr/blog/blob - lib/remote.js
8b29c4e6f6bf7d91d3c97ac9fea39f0aa1d4fbbc
[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
15 await writeFile(remoteConfig, remote);
16 },
17
18 async remove(remoteConfig) {
19
20 await rm(remoteConfig, { force: true })
21 },
22
23 async syncUp(remoteConfig, blogDirectory) {
24
25 await this._executeMethodOnStrategy(remoteConfig, 'syncUp', blogDirectory);
26 },
27
28 async syncDown(remoteConfig, blogDirectory) {
29
30 await this._executeMethodOnStrategy(remoteConfig, 'syncDown', blogDirectory);
31 },
32
33 async _executeMethodOnStrategy(remoteConfig, method, blogDirectory) {
34
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) {
45
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 }