Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): standalone CLI with better UX #71

Merged
merged 19 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"eslint.experimental.useFlatConfig": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.fixAll.eslint": true, // this allows ESLint to auto fix on save
"source.organizeImports": false
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},
"editor.formatOnSave": false,
"eslint.validate": [
Expand Down Expand Up @@ -40,15 +40,19 @@
"jsxs",
"lebab",
"Minifier",
"outro",
"Paneview",
"picocolors",
"poolifier",
"preact",
"quux",
"splitpanes",
"taze",
"testcase",
"testcases",
"unminified",
"unminify",
"Unminifying",
"unzlib",
"utoa",
"vutai",
Expand Down
56 changes: 36 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,51 @@ Test the tool and see it in action at [Playground][Playground].

## 🖥 Using the CLI

```
npx @wakaru/unpacker <files...> [options]
### Interactive mode

By default, the CLI will run in interactive mode and guide you through the process.\
You can also pass [options](#options) to skip some steps in the interactive mode.

```sh
npx @wakaru/cli
# or
pnpm dlx @wakaru/unpacker <files...> [options]
pnpm dlx @wakaru/cli
```

For example:
### Options

```
npx @wakaru/unpacker input.js # unpack bundled code into multiple files
cd ./out # go to the output directory
npx @wakaru/unminify ./ # unminify all files in the directory
```
Run `npx @wakaru/cli --help` to see the full list of options.

Files can also be specified as glob patterns. For example:
| Option | Default | Description |
| --------------- | ------- | --------------------------------------- |
| `--output` | `"out"` | Output directory |
| `--force` | `false` | Force overwrite output directory |
| `--concurrency` | `1` | Specific the number of concurrent tasks |
| `--perf` | `false` | Show performance metrics |

```sh
npx @wakaru/unminify ./src/**/*.js
`--concurrency` can be used to speed up the process. But please aware that the process might OOM if the input file is too large.

### Non-interactive mode

If you want to run the CLI in non-interactive mode, you can specify the feature by passing the feature name as the first argument.

`unpacker` and `unminify` will run only the corresponding feature.\
`all` will run both `unpacker` and `unminify` sequentially.

```
npx @wakaru/cli all <files...> [options]
npx @wakaru/cli unpacker <files...> [options]
npx @wakaru/cli unminify <files...> [options]
```

### Options
These options are **only** available in `all` mode.

| Option | Default | Description |
| ---------- | ------- | -------------------------------- |
| `--output` | `./out` | Output directory |
| `--force` | `false` | Force overwrite output directory |
| `--log-level` | `info` | Log level (`error`, `warn`, `info`, `debug`, `silent`) |
| Option | Default | Description |
| ------------------- | ---------------- | ---------------------------------- |
| `--unpacker-output` | `"out/unpack"` | Override unpacker output directory |
| `--unminify-output` | `"out/unminify"` | Override unminify output directory |

Note: Currently, the log level is not very accurate. It might still print some logs even if `silent` is specified. This will be improved in the future release.
When running a single feature (either `unpacker` or `unminify`), the CLI will only uses the path specified in the `--output` option. This means that, unlike in the `all` mode where subdirectories (`out/unpack` and `out/unminify`) are automatically created within the output directory, in single feature mode, the output files are placed directly in the specified `--output` directory without any additional subdirectories.

## 📦 Using the API

Expand Down Expand Up @@ -121,7 +137,7 @@ You can check all the rules at [/unminify/src/transformations/index.ts](https://

Please aware that this project is still in early development. The API might change in the future.

And the bundle size of these packages are huge. It might be reduced in the future. Use with caution on the browser (Yes, like the playground, it can run on the browser).
And the bundle size of these packages are huge. It might be reduced in the future. Use with caution on the browser (Yes, like the playground, it can run on the browser).

## Legal Disclaimer

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"private": true,
"packageManager": "[email protected]",
"description": "🔪📦 Javascript decompiler, unpacker and unminify toolkit",
"author": "pionxzh",
"author": "Pionxzh",
"license": "MIT",
"homepage": "https://github.com/pionxzh/wakaru",
"engines": {
"node": ">=16.0.0"
"node": ">=18.0.0"
},
"scripts": {
"build": "turbo run build",
Expand Down Expand Up @@ -41,7 +41,8 @@
},
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
"[email protected]": "patches/[email protected]",
"@clack/[email protected]": "patches/@[email protected]"
}
},
"resolutions": {
Expand Down
5 changes: 3 additions & 2 deletions packages/ast-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
"./reference": "./src/reference.ts",
"./scope": "./src/scope.ts",
"./types": "./src/types.ts",
"./wrapAstTransformation": "./src/wrapAstTransformation.ts"
"./wrapAstTransformation": "./src/wrapAstTransformation.ts",
"./wrapStringTransformation": "./src/wrapStringTransformation.ts"
},
"files": [
"dist",
"package.json"
],
"engines": {
"node": ">=16.0.0"
"node": ">=18.0.0"
},
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap --clean",
Expand Down
18 changes: 18 additions & 0 deletions packages/ast-utils/src/wrapStringTransformation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Options, Transform } from 'jscodeshift'

