Skip to content

Commit

Permalink
Merge pull request #5 from ext/feature/stdin
Browse files Browse the repository at this point in the history
feat: support reading tarball from stdin
  • Loading branch information
ext committed Nov 16, 2020
2 parents e8d0294 + 85981c4 commit 14fc3e1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Default is to find `package.json` from current directory tree and derive tarball

If `--tarball` is used `package.json` is extracted from the tarball.

To read from stdin use `--tarball -`.
This can be used to quickly examine packages from https://www.npmjs.com/:

> curl -s \$(npm view lodash dist.tarball) | npx npm-pkg-lint -t -
## Disallowed files

Disallows certain files from being included in the package tarball.
Expand Down
47 changes: 30 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"@html-validate/stylish": "^1.0.0",
"argparse": "^2.0.1",
"find-up": "^5.0.0",
"tar": "^6.0.5"
"tar": "^6.0.5",
"tmp": "^0.2.1"
},
"devDependencies": {
"@commitlint/cli": "11.0.0",
Expand All @@ -75,6 +76,7 @@
"@types/jest": "26.0.15",
"@types/node": "11.15.35",
"@types/tar": "4.0.3",
"@types/tmp": "^0.2.0",
"@typescript-eslint/eslint-plugin": "4.7.0",
"@typescript-eslint/parser": "4.7.0",
"eslint": "7.13.0",
Expand Down
31 changes: 30 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable no-console, no-process-exit -- this is a cli tool */

import { existsSync, promises as fs } from "fs";
import { existsSync, createWriteStream, promises as fs } from "fs";
import path from "path";
import { ArgumentParser } from "argparse";
import findUp from "find-up";
import tmp from "tmp";
import stylish from "@html-validate/stylish";
import { setupBlacklist } from "./blacklist";
import { verify } from "./verify";
Expand All @@ -24,6 +25,26 @@ interface GetPackageJsonResults {
pkgPath?: string;
}

async function preloadStdin(): Promise<string> {
return new Promise((resolve, reject) => {
tmp.file((err, path, fd) => {
if (err) {
reject(err);
return;
}

const st = createWriteStream(null, { fd, autoClose: true });
process.stdin.pipe(st);
st.on("finish", () => {
resolve(path);
});
st.on("error", (err) => {
reject(err);
});
});
});
}

async function getPackageJson(args: ParsedArgs): Promise<GetPackageJsonResults> {
/* get from explicit path passed as argument */
if (args.pkgfile) {
Expand Down Expand Up @@ -65,6 +86,14 @@ async function run(): Promise<void> {
parser.add_argument("-p", "--pkgfile", { help: "specify package.json location" });

const args: ParsedArgs = parser.parse_args();

/* this library assumes the file source can be randomly accessed but with
* stdin this is not possible so stdin is read into a temporary file which is
* used instead */
if (args.tarball === "-") {
args.tarball = await preloadStdin();
}

const { pkg, pkgPath } = await getPackageJson(args);

if (!pkg) {
Expand Down

0 comments on commit 14fc3e1

Please sign in to comment.