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