Performance improvements for "build-plugin" task
Summary: Few improvements for "build-plugin" task which together with Sandcastle command changes (D26872427) helps to build all plugins in CI ~30% faster if most of them has not changed (which is usually the case): 1) compute package checksum in the same script to not call additional yarn scripts for each plugin 2) avoid packaging plugin if it's checksum has not changed since last release Reviewed By: mweststrate Differential Revision: D26872253 fbshipit-source-id: 968102d32a1550ea7503f1169f0ef2863296383f
This commit is contained in:
committed by
Facebook GitHub Bot
parent
5df0fd6e52
commit
baeb8ba5be
50
desktop/pkg-lib/src/computePackageChecksum.ts
Normal file
50
desktop/pkg-lib/src/computePackageChecksum.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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 packlist from 'npm-packlist';
|
||||
import path from 'path';
|
||||
import crypto from 'crypto';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
export default async function computePackageChecksum(
|
||||
dir: string,
|
||||
): Promise<string> {
|
||||
const hash = crypto.createHash('sha1');
|
||||
hash.setEncoding('hex');
|
||||
const files = (await packlist({path: dir})).sort();
|
||||
for (const file of files) {
|
||||
// add hash of relative file path
|
||||
hash.write(process.platform === 'win32' ? file.replace(/\\/g, '/') : file);
|
||||
|
||||
const filePath = path.resolve(dir, file);
|
||||
|
||||
if (file === 'package.json') {
|
||||
// add hash of package.json with version set to "0.0.0" to avoid changing hash when only version changed
|
||||
const packageJson = await fs.readJson(filePath);
|
||||
if (packageJson.version) {
|
||||
packageJson.version = '0.0.0';
|
||||
}
|
||||
hash.write(JSON.stringify(packageJson));
|
||||
} else {
|
||||
// add hash of file content
|
||||
const stream = fs.createReadStream(filePath);
|
||||
try {
|
||||
stream.pipe(hash, {end: false});
|
||||
await new Promise((resolve, reject) => {
|
||||
stream.once('end', resolve);
|
||||
stream.once('error', reject);
|
||||
});
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
hash.end();
|
||||
return hash.read();
|
||||
}
|
||||
@@ -9,3 +9,4 @@
|
||||
|
||||
export {default as runBuild} from './runBuild';
|
||||
export {default as getWatchFolders} from './getWatchFolders';
|
||||
export {default as computePackageChecksum} from './computePackageChecksum';
|
||||
|
||||
@@ -11,6 +11,7 @@ import Metro from 'metro';
|
||||
import getWatchFolders from './getWatchFolders';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import {getInstalledPluginDetails} from 'flipper-plugin-lib';
|
||||
|
||||
let metroDir: string | undefined;
|
||||
const metroDirPromise = getMetroDir().then((dir) => (metroDir = dir));
|
||||
@@ -30,19 +31,29 @@ async function getMetroDir() {
|
||||
return __dirname;
|
||||
}
|
||||
|
||||
export default async function runBuild(
|
||||
inputDirectory: string,
|
||||
entry: string,
|
||||
out: string,
|
||||
dev: boolean,
|
||||
) {
|
||||
export default async function bundlePlugin(pluginDir: string, dev: boolean) {
|
||||
const stat = await fs.lstat(pluginDir);
|
||||
if (!stat.isDirectory()) {
|
||||
throw new Error(`Plugin source ${pluginDir} is not a directory.`);
|
||||
}
|
||||
const packageJsonPath = path.join(pluginDir, 'package.json');
|
||||
if (!(await fs.pathExists(packageJsonPath))) {
|
||||
throw new Error(
|
||||
`package.json is not found in plugin source directory ${pluginDir}.`,
|
||||
);
|
||||
}
|
||||
const plugin = await getInstalledPluginDetails(pluginDir);
|
||||
const entry = plugin.source;
|
||||
const out = path.resolve(pluginDir, plugin.main);
|
||||
await fs.ensureDir(path.dirname(out));
|
||||
|
||||
const sourceMapUrl = null; // inline source map
|
||||
const baseConfig = await Metro.loadConfig();
|
||||
const config = Object.assign({}, baseConfig, {
|
||||
reporter: {update: () => {}},
|
||||
projectRoot: inputDirectory,
|
||||
projectRoot: pluginDir,
|
||||
watchFolders: [metroDir || (await metroDirPromise)].concat(
|
||||
await getWatchFolders(inputDirectory),
|
||||
await getWatchFolders(pluginDir),
|
||||
),
|
||||
serializer: {
|
||||
...baseConfig.serializer,
|
||||
|
||||
Reference in New Issue
Block a user