sonarqube-scanner
Version:
SonarQube/SonarCloud Scanner for the JavaScript world
141 lines (140 loc) • 4.72 kB
JavaScript
;
/*
* sonar-scanner-npm
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDeps = getDeps;
exports.setDeps = setDeps;
exports.resetDeps = resetDeps;
const node_child_process_1 = require("node:child_process");
const node_fs_1 = __importDefault(require("node:fs"));
const node_util_1 = __importDefault(require("node:util"));
const file_1 = require("./file");
const java_1 = require("./java");
const process_1 = require("./process");
const request_1 = require("./request");
const scanner_cli_1 = require("./scanner-cli");
const scanner_engine_1 = require("./scanner-engine");
const execAsync = node_util_1.default.promisify(node_child_process_1.exec);
/**
* Creates the default dependencies using real implementations.
*/
function createDefaultDeps() {
return {
fs: {
existsSync: node_fs_1.default.existsSync,
readFileSync: node_fs_1.default.readFileSync,
readFile: node_fs_1.default.readFile,
mkdirSync: node_fs_1.default.mkdirSync,
createReadStream: node_fs_1.default.createReadStream,
createWriteStream: node_fs_1.default.createWriteStream,
remove: (filePath) => node_fs_1.default.promises.rm(filePath, { recursive: true, force: true }),
writeFile: (filePath, data) => node_fs_1.default.promises.writeFile(filePath, data),
exists: async (filePath) => {
try {
await node_fs_1.default.promises.access(filePath);
return true;
}
catch {
return false;
}
},
ensureDir: (dirPath) => node_fs_1.default.promises.mkdir(dirPath, { recursive: true }).then(() => { }),
},
process: {
get platform() {
return process.platform;
},
get arch() {
return process.arch;
},
get env() {
return process.env;
},
cwd: () => process.cwd(),
},
http: {
fetch: request_1.fetch,
download: request_1.download,
},
spawn: node_child_process_1.spawn,
execAsync,
scan: {
serverSupportsJREProvisioning: java_1.serverSupportsJREProvisioning,
fetchJRE: java_1.fetchJRE,
downloadScannerCli: scanner_cli_1.downloadScannerCli,
runScannerCli: scanner_cli_1.runScannerCli,
fetchScannerEngine: scanner_engine_1.fetchScannerEngine,
runScannerEngine: scanner_engine_1.runScannerEngine,
locateExecutableFromPath: process_1.locateExecutableFromPath,
},
file: {
extractArchive: file_1.extractArchive,
},
};
}
// Module-level dependency container
let deps = createDefaultDeps();
/**
* Get the current dependency container.
* Internal functions use this to access dependencies.
*/
function getDeps() {
return deps;
}
/**
* Set/override dependencies. Used for testing.
* Merges the provided partial dependencies with the defaults.
*
* @param newDeps - Partial dependencies to override
*/
function setDeps(newDeps) {
const defaults = createDefaultDeps();
deps = {
...defaults,
...newDeps,
// Deep merge nested objects
fs: {
...defaults.fs,
...newDeps.fs,
},
process: {
...defaults.process,
...newDeps.process,
},
http: {
...defaults.http,
...newDeps.http,
},
scan: {
...defaults.scan,
...newDeps.scan,
},
file: {
...defaults.file,
...newDeps.file,
},
};
}
/**
* Reset dependencies to defaults. Should be called in afterEach() in tests.
*/
function resetDeps() {
deps = createDefaultDeps();
}