Socket
Socket
Sign inDemoInstall

pure-index

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pure-index - npm Package Compare versions

Comparing version 0.0.47 to 0.0.48

dist/bin/collectUsages.d.ts

1

dist/api/collectUsages.d.ts

@@ -7,2 +7,3 @@ import { type Config } from '../getConfig/index.js';

extensions?: Config['extensions'];
parserConfig?: Config['parserConfig'];
};

@@ -9,0 +10,0 @@ declare const collectUsages: (name: string, list: ListItem[]) => Promise<{

@@ -12,2 +12,3 @@ import { collectUsages as _collectUsages } from '../collectUsages.js';

collectUsages: name,
parserConfig: x.parserConfig || BASE_CONFIG.parserConfig,
},

@@ -14,0 +15,0 @@ }));

@@ -18,2 +18,3 @@ import { findUnusedExports as _findUnusedExports } from '../findUnusedExports.js';

extensions?: Config['extensions'];
parserConfig?: Config['parserConfig'];
};

@@ -20,0 +21,0 @@ declare const findUnusedExports: (entry: string, list: ListItem[]) => Promise<ExtractError<"unused_exports"> | {

@@ -33,2 +33,3 @@ import { findUnusedExports as _findUnusedExports } from '../findUnusedExports.js';

dir: x.dir,
parserConfig: x.parserConfig || BASE_CONFIG.parserConfig,
},

@@ -35,0 +36,0 @@ }));

48

