X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/bd8c1fa2774b98b22939d60c06a06525691867ce..d620665f6b2e1ae5db4c98a09e35bd63133ae87f:/lib/remote.js?ds=sidebyside diff --git a/lib/remote.js b/lib/remote.js index 569cd25..ad0a4fc 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -1,32 +1,40 @@ -const { readFile, rm, writeFile } = require('fs/promises'); +import { readFile, writeFile } from 'fs/promises'; +import { rmIfExists } from './utils.js'; + +import GitStrategy from './remotes/git.js'; const internals = { strings: { configurationNotFound: 'Remote configuration not set, consult help for more info.' }, strategies: [ - require('./remotes/git') + GitStrategy ] }; -module.exports = { +export default { async add(remoteConfig, remote) { + await writeFile(remoteConfig, remote); }, async remove(remoteConfig) { - await rm(remoteConfig, { force: true }) + + await rmIfExists(remoteConfig); }, async syncUp(remoteConfig, blogDirectory) { + await this._executeMethodOnStrategy(remoteConfig, 'syncUp', blogDirectory); }, async syncDown(remoteConfig, blogDirectory) { + await this._executeMethodOnStrategy(remoteConfig, 'syncDown', blogDirectory); }, async _executeMethodOnStrategy(remoteConfig, method, blogDirectory) { + const remote = await this._ensureConfiguration(remoteConfig); for (const strategy of internals.strategies) { @@ -37,6 +45,7 @@ module.exports = { }, async _ensureConfiguration(remoteConfig) { + try { const configuration = await readFile(remoteConfig, { encoding: 'utf8' }); return configuration; @@ -45,4 +54,4 @@ module.exports = { throw new Error(internals.strings.configurationNotFound); } } -} +};