expose-kit 0.6.0 → 0.7.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.
package/README.md CHANGED
@@ -258,6 +258,33 @@ Notes:
258
258
 
259
259
  ---
260
260
 
261
+ ### `expose remove-updater`
262
+
263
+ Replace safe update expressions with += or -=.
264
+ ```js
265
+ // before
266
+ a++;
267
+ --b;
268
+ // after
269
+ a += 1;
270
+ b -= 1;
271
+ ```
272
+ Example is [here](https://github.com/evex-dev/expose-kit/tree/main/commands/remove-updater/mocks).
273
+
274
+ ```bash
275
+ expose remove-updater path/to/file.js --output path/to/file.remove-updater.js
276
+ ```
277
+
278
+ Args:
279
+ - `--o, --output <file>`
280
+ Output file path
281
+
282
+ Notes:
283
+ - Only replaces update expressions whose value is not used.
284
+ - Safe for expression statements and for-loop update clauses.
285
+
286
+ ---
287
+
261
288
  ### `expose remove-unused`
262
289
 
263
290
  Remove unused variabless.
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // index.ts
4
4
  import { Command } from "commander";
5
- import { dirname as dirname7, join as join7 } from "path";
5
+ import { dirname as dirname8, join as join8 } from "path";
6
6
  import { fileURLToPath } from "url";
7
7
  import chalk4 from "chalk";
8
8
 
@@ -1217,7 +1217,7 @@ var pre_evaluate_default = createCommand((program2) => {
1217
1217
  );
1218
1218
  });
1219
1219
 
1220
- // commands/remove-unused/index.ts
1220
+ // commands/remove-updater/index.ts
1221
1221
  import { readFileSync as readFileSync7, writeFileSync as writeFileSync6 } from "fs";
1222
1222
  import { basename as basename6, dirname as dirname6, extname as extname6, join as join6 } from "path";
1223
1223
  import { parse as parse7 } from "@babel/parser";
@@ -1228,19 +1228,113 @@ import loading7 from "loading-cli";
1228
1228
  var createDefaultOutputPath6 = (inputPath) => {
1229
1229
  const ext = extname6(inputPath);
1230
1230
  if (!ext) {
1231
- return `${inputPath}.remove-unused.js`;
1231
+ return `${inputPath}.remove-updater.js`;
1232
1232
  }
1233
1233
  const base = basename6(inputPath, ext);
1234
- return join6(dirname6(inputPath), `${base}.remove-unused${ext}`);
1234
+ return join6(dirname6(inputPath), `${base}.remove-updater${ext}`);
1235
1235
  };
