@xylabs/ts-scripts-yarn3 6.4.0 → 6.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/deplint.mjs +15 -10
- package/dist/actions/deplint.mjs.map +1 -1
- package/dist/actions/index.mjs +15 -10
- package/dist/actions/index.mjs.map +1 -1
- package/dist/bin/xy.mjs +15 -10
- package/dist/bin/xy.mjs.map +1 -1
- package/dist/index.mjs +15 -10
- package/dist/index.mjs.map +1 -1
- package/dist/xy/index.mjs +15 -10
- package/dist/xy/index.mjs.map +1 -1
- package/dist/xy/xy.mjs +15 -10
- package/dist/xy/xy.mjs.map +1 -1
- package/dist/xy/xyLintCommands.mjs +15 -10
- package/dist/xy/xyLintCommands.mjs.map +1 -1
- package/package.json +2 -2
package/dist/actions/deplint.mjs
CHANGED
|
@@ -65,7 +65,8 @@ function getImportsFromFile(filePath, importPaths) {
|
|
|
65
65
|
ts.forEachChild(node, visit);
|
|
66
66
|
}
|
|
67
67
|
visit(sourceFile);
|
|
68
|
-
const
|
|
68
|
+
const importsStartsWithExcludes = [".", "#", "node:", "@types/"];
|
|
69
|
+
const cleanedImports = imports.filter((imp) => !importsStartsWithExcludes.some((exc) => imp.startsWith(exc))).map(getBasePackageName);
|
|
69
70
|
for (const imp of cleanedImports) {
|
|
70
71
|
importPaths[imp] = importPaths[imp] || [];
|
|
71
72
|
importPaths[imp].push(filePath);
|
|
@@ -107,7 +108,8 @@ function getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }) {
|
|
|
107
108
|
function check({
|
|
108
109
|
name,
|
|
109
110
|
location,
|
|
110
|
-
devDeps = false
|
|
111
|
+
devDeps = false,
|
|
112
|
+
peerDeps = false
|
|
111
113
|
}) {
|
|
112
114
|
const { prodSourceFiles, devSourceFiles } = findFiles(location);
|
|
113
115
|
const {
|
|
@@ -123,6 +125,7 @@ function check({
|
|
|
123
125
|
} = getDependenciesFromPackageJson(`${location}/package.json`);
|
|
124
126
|
let unlistedDependencies = 0;
|
|
125
127
|
let unlistedDevDependencies = 0;
|
|
128
|
+
let unusedDependencies = 0;
|
|
126
129
|
for (const imp of externalProdImports) {
|
|
127
130
|
if (!dependencies.includes(imp) && !peerDependencies.includes(imp)) {
|
|
128
131
|
unlistedDependencies++;
|
|
@@ -133,20 +136,22 @@ function check({
|
|
|
133
136
|
}
|
|
134
137
|
for (const dep of dependencies) {
|
|
135
138
|
if (!externalProdImports.includes(dep)) {
|
|
136
|
-
|
|
139
|
+
unusedDependencies++;
|
|
137
140
|
console.log(`[${chalk.blue(name)}] Unused dependency in package.json: ${chalk.red(dep)}`);
|
|
138
141
|
console.log(` ${location}/package.json
|
|
139
142
|
`);
|
|
140
143
|
console.log("");
|
|
141
144
|
}
|
|
142
145
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
if (peerDeps) {
|
|
147
|
+
for (const dep of peerDependencies) {
|
|
148
|
+
if (!externalProdImports.includes(dep)) {
|
|
149
|
+
unusedDependencies++;
|
|
150
|
+
console.log(`[${chalk.blue(name)}] Unused peerDependency in package.json: ${chalk.red(dep)}`);
|
|
151
|
+
console.log(` ${location}/package.json
|
|
148
152
|
`);
|
|
149
|
-
|
|
153
|
+
console.log("");
|
|
154
|
+
}
|
|
150
155
|
}
|
|
151
156
|
}
|
|
152
157
|
if (devDeps) {
|
|
@@ -158,7 +163,7 @@ function check({
|
|
|
158
163
|
}
|
|
159
164
|
}
|
|
160
165
|
}
|
|
161
|
-
const totalErrors = unlistedDependencies + unlistedDevDependencies;
|
|
166
|
+
const totalErrors = unlistedDependencies + unlistedDevDependencies + unusedDependencies;
|
|
162
167
|
return totalErrors;
|
|
163
168
|
}
|
|
164
169
|
var deplint = ({ pkg }) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/actions/deplint.ts","../../src/lib/yarn/workspace/yarnWorkspaces.ts","../../src/lib/yarn/workspace/yarnWorkspace.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\n\nimport chalk from 'chalk'\nimport { globSync } from 'glob'\nimport ts from 'typescript'\n\nimport { yarnWorkspace, yarnWorkspaces } from '../lib/index.ts'\n\nfunction getDependenciesFromPackageJson(packageJsonPath: string) {\n const packageJsonFullPath = path.resolve(packageJsonPath)\n const rawContent = fs.readFileSync(packageJsonFullPath, 'utf8')\n const packageJson = JSON.parse(rawContent)\n\n const dependencies = packageJson.dependencies\n ? Object.keys(packageJson.dependencies)\n : []\n\n const devDependencies = packageJson.devDependencies\n ? Object.keys(packageJson.devDependencies)\n : []\n\n const peerDependencies = packageJson.peerDependencies\n ? Object.keys(packageJson.peerDependencies)\n : []\n\n return {\n dependencies, devDependencies, peerDependencies,\n }\n}\n\nfunction getBasePackageName(importName: string) {\n if (importName.startsWith('@')) {\n const parts = importName.split('/')\n return parts.length >= 2 ? `${parts[0]}/${parts[1]}` : importName\n }\n return importName.split('/')[0]\n}\n\nfunction getImportsFromFile(filePath: string, importPaths: Record<string, string[]>) {\n const sourceCode = fs.readFileSync(filePath, 'utf8')\n\n const sourceFile = ts.createSourceFile(\n path.basename(filePath),\n sourceCode,\n ts.ScriptTarget.Latest,\n true,\n )\n\n const imports: string[] = []\n\n function visit(node: ts.Node) {\n if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {\n const moduleSpecifier = (node.moduleSpecifier)?.getFullText()\n if (moduleSpecifier) {\n const trimmed = moduleSpecifier.split(\"'\").at(1) ?? moduleSpecifier\n imports.push(trimmed)\n }\n }\n ts.forEachChild(node, visit)\n }\n\n visit(sourceFile)\n\n const cleanedImports = imports.filter(imp => !imp.startsWith('.') && !imp.startsWith('#') && !imp.startsWith('node:')).map(getBasePackageName)\n\n for (const imp of cleanedImports) {\n importPaths[imp] = importPaths[imp] || []\n importPaths[imp].push(filePath)\n }\n\n return cleanedImports\n}\n\nfunction findFilesByGlob(cwd: string, pattern: string) {\n return globSync(pattern, { cwd, absolute: true })\n}\n\nfunction findFiles(path: string) {\n const allSourceInclude = ['./src/**/*.{ts,tsx}']\n const prodExcludeEndswith = ['.spec.ts', '.stories.tsx']\n const prodExcludeIncludes = ['/spec/', '/stories/', '/scripts/']\n const allSourceFiles = allSourceInclude.flatMap(pattern => findFilesByGlob(path, pattern))\n\n const prodSourceFiles = allSourceFiles.filter(file => !prodExcludeEndswith.some(ext => file.endsWith(ext))\n && !prodExcludeIncludes.some(excl => file.includes(excl)))\n\n const devSourceFiles = allSourceFiles.filter(file => !prodSourceFiles.includes(file))\n return {\n allSourceFiles, prodSourceFiles, devSourceFiles,\n }\n}\n\nfunction getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }: { devSourceFiles: string[]; prodSourceFiles: string[] }) {\n const prodImportPaths: Record<string, string[]> = {}\n const prodImports = prodSourceFiles.flatMap(path => getImportsFromFile(path, prodImportPaths))\n const devImportPaths: Record<string, string[]> = {}\n const devImports = devSourceFiles.flatMap(path => getImportsFromFile(path, devImportPaths))\n const externalProdImports = prodImports.filter(imp => !imp.startsWith('.') && !imp.startsWith('#') && !imp.startsWith('node:'))\n const externalDevImports = devImports.filter(imp => !imp.startsWith('.') && !imp.startsWith('#') && !imp.startsWith('node:'))\n return {\n prodImports, devImports, prodImportPaths, devImportPaths, externalProdImports, externalDevImports,\n }\n}\n\nfunction check({\n name, location, devDeps = false,\n}: { devDeps?: boolean; location: string; name: string }) {\n const { prodSourceFiles, devSourceFiles } = findFiles(location)\n const {\n prodImportPaths, devImportPaths, externalProdImports, externalDevImports,\n } = getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles })\n\n const {\n dependencies, devDependencies, peerDependencies,\n } = getDependenciesFromPackageJson(`${location}/package.json`)\n\n let unlistedDependencies = 0\n let unlistedDevDependencies = 0\n\n for (const imp of externalProdImports) {\n if (!dependencies.includes(imp) && !peerDependencies.includes(imp)) {\n unlistedDependencies++\n console.log(`[${chalk.blue(name)}] Missing dependency in package.json: ${chalk.red(imp)}`)\n console.log(` ${prodImportPaths[imp].join('\\n')}`)\n console.log('')\n }\n }\n\n for (const dep of dependencies) {\n if (!externalProdImports.includes(dep)) {\n unlistedDependencies++\n console.log(`[${chalk.blue(name)}] Unused dependency in package.json: ${chalk.red(dep)}`)\n console.log(` ${location}/package.json\\n`)\n console.log('')\n }\n }\n\n for (const dep of peerDependencies) {\n if (!externalProdImports.includes(dep)) {\n unlistedDependencies++\n console.log(`[${chalk.blue(name)}] Unused peerDependency in package.json: ${chalk.red(dep)}`)\n console.log(` ${location}/package.json\\n`)\n console.log('')\n }\n }\n\n if (devDeps) {\n for (const imp of externalDevImports) {\n if (!devDependencies.includes(imp)) {\n unlistedDevDependencies++\n console.log(`[${chalk.blue(name)}] Missing devDependency in package.json: ${chalk.red(imp)}`)\n console.log(` Found in: ${devImportPaths[imp].join(', ')}`)\n }\n }\n }\n\n const totalErrors = unlistedDependencies + unlistedDevDependencies\n\n return totalErrors\n}\n\nexport const deplint = ({ pkg }: { pkg: string }) => {\n if (pkg) {\n const { location, name } = yarnWorkspace(pkg)\n\n console.log(`Running Deplint for ${name}`)\n check({\n name, location, devDeps: true,\n })\n } else {\n const workspaces = yarnWorkspaces()\n\n console.log('Deplint Started...')\n\n let totalErrors = 0\n\n for (const workspace of workspaces) {\n totalErrors += check(workspace)\n }\n\n if (totalErrors > 0) {\n console.log(`Found ${chalk.red(totalErrors)} unlisted imports.`)\n } else {\n console.log(`No unlisted imports found. ${chalk.green('✔')}`)\n }\n }\n return 0\n}\n","import { spawnSync } from 'node:child_process'\n\nimport type { Workspace } from './Workspace.ts'\n\nexport const yarnWorkspaces = (): Workspace[] => {\n const result = spawnSync('yarn', ['workspaces', 'list', '--json', '--recursive'], { encoding: 'utf8', shell: true })\n if (result.error) {\n throw result.error\n }\n return (\n result.stdout\n .toString()\n // NOTE: This probably doesn't work on Windows\n // TODO: Replace /r/n with /n first\n .split('\\n')\n .slice(0, -1)\n .map((item) => {\n return JSON.parse(item)\n })\n )\n}\n","import type { Workspace } from './Workspace.ts'\nimport { yarnWorkspaces } from './yarnWorkspaces.ts'\n\nexport const yarnWorkspace = (pkg: string): Workspace => {\n const workspace = yarnWorkspaces().find(({ name }) => name === pkg)\n if (!workspace) throw new Error(`Workspace ${pkg} not found`)\n return workspace\n}\n"],"mappings":";AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AAEjB,OAAO,WAAW;AAClB,SAAS,gBAAgB;AACzB,OAAO,QAAQ;;;ACLf,SAAS,iBAAiB;AAInB,IAAM,iBAAiB,MAAmB;AAC/C,QAAM,SAAS,UAAU,QAAQ,CAAC,cAAc,QAAQ,UAAU,aAAa,GAAG,EAAE,UAAU,QAAQ,OAAO,KAAK,CAAC;AACnH,MAAI,OAAO,OAAO;AAChB,UAAM,OAAO;AAAA,EACf;AACA,SACE,OAAO,OACJ,SAAS,EAGT,MAAM,IAAI,EACV,MAAM,GAAG,EAAE,EACX,IAAI,CAAC,SAAS;AACb,WAAO,KAAK,MAAM,IAAI;AAAA,EACxB,CAAC;AAEP;;;ACjBO,IAAM,gBAAgB,CAAC,QAA2B;AACvD,QAAM,YAAY,eAAe,EAAE,KAAK,CAAC,EAAE,KAAK,MAAM,SAAS,GAAG;AAClE,MAAI,CAAC,UAAW,OAAM,IAAI,MAAM,aAAa,GAAG,YAAY;AAC5D,SAAO;AACT;;;AFEA,SAAS,+BAA+B,iBAAyB;AAC/D,QAAM,sBAAsB,KAAK,QAAQ,eAAe;AACxD,QAAM,aAAa,GAAG,aAAa,qBAAqB,MAAM;AAC9D,QAAM,cAAc,KAAK,MAAM,UAAU;AAEzC,QAAM,eAAe,YAAY,eAC7B,OAAO,KAAK,YAAY,YAAY,IACpC,CAAC;AAEL,QAAM,kBAAkB,YAAY,kBAChC,OAAO,KAAK,YAAY,eAAe,IACvC,CAAC;AAEL,QAAM,mBAAmB,YAAY,mBACjC,OAAO,KAAK,YAAY,gBAAgB,IACxC,CAAC;AAEL,SAAO;AAAA,IACL;AAAA,IAAc;AAAA,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AAM,IAAI,GAAG,CAAC,EAAE;AAC5F,gBAAQ,IAAI,eAAe,eAAe,GAAG,EAAE,KAAK,IAAI,CAAC,EAAE;AAAA,MAC7D;AAAA,IACF;AAAA,EACF;AAEA,QAAM,cAAc,uBAAuB;AAE3C,SAAO;AACT;AAEO,IAAM,UAAU,CAAC,EAAE,IAAI,MAAuB;AACnD,MAAI,KAAK;AACP,UAAM,EAAE,UAAU,KAAK,IAAI,cAAc,GAAG;AAE5C,YAAQ,IAAI,uBAAuB,IAAI,EAAE;AACzC,UAAM;AAAA,MACJ;AAAA,MAAM;AAAA,MAAU,SAAS;AAAA,IAC3B,CAAC;AAAA,EACH,OAAO;AACL,UAAM,aAAa,eAAe;AAElC,YAAQ,IAAI,oBAAoB;AAEhC,QAAI,cAAc;AAElB,eAAW,aAAa,YAAY;AAClC,qBAAe,MAAM,SAAS;AAAA,IAChC;AAEA,QAAI,cAAc,GAAG;AACnB,cAAQ,IAAI,SAAS,MAAM,IAAI,WAAW,CAAC,oBAAoB;AAAA,IACjE,OAAO;AACL,cAAQ,IAAI,8BAA8B,MAAM,MAAM,QAAG,CAAC,EAAE;AAAA,IAC9D;AAAA,EACF;AACA,SAAO;AACT;","names":["path"]}
|
|
1
|
+
{"version":3,"sources":["../../src/actions/deplint.ts","../../src/lib/yarn/workspace/yarnWorkspaces.ts","../../src/lib/yarn/workspace/yarnWorkspace.ts"],"sourcesContent":["/* eslint-disable max-statements */\nimport fs from 'node:fs'\nimport path from 'node:path'\n\nimport chalk from 'chalk'\nimport { globSync } from 'glob'\nimport ts from 'typescript'\n\nimport { yarnWorkspace, yarnWorkspaces } from '../lib/index.ts'\n\nfunction getDependenciesFromPackageJson(packageJsonPath: string) {\n const packageJsonFullPath = path.resolve(packageJsonPath)\n const rawContent = fs.readFileSync(packageJsonFullPath, 'utf8')\n const packageJson = JSON.parse(rawContent)\n\n const dependencies = packageJson.dependencies\n ? Object.keys(packageJson.dependencies)\n : []\n\n const devDependencies = packageJson.devDependencies\n ? Object.keys(packageJson.devDependencies)\n : []\n\n const peerDependencies = packageJson.peerDependencies\n ? Object.keys(packageJson.peerDependencies)\n : []\n\n return {\n dependencies, devDependencies, peerDependencies,\n }\n}\n\nfunction getBasePackageName(importName: string) {\n if (importName.startsWith('@')) {\n const parts = importName.split('/')\n return parts.length >= 2 ? `${parts[0]}/${parts[1]}` : importName\n }\n return importName.split('/')[0]\n}\n\nfunction getImportsFromFile(filePath: string, importPaths: Record<string, string[]>) {\n const sourceCode = fs.readFileSync(filePath, 'utf8')\n\n const sourceFile = ts.createSourceFile(\n path.basename(filePath),\n sourceCode,\n ts.ScriptTarget.Latest,\n true,\n )\n\n const imports: string[] = []\n\n function visit(node: ts.Node) {\n if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {\n const moduleSpecifier = (node.moduleSpecifier)?.getFullText()\n if (moduleSpecifier) {\n const trimmed = moduleSpecifier.split(\"'\").at(1) ?? moduleSpecifier\n imports.push(trimmed)\n }\n }\n ts.forEachChild(node, visit)\n }\n\n visit(sourceFile)\n\n const importsStartsWithExcludes = ['.', '#', 'node:', '@types/']\n\n const cleanedImports = imports.filter(imp => !importsStartsWithExcludes.some(exc => imp.startsWith(exc))).map(getBasePackageName)\n\n for (const imp of cleanedImports) {\n importPaths[imp] = importPaths[imp] || []\n importPaths[imp].push(filePath)\n }\n\n return cleanedImports\n}\n\nfunction findFilesByGlob(cwd: string, pattern: string) {\n return globSync(pattern, { cwd, absolute: true })\n}\n\nfunction findFiles(path: string) {\n const allSourceInclude = ['./src/**/*.{ts,tsx}']\n const prodExcludeEndswith = ['.spec.ts', '.stories.tsx']\n const prodExcludeIncludes = ['/spec/', '/stories/', '/scripts/']\n const allSourceFiles = allSourceInclude.flatMap(pattern => findFilesByGlob(path, pattern))\n\n const prodSourceFiles = allSourceFiles.filter(file => !prodExcludeEndswith.some(ext => file.endsWith(ext))\n && !prodExcludeIncludes.some(excl => file.includes(excl)))\n\n const devSourceFiles = allSourceFiles.filter(file => !prodSourceFiles.includes(file))\n return {\n allSourceFiles, prodSourceFiles, devSourceFiles,\n }\n}\n\nfunction getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }: { devSourceFiles: string[]; prodSourceFiles: string[] }) {\n const prodImportPaths: Record<string, string[]> = {}\n const prodImports = prodSourceFiles.flatMap(path => getImportsFromFile(path, prodImportPaths))\n const devImportPaths: Record<string, string[]> = {}\n const devImports = devSourceFiles.flatMap(path => getImportsFromFile(path, devImportPaths))\n const externalProdImports = prodImports.filter(imp => !imp.startsWith('.') && !imp.startsWith('#') && !imp.startsWith('node:'))\n const externalDevImports = devImports.filter(imp => !imp.startsWith('.') && !imp.startsWith('#') && !imp.startsWith('node:'))\n return {\n prodImports, devImports, prodImportPaths, devImportPaths, externalProdImports, externalDevImports,\n }\n}\n\nfunction check({\n name, location, devDeps = false, peerDeps = false,\n}: { devDeps?: boolean; location: string; name: string; peerDeps?: boolean }) {\n const { prodSourceFiles, devSourceFiles } = findFiles(location)\n const {\n prodImportPaths, devImportPaths, externalProdImports, externalDevImports,\n } = getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles })\n\n const {\n dependencies, devDependencies, peerDependencies,\n } = getDependenciesFromPackageJson(`${location}/package.json`)\n\n let unlistedDependencies = 0\n let unlistedDevDependencies = 0\n let unusedDependencies = 0\n\n for (const imp of externalProdImports) {\n if (!dependencies.includes(imp) && !peerDependencies.includes(imp)) {\n unlistedDependencies++\n console.log(`[${chalk.blue(name)}] Missing dependency in package.json: ${chalk.red(imp)}`)\n console.log(` ${prodImportPaths[imp].join('\\n')}`)\n console.log('')\n }\n }\n\n for (const dep of dependencies) {\n if (!externalProdImports.includes(dep)) {\n unusedDependencies++\n console.log(`[${chalk.blue(name)}] Unused dependency in package.json: ${chalk.red(dep)}`)\n console.log(` ${location}/package.json\\n`)\n console.log('')\n }\n }\n\n if (peerDeps) {\n for (const dep of peerDependencies) {\n if (!externalProdImports.includes(dep)) {\n unusedDependencies++\n console.log(`[${chalk.blue(name)}] Unused peerDependency in package.json: ${chalk.red(dep)}`)\n console.log(` ${location}/package.json\\n`)\n console.log('')\n }\n }\n }\n\n if (devDeps) {\n for (const imp of externalDevImports) {\n if (!devDependencies.includes(imp)) {\n unlistedDevDependencies++\n console.log(`[${chalk.blue(name)}] Missing devDependency in package.json: ${chalk.red(imp)}`)\n console.log(` Found in: ${devImportPaths[imp].join(', ')}`)\n }\n }\n }\n\n const totalErrors = unlistedDependencies + unlistedDevDependencies + unusedDependencies\n\n return totalErrors\n}\n\nexport const deplint = ({ pkg }: { pkg: string }) => {\n if (pkg) {\n const { location, name } = yarnWorkspace(pkg)\n\n console.log(`Running Deplint for ${name}`)\n check({\n name, location, devDeps: true,\n })\n } else {\n const workspaces = yarnWorkspaces()\n\n console.log('Deplint Started...')\n\n let totalErrors = 0\n\n for (const workspace of workspaces) {\n totalErrors += check(workspace)\n }\n\n if (totalErrors > 0) {\n console.log(`Found ${chalk.red(totalErrors)} unlisted imports.`)\n } else {\n console.log(`No unlisted imports found. ${chalk.green('✔')}`)\n }\n }\n return 0\n}\n","import { spawnSync } from 'node:child_process'\n\nimport type { Workspace } from './Workspace.ts'\n\nexport const yarnWorkspaces = (): Workspace[] => {\n const result = spawnSync('yarn', ['workspaces', 'list', '--json', '--recursive'], { encoding: 'utf8', shell: true })\n if (result.error) {\n throw result.error\n }\n return (\n result.stdout\n .toString()\n // NOTE: This probably doesn't work on Windows\n // TODO: Replace /r/n with /n first\n .split('\\n')\n .slice(0, -1)\n .map((item) => {\n return JSON.parse(item)\n })\n )\n}\n","import type { Workspace } from './Workspace.ts'\nimport { yarnWorkspaces } from './yarnWorkspaces.ts'\n\nexport const yarnWorkspace = (pkg: string): Workspace => {\n const workspace = yarnWorkspaces().find(({ name }) => name === pkg)\n if (!workspace) throw new Error(`Workspace ${pkg} not found`)\n return workspace\n}\n"],"mappings":";AACA,OAAO,QAAQ;AACf,OAAO,UAAU;AAEjB,OAAO,WAAW;AAClB,SAAS,gBAAgB;AACzB,O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AACT;AAEO,IAAM,UAAU,CAAC,EAAE,IAAI,MAAuB;AACnD,MAAI,KAAK;AACP,UAAM,EAAE,UAAU,KAAK,IAAI,cAAc,GAAG;AAE5C,YAAQ,IAAI,uBAAuB,IAAI,EAAE;AACzC,UAAM;AAAA,MACJ;AAAA,MAAM;AAAA,MAAU,SAAS;AAAA,IAC3B,CAAC;AAAA,EACH,OAAO;AACL,UAAM,aAAa,eAAe;AAElC,YAAQ,IAAI,oBAAoB;AAEhC,QAAI,cAAc;AAElB,eAAW,aAAa,YAAY;AAClC,qBAAe,MAAM,SAAS;AAAA,IAChC;AAEA,QAAI,cAAc,GAAG;AACnB,cAAQ,IAAI,SAAS,MAAM,IAAI,WAAW,CAAC,oBAAoB;AAAA,IACjE,OAAO;AACL,cAAQ,IAAI,8BAA8B,MAAM,MAAM,QAAG,CAAC,EAAE;AAAA,IAC9D;AAAA,EACF;AACA,SAAO;AACT;","names":["path"]}
|
package/dist/actions/index.mjs
CHANGED
|
@@ -717,7 +717,8 @@ function getImportsFromFile(filePath, importPaths) {
|
|
|
717
717
|
ts.forEachChild(node, visit);
|
|
718
718
|
}
|
|
719
719
|
visit(sourceFile);
|
|
720
|
-
const
|
|
720
|
+
const importsStartsWithExcludes = [".", "#", "node:", "@types/"];
|
|
721
|
+
const cleanedImports = imports.filter((imp) => !importsStartsWithExcludes.some((exc) => imp.startsWith(exc))).map(getBasePackageName);
|
|
721
722
|
for (const imp of cleanedImports) {
|
|
722
723
|
importPaths[imp] = importPaths[imp] || [];
|
|
723
724
|
importPaths[imp].push(filePath);
|
|
@@ -759,7 +760,8 @@ function getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }) {
|
|
|
759
760
|
function check({
|
|
760
761
|
name,
|
|
761
762
|
location,
|
|
762
|
-
devDeps = false
|
|
763
|
+
devDeps = false,
|
|
764
|
+
peerDeps = false
|
|
763
765
|
}) {
|
|
764
766
|
const { prodSourceFiles, devSourceFiles } = findFiles(location);
|
|
765
767
|
const {
|
|
@@ -775,6 +777,7 @@ function check({
|
|
|
775
777
|
} = getDependenciesFromPackageJson(`${location}/package.json`);
|
|
776
778
|
let unlistedDependencies = 0;
|
|
777
779
|
let unlistedDevDependencies = 0;
|
|
780
|
+
let unusedDependencies = 0;
|
|
778
781
|
for (const imp of externalProdImports) {
|
|
779
782
|
if (!dependencies.includes(imp) && !peerDependencies.includes(imp)) {
|
|
780
783
|
unlistedDependencies++;
|
|
@@ -785,20 +788,22 @@ function check({
|
|
|
785
788
|
}
|
|
786
789
|
for (const dep of dependencies) {
|
|
787
790
|
if (!externalProdImports.includes(dep)) {
|
|
788
|
-
|
|
791
|
+
unusedDependencies++;
|
|
789
792
|
console.log(`[${chalk13.blue(name)}] Unused dependency in package.json: ${chalk13.red(dep)}`);
|
|
790
793
|
console.log(` ${location}/package.json
|
|
791
794
|
`);
|
|
792
795
|
console.log("");
|
|
793
796
|
}
|
|
794
797
|
}
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
798
|
+
if (peerDeps) {
|
|
799
|
+
for (const dep of peerDependencies) {
|
|
800
|
+
if (!externalProdImports.includes(dep)) {
|
|
801
|
+
unusedDependencies++;
|
|
802
|
+
console.log(`[${chalk13.blue(name)}] Unused peerDependency in package.json: ${chalk13.red(dep)}`);
|
|
803
|
+
console.log(` ${location}/package.json
|
|
800
804
|
`);
|
|
801
|
-
|
|
805
|
+
console.log("");
|
|
806
|
+
}
|
|
802
807
|
}
|
|
803
808
|
}
|
|
804
809
|
if (devDeps) {
|
|
@@ -810,7 +815,7 @@ function check({
|
|
|
810
815
|
}
|
|
811
816
|
}
|
|
812
817
|
}
|
|
813
|
-
const totalErrors = unlistedDependencies + unlistedDevDependencies;
|
|
818
|
+
const totalErrors = unlistedDependencies + unlistedDevDependencies + unusedDependencies;
|
|
814
819
|
return totalErrors;
|
|
815
820
|
}
|
|
816
821
|
var deplint = ({ pkg }) => {
|