kibana/packages/kbn-eslint-plugin-eslint/helpers/find_kibana_root.js
Luke Elmers b6287708f6
Adds AGPL 3.0 license (#192025)
Updates files outside of x-pack to be triple-licensed under Elastic
License 2.0, AGPL 3.0, or SSPL 1.0.
2024-09-06 19:02:41 -06:00

50 lines
1.8 KiB
JavaScript

/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
const path = require('path');
const fs = require('fs');
function isKibanaRoot(maybeKibanaRoot) {
try {
const packageJsonPath = path.join(maybeKibanaRoot, 'package.json');
fs.accessSync(packageJsonPath, fs.constants.R_OK);
const packageJsonContent = fs.readFileSync(packageJsonPath);
return JSON.parse(packageJsonContent).name === 'kibana';
} catch (e) {
return false;
}
}
module.exports = function findKibanaRoot() {
let maybeKibanaRoot = path.resolve(__dirname, '../../..');
// when using syslinks, __dirname reports outside of the repo
// if that's the case, the path will contain .cache/bazel
if (!maybeKibanaRoot.includes('.cache/bazel')) {
return maybeKibanaRoot;
}
// process.argv[1] would be the eslint binary, a correctly-set editor
// will use a local eslint inside the repo node_modules and its value
// should be `ACTUAL_KIBANA_ROOT/node_modules/.bin/eslint`
maybeKibanaRoot = path.resolve(process.argv[1], '../../../');
if (isKibanaRoot(maybeKibanaRoot)) {
return maybeKibanaRoot;
}
// eslint should run on the repo root level
// try to use process.cwd as the kibana root
maybeKibanaRoot = process.cwd();
if (isKibanaRoot(maybeKibanaRoot)) {
return maybeKibanaRoot;
}
// fallback to the first predicted path (original script)
return maybeKibanaRoot;
};