Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

knip

Package Overview
Dependencies
Maintainers
1
Versions
423
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knip - npm Package Compare versions

Comparing version 0.8.1 to 0.8.2

dist/progress.d.ts

15

dist/cli.js

@@ -40,3 +40,3 @@ #!/usr/bin/env node

const workingDir = dir ? node_path_1.default.resolve(dir) : cwd;
const isShowProgress = noProgress === false ? process.stdout.isTTY && typeof process.stdout.cursorTo === 'function' : !noProgress;
const isShowProgress = !isDebug && noProgress === false && process.stdout.isTTY && typeof process.stdout.cursorTo === 'function';
const printReport = reporter in reporters_1.default ? reporters_1.default[reporter] : require(node_path_1.default.join(workingDir, reporter));

@@ -64,9 +64,8 @@ const run = async () => {

printReport({ report, issues, cwd, workingDir, isDev, options: reporterOptions });
const reportGroup = report.files ? 'files' : Object.keys(report).find(key => report[key]);
const counterGroup = reportGroup === 'unlisted' ? 'unresolved' : reportGroup;
if (counterGroup) {
const count = counters[counterGroup];
if (count > Number(maxIssues))
process.exit(count);
}
const totalErrorCount = Object.keys(report)
.filter((reportGroup) => report[reportGroup])
.map(reportGroup => reportGroup === 'unlisted' ? 'unresolved' : reportGroup)
.reduce((errorCount, reportGroup) => errorCount + counters[reportGroup], 0);
if (totalErrorCount > Number(maxIssues))
process.exit(totalErrorCount);
}

@@ -73,0 +72,0 @@ catch (error) {

@@ -5,14 +5,3 @@ import type { UnresolvedConfiguration } from './types';

issues: import("./types").Issues;
counters: {
files: number;
dependencies: number;
devDependencies: number;
unresolved: number;
exports: number;
types: number;
nsExports: number;
nsTypes: number;
duplicates: number;
processed: number;
};
counters: import("./types").Counters;
}>;

@@ -16,5 +16,8 @@ "use strict";

