refactor(cli): Make CLI testable/managable with swc-bump (#8045)

**Description:**

It was hard to maintain CLI because actual logic was in `./bindings`.
This commit is contained in:
Donny/강동윤 2023-10-03 00:46:14 +09:00 committed by GitHub
parent 2bf935c68b
commit f717cf21cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 234 additions and 105 deletions

View File

@ -244,12 +244,18 @@ jobs:
- crate: swc_cached
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_cli_impl
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_common
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_common
os: windows-latest
runner: windows-latest
- crate: swc_compiler_base
os: ubuntu-latest
runner: ubuntu-latest
- crate: swc_config
os: ubuntu-latest
runner: ubuntu-latest
@ -590,13 +596,6 @@ jobs:
run: |
jest --version && mocha --version
- name: Cache rust artifacts
uses: Swatinem/rust-cache@v2
if: matrix.settings.crate == 'swc' || matrix.settings.crate == 'swc_plugin_runner'
with:
shared-key: cargo-test-${{ matrix.settings.crate }}
cache-on-failure: true
- name: Configure execution cache
shell: bash
run: |
@ -711,14 +710,6 @@ jobs:
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
cd bindings && cargo update -p swc_core
- name: Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/
**/target/
key: ${{ runner.os }}-node-api-test
- name: Set platform name
run: |
export NODE_PLATFORM_NAME=$(node -e "console.log(require('os').platform())")
@ -749,14 +740,6 @@ jobs:
node-version: 14
cache: "yarn"
- name: Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/
**/target/
key: ${{ runner.os }}-cargo-integration
- name: Set platform name
run: |
export NODE_PLATFORM_NAME=$(node -e "console.log(require('os').platform())")
@ -833,3 +816,20 @@ jobs:
- name: (swcpack) example react app
run: |
(cd crates/swc_node_bundler/tests/integration/react && yarn && npx spack)
done:
needs:
- cargo-fmt
- cargo-clippy
- cargo-deny
- cargo-check
- test-wasm
- cargo-test
- node-test
- integration-test
if: always()
runs-on: ubuntu-latest
name: Done
steps:
- run: exit 1
if: ${{ always() && (contains(needs.*.result, 'failure') || contains(needs.*.result, 'skipped') || contains(needs.*.result, 'cancelled')) }}

123
Cargo.lock generated
View File

@ -460,19 +460,20 @@ dependencies = [
[[package]]
name = "clap"
version = "3.1.0"
version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5f1fea81f183005ced9e59cdb01737ef2423956dac5a6d731b06b2ecfaa3467"
checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
dependencies = [
"atty",
"bitflags 1.3.2",
"clap_derive",
"clap_lex 0.2.4",
"indexmap",
"lazy_static",
"os_str_bytes",
"once_cell",
"strsim",
"termcolor",
"textwrap",
"terminal_size 0.2.6",
"textwrap 0.16.0",
]
[[package]]
@ -492,14 +493,14 @@ checksum = "2e53afce1efce6ed1f633cf0e57612fe51db54a1ee4fd8f8503d078fe02d69ae"
dependencies = [
"anstyle",
"bitflags 1.3.2",
"clap_lex",
"clap_lex 0.5.0",
]
[[package]]
name = "clap_derive"
version = "3.1.0"
version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fd1122e63869df2cb309f449da1ad54a7c6dfeb7c7e6ccd8e0825d9eb93bb72"
checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008"
dependencies = [
"heck 0.4.0",
"proc-macro-error",
@ -508,6 +509,15 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "clap_lex"
version = "0.5.0"
@ -533,7 +543,7 @@ dependencies = [
"encode_unicode",
"libc",
"once_cell",
"terminal_size",
"terminal_size 0.1.17",
"unicode-width",
"winapi",
]
@ -967,7 +977,7 @@ name = "dbg-swc"
version = "0.87.46"
dependencies = [
"anyhow",
"clap 3.1.0",
"clap 3.2.25",
"dialoguer",
"flate2",
"rayon",
@ -1761,6 +1771,12 @@ dependencies = [
"testing",
]
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]]
name = "jsonc-parser"
version = "0.21.0"
@ -2009,8 +2025,8 @@ dependencies = [
"supports-color",
"supports-hyperlinks",
"supports-unicode",
"terminal_size",
"textwrap",
"terminal_size 0.1.17",
"textwrap 0.14.2",
"thiserror",
"unicode-width",
]
@ -2381,9 +2397,6 @@ name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
[[package]]
name = "output_vt100"
@ -2429,12 +2442,30 @@ dependencies = [
"windows-targets",
]
[[package]]
name = "path-absolutize"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4af381fe79fa195b4909485d99f73a80792331df0625188e707854f0b3383f5"
dependencies = [
"path-dedot",
]
[[package]]
name = "path-clean"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecba01bf2678719532c5e3059e0b5f0811273d94b397088b82e3bd0a78c78fdd"
[[package]]
name = "path-dedot"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07ba0ad7e047712414213ff67533e6dd477af0a4e1d14fb52343e53d30ea9397"
dependencies = [
"once_cell",
]
[[package]]
name = "pathdiff"
version = "0.2.1"
@ -3649,6 +3680,28 @@ dependencies = [
"serde",
]
[[package]]
name = "swc_cli_impl"
version = "0.1.0"
dependencies = [
"anyhow",
"atty",
"clap 3.2.25",
"glob",
"path-absolutize",
"rayon",
"relative-path",
"serde",
"serde_json",
"sourcemap",
"swc_core",
"tracing",
"tracing-chrome",
"tracing-futures",
"tracing-subscriber",
"walkdir",
]
[[package]]
name = "swc_common"
version = "0.32.1"
@ -5057,6 +5110,16 @@ dependencies = [
"winapi",
]
[[package]]
name = "terminal_size"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e6bf6f19e9f8ed8d4048dc22981458ebcf406d67e94cd422e5ecd73d63b3237"
dependencies = [
"rustix",
"windows-sys 0.48.0",
]
[[package]]
name = "termios"
version = "0.3.3"
@ -5110,6 +5173,15 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "textwrap"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
dependencies = [
"terminal_size 0.2.6",
]
[[package]]
name = "thiserror"
version = "1.0.40"
@ -5363,6 +5435,17 @@ dependencies = [
"syn 2.0.37",
]
[[package]]
name = "tracing-chrome"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb13184244c7cd22758b79e7c993c515ad67a8e730edcb7e05fe7bcabb283c7"
dependencies = [
"json",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-core"
version = "0.1.31"
@ -5373,6 +5456,16 @@ dependencies = [
"valuable",
]
[[package]]
name = "tracing-futures"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2"
dependencies = [
"pin-project",
"tracing",
]
[[package]]
name = "tracing-log"
version = "0.1.3"

View File

@ -1,6 +1,7 @@
[workspace]
members = [
"crates/swc_core",
"crates/swc_cli_impl",
"crates/dbg-swc",
"crates/jsdoc",
"crates/binding_macros",

30
bindings/Cargo.lock generated
View File

@ -443,9 +443,9 @@ dependencies = [
[[package]]
name = "clap"
version = "3.2.23"
version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5"
checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
dependencies = [
"atty",
"bitflags 1.3.2",
@ -461,9 +461,9 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "3.2.18"
version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008"
dependencies = [
"heck 0.4.1",
"proc-macro-error",
@ -1832,9 +1832,9 @@ checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "os_str_bytes"
version = "6.5.0"
version = "6.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267"
checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac"
[[package]]
name = "overload"
@ -1873,9 +1873,9 @@ dependencies = [
[[package]]
name = "path-absolutize"
version = "3.0.14"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f1d4993b16f7325d90c18c3c6a3327db7808752db8d208cea0acee0abd52c52"
checksum = "e4af381fe79fa195b4909485d99f73a80792331df0625188e707854f0b3383f5"
dependencies = [
"path-dedot",
]
@ -1888,9 +1888,9 @@ checksum = "ecba01bf2678719532c5e3059e0b5f0811273d94b397088b82e3bd0a78c78fdd"
[[package]]
name = "path-dedot"
version = "3.0.18"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a81540d94551664b72b72829b12bd167c73c9d25fbac0e04fafa8023f7e4901"
checksum = "07ba0ad7e047712414213ff67533e6dd477af0a4e1d14fb52343e53d30ea9397"
dependencies = [
"once_cell",
]
@ -2954,6 +2954,16 @@ dependencies = [
[[package]]
name = "swc_cli"
version = "0.91.80"
dependencies = [
"anyhow",
"swc_cli_impl",
]
[[package]]
name = "swc_cli_impl"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be08b334a3aedb93d67d50ec07ab4cb2f82930d5f82e3dbe24bb7beaf4f9b5c0"
dependencies = [
"anyhow",
"atty",

View File

@ -14,33 +14,14 @@ name = "swc"
path = "./src/main.rs"
[features]
default = []
default = [
"swc_cli_impl/default"
]
plugin = [
"swc_core/plugin_transform_host_native",
"swc_core/plugin_transform_host_native_filesystem_cache",
"swc_cli_impl/plugin"
]
[dependencies]
anyhow = "1.0.66"
atty = "0.2.14"
clap = { version = "3.1.0", features = ["derive", "wrap_help"] }
glob = "0.3.0"
rayon = "1"
relative-path = "1.6.1"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["unbounded_depth"] }
sourcemap = "6.2.2"
swc_core = { version = "0.83.34", features = [
"trace_macro",
"common_concurrent",
"base_concurrent",
] }
tracing = "0.1.37"
tracing-chrome = "0.5.0"
tracing-futures = "0.2.5"
tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
walkdir = "2"
swc_cli_impl = "0.1.0"
[dependencies.path-absolutize]
features = ["once_cell_cache"]
version = "3.0.11"

View File

@ -1,17 +1,3 @@
use clap::Parser;
use commands::{Command, CommandRunner, PluginSubcommand, SwcCliOptions};
mod commands;
mod util;
fn main() -> anyhow::Result<()> {
let command = SwcCliOptions::parse().command;
match &command {
Command::Plugin(PluginSubcommand::New(options)) => options.execute(),
Command::Compile(options) => options.execute(),
Command::Minify(options) => options.execute(),
Command::Bundle(options) => options.execute(),
Command::Lint(options) => options.execute(),
}
swc_cli_impl::run()
}

View File

@ -0,0 +1,39 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>", "OJ Kwon <kwon.ohjoong@gmail.com>"]
description = "Commandline for SWC (Internal crate with implementation)"
edition = "2021"
include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0"
name = "swc_cli_impl"
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0"
[features]
default = []
plugin = [
"swc_core/plugin_transform_host_native",
"swc_core/plugin_transform_host_native_filesystem_cache",
]
[dependencies]
anyhow = "1.0.66"
atty = "0.2.14"
clap = { version = "3.2.25", features = ["derive", "wrap_help"] }
glob = "0.3.0"
path-absolutize = { version = "3.0.11", features = ["once_cell_cache"] }
rayon = "1"
relative-path = "1.6.1"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["unbounded_depth"] }
sourcemap = "6.2.2"
tracing = "0.1.37"
tracing-chrome = "0.5.0"
tracing-futures = "0.2.5"
tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
walkdir = "2"
swc_core = { version = "0.83.34", features = [
"trace_macro",
"common_concurrent",
"base_concurrent",
], path = "../swc_core" }

View File

@ -62,7 +62,7 @@ fn get_name(option: &PluginScaffoldOptions) -> Result<&str> {
/// Note: this is slim implementation does not support different vcs other than
/// git at the moment.
fn write_ignore_file(base_path: &Path) -> Result<()> {
let ignore_list: Vec<String> = vec!["/target", "^target/", "target"]
let ignore_list: Vec<String> = ["/target", "^target/", "target"]
.iter()
.map(|v| v.to_string())
.collect();
@ -149,7 +149,7 @@ impl super::CommandRunner for PluginScaffoldOptions {
// Create `Cargo.toml` file with necessary sections
fs::write(
&path.join("Cargo.toml"),
path.join("Cargo.toml"),
format!(
r#"[package]
name = "{}"
@ -190,7 +190,7 @@ swc_core = {{ version = "{}", features = ["ecma_plugin_transform"] }}
let cargo_config_path = path.join(".cargo");
create_dir_all(&cargo_config_path).context("`create_dir_all` failed")?;
fs::write(
&cargo_config_path.join("config"),
cargo_config_path.join("config"),
r#"# These command aliases are not final, may change
[alias]
# Alias to build actual plugin binary for the specified target.
@ -205,10 +205,10 @@ build-wasm32 = "build --target wasm32-unknown-unknown"
let dist_output_path = format!(
"target/{}/release/{}.wasm",
build_target,
name.replace("-", "_")
name.replace('-', "_")
);
fs::write(
&path.join("package.json"),
path.join("package.json"),
format!(
r#"{{
"name": "{}",
@ -235,7 +235,7 @@ build-wasm32 = "build --target wasm32-unknown-unknown"
let src_path = path.join("src");
create_dir_all(&src_path)?;
fs::write(
&src_path.join("lib.rs"),
src_path.join("lib.rs"),
r##"use swc_core::ecma::{
ast::Program,
transforms::testing::test,

View File

@ -0,0 +1,17 @@
use clap::Parser;
use commands::{Command, CommandRunner, PluginSubcommand, SwcCliOptions};
mod commands;
mod util;
pub fn run() -> anyhow::Result<()> {
let command = SwcCliOptions::parse().command;
match &command {
Command::Plugin(PluginSubcommand::New(options)) => options.execute(),
Command::Compile(options) => options.execute(),
Command::Minify(options) => options.execute(),
Command::Bundle(options) => options.execute(),
Command::Lint(options) => options.execute(),
}
}

View File

@ -13,20 +13,22 @@ version = "0.1.5"
node = ["napi", "napi-derive"]
[dependencies]
anyhow = "1.0.75"
base64 = "0.13.0"
pathdiff = "0.2.0"
serde = { version = "1.0.188", features = ["derive"] }
sourcemap = "6.2"
swc_atoms = { version = "0.5.9", path = "../swc_atoms" }
swc_common = { version = "0.32.1", path = "../swc_common" }
swc_config = { version = "0.1.7", path = "../swc_config" }
swc_ecma_ast = { version = "0.109.1", path = "../swc_ecma_ast" }
anyhow = "1.0.75"
base64 = "0.13.0"
pathdiff = "0.2.0"
serde = { version = "1.0.188", features = ["derive"] }
sourcemap = "6.2"
swc_atoms = { version = "0.5.9", path = "../swc_atoms" }
swc_common = { version = "0.32.1", path = "../swc_common", features = [
"sourcemap",
] }
swc_config = { version = "0.1.7", path = "../swc_config" }
swc_ecma_ast = { version = "0.109.1", path = "../swc_ecma_ast" }
swc_ecma_codegen = { version = "0.145.5", path = "../swc_ecma_codegen" }
swc_ecma_minifier = { version = "0.187.26", path = "../swc_ecma_minifier" }
swc_ecma_parser = { version = "0.140.0", path = "../swc_ecma_parser" }
swc_ecma_visit = { version = "0.95.1", path = "../swc_ecma_visit" }
swc_timer = { version = "0.20.1", path = "../swc_timer" }
swc_ecma_parser = { version = "0.140.0", path = "../swc_ecma_parser" }
swc_ecma_visit = { version = "0.95.1", path = "../swc_ecma_visit" }
swc_timer = { version = "0.20.1", path = "../swc_timer" }
[dependencies.napi-derive]
default-features = false