expose-kit 0.9.0 → 0.10.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 +27 -0
- package/dist/index.js +192 -8
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -346,6 +346,33 @@ Notes:
|
|
|
346
346
|
|
|
347
347
|
---
|
|
348
348
|
|
|
349
|
+
### `expose sequence-split`
|
|
350
|
+
|
|
351
|
+
Split sequence expressions into statements.
|
|
352
|
+
```js
|
|
353
|
+
// before
|
|
354
|
+
a, b, c;
|
|
355
|
+
// after
|
|
356
|
+
a;
|
|
357
|
+
b;
|
|
358
|
+
c;
|
|
359
|
+
```
|
|
360
|
+
Example is [here](https://github.com/evex-dev/expose-kit/tree/main/commands/sequence-split/mocks).
|
|
361
|
+
|
|
362
|
+
```bash
|
|
363
|
+
expose sequence-split path/to/file.js --output path/to/file.sequence-split.js
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
Args:
|
|
367
|
+
- `--o, --output <file>`
|
|
368
|
+
Output file path
|
|
369
|
+
|
|
370
|
+
Notes:
|
|
371
|
+
- Splits safe sequence expressions into standalone statements.
|
|
372
|
+
- Also normalizes single-statement control flow blocks.
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
349
376
|
### `expose remove-unused`
|
|
350
377
|
|
|
351
378
|
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
|
|
5
|
+
import { dirname as dirname11, join as join11 } from "path";
|
|
6
6
|
import { fileURLToPath } from "url";
|
|
7
7
|
import chalk4 from "chalk";
|
|
8
8
|
|
|
@@ -1843,6 +1843,189 @@ var fn_inliner_default = createCommand((program2) => {
|
|
|
1843
1843
|
);
|
|
1844
1844
|
});
|
|
1845
1845
|
|
|
1846
|
+
// commands/sequence-split/index.ts
|
|
1847
|
+
import { readFileSync as readFileSync11, writeFileSync as writeFileSync10 } from "fs";
|
|
1848
|
+
import { basename as basename10, dirname as dirname10, extname as extname10, join as join10 } from "path";
|
|
1849
|
+
import { parse as parse11 } from "@babel/parser";
|
|
1850
|
+
import traverse10 from "@babel/traverse";
|
|
1851
|
+
import generate10 from "@babel/generator";
|
|
1852
|
+
import * as t9 from "@babel/types";
|
|
1853
|
+
import loading11 from "loading-cli";
|
|
1854
|
+
var walk2 = patchDefault(traverse10);
|
|
1855
|
+
var createDefaultOutputPath10 = (inputPath) => {
|
|
1856
|
+
const ext = extname10(inputPath);
|
|
1857
|
+
if (!ext) {
|
|
1858
|
+
return `${inputPath}.sequence-split.js`;
|
|
1859
|
+
}
|
|
1860
|
+
const base = basename10(inputPath, ext);
|
|
1861
|
+
return join10(dirname10(inputPath), `${base}.sequence-split${ext}`);
|
|
1862
|
+
};
|
|
1863
|
+
var isExcluded = (node) => {
|
|
1864
|
+
return t9.isIdentifier(node) && node.name === "eval";
|
|
1865
|
+
};
|
|
1866
|
+
var sequenceSplit = (code, filename) => {
|
|
1867
|
+
const ast = parse11(code, createParseOptions(filename));
|
|
1868
|
+
let changedCount = 0;
|
|
1869
|
+
const markChanged = () => {
|
|
1870
|
+
changedCount += 1;
|
|
1871
|
+
};
|
|
1872
|
+
walk2(ast, {
|
|
1873
|
+
ConditionalExpression(path) {
|
|
1874
|
+
if (!path.parentPath || !path.parentPath.isExpressionStatement()) return;
|
|
1875
|
+
const replacement = t9.ifStatement(
|
|
1876
|
+
path.node.test,
|
|
1877
|
+
t9.expressionStatement(path.node.consequent),
|
|
1878
|
+
t9.expressionStatement(path.node.alternate)
|
|
1879
|
+
);
|
|
1880
|
+
if (path.parentPath.parentPath && path.parentPath.parentPath.key === "alternate" && path.parentPath.parentPath.isBlockStatement() && path.parentPath.parentPath.node.body.length === 1) {
|
|
1881
|
+
path.parentPath.parentPath.replaceWith(replacement);
|
|
1882
|
+
} else {
|
|
1883
|
+
path.parentPath.replaceWith(replacement);
|
|
1884
|
+
}
|
|
1885
|
+
path.skip();
|
|
1886
|
+
markChanged();
|
|
1887
|
+
},
|
|
1888
|
+
LogicalExpression(path) {
|
|
1889
|
+
if (!path.parentPath || !path.parentPath.isExpressionStatement()) return;
|
|
1890
|
+
if (path.node.operator !== "&&" && path.node.operator !== "||") return;
|
|
1891
|
+
const test = path.node.operator === "&&" ? path.node.left : t9.unaryExpression("!", path.node.left);
|
|
1892
|
+
const replacement = t9.ifStatement(
|
|
1893
|
+
test,
|
|
1894
|
+
t9.expressionStatement(path.node.right)
|
|
1895
|
+
);
|
|
1896
|
+
if (path.parentPath.parentPath && path.parentPath.parentPath.key === "alternate" && path.parentPath.parentPath.isBlockStatement() && path.parentPath.parentPath.node.body.length === 1) {
|
|
1897
|
+
path.parentPath.parentPath.replaceWith(replacement);
|
|
1898
|
+
} else {
|
|
1899
|
+
path.parentPath.replaceWith(replacement);
|
|
1900
|
+
}
|
|
1901
|
+
path.skip();
|
|
1902
|
+
markChanged();
|
|
1903
|
+
},
|
|
1904
|
+
"ForStatement|WhileStatement|DoWhileStatement"(path) {
|
|
1905
|
+
if (!path.isForStatement() && !path.isWhileStatement() && !path.isDoWhileStatement()) return;
|
|
1906
|
+
if (t9.isBlockStatement(path.node.body)) return;
|
|
1907
|
+
path.node.body = t9.blockStatement([path.node.body]);
|
|
1908
|
+
markChanged();
|
|
1909
|
+
},
|
|
1910
|
+
IfStatement(path) {
|
|
1911
|
+
if (!t9.isBlockStatement(path.node.consequent)) {
|
|
1912
|
+
path.node.consequent = t9.blockStatement([path.node.consequent]);
|
|
1913
|
+
markChanged();
|
|
1914
|
+
}
|
|
1915
|
+
if (path.node.alternate && !t9.isBlockStatement(path.node.alternate) && !t9.isIfStatement(path.node.alternate)) {
|
|
1916
|
+
path.node.alternate = t9.blockStatement([path.node.alternate]);
|
|
1917
|
+
markChanged();
|
|
1918
|
+
}
|
|
1919
|
+
},
|
|
1920
|
+
VariableDeclaration(path) {
|
|
1921
|
+
if (path.node.declarations.length <= 1) return;
|
|
1922
|
+
const replacements = path.node.declarations.map(
|
|
1923
|
+
(declaration) => t9.variableDeclaration(path.node.kind, [declaration])
|
|
1924
|
+
);
|
|
1925
|
+
if (path.parentPath?.isForStatement() && path.parentKey === "init") {
|
|
1926
|
+
const lastDeclaration = replacements.pop();
|
|
1927
|
+
if (lastDeclaration) {
|
|
1928
|
+
path.parentPath.insertBefore(replacements);
|
|
1929
|
+
path.parentPath.node.init = lastDeclaration;
|
|
1930
|
+
}
|
|
1931
|
+
} else {
|
|
1932
|
+
path.replaceWithMultiple(replacements);
|
|
1933
|
+
}
|
|
1934
|
+
markChanged();
|
|
1935
|
+
},
|
|
1936
|
+
SequenceExpression(path) {
|
|
1937
|
+
const expressions = path.node.expressions;
|
|
1938
|
+
if (expressions.length === 1 && expressions[0]) {
|
|
1939
|
+
path.replaceWith(expressions[0]);
|
|
1940
|
+
markChanged();
|
|
1941
|
+
return;
|
|
1942
|
+
}
|
|
1943
|
+
let outerPath = path;
|
|
1944
|
+
while (!t9.isStatement(outerPath.node)) {
|
|
1945
|
+
const parent = outerPath.parentPath;
|
|
1946
|
+
if (!parent) return;
|
|
1947
|
+
if (parent.isConditionalExpression() && (outerPath.key === "consequent" || outerPath.key === "alternate") || parent.isLogicalExpression() && outerPath.key === "right" || parent.isForStatement() && (outerPath.key === "test" || outerPath.key === "update") || parent.isDoWhileStatement() && outerPath.key === "test" || parent.isArrowFunctionExpression() && outerPath.key === "body") {
|
|
1948
|
+
return;
|
|
1949
|
+
}
|
|
1950
|
+
outerPath = parent;
|
|
1951
|
+
}
|
|
1952
|
+
const lastExpression = expressions[expressions.length - 1];
|
|
1953
|
+
if (lastExpression && isExcluded(lastExpression)) {
|
|
1954
|
+
const firstExpressions = expressions.splice(0, expressions.length - 2);
|
|
1955
|
+
if (firstExpressions.length > 0) {
|
|
1956
|
+
const expressionStatements = firstExpressions.map(
|
|
1957
|
+
(expression) => t9.expressionStatement(expression)
|
|
1958
|
+
);
|
|
1959
|
+
outerPath.insertBefore(expressionStatements);
|
|
1960
|
+
markChanged();
|
|
1961
|
+
}
|
|
1962
|
+
} else {
|
|
1963
|
+
const finalExpression = expressions.splice(expressions.length - 1, 1)[0];
|
|
1964
|
+
const expressionStatements = expressions.map(
|
|
1965
|
+
(expression) => t9.expressionStatement(expression)
|
|
1966
|
+
);
|
|
1967
|
+
outerPath.insertBefore(expressionStatements);
|
|
1968
|
+
if (finalExpression) {
|
|
1969
|
+
path.replaceWith(finalExpression);
|
|
1970
|
+
}
|
|
1971
|
+
markChanged();
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
});
|
|
1975
|
+
return {
|
|
1976
|
+
code: patchDefault(generate10)(ast).code,
|
|
1977
|
+
changedCount
|
|
1978
|
+
};
|
|
1979
|
+
};
|
|
1980
|
+
var sequence_split_default = createCommand((program2) => {
|
|
1981
|
+
program2.command("sequence-split").description("Split sequence expressions into statements").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(
|
|
1982
|
+
async (fileArgument, options) => {
|
|
1983
|
+
await timeout(
|
|
1984
|
+
async ({ finish }) => {
|
|
1985
|
+
const filename = fileArgument ?? options.file ?? await createPrompt("Enter the file path:");
|
|
1986
|
+
if (!filename) {
|
|
1987
|
+
showError("No file provided");
|
|
1988
|
+
return finish();
|
|
1989
|
+
}
|
|
1990
|
+
try {
|
|
1991
|
+
const fileContent = readFileSync11(filename, "utf8");
|
|
1992
|
+
const defaultOutputPath = createDefaultOutputPath10(filename);
|
|
1993
|
+
let outputPath = options.output;
|
|
1994
|
+
if (!outputPath) {
|
|
1995
|
+
const promptPath = (await createPrompt("Enter the output file path:"))?.trim();
|
|
1996
|
+
outputPath = promptPath || defaultOutputPath;
|
|
1997
|
+
}
|
|
1998
|
+
const loader = loading11("Splitting sequences...").start();
|
|
1999
|
+
try {
|
|
2000
|
+
const { code: output, changedCount } = sequenceSplit(
|
|
2001
|
+
fileContent,
|
|
2002
|
+
filename
|
|
2003
|
+
);
|
|
2004
|
+
writeFileSync10(outputPath, output, "utf8");
|
|
2005
|
+
loader.succeed(
|
|
2006
|
+
`Saved sequence-split file to: ${outputPath} (${diff(fileContent, output).length} lines changed, ${changedCount} transforms)`
|
|
2007
|
+
);
|
|
2008
|
+
return finish();
|
|
2009
|
+
} catch (error) {
|
|
2010
|
+
loader.fail("Failed to apply sequence-split transform");
|
|
2011
|
+
showError(
|
|
2012
|
+
`Error transforming file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2013
|
+
);
|
|
2014
|
+
return finish();
|
|
2015
|
+
}
|
|
2016
|
+
} catch (error) {
|
|
2017
|
+
showError(
|
|
2018
|
+
`Error reading file '${filename}': ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2019
|
+
);
|
|
2020
|
+
return finish();
|
|
2021
|
+
}
|
|
2022
|
+
},
|
|
2023
|
+
options.unlimited ? null : 120 * 1e3
|
|
2024
|
+
);
|
|
2025
|
+
}
|
|
2026
|
+
);
|
|
2027
|
+
});
|
|
2028
|
+
|
|
1846
2029
|
// utils/cli/showCredit.ts
|
|
1847
2030
|
import chalk3 from "chalk";
|
|
1848
2031
|
var beautify = (strings, ...values) => {
|
|
@@ -1867,10 +2050,10 @@ var calmGradienrain = (text) => {
|
|
|
1867
2050
|
const endHue = 300;
|
|
1868
2051
|
const saturation = 0.45;
|
|
1869
2052
|
const value = 0.8;
|
|
1870
|
-
const ease = (
|
|
2053
|
+
const ease = (t10) => t10 * t10 * (3 - 2 * t10);
|
|
1871
2054
|
return text.split("").map((char, i) => {
|
|
1872
|
-
const
|
|
1873
|
-
const hue = startHue + (endHue - startHue) *
|
|
2055
|
+
const t10 = ease(i / Math.max(text.length - 1, 1));
|
|
2056
|
+
const hue = startHue + (endHue - startHue) * t10;
|
|
1874
2057
|
const c = value * saturation;
|
|
1875
2058
|
const h = hue / 60;
|
|
1876
2059
|
const x = c * (1 - Math.abs(h % 2 - 1));
|
|
@@ -1894,10 +2077,10 @@ ${calmGradienrain(`Expose Kit v${VERSION}`)}
|
|
|
1894
2077
|
`;
|
|
1895
2078
|
|
|
1896
2079
|
// index.ts
|
|
1897
|
-
import { readFileSync as
|
|
2080
|
+
import { readFileSync as readFileSync12 } from "fs";
|
|
1898
2081
|
var __filename = fileURLToPath(import.meta.url);
|
|
1899
|
-
var __dirname =
|
|
1900
|
-
var pkg = JSON.parse(
|
|
2082
|
+
var __dirname = dirname11(__filename);
|
|
2083
|
+
var pkg = JSON.parse(readFileSync12(join11(__dirname, "package.json"), "utf8"));
|
|
1901
2084
|
console.log(showCredit(pkg.version));
|
|
1902
2085
|
console.log();
|
|
1903
2086
|
var program = new Command();
|
|
@@ -1916,7 +2099,8 @@ var commands = [
|
|
|
1916
2099
|
remove_updater_default,
|
|
1917
2100
|
remove_reassign_default,
|
|
1918
2101
|
fn_inliner_default,
|
|
1919
|
-
remove_unused_default
|
|
2102
|
+
remove_unused_default,
|
|
2103
|
+
sequence_split_default
|
|
1920
2104
|
];
|
|
1921
2105
|
for (const command of commands) {
|
|
1922
2106
|
command(program);
|
package/dist/package.json
CHANGED