|
/********************************************************* |
|
* |
|
* @TITLE: Simplenote and Boostrap Note Syncronization |
|
* |
|
* @DESCRIPTION: Supports two modes one which exports |
|
* Boostrap notes and exports them to nvAlt .txt files. |
|
* The other will export nvALT txt files to Boostrap .cson files |
|
* |
|
* @INSTRUCTIONS: Change readDir's and writeDir's at the top of |
|
* both functions appropriately then run with no parameters for |
|
* insrtructions. |
|
* |
|
* I recommend changing the folder id in skeleton.js. It may |
|
* even be required. |
|
* |
|
* @AUTHOR: Eric Soukenka |
|
* @DATE: April 20th 2017 |
|
* |
|
* @NOTE: nvALT must have syncronization setting set to Plain |
|
* Text files. |
|
* |
|
* @NOTE: Ensure you restart Boostrap after import to avoid |
|
* duplicate entries. |
|
* |
|
* @NOTE: NOT YET POLISHED meaning it has a lot of my specific |
|
* requirements and even the code is relatively embarrising :) |
|
* |
|
* !!! #################### !!! |
|
* ####### IMPORTANT ##### !!! |
|
* !!! Restart Boostrap after import!!!!! |
|
* |
|
********************************************************* */ |
|
var parseJson = require('parse-json'); |
|
var fs = require('fs'); |
|
var path = require('path'); |
|
var CSON = require('cson'); |
|
var crypto = require('crypto'); |
|
|
|
|
|
|
|
// TXT --> CSON |
|
convertCsonToTxt = () => { |
|
var skeleton = CSON.parseCSONFile('skeleton.cson'); |
|
var readDir = '/Users/esouke/Boostnote/notes'; |
|
var writeDir = '/Users/esouke/Documents/Notes'; |
|
var separator = '\n=========================================================\n' |
|
|
|
fs.readdirSync(writeDir).forEach(function(file) { |
|
if (path.extname(file) == '.txt') { |
|
fs.unlink(writeDir + '/' + file) |
|
} |
|
}) |
|
files = fs.readdirSync(readDir); |
|
filelist = []; |
|
files.forEach(function(readfile) { |
|
console.log(readfile) |
|
if (path.extname(readfile) == '.cson') { |
|
var txt = fs.readFileSync(readDir + '/' + readfile).toString(); |
|
var obj = CSON.parse(txt); |
|
var writeFile = writeDir + '/' + obj.title + '.txt'; |
|
var writeBody = obj.content.replace(obj.title, '').replace(/\s+.*====/, '').replace(obj.title, '').replace(/^\s+|\s+$/g, ''); |
|
fs.writeFileSync(writeFile, obj.title + separator + writeBody); |
|
} |
|
}) |
|
console.log("IMPORTANT! Restart Boostrap if it is running!!!"); |
|
} |
|
|
|
// CSON --> TXT |
|
convertTxtToCson = () => { |
|
var skeleton = CSON.parseCSONFile('skeleton.cson'); |
|
var readDir = '/Users/esouke/Documents/Notes'; |
|
var writeDir = '/Users/esouke/Boostnote/notes'; |
|
var separator = '\n=========================================================\n' |
|
|
|
fs.readdirSync(writeDir).forEach(function(file) { |
|
if (path.extname(file) == '.cson') { |
|
fs.unlink(writeDir + '/' + file) |
|
} |
|
}) |
|
files = fs.readdirSync(readDir); |
|
filelist = []; |
|
files.forEach(function(readfile) { |
|
if (path.extname(readfile) == '.txt') { |
|
var txt = fs.readFileSync(readDir + '/' + readfile).toString(); |
|
var writeFile = writeDir + '/' + crypto.randomBytes(10).toString('hex') + '.cson'; |
|
readfile = readfile.replace(/\.[^/.]+$/, ""); |
|
|
|
skeleton.title = readfile; |
|
var writeBody = txt.replace(readfile, '').replace(/\s+.*====/, '').replace(readfile, '').replace(/^\s+|\s+$/g, '') |
|
|
|
skeleton.content = skeleton.title + separator + writeBody; |
|
|
|
fs.writeFileSync(writeFile, CSON.stringify(skeleton)); |
|
|
|
skeleton.title = 'unknown'; |
|
skeleton.content = 'unknown'; |
|
} |
|
}); |
|
} |
|
|
|
var mode = process.argv[2]; |
|
if(mode == '-h' || mode == undefined) { |
|
console.log('This will syncronize Boostrap and Simplenote'); |
|
console.log(`node ${process.argv[1]} simple-to-boost`); |
|
console.log(`node ${process.argv[1]} boost-to-simple`); |
|
} else if (mode == 'simple-to-boost') { |
|
console.log('Syncing Simplenote to Boostrap'); |
|
convertTxtToCson(); |
|
} else if (mode == 'boost-to-simple') { |
|
console.log('Syncing Boostrap to Simplenote'); |
|
convertCsonToTxt(); |
|
} |