Skip to content

Commit

Permalink
feat: links
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly committed Dec 2, 2023
1 parent 029f03b commit 3f8c65e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
8 changes: 7 additions & 1 deletion __snapshots__/depWalker.spec.js.snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ exports['walk @slimio/is 1'] = {
]
},
"gitUrl": null,
"integrity": "c9781c55ab750e58bed9ce2560581ff4087b8c3129462543fa6fee4e717ba2a9"
"integrity": "c9781c55ab750e58bed9ce2560581ff4087b8c3129462543fa6fee4e717ba2a9",
"links": {
"npm": "https://www.npmjs.com/package/@slimio/is/v/1.5.1",
"homepage": "https://github.com/SlimIO/is#readme",
"github": "https://github.com/SlimIO/is",
"gitlab": null
}
}
},
"vulnerabilities": [],
Expand Down
3 changes: 2 additions & 1 deletion src/npmRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import semver from "semver";
import { packument, packumentVersion } from "@nodesecure/npm-registry-sdk";

// Import Internal Dependencies
import { parseAuthor } from "./utils/index.js";
import { parseAuthor, getLinks } from "./utils/index.js";

export async function manifestMetadata(name, version, metadata) {
try {
Expand Down Expand Up @@ -85,6 +85,7 @@ export async function packageMetadata(name, version, options) {
}
}

Object.assign(ref.versions[version], { links: getLinks(pkg.versions[version]) });
Object.assign(ref.metadata, metadata);
}
catch {
Expand Down
4 changes: 4 additions & 0 deletions src/tarball.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export async function scanDirOrArchive(name, version, options) {
});
await timers.setImmediate();
}
else {
// Set links to an empty object because theses are generated only for NPM tarballs
Object.assign(ref, { links: {} });
}

// Read the package.json at the root of the directory or archive.
const {
Expand Down
32 changes: 32 additions & 0 deletions src/utils/getLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Import Third-party Dependencies
import { packument } from "@nodesecure/npm-registry-sdk";

function getVCSRepositoryURL(host, link) {
try {
const url = new URL(link);
const { hostname, pathname } = url;

if (hostname !== host) {
return null;
}

const [owner, repo] = pathname.split("/").filter(Boolean).map((curr) => curr.replace(".git", ""));

return `https://${host}/${owner}/${repo}`;
}
catch {
return null;
}
}

export function getLinks(pkg) {
const homepage = pkg.homepage || null;
const repositoryUrl = pkg.repository?.url || null;

return {
npm: `https://www.npmjs.com/package/${pkg.name}/v/${pkg.version}`,
homepage,
github: getVCSRepositoryURL("github.com", homepage ?? repositoryUrl),
gitlab: getVCSRepositoryURL("gitlab.com", homepage ?? repositoryUrl)
};
}
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from "./analyzeDependencies.js";
export * from "./booleanToFlags.js";
export * from "./addMissingVersionFlags.js";
export * from "./parseManifestAuthor.js";
export * from "./getLinks.js";

export const NPM_TOKEN = typeof process.env.NODE_SECURE_TOKEN === "string" ?
{ token: process.env.NODE_SECURE_TOKEN } :
Expand Down
31 changes: 31 additions & 0 deletions test/npmRegistry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,35 @@ test("registry.packageMetadata", async() => {
assert.ok(ref.metadata.hasManyPublishers);
assert.ok(typeof ref.metadata.publishedCount === "number");
assert.ok(is.date(new Date(ref.metadata.lastUpdateAt)));

assert.deepEqual(ref.versions["1.5.0"].links, {
npm: "https://www.npmjs.com/package/@slimio/is/v/1.5.0",
homepage: "https://github.com/SlimIO/is#readme",
github: "https://github.com/SlimIO/is",
gitlab: null
});
});

test("registry.packageMetadata should find GitLab links", async() => {
const ref = {
metadata: {},
versions: {
"71.2.0": {
flags: []
}
}
};
const logger = new Logger().start("registry");

await registry.packageMetadata("@gitlab/ui", "71.2.0", {
ref,
logger
});

assert.deepEqual(ref.versions["71.2.0"].links, {
npm: "https://www.npmjs.com/package/@gitlab/ui/v/71.2.0",
homepage: "https://gitlab.com/gitlab-org/gitlab-ui#readme",
github: null,
gitlab: "https://gitlab.com/gitlab-org/gitlab-ui"
});
});
12 changes: 12 additions & 0 deletions types/scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ declare namespace Scanner {
at: string;
}

export interface DepdencyLinks {
/** NPM Registry page */
npm: string;
/** Homepage URL */
homepage?: string;
/** GitHub repository URL */
github?: string;
/** GitLab repository URL */
gitlab?: string;
}

export interface DependencyVersion {
/** Id of the package (useful for usedBy relation) */
id: number;
Expand Down Expand Up @@ -111,6 +122,7 @@ declare namespace Scanner {
* (Not supported on GIT dependency)
*/
integrity?: string;
links: DepdencyLinks;
}

export interface Dependency {
Expand Down

0 comments on commit 3f8c65e

Please sign in to comment.