npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@manuth/package-json-editor

v3.2.1

Published

Provides types and tools for editing `package.json` files

Downloads

2,852

Readme

NPMPackageEditor

Provides types and tools for editing package.json files

status-badge

Installing NPMPackageEditor

NPMPackageEditor can be installed using the npm-cli:

npm install --save @manuth/package-json-editor

Using NPMPackageEditor

General

NPMPackageEditor provides useful tools for creating, editing and analyzing package.json-files.

Usage

Type-Checking for package.json Metadata

Using a code-editor with typescript support provides autocompletion for package.json-metadata.

import { writeFileSync } from "fs";
import { IPackageMetadata, Package } from "@manuth/package-json-editor";

let packageMeta: IPackageMetadata;
packageMeta = {
    name: "example",
    version: "1.0.0"
};

writeFileSync(Package.FileName, JSON.stringify(packageMeta));

Creating a Package-Object

You can create a Package object by passing a path to a package.json file or by passing the package.json-metadata as an object or nothing to create an empty package:

import { join } from "node:path";
import { cwd } from "node:process";
import { Package } from "@manuth/package-json-editor";

let packagePath = join(cwd(), Package.FileName);

// Option 1: Passing nothing
let package = new Package();

// Option 2: Passing the filename
let package = new Package(packagePath); // Loads the metadata from the specified file

// Option 3: Passing the metadata
let package = new Package( // Loads the metadata from the specified object
    {
        name: "example",
        version: "0.0.0",
        author: "John Doe",
        maintainers: [
            {
                name: "John Doe",
                email: "[email protected]"
            },
            "Jane Doe <[email protected]>"
        ]
    });

// Option 4: Passing the filename and the metadata
let package = new Package(packagePath, { name: "example" }); // Loads the metadata from the specified object

Normalizing Meta-Data

Using the Package.Normalize method, some properties of the package are set automatically.

  • If bin is a string, it is set to an object with a property named like the package's name and its value set to the original string.
  • If man is a string, it is set to an array containing said string.

If the FileName property of the package is set, following properties will be normalized in addition:

  • If undefined, description is automatically loaded from the README file
  • If the package is located inside a GitHub repository, bugs and homepage are automatically set if they're undefined
  • If the package is located inside a git repository, the repository property is set accordingly, if undefined
import { join } from "node:path";
import { Package } from "@manuth/package-json-editor";

let packagePath = join("path", "to", "package", Package.FileName);

let package = new Package(packagePath);
await package.Normalize();
// or
let package = new Package({});
package.FileName = packagePath;
await package.Normalize();
// or
let package = new Package({});
await package.Normalize();

Editing Meta-Data

The Package-class allows you to easily edit the metadata by providing useful abstractions for bug-info, persons (such as author, contributors etc.) and dependencies.

Editing Persons

That way you can always be sure there's an Author property to edit even if no author is specified in the source package.

import { Package } from "@manuth/package-json-editor";

let package = new Package(
    {
        name: "example"
    });

package.Author.Name = "John Doe";
package.Author.EMail = "[email protected]";
Editing Dependencies

Handling dependencies is the key feature of this package.
Dependencies are represented by a class that allows you to easily add, remove, set and manage dependencies.
The dependencies of the Package class are ordered alphabetically out of the box.

import { Package } from "@manuth/package-json-editor";

let package = new Package(
    {
        name: "example",
        dependencies: {
            eslint: "*",
            tslint: "*"
        }
    });

package.Dependencies.Set("eslint", "^7.0.0");
package.Dependencies.Remove("tslint");
package.Dependencies.Add("@typescript-eslint/eslint-plugin", "*");
package.Dependencies.Add("@typescript-eslint/parser", "*");

The DependencyCollection class allows you to easily create dependency-sets for certain purposes and adding them to a Package object or even another DependencyCollection.

import { writeFileSync } from "fs";
import { Package, DependencyCollection } from "@manuth/package-json-editor";

let package = new Package(
    {
        name: "example",
        devDependencies: {
            typescript: "*",
            "@types/node": "*"
        }
    });

let eslintDependencies = new DependencyCollection(
    {
        devDependencies: {
            eslint: "*",
            "@typescript-eslint/eslint-plugin": "*",
            "@typescript-eslint/parser": "*"
        }
    });

let tslintDependencies = new DependencyCollection(
    {
        devDependencies: {
            tslint: "*"
        }
    });

export function installLinter(eslint: boolean)
{
    package.Register(eslint ? eslintDependencies : tslintDependencies);
    writeFileSync("package.json", JSON.stringify(package.ToJSON()));
}

This is especially useful when creating proper package.json files in Yeoman-generators.