diff --git a/.github/workflows/package_arm64.yml b/.github/workflows/package_arm64.yml index 709b973e4f..42807062ed 100644 --- a/.github/workflows/package_arm64.yml +++ b/.github/workflows/package_arm64.yml @@ -45,7 +45,7 @@ jobs: - name: Build for arm64 run: | - npm run pkg -- --skip-build --arch=arm64 + npm run pkg -- --bundle --skip-build --arch=arm64 - name: Upload artifacts if: github.event_name == 'workflow_dispatch' diff --git a/esbuild.js b/esbuild.js index 4c0a6e8c6b..acd52f9c13 100644 --- a/esbuild.js +++ b/esbuild.js @@ -1,9 +1,31 @@ const esbuild = require('esbuild') const { cp, stat, readFile, writeFile } = require('fs/promises') const { exists, emptyDir } = require('fs-extra') +const { join } = require('path') const outputDir = 'build' +function cleanPkgJson(json) { + delete json.devDependencies + delete json['release-it'] + delete json.optionalDependencies + delete json.dependencies + return json +} + +/** + * Remove useless fields from package.json, this is needed mostly for `pkg` + * otherwise it will try to bundle dependencies + */ +async function patchPkgJson(path) { + const pkgJsonPath = join(outputDir, path, 'package.json') + const pkgJson = require('./' + pkgJsonPath) + cleanPkgJson(pkgJson) + delete pkgJson.scripts + delete pkgJson.exports + await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2)) +} + // from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487 const nativeNodeModulesPlugin = { name: 'native-node-modules', @@ -134,6 +156,27 @@ async function main() { console.log(`Asset "${path}" does not exist. Skipping...`) } } + + // create main patched packege.json + const pkgJson = require('./package.json') + cleanPkgJson(pkgJson) + + pkgJson.scripts = { + start: 'node index.js', + } + + pkgJson.bin = 'index.js' + pkgJson.pkg = { + assets: ['dist/**', 'snippets/**', 'node_modules/**'], + } + + await writeFile( + `${outputDir}/package.json`, + JSON.stringify(pkgJson, null, 2), + ) + + await patchPkgJson('node_modules/@zwave-js/config') + await patchPkgJson('node_modules/zwave-js') } main().catch((err) => { diff --git a/package.json b/package.json index 053b6def36..5551d148d2 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "requireCleanWorkingDir": false }, "hooks": { - "before:release": "./package.sh --release" + "before:release": "./package.sh --bundle" }, "npm": { "publish": true diff --git a/package.sh b/package.sh index 03f72f01f5..0961dd1861 100755 --- a/package.sh +++ b/package.sh @@ -33,6 +33,11 @@ ask() { done } +pkg() { + echo "Executing command: npx pkg $@" + npx pkg $@ +} + APP=$(node -p "require('./package.json').name") PKG_FOLDER="pkg" @@ -67,14 +72,20 @@ if [ ! -z "$1" ]; then echo "## Skipping build..." fi + # if --bundle is passed as argument, cd to `build` folder + if [[ "$@" == *"--bundle"* ]]; then + echo "## Building bundle..." + echo '' + npm run bundle + echo "## Changing directory to build folder" + cd build + fi + if [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then - echo "Executing command: pkg package.json -t node$NODE_MAJOR-linux-arm64 --out-path $PKG_FOLDER" pkg package.json -t node$NODE_MAJOR-linux-arm64 --out-path $PKG_FOLDER elif [ "$ARCH" = "armv7" ]; then - echo "Executing command: pkg package.json -t node$NODE_MAJOR-linux-armv7 --out-path $PKG_FOLDER" pkg package.json -t node$NODE_MAJOR-linux-armv7 --out-path $PKG_FOLDER --public-packages=* else - echo "Executing command: pkg package.json -t node$NODE_MAJOR-linux-x64,node$NODE_MAJOR-win-x64 --out-path $PKG_FOLDER" pkg package.json -t node$NODE_MAJOR-linux-x64,node$NODE_MAJOR-win-x64 --out-path $PKG_FOLDER fi