const debug_1 = require("./util/debug");
const progress_1 = require("./progress");
const main = async (options) => {
const { cwd, workingDir, configFilePath: configFilePathArg, tsConfigFilePath: tsConfigFilePathArg, include, exclude, ignore, gitignore, isIncludeEntryFiles, isDev, isShowProgress, jsDoc, debug, } = options;
const updateMessage = (0, progress_1.getMessageUpdater)(options);
(0, debug_1.debugLogObject)(options, 1, 'Unresolved onfiguration', options);
updateMessage('Reading configuration and manifest files...');
const manifestPath = await (0, fs_1.findFile)(cwd, workingDir, 'package.json');

@@ -55,2 +58,3 @@ const manifest = manifestPath && require(manifestPath);

: { compilerOptions: { allowJs: true } };
updateMessage('Resolving entry files...');
const entryPaths = await (0, path_1.resolvePaths)({

@@ -70,2 +74,3 @@ cwd,

(0, debug_1.debugLogSourceFiles)(options, 1, 'Included production source files', productionFiles);
updateMessage('Resolving project files...');
const projectPaths = await (0, path_1.resolvePaths)({

@@ -85,2 +90,3 @@ cwd,

else {
updateMessage('Resolving project files...');
const project = (0, project_1.createProject)({ tsConfigFilePath: resolvedTsConfigFilePath });

@@ -87,0 +93,0 @@ const files = project.getSourceFiles();

@@ -7,5 +7,8 @@ "use strict";

const node_path_1 = __importDefault(require("node:path"));
const logIssueLine = ({ issue, workingDir, padding }) => {
const TRUNCATE_WIDTH = 40;
const logIssueLine = (issue, workingDir, maxWidth) => {
const symbols = issue.symbols ? issue.symbols.join(', ') : issue.symbol;
console.log(`${symbols.padEnd(padding + 2)}${issue.symbolType?.padEnd(11) || ''}${node_path_1.default.relative(workingDir, issue.filePath)}`);
const truncatedSymbol = symbols.length > maxWidth ? symbols.slice(0, maxWidth - 3) + '...' : symbols;
const filePath = node_path_1.default.relative(workingDir, issue.filePath);
console.log(`${truncatedSymbol.padEnd(maxWidth + 2)}${issue.symbolType?.padEnd(11) || ''}${filePath}`);
};

@@ -21,8 +24,8 @@ const logIssueGroupResult = (issues, workingDir, title) => {

};
const logIssueGroupResults = (issues, workingDir, title) => {
const logIssueGroupResults = (issues, workingDir, title, isTruncate = false) => {
title && console.log(`--- ${title} (${issues.length})`);
if (issues.length) {
const sortedByFilePath = issues.sort((a, b) => (a.filePath > b.filePath ? 1 : -1));
const padding = [...issues].sort((a, b) => b.symbol.length - a.symbol.length)[0].symbol.length;
sortedByFilePath.forEach(issue => logIssueLine({ issue, workingDir, padding }));
const maxWidth = isTruncate ? TRUNCATE_WIDTH : issues.reduce((max, issue) => Math.max(issue.symbol.length, max), 0);
sortedByFilePath.forEach(issue => logIssueLine(issue, workingDir, maxWidth));
}

@@ -69,4 +72,4 @@ else {

const unreferencedDuplicates = Object.values(issues.duplicates).map(Object.values).flat();
logIssueGroupResults(unreferencedDuplicates, workingDir, reportMultipleGroups && 'DUPLICATE EXPORTS');
logIssueGroupResults(unreferencedDuplicates, workingDir, reportMultipleGroups && 'DUPLICATE EXPORTS', true);
}
};

@@ -1,16 +0,5 @@

import type { Configuration, Issues } from './types';
import type { Configuration, Issues, Counters } from './types';
export declare function findIssues(configuration: Configuration): Promise<{
issues: Issues;
counters: {
files: number;
dependencies: number;
devDependencies: number;
unresolved: number;
exports: number;
types: number;
nsExports: number;
nsTypes: number;
duplicates: number;
processed: number;
};
counters: Counters;
}>;

@@ -13,8 +13,8 @@ "use strict";

const dependencies_1 = require("./util/dependencies");
const log_1 = require("./log");
const debug_1 = require("./util/debug");
const lineRewriter = new log_1.LineRewriter();
const progress_1 = require("./progress");
async function findIssues(configuration) {
const { workingDir, isShowProgress, report, isDev, jsDocOptions } = configuration;
const { workingDir, report, isDev, jsDocOptions } = configuration;
const { entryFiles, productionFiles, projectFiles, isIncludeEntryFiles } = configuration;
const updateMessage = (0, progress_1.getMessageUpdater)(configuration);
const { getUnresolvedDependencies, getUnusedDependencies, getUnusedDevDependencies } = (0, dependencies_1.getDependencyAnalyzer)(configuration);

@@ -49,23 +49,5 @@ const [usedProductionFiles, unreferencedProductionFiles] = (0, project_1.partitionSourceFiles)(projectFiles, productionFiles);

processed: issues.files.size,
total: projectFiles.length,
};
const updateProcessingOutput = (item) => {
if (!isShowProgress)
return;
const counter = counters.processed;
const total = projectFiles.length;
const percentage = Math.floor((counter / total) * 100);
const messages = [(0, log_1.getLine)(`${percentage}%`, `of files processed (${counter} of ${total})`)];
report.files && messages.push((0, log_1.getLine)(unreferencedProductionFiles.length, 'unused files'));
report.unlisted && messages.push((0, log_1.getLine)(counters.unresolved, 'unlisted dependencies'));
report.exports && messages.push((0, log_1.getLine)(counters.exports, 'unused exports'));
report.nsExports && messages.push((0, log_1.getLine)(counters.nsExports, 'unused exports in namespace'));
report.types && messages.push((0, log_1.getLine)(counters.types, 'unused types'));
report.nsTypes && messages.push((0, log_1.getLine)(counters.nsTypes, 'unused types in namespace'));
report.duplicates && messages.push((0, log_1.getLine)(counters.duplicates, 'duplicate exports'));
if (counter < total) {
messages.push('');
messages.push(`Processing: ${node_path_1.default.relative(workingDir, item.filePath)}`);
}
lineRewriter.update(messages);
};
const updateCounters = (0, progress_1.getCountersUpdater)(configuration, counters);
const addSymbolIssue = (issueType, issue) => {

@@ -77,3 +59,3 @@ const { filePath, symbol } = issue;

counters[issueType]++;
updateProcessingOutput(issue);
updateCounters(issue);
};

@@ -85,4 +67,5 @@ const addProjectIssue = (issueType, issue) => {

}
updateProcessingOutput(issue);
updateCounters(issue);
};
updateMessage('Connecting the dots...');
if (report.dependencies ||

@@ -204,6 +187,5 @@ report.unlisted ||

}
if (isShowProgress)
lineRewriter.resetLines();
updateCounters();
return { issues, counters };
}
exports.findIssues = findIssues;

@@ -81,2 +81,3 @@ import { SourceFile } from 'ts-morph';

};
export declare type Counters = Record<IssueType | 'processed' | 'total', number>;
export declare type ReporterOptions = {

@@ -83,0 +84,0 @@ report: Report;

{
"name": "knip",
"version": "0.8.1",
"version": "0.8.2",
"description": "Find unused files, dependencies and exports in your TypeScript and JavaScript project",

@@ -27,3 +27,3 @@ "keywords": [

"scripts": {
"knip": "node ./dist/cli.js",
"knip": "node ./dist/cli.js --include files,dependencies,unlisted",
"test": "node --loader tsx --test test/*.spec.ts",

@@ -30,0 +30,0 @@ "watch": "tsc --watch",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc