Skip to content

Commit

Permalink
修复同步更新命令执行失败不反馈
Browse files Browse the repository at this point in the history
  • Loading branch information
q2316367743 committed Aug 20, 2022
1 parent a31ae27 commit d9cfe59
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 32 deletions.
21 changes: 16 additions & 5 deletions electron/handle/NativeHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@ ipcMain.handle('native:invoke:sync', (_event, args) => {

// 异步命令,直接监听
ipcMain.on('native:invoke:async', (event, args) => {
console.log('native:invoke:async');
console.log(`native:invoke:async:${args.id}`);
// 每一个都同步执行
child_process.exec(`"${args.command}" ${args.arg}`, {
encoding: "utf-8",
cwd: args.currentDir
}, () => {
// 全部执行结束,发送完成事件
console.log(`native:invoke:async:${args.id}`);
event.sender.send(`native:invoke:async:${args.id}`);
}, (error, stdout, stderr) => {
if (error) {
console.error(error);
// 全部执行结束,发送完成事件
console.log(`native:invoke:async:error:${args.id} - ${args.arg}`, error);
event.sender.send(`native:invoke:async:error:${args.id}`, error);
}
if (stdout) {
console.log(`native:invoke:async:success:${args.id} - ${args.arg}`, stdout);
event.sender.send(`native:invoke:async:success:${args.id}`, stdout);
}
if (stderr) {
console.log(`native:invoke:async:warning:${args.id} - ${args.arg}`, stderr);
event.sender.send(`native:invoke:async:warning:${args.id}`, stderr);
}
});
});

Expand Down
10 changes: 8 additions & 2 deletions src/api/NativeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ export default {
currentDir: options.currentDir
});
// 监听完成事件
ipcRenderer.on(`native:invoke:async:${id}`, () => {
options.callback()
ipcRenderer.on(`native:invoke:async:error:${id}`, (e: Error) => {
options.error(e)
});
ipcRenderer.on(`native:invoke:async:warning:${id}`, (_event: any, message: string) => {
options.warning(message)
});
ipcRenderer.on(`native:invoke:async:success:${id}`, () => {
options.success()
});
return Promise.resolve();
},
Expand Down
6 changes: 5 additions & 1 deletion src/api/entities/CommandAsyncOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export default interface CommandAsyncOptions {
/**
* 完成回调
*/
callback: () => void;
error: (e: Error) => void;

success: () => void;

warning: (message: string) => void;

}
1 change: 1 addition & 0 deletions src/i18n/language/enUs.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"root": "Site root Directory",
"url": "The web address must begin with http:// or https://"
},
"require": "url => URL is require",
"root": "root",
"title": "url",
"trailing_html": "Retention index.html",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/language/zhCn.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"root": "网站根目录",
"url": "网址, 必须以 http:// 或 https:// 开头"
},
"require": "网址 => URL是必填项",
"root": "根目录",
"title": "网址",
"trailing_html": "保留index.html",
Expand Down
10 changes: 10 additions & 0 deletions src/pages/config/hexo/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,23 @@ export default defineComponent({
this.hexo.timezone = item;
},
async save() {
let hexoUrl = this.hexo.url;
if (!hexoUrl || hexoUrl === '') {
ElMessage({
showClose: false,
type: 'warning',
message: this.$t('config.hexo.url.require')
});
return;
}
// 读取相关目录与内容
let hexoConfigBase = await Constant.FILE.HEXO_CONFIG_BASE();
let hexoConfigExtra = await Constant.FILE.HEXO_CONFIG_EXTRA();
let hexoConfig = await Constant.FILE.HEXO.CONFIG();
let baseConfig = this.hexo.render();
let extraConfig = this.extra;
try {
console.log(extraConfig)
// 在写入文件
await FileApi.writeFile(hexoConfigBase, baseConfig);
await FileApi.writeFile(hexoConfigExtra, extraConfig);
Expand Down
90 changes: 66 additions & 24 deletions src/strategy/blog/impl/HexoStrategyImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,18 @@ export default class HexoStrategyImpl implements BlogStrategy {
await this.invokeAsync(() => {
// 执行后置命令
console.log('执行后置命令', new Date().getTime());
this.copyPostImage().then(() => this.copyToDist().then(callback));
this.copyPostImage().then(() => this.copyToDist().then(callback))
.catch(e => {
console.error(e);
ElMessage({
showClose: true,
type: 'error',
message: '拷贝图片错误,' + e
});
});
}, () => {
// 报错了也要回调
callback();
});
console.log('命令已执行', new Date().getTime());
return Promise.resolve();
Expand Down Expand Up @@ -151,48 +162,79 @@ export default class HexoStrategyImpl implements BlogStrategy {
}

async invokeCommand(command: string): Promise<void> {
if (!(await this.isInit())) {
return Promise.reject("博客未初始化,请初始化后重试")
}
// 获取hexo命令目录
let hexoCommandPath = settingService.getEnvironment().hexoPath;
if (!hexoCommandPath || hexoCommandPath === "") {
return new Promise<void>((_resolve, reject) => {
reject("请配置hexo命令路径");
})
}
let hexoCommandPath = await this.getHexoCommandPath();
let hexoPath = await Constant.FOLDER.HEXO.BASE();
await NativeApi.invokeSync(hexoCommandPath, hexoPath, command);
return new Promise<void>((resolve) => {
resolve();
});
}

async invokeAsync(callback: () => void) {
if (!(await this.isInit())) {
return Promise.reject("博客未初始化,请初始化后重试")
}
// 获取hexo命令目录
let hexoCommandPath = settingService.getEnvironment().hexoPath;
if (!hexoCommandPath || hexoCommandPath === "") {
return new Promise<void>((_resolve, reject) => {
reject("请配置hexo命令路径");
})
}
async invokeAsync(callback: () => void, error: () => void) {
let hexoCommandPath = await this.getHexoCommandPath();
let hexoPath = await Constant.FOLDER.HEXO.BASE();
await NativeApi.invokeAsync({
command: hexoCommandPath,
args: Constant.HEXO.CLEAN,
currentDir: hexoPath,
callback: () => {
success: () => {
console.log('clean执行完成,开始执行deploy');
NativeApi.invokeAsync({
command: hexoCommandPath,
args: Constant.HEXO.DEPLOY,
currentDir: hexoPath,
callback: callback
success: callback,
warning: message => {
console.error(message);
ElMessage({
showClose: true,
type: 'warning',
message: message
});
error();
},
error: e => {
console.error(e);
ElMessage({
showClose: true,
type: 'error',
message: '' + e
});
error();
}
})
},
warning: message => {
console.error(message);
ElMessage({
showClose: true,
type: 'warning',
message: message
});
error();
},
error: e => {
console.error(e);
ElMessage({
showClose: true,
type: 'error',
message: '' + e
});
error();
}
})
}

async getHexoCommandPath(): Promise<string> {
if (!(await this.isInit())) {
return Promise.reject("博客未初始化,请初始化后重试")
}
// 获取hexo命令目录
let hexoCommandPath = settingService.getEnvironment().hexoPath;
if (!hexoCommandPath || hexoCommandPath === "") {
return Promise.reject("请配置hexo命令路径");
}
return Promise.resolve(hexoCommandPath);
}

}

0 comments on commit d9cfe59

Please sign in to comment.