npm install pkg-usage
# or
yarn add pkg-usage
Have you ever wondered how your library is being used in other repositories? It's your lucky day because this package aims to solve it.
Given a few options you can gets details about how your library is being imported along with its version
import { getPackagesUsages } from 'pkg-usage';
const usages: PackageUsage[] = getPackagesUsages({
packages: ['react'],
fileGlobs: `**/**.ts`,
packageJsonCWD: './package.json',
analyzeImportUsages: false,
});
console.log(usages);
Package Usage types
export type JSXElementUsage = {
line: number;
props: string[];
text: string;
};
export type CallExpressionUsage = {
line: number;
text: string;
};
export type ValueUsage = {
line: number;
text: string;
};
export type PropertyAccessExpressionUsage = {
line: number;
property: string;
text: string;
};
export type Usages =
| JSXElementUsage[]
| (CallExpressionUsage | PropertyAccessExpressionUsage | ValueUsage)[];
export type Import = {
name: string;
type: ExportType;
usages?: Usages;
};
export type FileUsage = {
name: string;
filePath: string;
imports: Import[];
};
export type PackageUsage = {
name: string;
count: number;
files?: FileUsage[];
version?: string;
};
Options | Description | Example |
---|---|---|
-p, --packages | Packages to analyze | -p vue,vuex or --packages="react,redux" |
-f, --file-globs | Files to analyze based on file globs | -f ".(ts|tsx)" or --file-globs=".(js|jsx)" |
-u, --analyze-import-usages | (Experimental) Analyzes import usages | -u |
-cwd, --package-json-CWD | Directory to start from | -cwd "/path/to/start/from" |
npx
is a package runner tool
npx pkg-usage -p "react,redux" -f "**/*.(ts|tsx)"
Or
npx pkg-usage --packages="vue,vuex" --file-globs="**/*.(js|vue)"
If you use npm 5.1 or earlier, you can't use npx. Instead, install it globally:
npm install -g pkg-usage
Now you can run:
pkg-usage -p "react,redux" --f "**/*.(ts|tsx)"