diff --git a/build/linux.js b/build/linux.js deleted file mode 100644 index 252babd..0000000 --- a/build/linux.js +++ /dev/null @@ -1,15 +0,0 @@ -const fs = require('fs-extra'); -const path = require('path'); - -const distDir = path.resolve(__dirname, '../dist'); -const publicDir = `${distDir}/tmp/public`; -const outDir = `${distDir}/linux`; - -async function main() { - await fs.emptyDir(outDir); - // перемещаем public на место - if (await fs.pathExists(publicDir)) - await fs.move(publicDir, `${outDir}/public`); -} - -main(); diff --git a/build/prepkg.js b/build/prepkg.js new file mode 100644 index 0000000..5e4ba08 --- /dev/null +++ b/build/prepkg.js @@ -0,0 +1,43 @@ +const fs = require('fs-extra'); +const path = require('path'); +const { execSync } = require('child_process'); + +const platform = process.argv[2]; + +const distDir = path.resolve(__dirname, '../dist'); +const tmpDir = `${distDir}/tmp`; +const publicDir = `${tmpDir}/public`; +const outDir = `${distDir}/${platform}`; + +async function build() { + if (platform != 'linux' && platform != 'win') + throw new Error(`Unknown platform: ${platform}`); + + await fs.emptyDir(outDir); + + // перемещаем public на место + if (await fs.pathExists(publicDir)) { + + const zipFile = `${tmpDir}/public.zip`; + const jsonFile = `${distDir}/public.json`;//distDir !!! + + await fs.remove(zipFile); + execSync(`zip -r ${zipFile} .`, {cwd: publicDir, stdio: 'inherit'}); + + const data = (await fs.readFile(zipFile)).toString('base64'); + await fs.writeFile(jsonFile, JSON.stringify({data})); + } else { + throw new Error(`publicDir: ${publicDir} does not exist`); + } +} + +async function main() { + try { + await build(); + } catch(e) { + console.error(e); + process.exit(1); + } +} + +main(); diff --git a/build/win.js b/build/win.js deleted file mode 100644 index 286eb12..0000000 --- a/build/win.js +++ /dev/null @@ -1,15 +0,0 @@ -const fs = require('fs-extra'); -const path = require('path'); - -const distDir = path.resolve(__dirname, '../dist'); -const publicDir = `${distDir}/tmp/public`; -const outDir = `${distDir}/win`; - -async function main() { - await fs.emptyDir(outDir); - // перемещаем public на место - if (await fs.pathExists(publicDir)) - await fs.move(publicDir, `${outDir}/public`); -} - -main(); \ No newline at end of file diff --git a/package.json b/package.json index 5d0095f..c5616f5 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,15 @@ "scripts": { "dev": "nodemon --inspect --ignore server/.inpx-web --ignore client --exec 'node --expose-gc server --lib-dir=server/.inpx-web/lib'", "build:client": "webpack --config build/webpack.prod.config.js", - "build:linux": "npm run build:client && node build/linux && pkg -t node16-linux-x64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/linux/inpx-web .", - "build:win": "npm run build:client && node build/win && pkg -t node16-win-x64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/win/inpx-web .", + "build:linux": "npm run build:client && node build/prepkg.js linux && pkg -t node16-linux-x64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/linux/inpx-web .", + "build:win": "npm run build:client && node build/prepkg.js win && pkg -t node16-win-x64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/win/inpx-web .", "build:client-dev": "webpack --config build/webpack.dev.config.js", "postinstall": "npm run build:client-dev" }, "bin": "server/index.js", "pkg": { - "scripts": "server/config/*.js" + "scripts": "server/config/*.js", + "assets": "dist/public.json" }, "devDependencies": { "@babel/core": "^7.18.9", diff --git a/server/core/WebWorker.js b/server/core/WebWorker.js index e37cb90..fcd61f2 100644 --- a/server/core/WebWorker.js +++ b/server/core/WebWorker.js @@ -464,11 +464,14 @@ class WebWorker { async periodicCleanDir(dirConfig) { try { + for (const config of dirConfig) + await fs.ensureDir(config.dir); + let lastCleanDirTime = 0; while (1) {// eslint-disable-line no-constant-condition //чистка папок if (Date.now() - lastCleanDirTime >= cleanDirPeriod) { - for (const config of Object.values(dirConfig)) { + for (const config of dirConfig) { try { await this.cleanDir(config); } catch(e) { diff --git a/server/createWebApp.js b/server/createWebApp.js new file mode 100644 index 0000000..3f472ae --- /dev/null +++ b/server/createWebApp.js @@ -0,0 +1,23 @@ +const fs = require('fs-extra'); + +const webApp = require('../dist/public.json'); +const ZipReader = require('./core/ZipReader'); + +module.exports = async(config) => { + const zipFile = `${config.tempDir}/public.zip`; + + await fs.writeFile(zipFile, webApp.data, {encoding: 'base64'}); + + const zipReader = new ZipReader(); + await zipReader.open(zipFile); + + await fs.remove(config.publicDir); + + try { + await zipReader.extractAllToDir(config.publicDir); + } finally { + await zipReader.close(); + } + + await fs.remove(zipFile); +}; \ No newline at end of file diff --git a/server/index.js b/server/index.js index afbb163..79f29e0 100644 --- a/server/index.js +++ b/server/index.js @@ -52,6 +52,10 @@ async function init() { await fs.ensureDir(config.tempDir); await fs.emptyDir(config.tempDir); + //web app + const createWebApp = require('./createWebApp'); + await createWebApp(config); + //cli if (argv.help) { showHelp();