Skip to content

Instantly share code, notes, and snippets.

@evilwk
Last active March 11, 2021 03:58
Show Gist options
  • Save evilwk/ae4bdff5f56f77efb6259e107624f2ed to your computer and use it in GitHub Desktop.
Save evilwk/ae4bdff5f56f77efb6259e107624f2ed to your computer and use it in GitHub Desktop.
vnote 3.0 beta版本配置回滚到 vnote 2.0
let fs = require("fs");
let path = require("path");
let props = {
folders: "sub_directories",
};
function rollback(filePath) {
if (!fs.existsSync(filePath)) {
console.log("directory not exists");
return;
}
let pa = fs.readdirSync(filePath);
pa.forEach(function (fileName, index) {
let fullPath = path.join(filePath, fileName);
let info = fs.statSync(fullPath);
if (info.isFile() && fileName == "vx.json") {
let isRoot = fs.existsSync(path.join(filePath, "vx_notebook"));
convertConfig(fullPath, isRoot);
} else if (
info.isDirectory() &&
!["vx_recycle_bin", "vx_notebook"].includes(fileName)
) {
rollback(fullPath);
}
});
}
function convertConfig(filePath, isRoot) {
let dirPath = path.dirname(filePath);
fs.readFile(filePath, function (err, data) {
let config = JSON.parse(data);
if (config["folders"]) {
let arr = config["folders"];
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i]["name"] == "vx_recycle_bin") {
console.log("remove item");
arr.splice(i, 1);
}
}
}
if (config["id"]) {
delete config["id"];
}
for (let [key, value] of Object.entries(props)) {
rename(config, key, value);
}
if (isRoot) {
config["recycle_bin_folder"] = "_v_recycle_bin";
config["attachment_folder"] = "_v_attachments";
config["image_folder"] = "";
config["tags"] = [];
}
fs.writeFile(
path.join(dirPath, "_vnote.json"),
JSON.stringify(config),
function (err) {
if (err) console.log(err);
}
);
});
}
function rename(config, oldName, newName) {
if (config[oldName]) {
config[newName] = config[oldName];
delete config[oldName];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment