Skip to content

Commit

Permalink
[7.17] chore(NA): auto bootstrap after removing node modules manually (
Browse files Browse the repository at this point in the history
…#134961) (#134969)

* chore(NA): auto bootstrap after removing node modules manually (#134961)

* chore(NA): mechanism for autobootstrap when manually removing node_modules

* fix(NA): check folders with isDirectory

* fix(NA): check folders with isDirectory

* fix(NA): check folders with isDirectory

* docs(NA): update typo on code comment

(cherry picked from commit 9a1e1d0)

# Conflicts:
#	packages/kbn-pm/dist/index.js
#	packages/kbn-pm/src/commands/bootstrap.ts
#	packages/kbn-pm/src/utils/bazel/index.ts

* update kbn/pm dist

* refact(NA): missing comma

Co-authored-by: spalger <[email protected]>
  • Loading branch information
mistic and spalger committed Jun 23, 2022
1 parent a6a2a6e commit cd49631
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 32 deletions.
34 changes: 30 additions & 4 deletions packages/kbn-pm/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8910,9 +8910,11 @@ const BootstrapCommand = {
const kibanaProjectPath = ((_projects$get = projects.get('kibana')) === null || _projects$get === void 0 ? void 0 : _projects$get.path) || '';
const runOffline = (options === null || options === void 0 ? void 0 : options.offline) === true;
const reporter = _kbn_dev_utils_ci_stats_reporter__WEBPACK_IMPORTED_MODULE_1__["CiStatsReporter"].fromEnv(_utils_log__WEBPACK_IMPORTED_MODULE_2__["log"]);
const timings = []; // Force install is set in case a flag is passed into yarn kbn bootstrap
const timings = []; // Force install is set in case a flag is passed into yarn kbn bootstrap or
// our custom logic have determined there is a chance node_modules have been manually deleted and as such bazel
// tracking mechanism is no longer valid

const forceInstall = !!options && options['force-install'] === true; // Install bazel machinery tools if needed
const forceInstall = !!options && options['force-install'] === true || (await Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_9__["haveNodeModulesBeenManuallyDeleted"])(kibanaProjectPath)); // Install bazel machinery tools if needed

await Object(_utils_bazel__WEBPACK_IMPORTED_MODULE_9__["installBazelTools"])(rootPath); // Setup remote cache settings in .bazelrc.cache if needed

Expand Down Expand Up @@ -54857,8 +54859,10 @@ __webpack_require__.r(__webpack_exports__);

/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "runIBazel", function() { return _run__WEBPACK_IMPORTED_MODULE_2__["runIBazel"]; });

/* harmony import */ var _yarn_integrity__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(547);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "removeYarnIntegrityFileIfExists", function() { return _yarn_integrity__WEBPACK_IMPORTED_MODULE_3__["removeYarnIntegrityFileIfExists"]; });
/* harmony import */ var _yarn__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(547);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "removeYarnIntegrityFileIfExists", function() { return _yarn__WEBPACK_IMPORTED_MODULE_3__["removeYarnIntegrityFileIfExists"]; });

/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "haveNodeModulesBeenManuallyDeleted", function() { return _yarn__WEBPACK_IMPORTED_MODULE_3__["haveNodeModulesBeenManuallyDeleted"]; });

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
Expand Down Expand Up @@ -61285,6 +61289,7 @@ function observeReadable(readable) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removeYarnIntegrityFileIfExists", function() { return removeYarnIntegrityFileIfExists; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "haveNodeModulesBeenManuallyDeleted", function() { return haveNodeModulesBeenManuallyDeleted; });
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _fs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(252);
Expand All @@ -61296,6 +61301,7 @@ __webpack_require__.r(__webpack_exports__);
* Side Public License, v 1.
*/

// yarn integrity file checker

async function removeYarnIntegrityFileIfExists(nodeModulesPath) {
try {
Expand All @@ -61307,6 +61313,26 @@ async function removeYarnIntegrityFileIfExists(nodeModulesPath) {
}
} catch {// no-op
}
} // yarn and bazel integration checkers

