feat: util to update fileSystemPaths.tid

This commit is contained in:
tiddlygit-test 2020-08-21 21:24:04 +08:00
parent 813d3305e9
commit 541fd62ccf
6 changed files with 89 additions and 17 deletions

View file

@ -0,0 +1,72 @@
const path = require('path');
const fs = require('fs-extra');
const { take, drop, compact } = require('lodash');
const getMatchPart = tagToMatch => `[!is[system]kin::to[${tagToMatch}]`;
const getPathPart = folderToPlace => `addprefix[subwiki/${folderToPlace}/]]`;
/**
* update $:/config/FileSystemPaths programmatically to make private tiddlers goto the sub-wiki
* @param {Object} workspace main wiki's workspace data
* @param {Object} newConfig { "tagName": Tag to indicate that a tiddler belongs to a sub-wiki, "subWikiFolderName": folder name inside the subwiki/ folder }
* @param {Object} oldConfig if you need to replace a line, you need to pass-in what old line looks like, so here we can find and replace it
*/
function updateSubWikiPluginContent(workspace, newConfig, oldConfig) {
const mainWikiPath = workspace.name;
const pluginPath = path.join(mainWikiPath, 'plugins', 'linonetwo', 'sub-wiki');
const FileSystemPathsTiddlerPath = path.join(pluginPath, 'FileSystemPaths.tid');
const FileSystemPathsFile = fs.readFileSync(FileSystemPathsTiddlerPath, 'utf-8');
let newFileSystemPathsFile = '';
// ignore the tags, title and type, 3 lines, and an empty line
const header = take(FileSystemPathsFile.split('\n'), 3);
const FileSystemPaths = compact(drop(FileSystemPathsFile.split('\n'), 3));
// if newConfig is undefined, but oldConfig is provided, we delete the old config
if (!newConfig) {
if (!oldConfig) {
throw new Error(
'Both newConfig and oldConfig are not provided in the updateSubWikiPluginContent() for',
JSON.stringify(workspace),
);
}
// find the old line, delete it
const newFileSystemPaths = FileSystemPaths.filter(
line =>
!(line.includes(getMatchPart(oldConfig.tagName)) && line.includes(getPathPart(oldConfig.subWikiFolderName))),
);
newFileSystemPathsFile = `${header.join('\n')}\n\n${newFileSystemPaths.join('\n')}`;
} else {
// if this config already exists, just return
if (
FileSystemPaths.some(
line =>
line.includes(getMatchPart(newConfig.tagName)) && line.includes(getPathPart(newConfig.subWikiFolderName)),
)
) {
return;
}
// prepare new line
const { tagName, subWikiFolderName } = newConfig;
const newConfigLine = getMatchPart(tagName) + getPathPart(subWikiFolderName);
// if we are just to add a new config, just append it to the end of the file
if (oldConfig) {
// find the old line, replace it with the new line
const newFileSystemPaths = FileSystemPaths.map(line => {
if (line.includes(getMatchPart(oldConfig.tagName)) && line.includes(getPathPart(oldConfig.subWikiFolderName))) {
return newConfigLine;
}
return line;
});
newFileSystemPathsFile = `${header.join('\n')}\n\n${newFileSystemPaths.join('\n')}`;
} else {
newFileSystemPathsFile = `${FileSystemPathsFile}\n${newConfigLine}`;
}
}
fs.writeFileSync(FileSystemPathsTiddlerPath, newFileSystemPathsFile);
}
module.exports = {
updateSubWikiPluginContent,
};

View file

@ -1,3 +0,0 @@
tags: $:/plugins/linonetwo/private-tiddlers-in-private-repo/readme
title: $:/config/FileSystemPaths
type: text/vnd.tiddlywiki

View file

@ -1,13 +0,0 @@
title: $:/plugins/linonetwo/private-tiddlers-in-private-repo/readme
created: 20200414135748497
modified: 20200602062349232
type: text/vnd.tiddlywiki
!! Usage
Edit `plugins/linonetwo/private-tiddlers-in-private-repo` using VSCode to be something like:
```tid
[!is[system]kin::to[APrivateContent]addprefix[/]addprefix[private-wiki]]
[!is[system]kin::to[Photos]addprefix[/]addprefix[private-wiki-photos]]
```

View file

@ -0,0 +1,3 @@
tags: $:/plugins/linonetwo/sub-wiki/readme
title: $:/config/FileSystemPaths
type: text/vnd.tiddlywiki

View file

@ -1,5 +1,5 @@
{
"title": "$:/plugins/linonetwo/private-tiddlers-in-private-repo",
"title": "$:/plugins/linonetwo/sub-wiki",
"name": "Private Tiddlers In Private Repo",
"description": "Allow you make some tiddlers private, and store them in a private repo.",
"author": "LinOnetwo",

View file

@ -0,0 +1,13 @@
title: $:/plugins/linonetwo/sub-wiki/readme
created: 20200414135748497
modified: 20200602062349232
type: text/vnd.tiddlywiki
!! Usage
Edit `plugins/linonetwo/sub-wiki/FileSystemPaths.tid` using VSCode or automatically updated by TiddlyGitDesktop (instead of edit it from the TiddlyWiki) to be something like:
```tid
[!is[system]kin::to[APrivateContent]addprefix[subwiki/private-wiki/]]
[!is[system]kin::to[Photos]addprefix[subwiki/private-wiki-photos/]]
```