Commit 89ebab63 by Steven Vachon Committed by GitHub

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

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