feat: clone a repo

This commit is contained in:
tiddlygit-test 2020-07-25 22:44:25 +08:00
parent 678207db2c
commit adde47efec
8 changed files with 102 additions and 22 deletions

View file

@ -366,8 +366,36 @@ async function getRemoteUrl(wikiFolderPath) {
return '';
}
async function clone(githubRepoUrl, repoFolderPath, userInfo) {
const logProgress = message => logger.notice(message, { handler: 'createWikiProgress', function: 'clone' });
const logInfo = message => logger.info(message, { function: 'clone' });
logProgress('准备克隆线上Wiki');
logProgress('开始初始化本地Git仓库');
const { login: username, accessToken } = userInfo;
logInfo(
`Using gitUrl ${githubRepoUrl} with username ${username} and accessToken ${truncate(accessToken, {
length: 24,
})}`,
);
await GitProcess.exec(['init'], repoFolderPath);
logProgress('仓库初始化完毕开始配置Github远端仓库');
await credentialOn(repoFolderPath, githubRepoUrl, userInfo);
logProgress('正在拉取Github远端仓库的数据需要的时间取决于网速和仓库大小请耐心等待');
const { stderr, exitCode } = await GitProcess.exec(['pull', 'origin', 'master:master'], repoFolderPath);
await credentialOff(repoFolderPath);
if (exitCode !== 0) {
logInfo(stderr);
const CONFIG_FAILED_MESSAGE = 'Git仓库配置失败详见错误日志';
logProgress(CONFIG_FAILED_MESSAGE);
throw new Error(CONFIG_FAILED_MESSAGE);
} else {
logProgress('Git仓库配置完毕');
}
}
module.exports = {
initWikiGit,
commitAndSync,
getRemoteUrl,
clone,
};

View file

@ -1,17 +1,33 @@
const fs = require('fs-extra');
const path = require('path');
const { TIDDLYWIKI_TEMPLATE_FOLDER_PATH, TIDDLERS_PATH } = require('../constants/paths');
const { TIDDLYWIKI_TEMPLATE_FOLDER_PATH, TIDDLERS_PATH } = require('../../constants/paths');
const { clone } = require('../git');
const { logger } = require('../log');
const getLogProgress = logger => message =>
const logProgress = message =>
logger.notice({
type: 'progress',
payload: { message, handler: 'createWikiProgress' },
});
async function createWiki(newFolderPath, folderName, logger) {
const logProgress = getLogProgress(logger);
/**
* Link a sub wiki to a main wiki, this will create a shortcut folder from main wiki to sub wiki, so when saving files to that shortcut folder, you will actually save file to the sub wiki
* @param {string} mainWikiPath folderPath of a wiki as link's destination
* @param {string} folderName sub-wiki's folder name
* @param {string} newWikiPath sub-wiki's folder path
*/
async function linkWiki(mainWikiPath, folderName, subWikiPath) {
const mainWikiTiddlersFolderPath = path.join(mainWikiPath, TIDDLERS_PATH, folderName);
try {
await fs.createSymlink(subWikiPath, mainWikiTiddlersFolderPath);
logProgress(`${subWikiPath}${mainWikiTiddlersFolderPath}的链接创建成功,将文件存入后者相当于将文件存入前者。`);
} catch {
throw new Error(`无法链接文件夹 "${subWikiPath}" 到 "${mainWikiTiddlersFolderPath}"`);
}
}
async function createWiki(newFolderPath, folderName) {
logProgress('开始用模板创建Wiki');
const newWikiPath = path.join(newFolderPath, folderName);
if (!(await fs.pathExists(newFolderPath))) {
@ -38,12 +54,9 @@ async function createWiki(newFolderPath, folderName, logger) {
* @param {string} mainWikiToLink
* @param {boolean} onlyLink not creating new subwiki folder, just link existed subwiki folder to main wiki folder
*/
async function createSubWiki(newFolderPath, folderName, mainWikiToLink, onlyLink = false, logger) {
const logProgress = getLogProgress(logger);
async function createSubWiki(newFolderPath, folderName, mainWikiToLink, onlyLink = false) {
logProgress('开始创建子Wiki');
const newWikiPath = path.join(newFolderPath, folderName);
const mainWikiTiddlersFolderPath = path.join(mainWikiToLink, TIDDLERS_PATH, folderName);
if (!(await fs.pathExists(newFolderPath))) {
throw new Error(`该目录不存在 "${newFolderPath}"`);
}
@ -51,6 +64,7 @@ async function createSubWiki(newFolderPath, folderName, mainWikiToLink, onlyLink
throw new Error(`Wiki已经存在于该位置 "${newWikiPath}"`);
}
logProgress('开始链接子Wiki到父Wiki');
await linkWiki(mainWikiToLink, folderName, newWikiPath);
if (!onlyLink) {
try {
await fs.mkdirs(newWikiPath);
@ -58,11 +72,7 @@ async function createSubWiki(newFolderPath, folderName, mainWikiToLink, onlyLink
throw new Error(`无法在该处创建文件夹 "${newWikiPath}"`);
}
}
try {
await fs.createSymlink(newWikiPath, mainWikiTiddlersFolderPath);
} catch {
throw new Error(`无法链接文件夹 "${newWikiPath}" 到 "${mainWikiTiddlersFolderPath}"`);
}
logProgress('子Wiki创建完毕');
}
@ -83,4 +93,13 @@ async function ensureWikiExist(wikiPath, shouldBeMainWiki) {
}
}
module.exports = { createWiki, createSubWiki, removeWiki, ensureWikiExist };
async function cloneWiki(parentFolderLocation, wikiFolderName, githubWikiUrl, userInfo) {
await clone(githubWikiUrl, path.join(parentFolderLocation, wikiFolderName), userInfo);
}
async function cloneSubWiki(parentFolderLocation, wikiFolderName, mainWikiPath, githubWikiUrl, userInfo) {
await clone(githubWikiUrl, path.join(parentFolderLocation, wikiFolderName), userInfo);
await linkWiki(mainWikiPath, wikiFolderName, path.join(parentFolderLocation, wikiFolderName));
}
module.exports = { createWiki, createSubWiki, removeWiki, ensureWikiExist, cloneWiki, cloneSubWiki };

View file

@ -7,7 +7,7 @@ const { initWikiGit, getRemoteUrl } = require('../libs/git');
const { stopWatchWiki } = require('../libs/wiki/watch-wiki');
const { stopWiki } = require('../libs/wiki/wiki-worker-mamager');
const { logger } = require('../libs/log');
const { createWiki, createSubWiki, removeWiki, ensureWikiExist } = require('../libs/create-wiki');
const { createWiki, createSubWiki, removeWiki, ensureWikiExist, cloneWiki, cloneSubWiki } = require('../libs/wiki/create-wiki');
const { ICON_PATH, REACT_PATH, DESKTOP_PATH, LOG_FOLDER } = require('../constants/paths');
const { getPreference, getPreferences, resetPreferences, setPreference } = require('../libs/preferences');
@ -60,7 +60,7 @@ const spellcheckLanguagesWindow = require('../windows/spellcheck-languages');
const loadListeners = () => {
ipcMain.handle('copy-wiki-template', async (event, newFolderPath, folderName) => {
try {
await createWiki(newFolderPath, folderName, logger);
await createWiki(newFolderPath, folderName);
return '';
} catch (error) {
return String(error);
@ -75,6 +75,24 @@ const loadListeners = () => {
return String(error);
}
});
ipcMain.handle('clone-wiki', async (event, parentFolderLocation, wikiFolderName, githubWikiUrl, userInfo) => {
try {
await cloneWiki(parentFolderLocation, wikiFolderName, githubWikiUrl, userInfo);
return '';
} catch (error) {
console.info(error);
return String(error);
}
});
ipcMain.handle('clone-sub-wiki', async (event, parentFolderLocation, wikiFolderName, mainWikiPath, githubWikiUrl, userInfo) => {
try {
await cloneSubWiki(parentFolderLocation, wikiFolderName, mainWikiPath, githubWikiUrl, userInfo);
return '';
} catch (error) {
console.info(error);
return String(error);
}
});
ipcMain.handle('ensure-wiki-exist', async (event, wikiPath, shouldBeMainWiki) => {
try {
await ensureWikiExist(wikiPath, shouldBeMainWiki);

View file

@ -13,7 +13,7 @@ import Alert from '@material-ui/lab/Alert';
import * as actions from '../../state/dialog-add-workspace/actions';
import type { IUserInfo } from './user-info';
import { requestCloneWiki, requestCloneSubWiki, getIconPath, initWikiGit } from '../../senders';
import { requestCloneWiki, requestCloneSubWiki, getIconPath } from '../../senders';
import useWikiCreationMessage from './use-wiki-creation-message';
@ -89,7 +89,7 @@ function CloneWikiDoneButton({
disabled={!parentFolderLocation || !githubWikiUrl || progressBarOpen || !userInfo}
onClick={async () => {
updateForm(workspaceFormData);
const cloneError = await requestCloneWiki(parentFolderLocation, wikiFolderName, githubWikiUrl);
const cloneError = await requestCloneWiki(parentFolderLocation, wikiFolderName, githubWikiUrl, userInfo);
if (cloneError) {
setWikiCreationMessage(cloneError);
} else {
@ -121,7 +121,7 @@ function CloneWikiDoneButton({
<CloseButton
variant="contained"
color="secondary"
disabled={!parentFolderLocation || !mainWikiToLink.name || !githubWikiUrl || progressBarOpen}
disabled={!parentFolderLocation || !mainWikiToLink.name || !githubWikiUrl || progressBarOpen || !userInfo}
onClick={async () => {
updateForm(workspaceFormData);
const creationError = await requestCloneSubWiki(
@ -129,6 +129,7 @@ function CloneWikiDoneButton({
wikiFolderName,
mainWikiToLink.name,
githubWikiUrl,
userInfo,
);
if (creationError) {
setWikiCreationMessage(creationError);

View file

@ -117,7 +117,7 @@ function DoneButton({
<CloseButton
variant="contained"
color="secondary"
disabled={!existedFolderLocation || !mainWikiToLink.name || !githubWikiUrl || progressBarOpen}
disabled={!existedFolderLocation || !mainWikiToLink.name || !githubWikiUrl || progressBarOpen || !userInfo}
onClick={async () => {
const wikiFolderName = basename(existedFolderLocation);
const parentFolderLocation = dirname(existedFolderLocation);

View file

@ -126,7 +126,7 @@ function NewWikiDoneButton({
<CloseButton
variant="contained"
color="secondary"
disabled={!parentFolderLocation || !mainWikiToLink.name || !githubWikiUrl || progressBarOpen}
disabled={!parentFolderLocation || !mainWikiToLink.name || !githubWikiUrl || progressBarOpen || !userInfo}
onClick={async () => {
updateForm(workspaceFormData);
let creationError = await requestCreateSubWiki(parentFolderLocation, wikiFolderName, mainWikiToLink.name);

View file

@ -27,7 +27,7 @@ export default function TabBar({ currentTab, currentTabSetter }: IProps) {
>
<Tab label="创建新WIKI" {...a11yProps(0)} />
<Tab label="打开本地WIKI" {...a11yProps(1)} />
<Tab label="拉取线上WIKI" {...a11yProps(2)} />
<Tab label="克隆线上WIKI" {...a11yProps(2)} />
</Tabs>
</Paper>
</AppBar>

View file

@ -1,5 +1,6 @@
// @flow
const { ipcRenderer } = window.require('electron');
import type { IUserInfo } from '../components/dialog-add-workspace/user-info';
export const requestCopyWikiTemplate = (newFolderPath: string, folderName: string) =>
ipcRenderer.invoke('copy-wiki-template', newFolderPath, folderName);
@ -11,6 +12,19 @@ export const requestCreateSubWiki = (
) => ipcRenderer.invoke('create-sub-wiki', newFolderPath, folderName, mainWikiToLink, onlyLink);
export const ensureWikiExist = (wikiPath: string, shouldBeMainWiki: boolean) =>
ipcRenderer.invoke('ensure-wiki-exist', wikiPath, shouldBeMainWiki);
export const requestCloneWiki = (
parentFolderLocation: string,
wikiFolderName: string,
githubWikiUrl: string,
userInfo: IUserInfo,
) => ipcRenderer.invoke('clone-wiki', parentFolderLocation, wikiFolderName, githubWikiUrl, userInfo);
export const requestCloneSubWiki = (
parentFolderLocation: string,
wikiFolderName: string,
mainWikiPath: string,
githubWikiUrl: string,
userInfo: IUserInfo,
) => ipcRenderer.invoke('clone-sub-wiki', parentFolderLocation, wikiFolderName, mainWikiPath, githubWikiUrl, userInfo);
export const requestOpen = (uri: string, isDirectory?: boolean) => ipcRenderer.send('request-open', uri, !!isDirectory);
export const requestShowMessageBox = (message: string, type: string) =>
ipcRenderer.send('request-show-message-box', message, type);