npm-packagr

Utility for build and publish npm package.

Usage no npm install needed!

<script type="module">
  import npmPackagr from 'https://cdn.skypack.dev/npm-packagr';
</script>

README

npm packagr

build: passing d.ts: ✔ license: MIT

The utility to build and publish a npm package.

Why?

If your package sources transpile before publishing. This utility helps you to build your package step by step and publish it.

Example

For example, the package has structure:

.
├── src
│   └── index.ts
├── LICENSE
├── npm-packagr.ts
├── package.json
├── README.md
└── tsconfig.json

File npm-packagr.ts contains:

import { npmPackagr } from "npm-packagr";
import { assets, doIf, packageJSON } from "npm-packagr/pipelines";

npmPackagr({
    packageDirectory: "some/path", // "package" by default
    pipelines: [
        // some manipulations with package.json
        packageJSON((packageJson) => {
            delete packageJson.scripts;
            delete packageJson.devDependencies;

            packageJson.types = "types/";
        }),

        doIf("build", [
            ({ exec }) => {
                exec("tsc");
            },

            // needs to copy files to package
            assets("LICENSE", "README.md"),
        ]),

        doIf("dev", [
            ({ exec }) => {
                exec("tsc --watch");
            },
        ]),
    ],
});

Now, execute in the shell:

npx packagr --target build

It creates package directory that will seem like this:

.
├── package
│   ├── LICENSE
│   ├── index.d.ts
│   ├── index.js
│   ├── package.json
│   └── README.md
├── src
│   └── index.ts
├── LICENSE
├── npm-packagr.ts
├── package.json
├── README.md
└── tsconfig.json

Also you can see publisher.ts file of npm-packagr. It has been published by npm-packagr!)

Pipelines

assets

Copy files to root of package directory.

assets("a", "b/c"); // same as cp -R a b/c package

Also can copy file to some target path in package directory.

assets({ from: "a", to: "b" }); // same as cp -R x package/b

badge

Create badges and add links to README.md. Using a package-badges

doIf

Run pipelines by the target option.

doIf("target-name", [
    () => {
        console.log("show if run:");
        console.log("npx packagr --target target-name");
    },
]);

file

Write a file.

file(() => ({
    name: "cli.js",
    content: `
        #!/usr/bin/env node

        require("..");
    `,
}));

git

Git actions and conditions.

git("branch", "brunchName"); // stop if the current brunch is not a "brunchName"
git("check-status"); // stop if "git status" returns some changes
git("commit", "message"); // git commit -m "message"
git("push"); // git push

npx

Run npx.

npx("tsc")
// equals
({ exec }) => exec("npx tsc")

packageJSON

Manipulations with a package.json.

publish

Publish a package.

publish(); // npm publish

// or you can provide the npm account
publish({ account: "name", email: "e@mail" });

test

Run a test.

version

Bump a package version.

Custom pipeline

Pipeline is just a function. The fisrt argument is the context that is an object contains some functions from shelljs and

  • exec
  • packageDirectory path to package directory.
  • sh
  • target target option.
import { Pipeline } from "./package/pipelines";

const myPipeline: Pipeline = ({
    cp, // shelljs
    exec,
    exit, // shelljs
    ls, // shelljs
    mkdir, // shelljs
    mv, // shelljs
    packageDirectory,
    rm, // shelljs
    sed, // shelljs
    sh,
    target,
    test, // shelljs
}) => {
    /* ... */
};

exec

Executes a command with output to console (or silent) and returns true if success.

const success: boolean = exec("tsc");
// or
const success: boolean = exec("tsc", { silent: true });

sh

Executes a command with no output to console and returns string of the output.

const version: string = sh("node --version");