Merge pull request #20 from felickz/main

Add Windows support ( closes #19)
This commit is contained in:
Lane Seppala
2023-05-08 10:51:53 -06:00
committed by GitHub
3 changed files with 25 additions and 22 deletions

View File

@@ -18,7 +18,7 @@ import { unmockedModulePathPatterns } from './jest.config'
dotenv.config();
export default class ComponentDetection {
public static componentDetectionPath = './component-detection';
public static componentDetectionPath = process.platform === "win32" ? './component-detection.exe' : './component-detection';
public static outputPath = './output.json';
// This is the default entry point for this class.
@@ -30,18 +30,18 @@ export default class ComponentDetection {
// Get the latest release from the component-detection repo, download the tarball, and extract it
public static async downloadLatestRelease() {
try {
core.debug("Downloading latest release");
core.debug(`Downloading latest release for ${process.platform}`);
const downloadURL = await this.getLatestReleaseURL();
const blob = await (await fetch(new URL(downloadURL))).blob();
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// Write the blob to a file
core.debug("Writing binary to file");
await fs.writeFileSync(this.componentDetectionPath, buffer, {mode: 0o777, flag: 'w'});
core.debug(`Writing binary to file ${this.componentDetectionPath}`);
await fs.writeFileSync(this.componentDetectionPath, buffer, { mode: 0o777, flag: 'w' });
} catch (error: any) {
core.error(error);
}
}
}
// Run the component-detection CLI on the path specified
@@ -64,21 +64,21 @@ export default class ComponentDetection {
return parameters;
}
public static async getManifestsFromResults(): Promise<Manifest[]| undefined> {
public static async getManifestsFromResults(): Promise<Manifest[] | undefined> {
core.info("Getting manifests from results");
// Parse the result file and add the packages to the package cache
const packageCache = new PackageCache();
const packages: Array<ComponentDetectionPackage>= [];
const packages: Array<ComponentDetectionPackage> = [];
const results = await fs.readFileSync(this.outputPath, 'utf8');
var json: any = JSON.parse(results);
json.componentsFound.forEach(async (component: any) => {
const packageUrl = ComponentDetection.makePackageUrl(component.component.packageUrl);
if (!packageCache.hasPackage(packageUrl)) {
const pkg = new ComponentDetectionPackage(packageUrl, component.component.id,
component.isDevelopmentDependency,component.topLevelReferrers,component.locationsFoundAt, component.containerDetailIds, component.containerLayerIds);
const pkg = new ComponentDetectionPackage(packageUrl, component.component.id,
component.isDevelopmentDependency, component.topLevelReferrers, component.locationsFoundAt, component.containerDetailIds, component.containerLayerIds);
packageCache.addPackage(pkg);
packages.push(pkg);
}
@@ -108,7 +108,8 @@ export default class ComponentDetection {
if (pkg.topLevelReferrers.length == 0) {
manifests.find((manifest: Manifest) => manifest.name == location)?.addDirectDependency(pkg, ComponentDetection.getDependencyScope(pkg));
} else {
manifests.find((manifest: Manifest) => manifest.name == location)?.addIndirectDependency(pkg, ComponentDetection.getDependencyScope(pkg)); }
manifests.find((manifest: Manifest) => manifest.name == location)?.addIndirectDependency(pkg, ComponentDetection.getDependencyScope(pkg));
}
});
});
return manifests;
@@ -134,7 +135,7 @@ export default class ComponentDetection {
}
private static async getLatestReleaseURL(): Promise<string> {
const githubToken = core.getInput('token') || process.env.GITHUB_TOKEN || "";
const githubToken = core.getInput('token') || process.env.GITHUB_TOKEN || "";
const octokit = github.getOctokit(githubToken);
const owner = "microsoft";
const repo = "component-detection";
@@ -144,8 +145,9 @@ export default class ComponentDetection {
});
var downloadURL: string = "";
const assetName = process.platform === "win32" ? "component-detection-win-x64.exe" : "component-detection-linux-x64";
latestRelease.data.assets.forEach((asset: any) => {
if (asset.name === "component-detection-linux-x64") {
if (asset.name === assetName) {
downloadURL = asset.browser_download_url;
}
});
@@ -155,8 +157,8 @@ export default class ComponentDetection {
}
class ComponentDetectionPackage extends Package {
constructor(packageUrl: string, public id: string, public isDevelopmentDependency:boolean, public topLevelReferrers: [],
constructor(packageUrl: string, public id: string, public isDevelopmentDependency: boolean, public topLevelReferrers: [],
public locationsFoundAt: [], public containerDetailIds: [], public containerLayerIds: []) {
super(packageUrl);
}

9
dist/index.js generated vendored
View File

@@ -23332,13 +23332,13 @@ class ComponentDetection {
static downloadLatestRelease() {
return __awaiter(this, void 0, void 0, function* () {
try {
core.debug("Downloading latest release");
core.debug(`Downloading latest release for ${process.platform}`);
const downloadURL = yield this.getLatestReleaseURL();
const blob = yield (yield (0, cross_fetch_1.default)(new URL(downloadURL))).blob();
const arrayBuffer = yield blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// Write the blob to a file
core.debug("Writing binary to file");
core.debug(`Writing binary to file ${this.componentDetectionPath}`);
yield fs_1.default.writeFileSync(this.componentDetectionPath, buffer, { mode: 0o777, flag: 'w' });
}
catch (error) {
@@ -23440,8 +23440,9 @@ class ComponentDetection {
owner, repo
});
var downloadURL = "";
const assetName = process.platform === "win32" ? "component-detection-win-x64.exe" : "component-detection-linux-x64";
latestRelease.data.assets.forEach((asset) => {
if (asset.name === "component-detection-linux-x64") {
if (asset.name === assetName) {
downloadURL = asset.browser_download_url;
}
});
@@ -23450,7 +23451,7 @@ class ComponentDetection {
}
}
exports["default"] = ComponentDetection;
ComponentDetection.componentDetectionPath = './component-detection';
ComponentDetection.componentDetectionPath = process.platform === "win32" ? './component-detection.exe' : './component-detection';
ComponentDetection.outputPath = './output.json';
class ComponentDetectionPackage extends dependency_submission_toolkit_1.Package {
constructor(packageUrl, id, isDevelopmentDependency, topLevelReferrers, locationsFoundAt, containerDetailIds, containerLayerIds) {

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long