diff --git a/desktop/scripts/build-flipper-server-release.tsx b/desktop/scripts/build-flipper-server-release.tsx index afdb6b537..f775941a2 100644 --- a/desktop/scripts/build-flipper-server-release.tsx +++ b/desktop/scripts/build-flipper-server-release.tsx @@ -24,6 +24,16 @@ import fs from 'fs-extra'; import {downloadIcons} from './build-icons'; import {spawn} from 'promisify-child-process'; import {homedir} from 'os'; +import {need} from 'pkg-fetch'; + +// This needs to be tested individually. As of 2022Q2, node17 is not supported. +const SUPPORTED_NODE_PLATFORM = 'node16'; + +enum BuildPlatform { + LINUX = 'linux', + WINDOWS = 'windows', + MAC_X64 = 'mac-x64', +} const argv = yargs .usage('yarn build-flipper-server [args]') @@ -92,6 +102,21 @@ const argv = yargs 'Unique build identifier to be used as the version patch part for the build', type: 'number', }, + mac: { + describe: 'Build a platform-specific bundle for MacOS.', + type: 'boolean', + default: false, + }, + win: { + describe: 'Build a platform-specific bundle for Windows.', + type: 'boolean', + default: false, + }, + linux: { + describe: 'Build a platform-specific bundle for Linux.', + type: 'boolean', + default: false, + }, }) .help() .parse(process.argv.slice(1)); @@ -271,9 +296,7 @@ async function runPostBuildAction(archive: string, dir: string) { }); } else if (argv.start) { console.log(`⚙️ Starting flipper-server from build dir`); - await spawn('yarn', ['install', '--production', '--no-lockfile'], { - cwd: dir, - }); + await yarnInstall(dir); await spawn('./server.js', [argv.open ? '--open' : '--no-open'], { cwd: dir, stdio: 'inherit', @@ -281,6 +304,13 @@ async function runPostBuildAction(archive: string, dir: string) { } } +async function yarnInstall(dir: string) { + console.log(`⚙️ Running yarn install in ${dir}`); + await spawn('yarn', ['install', '--production', '--no-lockfile'], { + cwd: dir, + }); +} + async function buildServerRelease() { console.log(`⚙️ Starting build-flipper-server-release`); console.dir(argv); @@ -309,6 +339,65 @@ async function buildServerRelease() { const archive = await packNpmArchive(dir, versionNumber); await runPostBuildAction(archive, dir); + + const platforms: BuildPlatform[] = []; + if (argv.linux) { + platforms.push(BuildPlatform.LINUX); + } + // TODO: In the future, also cover aarch64 here. + if (argv.mac) { + platforms.push(BuildPlatform.MAC_X64); + } + if (argv.win) { + platforms.push(BuildPlatform.WINDOWS); + } + + if (platforms.length > 0) { + await yarnInstall(dir); + } + platforms.forEach(bundleServerReleaseForPlatform.bind(null, dir)); +} + +function nodeArchFromBuildPlatform(_platform: BuildPlatform): string { + // TODO: Change this as we support aarch64. + return 'x64'; +} + +function nodePlatformFromBuildPlatform(platform: BuildPlatform): string { + switch (platform) { + case BuildPlatform.LINUX: + return 'linux'; + case BuildPlatform.MAC_X64: + return 'macos'; + case BuildPlatform.WINDOWS: + return 'win32'; + default: + throw new Error(`Unsupported platform: ${platform}`); + } +} + +async function bundleServerReleaseForPlatform( + dir: string, + platform: BuildPlatform, +) { + console.log(`⚙️ Building platform-specific bundle for ${platform}`); + const outputDir = path.join( + distDir, + `flipper-server-${platform.toString().toLocaleLowerCase()}`, + ); + await fs.mkdirp(outputDir); + + console.log(`⚙️ Copying from ${dir} to ${outputDir}`); + await fs.copy(dir, outputDir); + + console.log(`⚙️ Downloading compatible node version`); + await need({ + arch: nodeArchFromBuildPlatform(platform), + platform: nodePlatformFromBuildPlatform(platform), + output: path.join(outputDir, 'node'), + nodeRange: SUPPORTED_NODE_PLATFORM, + }); + console.log(`✅ Wrote ${platform}-specific server version to ${outputDir}`); } buildServerRelease().catch((e) => { diff --git a/desktop/scripts/package.json b/desktop/scripts/package.json index 8ad8171ec..159b77028 100644 --- a/desktop/scripts/package.json +++ b/desktop/scripts/package.json @@ -32,6 +32,7 @@ "metro-minify-terser": "^0.66.2", "p-filter": "^2.1.0", "p-map": "^4.0.0", + "pkg-fetch": "^3.3.0", "promisify-child-process": "^4.1.0", "socket.io": "^4.4.1", "tmp": "^0.2.1", diff --git a/desktop/yarn.lock b/desktop/yarn.lock index ae82c200a..e07e8a624 100644 --- a/desktop/yarn.lock +++ b/desktop/yarn.lock @@ -9082,6 +9082,11 @@ mixin-deep@^1.2.0: for-in "^1.0.2" is-extendable "^1.0.1" +mkdirp-classic@^0.5.2: + version "0.5.3" + resolved "https://registry.yarnpkg.com/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz#fa10c9115cc6d8865be221ba47ee9bed78601113" + integrity sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A== + mkdirp@1.x, mkdirp@^1.0.3, mkdirp@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" @@ -9185,7 +9190,7 @@ node-fetch@2.6.0: resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd" integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA== -node-fetch@^2.2.0, node-fetch@^2.6.0, node-fetch@^2.6.1, node-fetch@^2.6.7: +node-fetch@^2.2.0, node-fetch@^2.6.0, node-fetch@^2.6.1, node-fetch@^2.6.6, node-fetch@^2.6.7: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ== @@ -9813,6 +9818,20 @@ pkg-dir@^4.2.0: dependencies: find-up "^4.0.0" +pkg-fetch@^3.3.0: + version "3.3.0" + resolved "https://registry.yarnpkg.com/pkg-fetch/-/pkg-fetch-3.3.0.tgz#3afc2fb7a19219839cf75654fa8b54a2630df891" + integrity sha512-xJnIZ1KP+8rNN+VLafwu4tEeV4m8IkFBDdCFqmAJz9K1aiXEtbARmdbEe6HlXWGSVuShSHjFXpfkKRkDBQ5kiA== + dependencies: + chalk "^4.1.2" + fs-extra "^9.1.0" + https-proxy-agent "^5.0.0" + node-fetch "^2.6.6" + progress "^2.0.3" + semver "^7.3.5" + tar-fs "^2.1.1" + yargs "^16.2.0" + plist@^3.0.1, plist@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/plist/-/plist-3.0.4.tgz#a62df837e3aed2bb3b735899d510c4f186019cbe" @@ -11778,6 +11797,16 @@ tar-fs@^2.0.0: pump "^3.0.0" tar-stream "^2.0.0" +tar-fs@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-2.1.1.tgz#489a15ab85f1f0befabb370b7de4f9eb5cbe8784" + integrity sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng== + dependencies: + chownr "^1.1.1" + mkdirp-classic "^0.5.2" + pump "^3.0.0" + tar-stream "^2.1.4" + tar-stream@^1.5.2: version "1.6.2" resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-1.6.2.tgz#8ea55dab37972253d9a9af90fdcd559ae435c555" @@ -11791,7 +11820,7 @@ tar-stream@^1.5.2: to-buffer "^1.1.1" xtend "^4.0.0" -tar-stream@^2.0.0, tar-stream@^2.2.0: +tar-stream@^2.0.0, tar-stream@^2.1.4, tar-stream@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-2.2.0.tgz#acad84c284136b060dc3faa64474aa9aebd77287" integrity sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==