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.
70 lines
2.2 KiB
70 lines
2.2 KiB
const { app } = require('electron')
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
// const unzipper = require('unzipper')
|
|
const fse = require('fs-extra');
|
|
|
|
// function unzip(source, dest) {
|
|
// return new Promise((resolve, reject) => {
|
|
// fs.createReadStream(source)
|
|
// .pipe(unzipper.Extract({ path: dest }))
|
|
// .promise()
|
|
// .then(() => resolve(true), (e) => reject(e))
|
|
// });
|
|
// }
|
|
|
|
async function checkAppFolder(folderPath) {
|
|
// check index.html
|
|
const htmlFilePath = path.resolve(folderPath, './index.html')
|
|
if (!await fs.existsSync(htmlFilePath)) {
|
|
throw new Error('软件包中缺少入口index.html文件')
|
|
}
|
|
const htmlStr = await fs.readFileSync(htmlFilePath).toString()
|
|
if (!htmlStr.includes('<html')) {
|
|
throw new Error("软件包中index.html不是合法的html文件")
|
|
}
|
|
|
|
// check logo.png
|
|
const logoFilePath = path.resolve(folderPath, './logo.png')
|
|
if (!await fs.existsSync(logoFilePath)) {
|
|
throw new Error('软件包中缺少图标文件logo.png')
|
|
}
|
|
|
|
// check app.json
|
|
const appJsonFilePath = path.resolve(folderPath, './app.json')
|
|
if (!await fs.existsSync(appJsonFilePath)) {
|
|
throw new Error('软件包中缺少app.json')
|
|
}
|
|
const jsonStr = await fs.readFileSync(appJsonFilePath).toString()
|
|
let json = {}
|
|
try {
|
|
json = JSON.parse(jsonStr)
|
|
} catch (e) {
|
|
throw new Error('解析app.json失败')
|
|
}
|
|
['id', 'name'].forEach((k) => {
|
|
if (!(typeof json[k] === 'string' && json[k].length >= 2)) {
|
|
throw new Error(`app.json里${k}字段不合法,必须是长度为2以上的字符串`)
|
|
}
|
|
})
|
|
return {
|
|
...json,
|
|
logoFilePath,
|
|
}
|
|
}
|
|
|
|
module.exports = async (event, zipPath) => {
|
|
const tempDir = path.resolve(app.getPath('temp'), `./s207/install_apps/${Date.now()}`)
|
|
|
|
// await unzip(zipPath, tempDir)
|
|
|
|
const { id, name } = await checkAppFolder(tempDir)
|
|
|
|
const targetDir = path.resolve(app.getPath('home'), `./s207/apps/${id}`)
|
|
if (fse.pathExistsSync(targetDir)) {
|
|
throw new Error(`【${name} id:${id}】应用已存在,请删除后重新安装`)
|
|
}
|
|
await fse.moveSync(tempDir, targetDir)
|
|
|
|
return targetDir
|
|
}
|