export interface StringTransformation<Params = object> {
(code: string, params: Params): string | void
}

export function wrapStringTransformation<Params extends Options>(
transformAST: StringTransformation<Params>,
): Transform {
// @ts-expect-error - jscodeshift is not happy
const transform: Transform = (file, api, options: Params) => {
const code = file.source
const result = transformAST(code, options)
return result ?? code
}

return transform
}
2 changes: 1 addition & 1 deletion packages/browserfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
},
"engines": {
"node": ">=16.0.0"
"node": ">=18.0.0"
},
"scripts": {
}
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# `@wakaru/cli`

## Development

To run the CLI in development mode, use the following command:

### MacOS / Linux

```bash
.\bin\run.sh all .\input.js --force
```

### Windows

```bash
.\bin\run.cmd all .\input.js --force
```

### Script

```bash
pnpm run dev-cli -- -- all .\iput.js --force
```

Please aware that double `--` is required to pass arguments to the script.
3 changes: 3 additions & 0 deletions packages/cli/bin/run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

pnpm exec tsup --silent --onSuccess "node %~dp0\..\dist\cli.cjs %*"
3 changes: 3 additions & 0 deletions packages/cli/bin/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

pnpm exec tsup --silent --onSuccess "node $(dirname "$0")/../dist/cli.cjs $*"
58 changes: 58 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@wakaru/cli",
"type": "module",
"version": "0.0.1",
"description": "🔪📦 Unminify and beautify bundled code",
"author": "Pionxzh",
"license": "MIT",
"bin": "dist/cli.cjs",
"files": [
"dist",
"package.json"
],
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"build": "tsup",
"dev-cli": "pnpm exec tsup --silent --onSuccess \"node ./dist/cli.cjs $npm_package_main\"",
"test": "vitest run --globals",
"test:update": "vitest run --update --globals",
"test:watch": "vitest watch --globals",
"test:type": "tsc --noEmit",
"lint": "eslint src",
"lint:fix": "eslint src --fix"
},
"dependencies": {
"@clack/core": "^0.3.3",
"@clack/prompts": "^0.7.0",
"fs-extra": "^11.1.1",
"globby": "^11.1.0",
"picocolors": "^1.0.0",
"poolifier": "^3.0.14",
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/jscodeshift": "^0.11.11",
"@types/yargs": "^17.0.32",
"@wakaru/ast-utils": "workspace:*",
"@wakaru/test-utils": "workspace:*",
"@wakaru/unminify": "workspace:*",
"@wakaru/unpacker": "workspace:*",
"tsup": "^7.3.0",
"typescript": "^5.3.2"
},
"publishConfig": {
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts"
}
}
Loading