Skip to content

Commit

Permalink
feat: a few chainable helpers for managing node package.json files
Browse files Browse the repository at this point in the history
  • Loading branch information
justinm committed Apr 28, 2022
1 parent fc452b0 commit edfe840
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/@pdkit/core/nodejs/constructs/PackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface NodePackageJsonProps {
readonly types?: string;
readonly bin?: Record<string, string>;
readonly scripts?: Record<string, string>;
readonly resolutions?: Record<string, string>;
readonly bugs?: {
readonly url?: string;
readonly email?: string;
Expand Down Expand Up @@ -48,6 +49,7 @@ export class PackageJson extends Manifest {
bugs: props.bugs,
files: props.files,
man: props.man,
resolutions: props.resolutions,
});
}

Expand Down
48 changes: 28 additions & 20 deletions packages/@pdkit/core/nodejs/project/NodeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,20 @@ export class NodeProject extends Project {
new GitIgnore(this, props.gitignore);
}

const addDependencies = (deps: Dependencies, type?: PackageDependencyType) => {
if (Array.isArray(deps)) {
deps.forEach((dep) => {
const d = dep as { name: string; version?: string };

if (d.name) {
new PackageDependency(this, d.name, { version: d.version, type });
} else {
new PackageDependency(this, dep as string, { type });
}
});
} else {
Object.keys(deps).forEach((dep) => new PackageDependency(this, dep, { version: deps[dep], type }));
}
};

if (props?.bundledDependencies) {
addDependencies(props?.bundledDependencies, PackageDependencyType.BUNDLED);
this.addDependencies(props?.bundledDependencies, PackageDependencyType.BUNDLED);
}

if (props?.dependencies) {
addDependencies(props?.dependencies);
this.addDependencies(props?.dependencies);
}

if (props?.devDependencies) {
addDependencies(props?.devDependencies, PackageDependencyType.DEV);
this.addDependencies(props?.devDependencies, PackageDependencyType.DEV);
}

if (props?.peerDependencies) {
addDependencies(props?.peerDependencies, PackageDependencyType.PEER);
this.addDependencies(props?.peerDependencies, PackageDependencyType.PEER);
}

if (!props?.disableAutoLib && Project.of(this).isDefaultProject) {
Expand Down Expand Up @@ -193,4 +177,28 @@ export class NodeProject extends Project {
.node.findAll()
.find((p) => (p as NodeProject).packageName === packageName) as NodeProject | undefined;
}

extraFields(fields: Record<string, unknown>) {
this.packageJson.addDeepFields(fields);

return this;
}

addDependencies(deps: Dependencies, type?: PackageDependencyType) {
if (Array.isArray(deps)) {
deps.forEach((dep) => {
const d = dep as { name: string; version?: string };

if (d.name) {
new PackageDependency(this, d.name, { version: d.version, type });
} else {
new PackageDependency(this, dep as string, { type });
}
});
} else {
Object.keys(deps).forEach((dep) => new PackageDependency(this, dep, { version: deps[dep], type }));
}

return this;
}
}

0 comments on commit edfe840

Please sign in to comment.