async function areNodeModulesPresent(kbnRootPath) {
try {
return await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'node_modules'));
} catch {
return false;
}
}

async function haveBazelFoldersBeenCreatedBefore(kbnRootPath) {
try {
return (await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'bazel-bin', 'packages'))) || (await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'bazel-kibana', 'packages'))) || (await Object(_fs__WEBPACK_IMPORTED_MODULE_1__["isDirectory"])(Object(path__WEBPACK_IMPORTED_MODULE_0__["resolve"])(kbnRootPath, 'bazel-out', 'host')));
} catch {
return false;
}
}

async function haveNodeModulesBeenManuallyDeleted(kbnRootPath) {
return !(await areNodeModulesPresent(kbnRootPath)) && (await haveBazelFoldersBeenCreatedBefore(kbnRootPath));
}

/***/ }),
Expand Down
15 changes: 12 additions & 3 deletions packages/kbn-pm/src/commands/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import { ICommand } from './';
import { readYarnLock } from '../utils/yarn_lock';
import { sortPackageJson } from '../utils/sort_package_json';
import { validateDependencies } from '../utils/validate_dependencies';
import { installBazelTools, removeYarnIntegrityFileIfExists, runBazel } from '../utils/bazel';
import {
installBazelTools,
haveNodeModulesBeenManuallyDeleted,
removeYarnIntegrityFileIfExists,
runBazel,
} from '../utils/bazel';
import { setupRemoteCache } from '../utils/bazel/setup_remote_cache';

export const BootstrapCommand: ICommand = {
Expand All @@ -37,8 +42,12 @@ export const BootstrapCommand: ICommand = {
const reporter = CiStatsReporter.fromEnv(log);
const timings = [];

// Force install is set in case a flag is passed into yarn kbn bootstrap
const forceInstall = !!options && options['force-install'] === true;
// Force install is set in case a flag is passed into yarn kbn bootstrap or
// our custom logic have determined there is a chance node_modules have been manually deleted and as such bazel
// tracking mechanism is no longer valid
const forceInstall =
(!!options && options['force-install'] === true) ||
(await haveNodeModulesBeenManuallyDeleted(kibanaProjectPath));

// Install bazel machinery tools if needed
await installBazelTools(rootPath);
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-pm/src/utils/bazel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
export * from './get_cache_folders';
export * from './install_tools';
export * from './run';
export * from './yarn_integrity';
export * from './yarn';
53 changes: 53 additions & 0 deletions packages/kbn-pm/src/utils/bazel/yarn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { join, resolve } from 'path';
import { isDirectory, isFile, tryRealpath, unlink } from '../fs';

// yarn integrity file checker
export async function removeYarnIntegrityFileIfExists(nodeModulesPath: string) {
try {
const nodeModulesRealPath = await tryRealpath(nodeModulesPath);
const yarnIntegrityFilePath = join(nodeModulesRealPath, '.yarn-integrity');

// check if the file exists and delete it in that case
if (await isFile(yarnIntegrityFilePath)) {
await unlink(yarnIntegrityFilePath);
}
} catch {
// no-op
}
}

// yarn and bazel integration checkers
async function areNodeModulesPresent(kbnRootPath: string) {
try {
return await isDirectory(resolve(kbnRootPath, 'node_modules'));
} catch {
return false;
}
}

async function haveBazelFoldersBeenCreatedBefore(kbnRootPath: string) {
try {
return (
(await isDirectory(resolve(kbnRootPath, 'bazel-bin', 'packages'))) ||
(await isDirectory(resolve(kbnRootPath, 'bazel-kibana', 'packages'))) ||
(await isDirectory(resolve(kbnRootPath, 'bazel-out', 'host')))
);
} catch {
return false;
}
}

export async function haveNodeModulesBeenManuallyDeleted(kbnRootPath: string) {
return (
!(await areNodeModulesPresent(kbnRootPath)) &&
(await haveBazelFoldersBeenCreatedBefore(kbnRootPath))
);
}
24 changes: 0 additions & 24 deletions packages/kbn-pm/src/utils/bazel/yarn_integrity.ts

This file was deleted.

0 comments on commit cd49631

Please sign in to comment.