From 908ba21682b0144b0f39b8e5872e9b60005e5148 Mon Sep 17 00:00:00 2001 From: lencx Date: Thu, 3 Mar 2022 22:44:22 +0800 Subject: [PATCH] fix: crate root --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- README.md | 18 +----------------- README.zh_CN.md | 15 +-------------- npm/README.md | 13 +++++++++++++ npm/package.json | 2 +- src/core/build.rs | 14 ++++++++++---- src/core/cli.rs | 14 ++++++++++---- src/core/link.rs | 2 +- src/utils.rs | 4 ++-- 10 files changed, 44 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c0f60..1f94b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.7.1 + +- fix: crate root + ## 0.7.0 - add `.rsw/rsw.crates` diff --git a/Cargo.toml b/Cargo.toml index 5a32866..31ef957 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsw" -version = "0.7.0" +version = "0.7.1" description = "wasm-pack based build tool" edition = "2021" authors = ["lencx "] diff --git a/README.md b/README.md index 6b1bf27..929b00d 100644 --- a/README.md +++ b/README.md @@ -8,22 +8,6 @@ ## rsw-rs -### Feature - -- rsw init -- rsw watch -- rsw build -- rsw new - - `wasm-pack` - - `rsw` - - `user` -- debug info - -### TODO - -- local sub-dependency file changes trigger hot updates -- integrate front-end scaffolding, such as `vite`, `webpack`, etc. - ## Pre-installed - [rust](https://www.rust-lang.org/learn/get-started) @@ -62,9 +46,9 @@ rsw clean ## Awesome rsw +- [[rsw demo] learn-wasm](https://github.com/lencx/learn-wasm) - 🎲 Learning WebAssembly - [vite-plugin-rsw](https://github.com/lencx/vite-plugin-rsw) - 🦀 wasm-pack plugin for Vite - [create-mpl](https://github.com/lencx/create-mpl) - ⚡️ Create a project in seconds! -- [learn-wasm](https://github.com/lencx/learn-wasm) - 🎲 Learning WebAssembly ## Logger diff --git a/README.zh_CN.md b/README.zh_CN.md index bc791d9..78ffcb1 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -8,19 +8,6 @@ ## rsw-rs -### 功能 - -- `rsw init` - 生成配置文件 `rsw.toml` -- `rsw build` - 基于 `rsw.toml` 配置同时构建多个 `rust crate` -- `rsw watch` - 基于 `rsw.toml` 配置同时监听多个 `rust crate` 中的文件变更,自动触发构建 -- `rsw new` - 基于 `rsw.toml` `[new]` 字段配置,默认使用 `wasm-pack` 创建项目 -- `RUST_LOG=rsw rsw ` - 输出关键日志信息,便于错误排查 - -## TODO - -- 本地依赖变更触发热更新 -- 集成前端脚手架,如 `vite`,`webpack` 等 - ## 预安装 - [rust](https://www.rust-lang.org/learn/get-started) @@ -59,9 +46,9 @@ rsw clean ## Awesome rsw +- [[rsw demo] learn-wasm](https://github.com/lencx/learn-wasm) - 🎲 Learning WebAssembly - [vite-plugin-rsw](https://github.com/lencx/vite-plugin-rsw) - 🦀 wasm-pack plugin for Vite - [create-mpl](https://github.com/lencx/create-mpl) - ⚡️ Create a project in seconds! -- [learn-wasm](https://github.com/lencx/learn-wasm) - 🎲 Learning WebAssembly ## 日志 diff --git a/npm/README.md b/npm/README.md index def4967..c024909 100644 --- a/npm/README.md +++ b/npm/README.md @@ -6,6 +6,19 @@ - [nodejs](https://nodejs.org) - [wasm-pack](https://github.com/rustwasm/wasm-pack) +## Quick start + +```bash +# https://github.com/lencx/create-mpl +# npm 6.x +npm init mpl@latest my-app --type wasm + +# npm 7+, extra double-dash is needed: +npm init mpl@latest my-app -- --type wasm +``` + +--- + ## Usgae ```bash diff --git a/npm/package.json b/npm/package.json index 0a88feb..2fb38df 100644 --- a/npm/package.json +++ b/npm/package.json @@ -1,6 +1,6 @@ { "name": "@rsw/cli", - "version": "0.7.0", + "version": "0.7.1", "description": "🦞 wasm-pack based build tool", "main": "binary.js", "author": "lencx ", diff --git a/src/core/build.rs b/src/core/build.rs index 4a1bda4..c989366 100644 --- a/src/core/build.rs +++ b/src/core/build.rs @@ -27,9 +27,14 @@ impl Build { let config = &self.config; let rsw_type = &self.rsw_type; let name = &config.name; + let root = config.root.as_ref().unwrap(); let out_dir = config.out_dir.as_ref().unwrap(); + let crate_root = PathBuf::from(root) + .join(name) + .canonicalize().unwrap(); + let build_name = crate_root.to_string_lossy().to_string(); let target = config.target.as_ref().unwrap(); - let mut args = vec!["build", name, "--out-dir", out_dir, "--target", target]; + let mut args = vec!["build", &build_name, "--out-dir", out_dir, "--target", target]; // profile let mut profile = config.build.as_ref().unwrap().profile.as_ref().unwrap(); @@ -46,7 +51,7 @@ impl Build { args.push(scope.as_str()); } - let metadata = get_crate_metadata(name.as_str()); + let metadata = get_crate_metadata(name, crate_root); info!("🚧 wasm-pack {}", args.join(" ")); let status = Command::new("wasm-pack") @@ -88,12 +93,13 @@ impl Build { None => {} } - // TODO: link if config.link.unwrap() { let cli = &self.cli; Link::new( cli.into(), - PathBuf::from(name).join(out_dir), + PathBuf::from(root) + .join(name) + .join(out_dir), name.to_string(), ) .init(); diff --git a/src/core/cli.rs b/src/core/cli.rs index d7e35ab..55bf60c 100644 --- a/src/core/cli.rs +++ b/src/core/cli.rs @@ -7,8 +7,8 @@ use std::path::PathBuf; use std::rc::Rc; use crate::config::{CrateConfig, RswConfig}; -use crate::core::{Build, Clean, Create, Init, Link, Watch}; -use crate::utils::{init_rsw_crates, rsw_watch_file}; +use crate::core::{Build, Clean, Create, Init, Link, Watch, RswErr}; +use crate::utils::{init_rsw_crates, rsw_watch_file, print}; #[derive(Parser)] #[clap(version, about, long_about = None)] @@ -104,11 +104,18 @@ impl Cli { let mut crates = Vec::new(); for i in &config.crates { let name = &i.name; + let root = i.root.as_ref().unwrap(); let out = i.out_dir.as_ref().unwrap(); + let crate_out = PathBuf::from(root) + .join(name).join(out); + crates.push(format!( "{} :~> {}", name, - PathBuf::from(name).join(out).to_string_lossy().to_string() + crate_out.canonicalize().unwrap_or_else(|e| { + print(RswErr::Crate(crate_out.to_string_lossy().to_string(), e)); + std::process::exit(1); + }).to_string_lossy().to_string() )); } init_rsw_crates(crates.join("\n").as_bytes()).unwrap(); @@ -116,7 +123,6 @@ impl Cli { config } pub fn wp_build(config: Rc, rsw_type: &str) { - // let crates_map = Rc::new(RefCell::new(HashMap::new())); let crates_map = Rc::new(RefCell::new(HashMap::new())); let cli = &config.cli.to_owned().unwrap(); diff --git a/src/core/link.rs b/src/core/link.rs index 91b1aaa..163f212 100644 --- a/src/core/link.rs +++ b/src/core/link.rs @@ -32,7 +32,7 @@ impl Link { pub fn yarn_link(&self) { // register package - // 1. cd + // 1. cd / // 2. yarn link Command::new(&self.cli) .current_dir(&self.cwd) diff --git a/src/utils.rs b/src/utils.rs index 2c2de9e..9a8a264 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -25,8 +25,8 @@ pub fn check_env_cmd(program: &str) -> bool { } // get fields from `Cargo.toml` -pub fn get_crate_metadata(name: &str) -> Value { - let crate_root = env::current_dir().unwrap().join(name).join("Cargo.toml"); +pub fn get_crate_metadata(name: &str, root: PathBuf) -> Value { + let crate_root = root.join("Cargo.toml"); let content = fs::read_to_string(crate_root).unwrap_or_else(|e| { // TODO: create crate print(RswErr::Crate(name.to_string(), e));