electron launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.2 KiB

4 years ago
  1. const { app } = require('electron')
  2. const fs = require('fs')
  3. const path = require('path')
  4. // const unzipper = require('unzipper')
  5. const fse = require('fs-extra');
  6. // function unzip(source, dest) {
  7. // return new Promise((resolve, reject) => {
  8. // fs.createReadStream(source)
  9. // .pipe(unzipper.Extract({ path: dest }))
  10. // .promise()
  11. // .then(() => resolve(true), (e) => reject(e))
  12. // });
  13. // }
  14. async function checkAppFolder(folderPath) {
  15. // check index.html
  16. const htmlFilePath = path.resolve(folderPath, './index.html')
  17. if (!await fs.existsSync(htmlFilePath)) {
  18. throw new Error('软件包中缺少入口index.html文件')
  19. }
  20. const htmlStr = await fs.readFileSync(htmlFilePath).toString()
  21. if (!htmlStr.includes('<html')) {
  22. throw new Error("软件包中index.html不是合法的html文件")
  23. }
  24. // check logo.png
  25. const logoFilePath = path.resolve(folderPath, './logo.png')
  26. if (!await fs.existsSync(logoFilePath)) {
  27. throw new Error('软件包中缺少图标文件logo.png')
  28. }
  29. // check app.json
  30. const appJsonFilePath = path.resolve(folderPath, './app.json')
  31. if (!await fs.existsSync(appJsonFilePath)) {
  32. throw new Error('软件包中缺少app.json')
  33. }
  34. const jsonStr = await fs.readFileSync(appJsonFilePath).toString()
  35. let json = {}
  36. try {
  37. json = JSON.parse(jsonStr)
  38. } catch (e) {
  39. throw new Error('解析app.json失败')
  40. }
  41. ['id', 'name'].forEach((k) => {
  42. if (!(typeof json[k] === 'string' && json[k].length >= 2)) {
  43. throw new Error(`app.json里${k}字段不合法,必须是长度为2以上的字符串`)
  44. }
  45. })
  46. return {
  47. ...json,
  48. logoFilePath,
  49. }
  50. }
  51. module.exports = async (event, zipPath) => {
  52. const tempDir = path.resolve(app.getPath('temp'), `./s207/install_apps/${Date.now()}`)
  53. // await unzip(zipPath, tempDir)
  54. const { id, name } = await checkAppFolder(tempDir)
  55. const targetDir = path.resolve(app.getPath('home'), `./s207/apps/${id}`)
  56. if (fse.pathExistsSync(targetDir)) {
  57. throw new Error(`${name} id:${id}】应用已存在,请删除后重新安装`)
  58. }
  59. await fse.moveSync(tempDir, targetDir)
  60. return targetDir
  61. }