Edit save + git commit + push sync
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
var Promise = require('bluebird'),
|
||||
path = require('path'),
|
||||
fs = Promise.promisifyAll(require("fs")),
|
||||
fs = Promise.promisifyAll(require("fs-extra")),
|
||||
_ = require('lodash'),
|
||||
farmhash = require('farmhash'),
|
||||
BSONModule = require('bson'),
|
||||
@@ -34,16 +34,16 @@ module.exports = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Fetch an entry from cache, otherwise the original
|
||||
* Fetch a document from cache, otherwise the original
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {Object} Page Data
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {Promise<Object>} Page Data
|
||||
*/
|
||||
fetch(entryPath) {
|
||||
|
||||
let self = this;
|
||||
|
||||
let cpath = path.join(self._cachePath, farmhash.fingerprint32(entryPath) + '.bson');
|
||||
let cpath = self.getCachePath(entryPath);
|
||||
|
||||
return fs.statAsync(cpath).then((st) => {
|
||||
return st.isFile();
|
||||
@@ -78,16 +78,16 @@ module.exports = {
|
||||
/**
|
||||
* Fetches the original document entry
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @param {Object} options The options
|
||||
* @return {Object} Page data
|
||||
* @param {String} entryPath The entry path
|
||||
* @param {Object} options The options
|
||||
* @return {Promise<Object>} Page data
|
||||
*/
|
||||
fetchOriginal(entryPath, options) {
|
||||
|
||||
let self = this;
|
||||
|
||||
let fpath = path.join(self._repoPath, entryPath + '.md');
|
||||
let cpath = path.join(self._cachePath, farmhash.fingerprint32(entryPath) + '.bson');
|
||||
let fpath = self.getFullPath(entryPath);
|
||||
let cpath = self.getCachePath(entryPath);
|
||||
|
||||
options = _.defaults(options, {
|
||||
parseMarkdown: true,
|
||||
@@ -174,8 +174,8 @@ module.exports = {
|
||||
/**
|
||||
* Gets the parent information.
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {Object|False} The parent information.
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {Promise<Object|False>} The parent information.
|
||||
*/
|
||||
getParentInfo(entryPath) {
|
||||
|
||||
@@ -183,10 +183,10 @@ module.exports = {
|
||||
|
||||
if(_.includes(entryPath, '/')) {
|
||||
|
||||
let parentParts = _.split(entryPath, '/');
|
||||
let parentPath = _.join(_.initial(parentParts),'/');
|
||||
let parentParts = _.initial(_.split(entryPath, '/'));
|
||||
let parentPath = _.join(parentParts,'/');
|
||||
let parentFile = _.last(parentParts);
|
||||
let fpath = path.join(self._repoPath, parentPath + '.md');
|
||||
let fpath = self.getFullPath(parentPath);
|
||||
|
||||
return fs.statAsync(fpath).then((st) => {
|
||||
if(st.isFile()) {
|
||||
@@ -210,6 +210,70 @@ module.exports = {
|
||||
return Promise.reject(new Error('Parent entry is root.'));
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the full original path of a document.
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {String} The full path.
|
||||
*/
|
||||
getFullPath(entryPath) {
|
||||
return path.join(this._repoPath, entryPath + '.md');
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the full cache path of a document.
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {String} The full cache path.
|
||||
*/
|
||||
getCachePath(entryPath) {
|
||||
return path.join(this._cachePath, farmhash.fingerprint32(entryPath) + '.bson');
|
||||
},
|
||||
|
||||
/**
|
||||
* Update an existing document
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @param {String} contents The markdown-formatted contents
|
||||
* @return {Promise<Boolean>} True on success, false on failure
|
||||
*/
|
||||
update(entryPath, contents) {
|
||||
|
||||
let self = this;
|
||||
let fpath = self.getFullPath(entryPath);
|
||||
|
||||
return fs.statAsync(fpath).then((st) => {
|
||||
if(st.isFile()) {
|
||||
return self.makePersistent(entryPath, contents).then(() => {
|
||||
return self.fetchOriginal(entryPath, {});
|
||||
});
|
||||
} else {
|
||||
return Promise.reject(new Error('Entry does not exist!'));
|
||||
}
|
||||
}).catch((err) => {
|
||||
return new Error('Entry does not exist!');
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Makes a document persistent to disk and git repository
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @param {String} contents The markdown-formatted contents
|
||||
* @return {Promise<Boolean>} True on success, false on failure
|
||||
*/
|
||||
makePersistent(entryPath, contents) {
|
||||
|
||||
let self = this;
|
||||
let fpath = self.getFullPath(entryPath);
|
||||
|
||||
return fs.outputFileAsync(fpath, contents).then(() => {
|
||||
return git.commitDocument(entryPath);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
};
|
@@ -131,6 +131,11 @@ module.exports = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sync with the remote repository
|
||||
*
|
||||
* @return {Promise} Resolve on sync success
|
||||
*/
|
||||
resync() {
|
||||
|
||||
let self = this;
|
||||
@@ -149,23 +154,20 @@ module.exports = {
|
||||
|
||||
// Check for changes
|
||||
|
||||
return self._git.exec('status').then((cProc) => {
|
||||
return self._git.exec('log', 'origin/' + self._repo.branch + '..HEAD').then((cProc) => {
|
||||
let out = cProc.stdout.toString();
|
||||
if(!_.includes(out, 'nothing to commit')) {
|
||||
|
||||
// Add, commit and push
|
||||
if(_.includes(out, 'commit')) {
|
||||
|
||||
winston.info('[GIT] Performing push to remote repository...');
|
||||
return self._git.add('-A').then(() => {
|
||||
return self._git.commit("Resync");
|
||||
}).then(() => {
|
||||
return self._git.push('origin', self._repo.branch);
|
||||
}).then(() => {
|
||||
return self._git.push('origin', self._repo.branch).then(() => {
|
||||
return winston.info('[GIT] Push completed.');
|
||||
});
|
||||
|
||||
} else {
|
||||
winston.info('[GIT] Repository is already up to date. Nothing to commit.');
|
||||
|
||||
winston.info('[GIT] Repository is already in sync.');
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -178,6 +180,30 @@ module.exports = {
|
||||
throw err;
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Commits a document.
|
||||
*
|
||||
* @param {String} entryPath The entry path
|
||||
* @return {Promise} Resolve on commit success
|
||||
*/
|
||||
commitDocument(entryPath) {
|
||||
|
||||
let self = this;
|
||||
let gitFilePath = entryPath + '.md';
|
||||
let commitMsg = '';
|
||||
|
||||
return self._git.exec('ls-files', gitFilePath).then((cProc) => {
|
||||
let out = cProc.stdout.toString();
|
||||
return _.includes(out, gitFilePath);
|
||||
}).then((isTracked) => {
|
||||
commitMsg = (isTracked) ? 'Updated ' + gitFilePath : 'Added ' + gitFilePath;
|
||||
return self._git.add(gitFilePath);
|
||||
}).then(() => {
|
||||
return self._git.commit(commitMsg);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
};
|
Reference in New Issue
Block a user