Skip to content

Commit

Permalink
Rebuild config "extends" field
Browse files Browse the repository at this point in the history
  • Loading branch information
Max J. Polster committed Mar 6, 2024
1 parent ab4b3b2 commit ced3e82
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
15 changes: 9 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
import color from "ansi-colors";
import createMatcher from "ignore";
import { readFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import parseArgs from "yargs-parser";
import createMatcher from "ignore";
import color from "ansi-colors";
import { getConfig } from "./config.js";
import { PackageJson, PackageLockJsonV3 } from "./types.js";

const args = parseArgs(process.argv.slice(2), {
Expand All @@ -17,11 +18,13 @@ const context = resolve(args.context ?? ".");

const packageFilename = join(context, "package.json");
const packageInfo: PackageJson = JSON.parse(await readFile(packageFilename, "utf-8"));
const config = packageInfo.nawCheckNpmDependencies;
if (!config) {
const configJson = packageInfo.nawCheckNpmDependencies;
if (!configJson) {
throw new Error("\"nawCheckNpmDependencies\" field is missing package.json");
}

const config = await getConfig(context, configJson);

const packageLockFilename = join(context, "package-lock.json");
const packageLock: PackageLockJsonV3 = JSON.parse(await readFile(packageLockFilename, "utf8"));
if (packageLock.lockfileVersion !== 3) {
Expand Down Expand Up @@ -67,7 +70,7 @@ for (const path in packageLock.packages) {
});
}

if (config.noDuplicates) {
if (config.noDuplicates.length > 0) {
const matcher = createMatcher({ ignoreCase: false });
matcher.add(config.noDuplicates);
for (const [name, group] of groupsByName) {
Expand All @@ -84,7 +87,7 @@ if (config.noDuplicates) {
}
}

if (config.sameVersions) {
if (config.sameVersions.length > 0) {
for (const pattern of config.sameVersions) {
const matcher = createMatcher({ ignoreCase: false });
matcher.add(Array.isArray(pattern) ? pattern : [pattern]);
Expand Down
45 changes: 45 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { readFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";

export interface ConfigJson {
extends?: string;
noDuplicates?: string[];
sameVersions?: (string | string[])[];
}

export interface Config {
noDuplicates: string[];
sameVersions: (string | string[])[];
}

export async function getConfig(context: string, json: ConfigJson | string): Promise<Config> {
const config: Config = {
noDuplicates: [],
sameVersions: [],
};

async function follow(filename: string): Promise<void> {
const json = JSON.parse(await readFile(filename, "utf-8"));
use(dirname(filename), json);
}

async function use(context: string, json: ConfigJson): Promise<void> {
if (json.extends) {
await follow(resolve(context, json.extends));
}
if (json.noDuplicates) {
config.noDuplicates?.push(...json.noDuplicates);
}
if (json.sameVersions) {
config.sameVersions?.push(...json.sameVersions);
}
}

if (typeof json === "string") {
await follow(resolve(context, json));
} else {
await use(context, json);
}

return config;
}
8 changes: 2 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@

export interface Config {
noDuplicates?: string[];
sameVersions?: (string | string[])[];
}
import { ConfigJson } from "./config.js";

export interface PackageJson {
nawCheckNpmDependencies?: Config;
nawCheckNpmDependencies?: ConfigJson | string;
}

export interface PackageLockJsonV3 {
Expand Down

0 comments on commit ced3e82

Please sign in to comment.