1236
- var removeUnusedVariables = (code, filename) => {
1236
+ var isSafeStandaloneUpdate = (path) => {
1237
+ const parent = path.parentPath;
1238
+ if (!parent) return false;
1239
+ if (parent.isExpressionStatement()) return true;
1240
+ if (parent.isForStatement() && path.key === "update") return true;
1241
+ return false;
1242
+ };
1243
+ var removeUpdaters = (code, filename) => {
1237
1244
  const ast = parse7(code, createParseOptions(filename));
1238
- let changed = false;
1245
+ let replacedCount = 0;
1239
1246
  patchDefault(traverse6)(ast, {
1247
+ UpdateExpression(path) {
1248
+ if (!isSafeStandaloneUpdate(path)) return;
1249
+ const operator = path.node.operator === "++" ? "+=" : "-=";
1250
+ const left = t5.cloneNode(path.node.argument, true);
1251
+ const replacement = t5.assignmentExpression(
1252
+ operator,
1253
+ left,
1254
+ t5.numericLiteral(1)
1255
+ );
1256
+ path.replaceWith(replacement);
1257
+ replacedCount += 1;
1258
+ }
1259
+ });
1260
+ return {
1261
+ code: patchDefault(generate6)(ast).code,
1262
+ replacedCount
1263
+ };
1264
+ };
1265
+ var remove_updater_default = createCommand((program2) => {
1266
+ program2.command("remove-updater").description("Replace safe update expressions with += or -=").argument("[file]", "The file to transform").option("--input, --file <file>", "The file to transform").option("--o, --output <file>", "Output file path").option("--unlimited", "Unlimited timeout").action(
1267
+ async (fileArgument, options) => {
1268
+ await timeout(
1269
+ async ({ finish }) => {
1270
+ const filename = fileArgument ?? options.file ?? await createPrompt("Enter the file path:");
1271
+ if (!filename) {
1272
+ showError("No file provided");
1273
+ return finish();
1274
+ }
1275
+ try {
1276
+ const fileContent = readFileSync7(filename, "utf8");
1277
+ const defaultOutputPath = createDefaultOutputPath6(filename);
1278
+ let outputPath = options.output;
1279
+ if (!outputPath) {
1280
+ const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
1281
+ outputPath = promptPath || defaultOutputPath;
1282
+ }
1283
+ const loader = loading7("Removing update expressions...").start();
1284
+ try {
1285
+ const { code: output, replacedCount } = removeUpdaters(
1286
+ fileContent,
1287
+ filename
1288
+ );
1289
+ writeFileSync6(outputPath, output, "utf8");
1290
+ loader.succeed(
1291
+ `Saved remove-updater file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${replacedCount} updates replaced)`
1292
+ );
1293
+ return finish();
1294
+ } catch (error) {
1295
+ loader.fail("Failed to apply remove-updater transform");
1296
+ showError(
1297
+ `Error transforming file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
1298
+ );
1299
+ return finish();
1300
+ }
1301
+ } catch (error) {
1302
+ showError(
1303
+ `Error reading file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
1304
+ );
1305
+ return finish();
1306
+ }
1307
+ },
1308
+ options.unlimited ? null : 120 * 1e3
1309
+ );
1310
+ }
1311
+ );
1312
+ });
1313
+
1314
+ // commands/remove-unused/index.ts
1315
+ import { readFileSync as readFileSync8, writeFileSync as writeFileSync7 } from "fs";
1316
+ import { basename as basename7, dirname as dirname7, extname as extname7, join as join7 } from "path";
1317
+ import { parse as parse8 } from "@babel/parser";
1318
+ import traverse7 from "@babel/traverse";
1319
+ import generate7 from "@babel/generator";
1320
+ import * as t6 from "@babel/types";
1321
+ import loading8 from "loading-cli";
1322
+ var createDefaultOutputPath7 = (inputPath) => {
1323
+ const ext = extname7(inputPath);
1324
+ if (!ext) {
1325
+ return `${inputPath}.remove-unused.js`;
1326
+ }
1327
+ const base = basename7(inputPath, ext);
1328
+ return join7(dirname7(inputPath), `${base}.remove-unused${ext}`);
1329
+ };
1330
+ var removeUnusedVariables = (code, filename) => {
1331
+ const ast = parse8(code, createParseOptions(filename));
1332
+ let changed = false;
1333
+ patchDefault(traverse7)(ast, {
1240
1334
  Scope(path) {
1241
1335
  for (const binding of Object.values(path.scope.bindings)) {
1242
1336
  if (!binding.referenced && binding.constantViolations.length === 0 && binding.path.key !== "handler" && !binding.path.isFunctionExpression()) {
1243
- if (t5.isProgram(binding.scope.block) && (binding.kind === "var" || binding.kind === "hoisted")) {
1337
+ if (t6.isProgram(binding.scope.block) && (binding.kind === "var" || binding.kind === "hoisted")) {
1244
1338
  continue;
1245
1339
  }
1246
1340
  const targets = binding.path.parentKey === "params" ? [...binding.referencePaths, ...binding.constantViolations] : [
@@ -1249,11 +1343,11 @@ var removeUnusedVariables = (code, filename) => {
1249
1343
  ...binding.constantViolations
1250
1344
  ];
1251
1345
  for (const targetPath of targets) {
1252
- if (targetPath.isVariableDeclarator() && (t5.isArrayPattern(targetPath.node.id) && targetPath.node.id.elements.length > 1 || t5.isObjectPattern(targetPath.node.id) && targetPath.node.id.properties.length > 1)) {
1346
+ if (targetPath.isVariableDeclarator() && (t6.isArrayPattern(targetPath.node.id) && targetPath.node.id.elements.length > 1 || t6.isObjectPattern(targetPath.node.id) && targetPath.node.id.properties.length > 1)) {
1253
1347
  continue;
1254
1348
  }
1255
1349
  if (targetPath.key === "consequent" || targetPath.key === "alternate" || targetPath.key === "body") {
1256
- targetPath.replaceWith(t5.blockStatement([]));
1350
+ targetPath.replaceWith(t6.blockStatement([]));
1257
1351
  } else {
1258
1352
  const parentPath = targetPath.parentPath;
1259
1353
  if (parentPath?.isVariableDeclaration() && parentPath.node.declarations.length === 1) {
@@ -1269,7 +1363,7 @@ var removeUnusedVariables = (code, filename) => {
1269
1363
  }
1270
1364
  });
1271
1365
  return {
1272
- code: patchDefault(generate6)(ast).code,
1366
+ code: patchDefault(generate7)(ast).code,
1273
1367
  changed
1274
1368
  };
1275
1369
  };
@@ -1284,20 +1378,20 @@ var remove_unused_default = createCommand((program2) => {
1284
1378
  return finish();
1285
1379
  }
1286
1380
  try {
1287
- const fileContent = readFileSync7(filename, "utf8");
1288
- const defaultOutputPath = createDefaultOutputPath6(filename);
1381
+ const fileContent = readFileSync8(filename, "utf8");
1382
+ const defaultOutputPath = createDefaultOutputPath7(filename);
1289
1383
  let outputPath = options.output;
1290
1384
  if (!outputPath) {
1291
1385
  const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
1292
1386
  outputPath = promptPath || defaultOutputPath;
1293
1387
  }
1294
- const loader = loading7("Removing unused variables...").start();
1388
+ const loader = loading8("Removing unused variables...").start();
1295
1389
  try {
1296
1390
  const { code: output, changed } = removeUnusedVariables(
1297
1391
  fileContent,
1298
1392
  filename
1299
1393
  );
1300
- writeFileSync6(outputPath, output, "utf8");
1394
+ writeFileSync7(outputPath, output, "utf8");
1301
1395
  const diffLines = diff(fileContent, output).length;
1302
1396
  loader.succeed(
1303
1397
  `Saved remove-unused file to: ${outputPath} (${diffLines} lines changed${changed ? ", removed unused declarations" : ", no changes"})`
@@ -1347,10 +1441,10 @@ var calmGradienrain = (text) => {
1347
1441
  const endHue = 300;
1348
1442
  const saturation = 0.45;
1349
1443
  const value = 0.8;
1350
- const ease = (t6) => t6 * t6 * (3 - 2 * t6);
1444
+ const ease = (t7) => t7 * t7 * (3 - 2 * t7);
1351
1445
  return text.split("").map((char, i) => {
1352
- const t6 = ease(i / Math.max(text.length - 1, 1));
1353
- const hue = startHue + (endHue - startHue) * t6;
1446
+ const t7 = ease(i / Math.max(text.length - 1, 1));
1447
+ const hue = startHue + (endHue - startHue) * t7;
1354
1448
  const c = value * saturation;
1355
1449
  const h = hue / 60;
1356
1450
  const x = c * (1 - Math.abs(h % 2 - 1));
@@ -1374,10 +1468,10 @@ ${calmGradienrain(`Expose Kit v${VERSION}`)}
1374
1468
  `;
1375
1469
 
1376
1470
  // index.ts
1377
- import { readFileSync as readFileSync8 } from "fs";
1471
+ import { readFileSync as readFileSync9 } from "fs";
1378
1472
  var __filename = fileURLToPath(import.meta.url);
1379
- var __dirname = dirname7(__filename);
1380
- var pkg = JSON.parse(readFileSync8(join7(__dirname, "package.json"), "utf8"));
1473
+ var __dirname = dirname8(__filename);
1474
+ var pkg = JSON.parse(readFileSync9(join8(__dirname, "package.json"), "utf8"));
1381
1475
  console.log(showCredit(pkg.version));
1382
1476
  console.log();
1383
1477
  var program = new Command();
@@ -1393,6 +1487,7 @@ var commands = [
1393
1487
  expand_object_default,
1394
1488
  object_packer_default,
1395
1489
  pre_evaluate_default,
1490
+ remove_updater_default,
1396
1491
  remove_unused_default
1397
1492
  ];
1398
1493
  for (const command of commands) {
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expose-kit",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "author": "EdamAmex <edame8080@gmail.com> (https://github.com/EdamAme-x)",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expose-kit",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "author": "EdamAmex <edame8080@gmail.com> (https://github.com/EdamAme-x)",