@nodesecure/tree-walker
Version:
NodeSecure tree walker
73 lines • 2.12 kB
JavaScript
export class Dependency {
static currentId = 1;
name;
version;
dev = false;
existOnRemoteRegistry = true;
dependencyCount = 0;
gitUrl = null;
warnings = [];
alias = {};
#flags = new Set();
#parent = null;
constructor(name, version, options = {}) {
this.name = name;
this.version = version;
const { parent = null, ...props } = options;
if (parent !== null) {
parent.addChildren();
}
this.#parent = parent;
Object.assign(this, props);
}
addChildren() {
this.dependencyCount += 1;
}
get spec() {
return `${this.name}@${this.version}`;
}
get flags() {
return [...this.#flags];
}
get parent() {
return this.#parent === null ? {} : { [this.#parent.name]: this.#parent.version };
}
addFlag(flagName, predicate = true) {
if (typeof flagName !== "string") {
throw new TypeError("flagName argument must be typeof string");
}
if (predicate) {
if (flagName === "hasDependencies" && this.#parent !== null) {
this.#parent.addFlag("hasIndirectDependencies");
}
this.#flags.add(flagName);
}
}
isGit(url) {
this.#flags.add("isGit");
if (typeof url === "string") {
this.gitUrl = url;
}
return this;
}
exportAsPlainObject(customId) {
if (this.warnings.length > 0) {
this.addFlag("hasWarnings");
}
return {
id: typeof customId === "number" ? customId : Dependency.currentId++,
type: "cjs",
name: this.name,
version: this.version,
usedBy: this.parent,
isDevDependency: this.dev,
existOnRemoteRegistry: this.existOnRemoteRegistry,
flags: this.flags,
warnings: this.warnings,
dependencyCount: this.dependencyCount,
gitUrl: this.gitUrl,
alias: this.alias
};
}
}
//# sourceMappingURL=Dependency.class.js.map