Skip to content

Commit

Permalink
Merge bfbf3fc into d77a1a8
Browse files Browse the repository at this point in the history
  • Loading branch information
ext committed Nov 16, 2020
2 parents d77a1a8 + bfbf3fc commit 1c7a853
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -14,6 +14,8 @@ Opinionated linter for NPM package tarball and `package.json` metadata.
Use `--tarball` and `--pkgfile` to specify custom locations.
Default is to find `package.json` from current directory tree and derive tarball filename from the `name` and `version` field.

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

## Disallowed files

Disallows certain files from being included in the package tarball.
Expand Down
42 changes: 34 additions & 8 deletions src/index.ts
Expand Up @@ -9,6 +9,7 @@ import { setupBlacklist } from "./blacklist";
import { verify } from "./verify";
import PackageJson from "./types/package-json";
import { tarballLocation } from "./tarball-location";
import { getFileContent } from "./tarball";

/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const { version } = require("../package.json");
Expand All @@ -18,13 +19,40 @@ interface ParsedArgs {
tarball?: string;
}

async function defaultPkgLocation(): Promise<string | undefined> {
interface GetPackageJsonResults {
pkg?: PackageJson;
pkgPath?: string;
}

async function getPackageJson(args: ParsedArgs): Promise<GetPackageJsonResults> {
/* get from explicit path passed as argument */
if (args.pkgfile) {
return {
pkg: JSON.parse(await fs.readFile(args.pkgfile, "utf-8")),
pkgPath: args.pkgfile,
};
}

/* extract package.json from explicit tarball location */
if (args.tarball) {
const contents = await getFileContent(args.tarball, ["package.json"]);
return {
pkg: JSON.parse(contents["package.json"].toString("utf-8")),
pkgPath: path.join(args.tarball, "package.json"),
};
}

/* try to locate package.json from file structure */
const pkgPath = await findUp("package.json");
if (pkgPath) {
return path.relative(process.cwd(), pkgPath);
} else {
return undefined;
const relPath = path.relative(process.cwd(), pkgPath);
return {
pkg: JSON.parse(await fs.readFile(relPath, "utf-8")),
pkgPath: relPath,
};
}

return {};
}

async function run(): Promise<void> {
Expand All @@ -37,16 +65,14 @@ async function run(): Promise<void> {
parser.add_argument("-p", "--pkgfile", { help: "specify package.json location" });

const args: ParsedArgs = parser.parse_args();
const pkgPath = args.pkgfile ?? (await defaultPkgLocation());
const { pkg, pkgPath } = await getPackageJson(args);

if (!pkgPath) {
if (!pkg) {
console.error("Failed to locate package.json and no location was specificed with `--pkgfile'");
process.exit(1);
}

const pkg: PackageJson = JSON.parse(await fs.readFile(pkgPath, "utf-8"));
const tarball = args.tarball ?? tarballLocation(pkg, pkgPath);

if (!existsSync(tarball)) {
console.error(`"${tarball}" does not exist, did you forget to run \`npm pack'?`);
process.exit(1);
Expand Down

0 comments on commit 1c7a853

Please sign in to comment.