From 666032fa46dd6e0804a1ab0d07d8b9a11eba0fb8 Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Mon, 22 Jun 2020 08:14:29 -0700 Subject: [PATCH] "yarn build-plugin" command added Summary: Added "build-plugin" command. Can be used like this: `yarn build-plugin fresco` which produces package to 'sonar/dist/plugins/fresco.tgz' Reviewed By: mweststrate Differential Revision: D22160253 fbshipit-source-id: 48b202984b0f515bf253b595be1f59953a9ce411 --- desktop/package.json | 2 ++ desktop/scripts/build-plugin.ts | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 desktop/scripts/build-plugin.ts diff --git a/desktop/package.json b/desktop/package.json index ead97556f..a2cf50c39 100644 --- a/desktop/package.json +++ b/desktop/package.json @@ -233,6 +233,8 @@ "build:dev": "cross-env NODE_ENV=development ./ts-node scripts/build-release.ts $@", "prebuild-headless": "yarn build:pkg", "build-headless": "cross-env NODE_ENV=production ./ts-node scripts/build-headless.ts $@", + "prebuild-plugin": "yarn build:pkg", + "build-plugin": "./ts-node scripts/build-plugin.ts", "open-dist": "open ../dist/mac/Flipper.app --args --launcher=false --inspect=9229", "fix": "eslint . --fix --ext .js,.ts,.tsx", "pretest": "yarn build:pkg", diff --git a/desktop/scripts/build-plugin.ts b/desktop/scripts/build-plugin.ts new file mode 100644 index 000000000..dba936a37 --- /dev/null +++ b/desktop/scripts/build-plugin.ts @@ -0,0 +1,54 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import {pluginsDir, rootDir, distDir} from './paths'; +import path from 'path'; +import fs from 'fs-extra'; +import {execSync} from 'child_process'; + +(async function buildPlugin() { + const pluginDirArg = process.argv[2]; + const pluginDir = await resolveAbsolutePluginDir(pluginDirArg); + if (path.relative(pluginsDir, pluginDir).includes('..')) { + throw new Error( + `Plugin dir ${pluginDir} is not inside plugins directory ${pluginsDir}`, + ); + } + const outputFile = path.join( + distDir, + 'plugins', + path.relative(pluginsDir, pluginDir) + '.tgz', + ); + await fs.ensureDir(path.dirname(outputFile)); + await fs.remove(outputFile); + const bundleCmd = `yarn flipper-pkg bundle "${pluginDir}" --production`; + const packCmd = `yarn pack --cwd "${pluginDir}" --filename ${outputFile}`; + execSync(bundleCmd, {cwd: rootDir, stdio: 'inherit'}); + execSync(packCmd, {cwd: rootDir, stdio: 'inherit'}); +})(); + +async function resolveAbsolutePluginDir(dir: string): Promise { + if (path.isAbsolute(dir)) { + return dir; + } + const resolvedFromPluginDir = path.resolve(pluginsDir, dir); + if (await fs.pathExists(resolvedFromPluginDir)) { + return resolvedFromPluginDir; + } + const resolvedFromCwd = path.resolve(process.cwd(), dir); + if (await fs.pathExists(resolvedFromCwd)) { + return resolvedFromCwd; + } + throw new Error( + `Cannot resolve plugin dir path. Paths checked: ${[ + resolvedFromPluginDir, + resolvedFromCwd, + ]}`, + ); +}