Commit 89ebab63 by Steven Vachon Committed by GitHub

Don't use `process.chdir` (#27915)

... because it's global
parent a078e402
...@@ -86,7 +86,6 @@ ...@@ -86,7 +86,6 @@
"node-sass": "^4.13.1", "node-sass": "^4.13.1",
"optimize-css-assets-webpack-plugin": "^5.0.3", "optimize-css-assets-webpack-plugin": "^5.0.3",
"ora": "^4.0.3", "ora": "^4.0.3",
"p-series": "^2.1.0",
"pixelmatch": "^5.1.0", "pixelmatch": "^5.1.0",
"pngjs": "^3.4.0", "pngjs": "^3.4.0",
"postcss-flexbugs-fixes": "4.2.0", "postcss-flexbugs-fixes": "4.2.0",
......
...@@ -7,25 +7,15 @@ import { useSpinner } from '../utils/useSpinner'; ...@@ -7,25 +7,15 @@ import { useSpinner } from '../utils/useSpinner';
import { Task, TaskRunner } from './task'; import { Task, TaskRunner } from './task';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import globby from 'globby'; import globby from 'globby';
import series from 'p-series';
let distDir: string, cwd: string; const clean = (cwd: string) => useSpinner('Cleaning', () => execa('npm', ['run', 'clean'], { cwd }));
const clean = () => useSpinner('Cleaning', () => execa('npm', ['run', 'clean'])); const compile = (cwd: string) =>
useSpinner('Compiling sources', () => execa('tsc', ['-p', './tsconfig.build.json'], { cwd }));
const compile = () => useSpinner('Compiling sources', () => execa('tsc', ['-p', './tsconfig.build.json'])); const bundle = (cwd: string) => useSpinner('Bundling', () => execa('npm', ['run', 'bundle'], { cwd }));
const bundle = () => useSpinner('Bundling', () => execa('npm', ['run', 'bundle'])); const preparePackage = async (packageDist: string, pkg: any) => {
interface SavePackageOptions {
path: string;
pkg: {};
}
const savePackage = ({ path, pkg }: SavePackageOptions) =>
useSpinner('Updating package.json', () => fs.writeFile(path, JSON.stringify(pkg, null, 2)));
const preparePackage = async (pkg: any) => {
pkg = cloneDeep(pkg); // avoid mutations pkg = cloneDeep(pkg); // avoid mutations
pkg.main = 'index.js'; pkg.main = 'index.js';
...@@ -47,26 +37,27 @@ const preparePackage = async (pkg: any) => { ...@@ -47,26 +37,27 @@ const preparePackage = async (pkg: any) => {
deps['@grafana/ui'] = version; deps['@grafana/ui'] = version;
} }
await savePackage({ await useSpinner('Updating package.json', () =>
path: `${cwd}/dist/package.json`, fs.writeFile(`${packageDist}/package.json`, JSON.stringify(pkg, null, 2))
pkg, );
});
}; };
const moveFiles = () => { const moveFiles = (fromPath: string, toPath: string) => {
const files = ['README.md', 'CHANGELOG.md', 'index.js']; const files = ['README.md', 'CHANGELOG.md', 'index.js'];
return useSpinner(`Moving ${files.join(', ')} files`, () => { return useSpinner(`Moving ${files.join(', ')} files`, () => {
const promises = files.map(file => fs.copyFile(`${cwd}/${file}`, `${distDir}/${file}`)); const promises = files.map(file => fs.copyFile(`${fromPath}/${file}`, `${toPath}/${file}`));
return Promise.all(promises); return Promise.all(promises);
}); });
}; };
const moveStaticFiles = async (pkg: any) => { const moveStaticFiles = async (packageRoot: string, pkg: any) => {
if (pkg.name.endsWith('/ui')) { if (pkg.name.endsWith('/ui')) {
return useSpinner('Moving static files', async () => { return useSpinner('Moving static files', async () => {
const staticFiles = await globby('src/**/*.{png,svg,gif,jpg}'); const staticFiles = await globby(`${packageRoot}/src/**/*.{png,svg,gif,jpg}`);
const promises = staticFiles.map(file => fs.copyFile(file, file.replace(/^src/, 'compiled'))); const pathSearch = new RegExp(`^${packageRoot}/src`);
const pathReplace = `${packageRoot}/compiled`;
const promises = staticFiles.map(file => fs.copyFile(file, file.replace(pathSearch, pathReplace)));
await Promise.all(promises); await Promise.all(promises);
}); });
} }
...@@ -81,26 +72,20 @@ const buildTaskRunner: TaskRunner<PackageBuildOptions> = async ({ scope }) => { ...@@ -81,26 +72,20 @@ const buildTaskRunner: TaskRunner<PackageBuildOptions> = async ({ scope }) => {
throw new Error('Provide packages with -s, --scope <packages>'); throw new Error('Provide packages with -s, --scope <packages>');
} }
const scopes = scope.split(',').map(s => { const scopes = scope.split(',').map(async s => {
return async () => { const packageRoot = path.resolve(__dirname, `../../../../grafana-${s}`);
cwd = path.resolve(__dirname, `../../../../grafana-${s}`); const packageDist = `${packageRoot}/dist`;
// Lerna executes this in package's dir context, but for testing purposes I want to be able to run from root: const pkg = require(`${packageRoot}/package.json`);
// grafana-toolkit package:build --scope=<package> console.log(chalk.yellow(`Building ${pkg.name} (package.json version: ${pkg.version})`));
process.chdir(cwd); await clean(packageRoot);
distDir = `${cwd}/dist`; await compile(packageRoot);
const pkg = require(`${cwd}/package.json`); await moveStaticFiles(packageRoot, pkg);
console.log(chalk.yellow(`Building ${pkg.name} (package.json version: ${pkg.version})`)); await bundle(packageRoot);
await preparePackage(packageDist, pkg);
await clean(); await moveFiles(packageRoot, packageDist);
await compile();
await moveStaticFiles(pkg);
await bundle();
await preparePackage(pkg);
await moveFiles();
};
}); });
await series(scopes); await Promise.all(scopes);
}; };
export const buildPackageTask = new Task<PackageBuildOptions>('Package build', buildTaskRunner); export const buildPackageTask = new Task<PackageBuildOptions>('Package build', buildTaskRunner);
import { Task, TaskRunner } from './task'; import { Task, TaskRunner } from './task';
import { pluginBuildRunner } from './plugin.build'; import { pluginBuildRunner } from './plugin.build';
import { restoreCwd } from '../utils/cwd';
import { getPluginJson } from '../../config/utils/pluginValidation'; import { getPluginJson } from '../../config/utils/pluginValidation';
import { getPluginId } from '../../config/utils/getPluginId'; import { getPluginId } from '../../config/utils/getPluginId';
...@@ -176,9 +175,7 @@ const packagePluginRunner: TaskRunner<PluginCIOptions> = async ({ signingAdmin } ...@@ -176,9 +175,7 @@ const packagePluginRunner: TaskRunner<PluginCIOptions> = async ({ signingAdmin }
console.log('Building ZIP'); console.log('Building ZIP');
let zipName = pluginInfo.id + '-' + pluginInfo.info.version + '.zip'; let zipName = pluginInfo.id + '-' + pluginInfo.info.version + '.zip';
let zipFile = path.resolve(packagesDir, zipName); let zipFile = path.resolve(packagesDir, zipName);
process.chdir(distDir); await execa('zip', ['-r', zipFile, '.'], { cwd: distDir });
await execa('zip', ['-r', zipFile, '.']);
restoreCwd();
const zipStats = fs.statSync(zipFile); const zipStats = fs.statSync(zipFile);
if (zipStats.size < 100) { if (zipStats.size < 100) {
...@@ -202,9 +199,7 @@ const packagePluginRunner: TaskRunner<PluginCIOptions> = async ({ signingAdmin } ...@@ -202,9 +199,7 @@ const packagePluginRunner: TaskRunner<PluginCIOptions> = async ({ signingAdmin }
console.log('Creating documentation zip'); console.log('Creating documentation zip');
zipName = pluginInfo.id + '-' + pluginInfo.info.version + '-docs.zip'; zipName = pluginInfo.id + '-' + pluginInfo.info.version + '-docs.zip';
zipFile = path.resolve(packagesDir, zipName); zipFile = path.resolve(packagesDir, zipName);
process.chdir(docsDir); await execa('zip', ['-r', zipFile, '.'], { cwd: docsDir });
await execa('zip', ['-r', zipFile, '.']);
restoreCwd();
info.docs = await getPackageDetails(zipFile, docsDir); info.docs = await getPackageDetails(zipFile, docsDir);
} }
......
import { promises as fs } from 'fs';
import { Task, TaskRunner } from '../task'; import { Task, TaskRunner } from '../task';
import { restoreCwd } from '../../utils/cwd';
import execa = require('execa'); import execa = require('execa');
const fs = require('fs');
const util = require('util');
const readdirPromise = util.promisify(fs.readdir);
interface BundeManagedOptions {} interface BundeManagedOptions {}
...@@ -15,13 +11,12 @@ const bundleManagedPluginsRunner: TaskRunner<BundeManagedOptions> = async () => ...@@ -15,13 +11,12 @@ const bundleManagedPluginsRunner: TaskRunner<BundeManagedOptions> = async () =>
await Promise.all( await Promise.all(
MANAGED_PLUGINS_SCOPES.map(async scope => { MANAGED_PLUGINS_SCOPES.map(async scope => {
try { try {
const plugins = await readdirPromise(`${MANAGED_PLUGINS_PATH}/${scope}`); const plugins = await fs.readdir(`${MANAGED_PLUGINS_PATH}/${scope}`);
if (plugins.length > 0) { if (plugins.length > 0) {
for (const plugin of plugins) { for (const plugin of plugins) {
process.chdir(`${MANAGED_PLUGINS_PATH}/${scope}/${plugin}`);
try { try {
console.log(`[${scope}]: ${plugin} building...`); console.log(`[${scope}]: ${plugin} building...`);
await execa('yarn', ['build']); await execa('yarn', ['build'], { cwd: `${MANAGED_PLUGINS_PATH}/${scope}/${plugin}` });
console.log(`[${scope}]: ${plugin} bundled`); console.log(`[${scope}]: ${plugin} bundled`);
} catch (e) { } catch (e) {
console.log(e.stdout); console.log(e.stdout);
...@@ -33,7 +28,6 @@ const bundleManagedPluginsRunner: TaskRunner<BundeManagedOptions> = async () => ...@@ -33,7 +28,6 @@ const bundleManagedPluginsRunner: TaskRunner<BundeManagedOptions> = async () =>
} }
}) })
); );
restoreCwd();
}; };
export const bundleManagedTask = new Task<BundeManagedOptions>('Bundle managed plugins', bundleManagedPluginsRunner); export const bundleManagedTask = new Task<BundeManagedOptions>('Bundle managed plugins', bundleManagedPluginsRunner);
const cwd = process.cwd();
export const changeCwdToGrafanaUi = () => {
process.chdir(`${cwd}/packages/grafana-ui`);
return process.cwd();
};
export const changeCwdToGrafanaToolkit = () => {
process.chdir(`${cwd}/packages/grafana-toolkit`);
return process.cwd();
};
export const changeCwdToGrafanaUiDist = () => {
process.chdir(`${cwd}/packages/grafana-ui/dist`);
};
export const restoreCwd = () => {
process.chdir(cwd);
};
type PackageId = 'ui' | 'data' | 'runtime' | 'toolkit';
export const changeCwdToPackage = (scope: PackageId) => {
try {
process.chdir(`${cwd}/packages/grafana-${scope}`);
} catch (e) {
throw e;
}
return process.cwd();
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment