diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2022-12-09 14:43:41 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2022-12-09 14:43:41 +0100 |
| commit | c5cbbd3835ccd509179504cdf7d5e74356d7dca5 (patch) | |
| tree | f5588fce8924772a54f599c5636640731bab640d /lib/remote.js | |
| parent | 24de2f063e5dfcad0086d1dc81de3cf012a00e4c (diff) | |
Add rudimentary sync support
Diffstat (limited to 'lib/remote.js')
| -rw-r--r-- | lib/remote.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/remote.js b/lib/remote.js new file mode 100644 index 0000000..d8f73f9 --- /dev/null +++ b/lib/remote.js @@ -0,0 +1,48 @@ +const { readFile, rm, writeFile } = require('fs/promises'); + +const internals = { + strings: { + configurationNotFound: 'Remote configuration not set, consult help for more info.' + }, + strategies: [ + require('./remotes/git') + ] +}; + +module.exports = { + async add(remoteConfig, remote) { + await writeFile(remoteConfig, remote); + }, + + async remove(remoteConfig) { + await rm(remoteConfig, { force: true }) + }, + + async syncUp(remoteConfig, blogDirectory) { + this._executeMethodOnStrategy(remoteConfig, 'syncUp', blogDirectory); + }, + + async syncDown(remoteConfig, blogDirectory) { + this._executeMethodOnStrategy(remoteConfig, 'syncDown', blogDirectory); + }, + + async _executeMethodOnStrategy(remoteConfig, method, blogDirectory) { + const remote = await this._ensureConfiguration(remoteConfig); + + for (const strategy of internals.strategies) { + if (strategy.canHandle(remote)) { + await strategy[method](remote, blogDirectory); + } + } + }, + + async _ensureConfiguration(remoteConfig) { + try { + const configuration = await readFile(remoteConfig, { encoding: 'utf8' }); + return configuration; + } + catch { + throw new Error(internals.strings.configurationNotFound); + } + } +} |