pruny 1.14.0 → 1.15.0

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.
Files changed (2) hide show
  1. package/dist/index.js +62 -6
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -9621,13 +9621,22 @@ function extractRoutePath(filePath) {
9621
9621
  }
9622
9622
  function extractExportedMethods(content) {
9623
9623
  const methods = [];
9624
+ const methodLines = {};
9625
+ const lines = content.split(`
9626
+ `);
9624
9627
  let match2;
9628
+ EXPORTED_METHOD_PATTERN.lastIndex = 0;
9625
9629
  while ((match2 = EXPORTED_METHOD_PATTERN.exec(content)) !== null) {
9626
9630
  if (match2[1]) {
9627
- methods.push(match2[1]);
9631
+ const methodName = match2[1];
9632
+ methods.push(methodName);
9633
+ const pos = match2.index;
9634
+ const lineNum = content.substring(0, pos).split(`
9635
+ `).length;
9636
+ methodLines[methodName] = lineNum;
9628
9637
  }
9629
9638
  }
9630
- return methods;
9639
+ return { methods, methodLines };
9631
9640
  }
9632
9641
  function extractNestRoutes(filePath, content, globalPrefix = "api") {
9633
9642
  const controllerMatch = content.match(NEST_CONTROLLER_PATTERN);
@@ -9640,12 +9649,16 @@ function extractNestRoutes(filePath, content, globalPrefix = "api") {
9640
9649
  while ((methodMatch = NEST_METHOD_PATTERN.exec(content)) !== null) {
9641
9650
  const methodType = methodMatch[1].toUpperCase();
9642
9651
  const methodPath = methodMatch[2] || "";
9652
+ const pos = methodMatch.index;
9653
+ const lineNum = content.substring(0, pos).split(`
9654
+ `).length;
9643
9655
  const fullPath = `/${globalPrefix}/${controllerPath}/${methodPath}`.replace(/\/+/g, "/").replace(/\/$/, "");
9644
9656
  const existing = routes.find((r) => r.path === fullPath);
9645
9657
  if (existing) {
9646
9658
  if (!existing.methods.includes(methodType)) {
9647
9659
  existing.methods.push(methodType);
9648
9660
  existing.unusedMethods.push(methodType);
9661
+ existing.methodLines[methodType] = lineNum;
9649
9662
  }
9650
9663
  } else {
9651
9664
  routes.push({
@@ -9655,7 +9668,8 @@ function extractNestRoutes(filePath, content, globalPrefix = "api") {
9655
9668
  used: false,
9656
9669
  references: [],
9657
9670
  methods: [methodType],
9658
- unusedMethods: [methodType]
9671
+ unusedMethods: [methodType],
9672
+ methodLines: { [methodType]: lineNum }
9659
9673
  });
9660
9674
  }
9661
9675
  }
@@ -9733,7 +9747,7 @@ async function scan(config) {
9733
9747
  });
9734
9748
  const nextRoutes = nextFiles.map((file) => {
9735
9749
  const content = readFileSync4(join4(cwd, file), "utf-8");
9736
- const methods = extractExportedMethods(content);
9750
+ const { methods, methodLines } = extractExportedMethods(content);
9737
9751
  return {
9738
9752
  type: "nextjs",
9739
9753
  path: extractRoutePath(file),
@@ -9741,7 +9755,8 @@ async function scan(config) {
9741
9755
  used: false,
9742
9756
  references: [],
9743
9757
  methods,
9744
- unusedMethods: [...methods]
9758
+ unusedMethods: [...methods],
9759
+ methodLines
9745
9760
  };
9746
9761
  });
9747
9762
  const nestPatterns = ["**/*.controller.ts"];
@@ -9953,7 +9968,7 @@ function findConfigFile(dir) {
9953
9968
  }
9954
9969
 
9955
9970
  // src/fixer.ts
9956
- import { readFileSync as readFileSync6, writeFileSync, unlinkSync } from "node:fs";
9971
+ import { readFileSync as readFileSync6, writeFileSync, unlinkSync, existsSync as existsSync5 } from "node:fs";
9957
9972
  import { join as join6 } from "node:path";
9958
9973
  function removeExportFromLine(rootDir, exp) {
9959
9974
  const fullPath = join6(rootDir, exp.file);
@@ -10039,6 +10054,32 @@ function isFileEmpty(content) {
10039
10054
  }
10040
10055
  return true;
10041
10056
  }
10057
+ function removeMethodFromRoute(rootDir, filePath, methodName, lineNum) {
10058
+ const fullPath = join6(rootDir, filePath);
10059
+ if (!existsSync5(fullPath))
10060
+ return false;
10061
+ try {
10062
+ const content = readFileSync6(fullPath, "utf-8");
10063
+ const lines = content.split(`
10064
+ `);
10065
+ const lineIndex = lineNum - 1;
10066
+ const deletedLines = deleteDeclaration(lines, lineIndex);
10067
+ if (deletedLines > 0) {
10068
+ const newContent = lines.join(`
10069
+ `);
10070
+ if (isFileEmpty(newContent)) {
10071
+ unlinkSync(fullPath);
10072
+ } else {
10073
+ writeFileSync(fullPath, newContent, "utf-8");
10074
+ }
10075
+ return true;
10076
+ }
10077
+ return false;
10078
+ } catch (err) {
10079
+ console.error(`Error removing method ${methodName} in ${filePath}:`, err);
10080
+ return false;
10081
+ }
10082
+ }
10042
10083
 
10043
10084
  // src/init.ts
10044
10085
  import { writeFileSync as writeFileSync2, existsSync as existsSync6 } from "node:fs";
@@ -10202,6 +10243,21 @@ program2.action(async (options) => {
10202
10243
  }
10203
10244
  }
10204
10245
  }
10246
+ const partiallyRoutes = result.routes.filter((r) => r.used && r.unusedMethods.length > 0);
10247
+ if (partiallyRoutes.length > 0) {
10248
+ console.log(source_default.yellow.bold(`\uD83D\uDD27 Fixing partially unused routes...
10249
+ `));
10250
+ for (const route of partiallyRoutes) {
10251
+ const sortedMethods = [...route.unusedMethods].filter((m) => route.methodLines[m] !== undefined).sort((a, b) => route.methodLines[b] - route.methodLines[a]);
10252
+ for (const method of sortedMethods) {
10253
+ const lineNum = route.methodLines[method];
10254
+ if (removeMethodFromRoute(config.dir, route.filePath, method, lineNum)) {
10255
+ console.log(source_default.green(` Fixed: Removed ${method} from ${route.path}`));
10256
+ }
10257
+ }
10258
+ }
10259
+ console.log("");
10260
+ }
10205
10261
  if (result.unusedExports && result.unusedExports.exports.length > 0) {
10206
10262
  console.log(source_default.yellow.bold(`\uD83D\uDD27 Fixing unused exports (removing "export" keyword)...
10207
10263
  `));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pruny",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "Find and remove unused Next.js API routes & Nest.js Controllers",
5
5
  "type": "module",
6
6
  "files": [