dist/bin/index.js
#!/usr/bin/env node
import { collectUsages } from '../collectUsages.js';
import { getConfig } from '../getConfig/index.js';
import { findUnusedExports } from '../findUnusedExports.js';
import { printSet, printError, createSpinner, readJSON } from '../shared/index.js';
import { collectUsages } from './collectUsages.js';
import { findUnusedExports } from './findUnusedExports.js';
const config = await getConfig();
if (config.collectUsages) {
const pkgName = config.collectUsages;
const spinner = createSpinner(`Collecting usages of ${pkgName}`);
// @ts-expect-error wtf
const result = await collectUsages({ config });
if (result.ok) {
spinner.success();
printSet(result.val.usages);
process.exit(0);
}
spinner.error();
printError({
text: `Nothing is used from ${pkgName}. Remove it.`,
});
process.exit(1);
await collectUsages({ config });
}
const { name } = await readJSON('package.json');
const pkg = { name, path: config.entry };
const spinner = createSpinner(`Checking exports from the ${pkg.name} package`);
const result = await findUnusedExports({ pkg, config });
if (result.ok) {
spinner.success();
process.exit(0);
}
spinner.error();
switch (result.err.reason) {
case 'no_exports':
printError({
text: `Nothing is exported from ${pkg.name}. Remove it.`,
});
break;
case 'no_imports':
printError({
text: `Nothing is imported from ${pkg.name}. Remove it.`,
});
break;
case 'unused_exports':
printError({
text: `Unused exports in ${pkg.name} package found`,
set: result.err.exports,
});
}
process.exit(1);
await findUnusedExports({ config });
import type { Config } from './getConfig/index.js';
type Params = {
config: Pick<Config, 'dir' | 'batch' | 'exclude' | 'extensions'> & {
config: Pick<Config, 'dir' | 'batch' | 'exclude' | 'extensions' | 'parserConfig'> & {
collectUsages: string;

@@ -5,0 +5,0 @@ };

@@ -5,3 +5,3 @@ import type { Config } from '../getConfig/index.js';

pkg: Pkg;
config: Pick<Config, 'dir' | 'batch' | 'exclude' | 'extensions'>;
config: Pick<Config, 'dir' | 'batch' | 'exclude' | 'extensions' | 'parserConfig'>;
cmd: Cmd;

@@ -8,0 +8,0 @@ };

@@ -10,3 +10,3 @@ import { processBatch } from './processBatch/index.js';

if (batch.length >= config.batch) {
await processBatch({ cmd, files: batch, pkg, tokens });
await processBatch({ cmd, files: batch, pkg, tokens, config });
batch = [];

@@ -16,5 +16,5 @@ }

if (batch.length > 0) {
await processBatch({ cmd, files: batch, pkg, tokens });
await processBatch({ cmd, files: batch, pkg, tokens, config });
}
};
export { fileTraversal };
import { type Cmd, type Pkg } from '../../shared/index.js';
import type { Config } from '../../getConfig/index.js';
type Params = {

@@ -7,4 +8,5 @@ cmd: Cmd;

tokens: string[];
config: Pick<Config, 'parserConfig'>;
};
declare const processBatch: ({ cmd, files, pkg, tokens }: Params) => Promise<void>;
declare const processBatch: ({ cmd, files, pkg, tokens, config }: Params) => Promise<void>;
export { processBatch };
import { notNil } from '../../shared/index.js';
import { findImport } from './findImport.js';
import { traversal } from './traversal.js';
const processBatch = async ({ cmd, files, pkg, tokens }) => {
const processBatch = async ({ cmd, files, pkg, tokens, config }) => {
const pathesPromise = files.map(async (path) => {

@@ -9,5 +9,7 @@ const found = await findImport({ path, tokens });

});
const filterPromise = (await Promise.all(pathesPromise)).filter(notNil).map((path) => traversal({ cmd, path, pkg }));
const filterPromise = (await Promise.all(pathesPromise))
.filter(notNil)
.map((path) => traversal({ cmd, path, pkg, config }));
await Promise.all(filterPromise);
};
export { processBatch };

@@ -0,1 +1,2 @@

import type { Config } from '../../getConfig/index.js';
import type { Cmd, Pkg } from '../../shared/index.js';

@@ -6,4 +7,5 @@ type Params = {

cmd: Cmd;
config: Pick<Config, 'parserConfig'>;
};
declare const traversal: ({ path, pkg, cmd }: Params) => Promise<void>;
declare const traversal: ({ path, pkg, cmd, config }: Params) => Promise<void>;
export { traversal };
import { parseFile } from '@swc/core';
const traversal = async ({ path, pkg, cmd }) => {
const ast = await parseFile(path, {
syntax: 'typescript',
comments: false,
});
const traversal = async ({ path, pkg, cmd, config }) => {
const ast = await parseFile(path, config.parserConfig);
for (const node of ast.body) {

@@ -8,0 +5,0 @@ if (node.type === 'ImportDeclaration' && node.source.value === pkg.name) {

@@ -1,4 +0,7 @@

import { fileTraversal } from './fileTraversal/index.js';
import { ObservableSet, type Result } from './shared/index.js';
type FindUnusedExports = (_: Omit<Parameters<typeof fileTraversal>[0], 'cmd'>) => Promise<Result<{
import { ObservableSet, type Pkg, type Result } from './shared/index.js';
import type { Config } from './getConfig/index.js';
type FindUnusedExports = (_: {
pkg: Pkg;
config: Pick<Config, 'dir' | 'batch' | 'exclude' | 'extensions' | 'parserConfig'>;
}) => Promise<Result<{
exports: ObservableSet;

@@ -5,0 +8,0 @@ }, {

@@ -5,3 +5,3 @@ import { getExports } from './getExports/index.js';

const findUnusedExports = async ({ pkg, config }) => {
const exports = await getExports({ pkg });
const exports = await getExports({ pkg, config });
const originalExportsSize = exports.size;

@@ -8,0 +8,0 @@ return new Promise(async (resolve) => {

@@ -0,1 +1,2 @@

import type { ParserConfig } from '@swc/core';
declare const BASE_CONFIG: {

@@ -8,5 +9,8 @@ batch: number;

extensions: string[];
parserConfig: ParserConfig;
};
type Config = Omit<typeof BASE_CONFIG, 'dir'> & {
type Config = Omit<typeof BASE_CONFIG, 'dir' | 'collectUsages' | 'parserConfig'> & {
dir: string;
collectUsages: string | null;
parserConfig: ParserConfig;
};

@@ -13,0 +17,0 @@ declare const getConfig: () => Promise<Config>;

import { lilconfig } from 'lilconfig';
import meow from 'meow';
import { cli } from './cli.js';
import { getRepoRoot } from '../shared/index.js';

@@ -11,24 +11,7 @@ const BASE_CONFIG = {

extensions: ['ts', 'tsx'],
parserConfig: {
syntax: 'typescript',
tsx: true,
},
};
const cli = meow(`
Options
--entry, -e path to the package index file. relative to the package directory
--exclude, -i list of directories that will be excluded when searching for imports
--extensions, -x list of file extensions to be considered during the search
--dir, -d path to the directory where imports should be searched for
--batch, -b number of files to be traversed in parallel
--collect-usages, -u outputs a list of all unique uses of the package
`, {
importMeta: import.meta,
allowUnknownFlags: false,
description: false,
flags: {
entry: { type: 'string', shortFlag: 'e' },
exclude: { type: 'string', shortFlag: 'i' },
extensions: { type: 'string', shortFlag: 'x' },
dir: { type: 'string', shortFlag: 'd' },
batch: { type: 'number', shortFlag: 'b' },
collectUsages: { type: 'string', shortFlag: 'u' },
},
});
const getConfig = async () => {

@@ -38,17 +21,15 @@ const result = (await lilconfig('pure-index', {

}).search()) || { config: BASE_CONFIG };
const { exclude = [], entry = BASE_CONFIG.entry, batch = BASE_CONFIG.batch, extensions = BASE_CONFIG.extensions, dir, } = result.config;
// @ts-expect-error 123
return result === null
? BASE_CONFIG
: {
entry: cli.flags.entry || entry,
exclude: cli.flags.exclude
? new Set([...BASE_CONFIG.exclude, ...cli.flags.exclude.split(',')])
: new Set([...BASE_CONFIG.exclude, ...exclude]),
batch: cli.flags.batch || batch,
collectUsages: cli.flags.collectUsages || BASE_CONFIG.collectUsages,
extensions: cli.flags.extensions ? cli.flags.extensions.split(',') : extensions,
dir: cli.flags.dir || dir || getRepoRoot(),
};
const { exclude = [], entry = BASE_CONFIG.entry, batch = BASE_CONFIG.batch, extensions = BASE_CONFIG.extensions, dir, parserConfig = BASE_CONFIG.parserConfig, } = result.config;
return {
entry: cli.flags.entry || entry,
exclude: cli.flags.exclude
? new Set([...BASE_CONFIG.exclude, ...cli.flags.exclude.split(',')])
: new Set([...BASE_CONFIG.exclude, ...exclude]),
batch: cli.flags.batch || batch,
collectUsages: cli.flags.collectUsages || BASE_CONFIG.collectUsages,
extensions: cli.flags.extensions ? cli.flags.extensions.split(',') : extensions,
dir: cli.flags.dir || dir || getRepoRoot(),
parserConfig,
};
};
export { getConfig, BASE_CONFIG };

@@ -0,6 +1,8 @@

import type { Config } from '../getConfig/index.js';
import { ObservableSet, type Pkg } from '../shared/index.js';
type Params = {
pkg: Pkg;
config: Pick<Config, 'parserConfig'>;
};
declare const getExports: ({ pkg }: Params) => Promise<ObservableSet>;
declare const getExports: ({ pkg, config }: Params) => Promise<ObservableSet>;
export { getExports };
import { parseFile } from '@swc/core';
import { ObservableSet } from '../shared/index.js';
const getExports = async ({ pkg }) => {
const getExports = async ({ pkg, config }) => {
const result = new ObservableSet();
const ast = await parseFile(pkg.path, {
syntax: 'typescript',
comments: false,
});
const ast = await parseFile(pkg.path, config.parserConfig);
for (const node of ast.body) {

@@ -10,0 +7,0 @@ if (node.type === 'ExportNamedDeclaration') {

{
"name": "pure-index",
"type": "module",
"version": "0.0.47",
"version": "0.0.48",
"description": "Utility for monorepos. It helps to find unused exports from packages or get a list of all unique uses of any package",

@@ -13,3 +13,3 @@ "main": "./dist/api/index.js",

"scripts": {
"build": "tsc --project ./tsconfig.build.json && tsc-alias",
"build": "rm -rf ./dist && tsc --project ./tsconfig.build.json && tsc-alias",
"prepublishOnly": "npm run build",

@@ -16,0 +16,0 @@ "test": "vitest"

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