diff --git a/public/constants/paths.js b/public/constants/paths.js index 2a0df575..10bff596 100644 --- a/public/constants/paths.js +++ b/public/constants/paths.js @@ -5,15 +5,17 @@ const REACT_PATH = isDev ? 'http://localhost:3000' : `file://${path.resolve(__dirname, '..', '..', 'build', 'index.html')}`; // .app/Contents/Resources/wiki/ -const TIDDLYWIKI_FOLDER_PATH = isDev +const TIDDLYWIKI_TEMPLATE_FOLDER_PATH = isDev ? path.resolve(__dirname, '..', '..', 'template', 'wiki') : path.resolve(process.resourcesPath, '..', 'wiki'); +const TIDDLERS_PATH = 'tiddlers' const ICON_PATH = isDev ? path.resolve(__dirname, '..', 'icon.png') : `file://${path.resolve(__dirname, '..', 'icon.png')}`; module.exports = { REACT_PATH, - TIDDLYWIKI_FOLDER_PATH, + TIDDLYWIKI_TEMPLATE_FOLDER_PATH, + TIDDLERS_PATH, ICON_PATH, }; diff --git a/public/libs/create-wiki.js b/public/libs/create-wiki.js index 47196e3c..f0378a7a 100644 --- a/public/libs/create-wiki.js +++ b/public/libs/create-wiki.js @@ -1,32 +1,45 @@ const fs = require('fs-extra'); const path = require('path'); -const { TIDDLYWIKI_FOLDER_PATH } = require('../constants/paths'); +const { TIDDLYWIKI_TEMPLATE_FOLDER_PATH, TIDDLERS_PATH } = require('../constants/paths'); async function createWiki(newFolderPath, folderName) { const newWikiPath = path.join(newFolderPath, folderName); if (!(await fs.pathExists(newFolderPath))) { throw new Error(`该目录不存在 "${newFolderPath}"`); } - if (!(await fs.pathExists(TIDDLYWIKI_FOLDER_PATH))) { - throw new Error(`Wiki模板缺失 "${TIDDLYWIKI_FOLDER_PATH}"`); + if (!(await fs.pathExists(TIDDLYWIKI_TEMPLATE_FOLDER_PATH))) { + throw new Error(`Wiki模板缺失 "${TIDDLYWIKI_TEMPLATE_FOLDER_PATH}"`); } if (await fs.pathExists(newWikiPath)) { throw new Error(`Wiki已经存在于该位置 "${newWikiPath}"`); } - // Start copying wiki template to destination - await fs.copy(TIDDLYWIKI_FOLDER_PATH, newWikiPath); + try { + await fs.copy(TIDDLYWIKI_TEMPLATE_FOLDER_PATH, newWikiPath); + } catch { + throw new Error(`无法在该处创建文件夹 "${newWikiPath}"`); + } } async function createSubWiki(newFolderPath, folderName, mainWikiToLink) { const newWikiPath = path.join(newFolderPath, folderName); + const mainWikiTiddlersFolderPath = path.join(mainWikiToLink, TIDDLERS_PATH, folderName); if (!(await fs.pathExists(newFolderPath))) { throw new Error(`该目录不存在 "${newFolderPath}"`); } if (await fs.pathExists(newWikiPath)) { throw new Error(`Wiki已经存在于该位置 "${newWikiPath}"`); } - await fs.mkdirs(path.join(newFolderPath, folderName)); + try { + await fs.mkdirs(newWikiPath); + } catch { + throw new Error(`无法在该处创建文件夹 "${newWikiPath}"`); + } + try { + await fs.createSymlink(newWikiPath, mainWikiTiddlersFolderPath); + } catch { + throw new Error(`无法链接文件夹 "${newWikiPath}" 到 "${mainWikiTiddlersFolderPath}"`); + } } module.exports = { createWiki, createSubWiki }; diff --git a/public/listeners/index.js b/public/listeners/index.js index a2551af8..94e05f9e 100755 --- a/public/listeners/index.js +++ b/public/listeners/index.js @@ -59,8 +59,6 @@ const loadListeners = () => { ipcMain.handle('copy-wiki-template', async (event, newFolderPath, folderName) => { try { await createWiki(newFolderPath, folderName); - // eslint-disable-next-line sonarjs/no-duplicate-string - return true; } catch (error) { return String(error); } @@ -68,7 +66,6 @@ const loadListeners = () => { ipcMain.handle('create-sub-wiki', async (event, newFolderPath, folderName, mainWikiToLink) => { try { await createSubWiki(newFolderPath, folderName, mainWikiToLink); - return true; } catch (error) { return String(error); } diff --git a/src/components/dialog-add-workspace/index.js b/src/components/dialog-add-workspace/index.js index 69bd75a8..eca7433a 100644 --- a/src/components/dialog-add-workspace/index.js +++ b/src/components/dialog-add-workspace/index.js @@ -17,7 +17,7 @@ import FolderIcon from '@material-ui/icons/Folder'; import GithubIcon from '@material-ui/icons/GitHub'; import connectComponent from '../../helpers/connect-component'; -import { updateForm, save, wikiCreationResult } from '../../state/dialog-add-workspace/actions'; +import { updateForm, save, setWikiCreationMessage } from '../../state/dialog-add-workspace/actions'; import { requestCopyWikiTemplate, @@ -64,7 +64,7 @@ const SoftLinkToMainWikiSelectInputLabel = styled(InputLabel)` margin-top: 5px; `; -function AddWorkspace({ wikiCreationMessage, onUpdateForm, onSave, onWikiCreationResult }) { +function AddWorkspace({ wikiCreationMessage, onUpdateForm, onSave, onSetWikiCreationMessage }) { const [isCreateMainWorkspace, isCreateMainWorkspaceSetter] = useState(countWorkspace() === 0); const [parentFolderLocation, parentFolderLocationSetter] = useState(''); const [wikiFolderLocation, wikiFolderLocationSetter] = useState(''); @@ -134,13 +134,21 @@ function AddWorkspace({ wikiCreationMessage, onUpdateForm, onSave, onWikiCreatio error={wikiCreationMessage} helperText={wikiCreationMessage} fullWidth - onChange={event => parentFolderLocationSetter(event.target.value)} + onChange={event => { + parentFolderLocationSetter(event.target.value); + onSetWikiCreationMessage(''); + }} label="知识库的父文件夹" value={parentFolderLocation} /> wikiFolderNameSetter(event.target.value)} + onChange={event => { + wikiFolderNameSetter(event.target.value); + onSetWikiCreationMessage(''); + }} label="知识库文件夹名" value={wikiFolderName} /> @@ -197,11 +205,11 @@ function AddWorkspace({ wikiCreationMessage, onUpdateForm, onSave, onWikiCreatio disabled={!parentFolderLocation} onClick={async () => { onUpdateForm(workspaceFormData); - const creationResult = await requestCopyWikiTemplate(parentFolderLocation, wikiFolderName); - if (creationResult) { - onSave(); + const creationError = await requestCopyWikiTemplate(parentFolderLocation, wikiFolderName); + if (creationError) { + onSetWikiCreationMessage(creationError); } else { - onWikiCreationResult(creationResult); + onSave(); } }} > @@ -232,11 +240,11 @@ function AddWorkspace({ wikiCreationMessage, onUpdateForm, onSave, onWikiCreatio disabled={!parentFolderLocation || !mainWikiToLink} onClick={async () => { onUpdateForm(workspaceFormData); - const creationResult = await requestCreateSubWiki(parentFolderLocation, wikiFolderName, mainWikiToLink); - if (creationResult) { - onSave(); + const creationError = await requestCreateSubWiki(parentFolderLocation, wikiFolderName, mainWikiToLink); + if (creationError) { + onSetWikiCreationMessage(creationError); } else { - onWikiCreationResult(creationResult); + onSave(); } }} > @@ -276,7 +284,7 @@ AddWorkspace.propTypes = { wikiCreationMessage: PropTypes.string, onUpdateForm: PropTypes.func.isRequired, onSave: PropTypes.func.isRequired, - onWikiCreationResult: PropTypes.func.isRequired, + onSetWikiCreationMessage: PropTypes.func.isRequired, }; const mapStateToProps = state => ({ @@ -286,7 +294,7 @@ const mapStateToProps = state => ({ const actionCreators = { updateForm, save, - wikiCreationResult, + setWikiCreationMessage, }; export default connectComponent(AddWorkspace, mapStateToProps, actionCreators); diff --git a/src/constants/actions.js b/src/constants/actions.js index fcfe0b89..c1e2249e 100644 --- a/src/constants/actions.js +++ b/src/constants/actions.js @@ -53,7 +53,7 @@ export const ABOUT_CLOSE = 'ABOUT_CLOSE'; export const ABOUT_OPEN = 'ABOUT_OPEN'; // Add Workspace -export const ADD_WORKSPACE_CREATE_WIKI_RESULT = 'ADD_WORKSPACE_CREATE_WIKI_RESULT'; +export const ADD_WORKSPACE_CREATE_WIKI_MESSAGE = 'ADD_WORKSPACE_CREATE_WIKI_MESSAGE'; export const ADD_WORKSPACE_GET_FAILED = 'ADD_WORKSPACE_GET_FAILED'; export const ADD_WORKSPACE_GET_REQUEST = 'ADD_WORKSPACE_GET_REQUEST'; export const ADD_WORKSPACE_GET_SUCCESS = 'ADD_WORKSPACE_GET_SUCCESS'; diff --git a/src/listeners/index.js b/src/listeners/index.js index afb7ddfd..24818cd3 100755 --- a/src/listeners/index.js +++ b/src/listeners/index.js @@ -1,4 +1,4 @@ -import { wikiCreationResult } from '../state/dialog-add-workspace/actions'; +import { setWikiCreationMessage } from '../state/dialog-add-workspace/actions'; import { setPreference } from '../state/preferences/actions'; import { setSystemPreference } from '../state/system-preferences/actions'; import { setWorkspace, setWorkspaces } from '../state/workspaces/actions'; @@ -29,7 +29,7 @@ const loadListeners = (store) => { } ipcRenderer.on('create-wiki-result', (event, message) => { - store.dispatch(wikiCreationResult(message)); + store.dispatch(setWikiCreationMessage(message)); }); ipcRenderer.on('log', (e, message) => { diff --git a/src/state/dialog-add-workspace/actions.js b/src/state/dialog-add-workspace/actions.js index ffc6edf0..201c2d64 100755 --- a/src/state/dialog-add-workspace/actions.js +++ b/src/state/dialog-add-workspace/actions.js @@ -2,7 +2,7 @@ import algoliasearch from 'algoliasearch'; import { - ADD_WORKSPACE_CREATE_WIKI_RESULT, + ADD_WORKSPACE_CREATE_WIKI_MESSAGE, ADD_WORKSPACE_GET_FAILED, ADD_WORKSPACE_GET_REQUEST, ADD_WORKSPACE_GET_SUCCESS, @@ -24,9 +24,9 @@ import { requestCreateWorkspace } from '../../senders'; const client = algoliasearch('OQ55YRVMNP', 'fc0fb115b113c21d58ed6a4b4de1565f'); const index = client.initIndex('apps'); -export const wikiCreationResult = resultMessage => ({ - type: ADD_WORKSPACE_CREATE_WIKI_RESULT, - value: resultMessage, +export const setWikiCreationMessage = message => ({ + type: ADD_WORKSPACE_CREATE_WIKI_MESSAGE, + value: message, }); export const saveCreatedWiki = () => (dispatch, getState) => { diff --git a/src/state/dialog-add-workspace/reducers.js b/src/state/dialog-add-workspace/reducers.js index a18c96c9..10d40f96 100755 --- a/src/state/dialog-add-workspace/reducers.js +++ b/src/state/dialog-add-workspace/reducers.js @@ -1,7 +1,7 @@ import { combineReducers } from 'redux'; import { - ADD_WORKSPACE_CREATE_WIKI_RESULT, + ADD_WORKSPACE_CREATE_WIKI_MESSAGE, ADD_WORKSPACE_GET_FAILED, ADD_WORKSPACE_GET_REQUEST, ADD_WORKSPACE_GET_SUCCESS, @@ -16,7 +16,7 @@ import { const wikiCreationMessage = (state = '', action) => { switch (action.type) { - case ADD_WORKSPACE_CREATE_WIKI_RESULT: + case ADD_WORKSPACE_CREATE_WIKI_MESSAGE: return action.value; default: return state;