Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 13, 2024
1 parent 89a384e commit 5730b49
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
3 changes: 2 additions & 1 deletion packages/knip/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { _glob, negate } from './util/glob.js';
import { getReferencedDependencyHandler } from './util/handle-dependency.js';
import { getHasStrictlyNsReferences, getType } from './util/has-strictly-ns-references.js';
import { getIsIdentifierReferencedHandler } from './util/is-identifier-referenced.js';
import { getEntryPathsFromManifest, getPackageNameFromModuleSpecifier } from './util/modules.js';
import { getPackageNameFromModuleSpecifier } from './util/modules.js';
import { getEntryPathsFromManifest } from './util/package-json.js';
import { dirname, join } from './util/path.js';
import { findMatch } from './util/regex.js';
import { getShouldIgnoreHandler, getShouldIgnoreTagHandler } from './util/tag.js';
Expand Down
32 changes: 0 additions & 32 deletions packages/knip/src/util/modules.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { isBuiltin } from 'node:module';
import { DT_SCOPE } from '../constants.js';
import type { PackageJson } from '../types/package-json.js';
import { _glob } from './glob.js';
import { getStringValues } from './object.js';
import { isAbsolute, toPosix } from './path.js';

export const getPackageNameFromModuleSpecifier = (moduleSpecifier: string) => {
Expand Down Expand Up @@ -36,35 +33,6 @@ export const getPackageFromDefinitelyTyped = (typedDependency: string) => {
return typedDependency;
};

export const getEntryPathsFromManifest = (
manifest: PackageJson,
sharedGlobOptions: { cwd: string; dir: string; gitignore: boolean; ignore: string[] }
) => {
const { main, bin, exports, types, typings } = manifest;

const entryPaths = new Set<string>();

if (typeof main === 'string') entryPaths.add(main);

if (bin) {
if (typeof bin === 'string') entryPaths.add(bin);
if (typeof bin === 'object') for (const id of Object.values(bin)) entryPaths.add(id);
}

if (exports) {
for (const item of getStringValues(exports)) entryPaths.add(item);
}

if (typeof types === 'string') entryPaths.add(types);
if (typeof typings === 'string') entryPaths.add(typings);

// Use glob, as we only want source files that:
// - exist
// - are not (generated) files that are .gitignore'd
// - do not match configured `ignore` patterns
return _glob({ ...sharedGlobOptions, patterns: Array.from(entryPaths) });
};

// Strip `?search` and other proprietary directives from the specifier (e.g. https://webpack.js.org/concepts/loaders/)
const matchDirectives = /^([?!|-]+)?([^!?:]+).*/;
export const sanitizeSpecifier = (specifier: string) => {
Expand Down
31 changes: 31 additions & 0 deletions packages/knip/src/util/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Borrowed from https://github.com/npm/package-json + https://github.com/npm/json-parse-even-better-errors
import { readFile, writeFile } from 'node:fs/promises';
import type { PackageJson } from '../types/package-json.js';
import { _glob } from './glob.js';
import { getStringValues } from './object.js';

const INDENT = Symbol.for('indent');
const NEWLINE = Symbol.for('newline');
Expand Down Expand Up @@ -40,3 +42,32 @@ export const save = async (filePath: string, content: ExtendedPackageJson) => {
const fileContent = `${JSON.stringify(content, null, space)}\n`.replace(/\n/g, EOL);
await writeFile(filePath, fileContent);
};

export const getEntryPathsFromManifest = (
manifest: PackageJson,
sharedGlobOptions: { cwd: string; dir: string; gitignore: boolean; ignore: string[] }
) => {
const { main, bin, exports, types, typings } = manifest;

const entryPaths = new Set<string>();

if (typeof main === 'string') entryPaths.add(main);

if (bin) {
if (typeof bin === 'string') entryPaths.add(bin);
if (typeof bin === 'object') for (const id of Object.values(bin)) entryPaths.add(id);
}

if (exports) {
for (const item of getStringValues(exports)) entryPaths.add(item);
}

if (typeof types === 'string') entryPaths.add(types);
if (typeof typings === 'string') entryPaths.add(typings);

// Use glob, as we only want source files that:
// - exist
// - are not (generated) files that are .gitignore'd
// - do not match configured `ignore` patterns
return _glob({ ...sharedGlobOptions, patterns: Array.from(entryPaths) });
};
4 changes: 2 additions & 2 deletions packages/knip/src/util/package-name.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { PackageJson } from '../types/package-json.js';
import { basename, dirname } from './path.js';

const getName2 = (parent: string, base: string) => (parent.charAt(0) === '@' ? `${parent}/${base}` : base);
const getPkgName = (parent: string, base: string) => (parent.charAt(0) === '@' ? `${parent}/${base}` : base);

const getName = (dir: string) => (dir ? getName2(basename(dirname(dir)), basename(dir)) : undefined);
const getName = (dir: string) => (dir ? getPkgName(basename(dirname(dir)), basename(dir)) : undefined);

export function getPackageName(pkg: PackageJson, pathname: string) {
const { name } = pkg;
Expand Down

0 comments on commit 5730b49

Please sign in to comment.