@dxworks/nuget-inspector
Version:
Adaptation of the BlackDuck Nuget Dotnet 5 Inspector
146 lines (126 loc) • 5.45 kB
JavaScript
const _package = require('../package.json')
const path = require('path')
const os = require('os')
const fs = require('fs')
const axios = require('axios')
const unzipper = require('unzipper')
const {spawnSync} = require('child_process')
async function downloadAndInstallNugetInspector() {
const version = _package.version
console.log('Checking if Nuget-Inspector ' + version + ' is installed and available')
const currentVersionFolder = path.resolve(os.homedir(), '.dxw', 'depinder', 'nuget-inspector', _package.version)
let platformName = getPlatformName();
const nugetInspectorExePath = path.resolve(currentVersionFolder, platformName === 'win' ? 'NugetDotnet5Inspector.exe' : 'NugetDotnet5Inspector');
if (!fs.existsSync(nugetInspectorExePath)) {
fs.mkdirSync(currentVersionFolder, {recursive: true})
console.log(`Downloading NugetInspector ${_package.version}`)
const downloadedFile = await downloadFile(`https://github.com/dxworks/nuget-dotnet5-inspector/releases/download/v${_package.version}-depinder/nuget-inspector-${platformName}.zip`, path.resolve(currentVersionFolder, 'nuget-inspector.zip'))
console.log(`Download Finished`)
console.log('Installing...')
await unzip(downloadedFile, {path: currentVersionFolder, overwriteRootDir: true})
fs.rmSync(downloadedFile, {force: true})
console.log('Install Finished')
fs.chmodSync(nugetInspectorExePath, '755')
} else {
console.log('Found local installation')
}
return {currentVersionFolder, nugetInspectorExePath};
}
async function runNugetInspector(options) {
const {currentVersionFolder, nugetInspectorExePath} = await downloadAndInstallNugetInspector();
// Slice off 'node' and script path from process.argv if no args provided
const args = options?.args ?? process.argv.slice(2);
const cwd = options?.workingDirectory ?? currentVersionFolder;
const result = spawnSync(nugetInspectorExePath, args, {cwd, stdio: 'inherit'});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
throw new Error(`NugetInspector exited with code ${result.status}`);
}
}
async function runNuGetInspectorProgrammatically(targetPath, outputDirectory, cwd) {
const {currentVersionFolder, nugetInspectorExePath} = await downloadAndInstallNugetInspector();
const args = [`-target_path=${targetPath}`, `-output_directory=${outputDirectory}`];
const workingDir = cwd ?? currentVersionFolder;
const result = spawnSync(nugetInspectorExePath, args, {cwd: workingDir});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
const stderr = result.stderr?.toString() || '';
throw new Error(`NugetInspector exited with code ${result.status}${stderr ? ': ' + stderr : ''}`);
}
return result.stdout?.toString() || '';
}
function getPlatformName() {
switch (process.platform) {
case 'win32':
return 'win'
case 'darwin':
return 'osx'
case 'linux':
return 'linux'
default:
throw Error('Nuget Inspector can only be installed on Windows, Mac or Linux systems')
}
}
async function downloadFile(url, filename, payload, progressBar) {
const file = fs.createWriteStream(filename, 'utf-8')
let receivedBytes = 0
const {data, headers, status} = await axios.get(url,
{
method: 'GET',
responseType: 'stream',
})
const totalBytes = headers['content-length'] ? +headers['content-length'] : 0
return new Promise((resolve, reject) => {
if (status !== 200) {
return reject('Response status was ' + status)
}
progressBar?.start(totalBytes, 0, payload)
data
.on('data', (chunk) => {
receivedBytes += chunk.length
progressBar?.update(receivedBytes, payload)
})
.pipe(file)
.on('finish', () => {
file.close()
resolve(filename)
})
.on('error', (err) => {
fs.unlinkSync(filename)
progressBar?.stop()
return reject(err)
})
})
}
async function unzip(zipFileName, options) {
return new Promise((resolve, reject) => {
if (options?.overwriteRootDir) {
fs.createReadStream(zipFileName)
.pipe(unzipper.Parse())
.on('entry', function (entry) {
const fullPathName = path.resolve(options.path, entry.path.substring(entry.path.indexOf('/') + 1, entry.path.length))
if (entry.type === 'Directory') {
if (!fs.existsSync(fullPathName))
fs.mkdirSync(fullPathName, {recursive: true})
} else
entry.pipe(fs.createWriteStream(fullPathName))
})
.on('finish', () => {
resolve()
})
.on('error', reject)
} else {
fs.createReadStream(zipFileName)
.pipe(unzipper.Extract(options))
.on('finish', () => {
resolve()
})
.on('error', reject)
}
})
}
module.exports = {runNugetInspector, runNuGetInspectorProgrammatically}