diff --git a/README.md b/README.md index e26e08ea..d638d2b1 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ visiting that repo! ## 🎙️ Commands -- [`init`](docs/init.md): Initialize an npm wasm pkg from a rustwasm crate +- [`init`](docs/init.md): [**Deprecated**] Initialize an npm wasm pkg from a rustwasm crate - [`build`](docs/build.md): Generate an npm wasm pkg from a rustwasm crate - [`pack`](docs/pack.md): Create a tarball of your rustwasm pkg - [`publish`](docs/publish.md): Publish your rustwasm pkg to a registry diff --git a/docs/init.md b/docs/init.md index 62cf418a..02d9924a 100644 --- a/docs/init.md +++ b/docs/init.md @@ -1,4 +1,4 @@ -# wasm-pack init +# wasm-pack init(Deprecated) The `wasm-pack init` command creates the files neccessary for JavaScript interoperability and for publishing a package to npm. This involves diff --git a/src/command/init.rs b/src/command/init.rs deleted file mode 100644 index 29ad44ec..00000000 --- a/src/command/init.rs +++ /dev/null @@ -1,138 +0,0 @@ -//! Initializing a crate for packing `.wasm`s. - -use command::utils::{create_pkg_dir, set_crate_path}; -use emoji; -use error::Error; -use indicatif::HumanDuration; -use manifest; -use progressbar::Step; -use readme; -use slog::Logger; -use std::path::PathBuf; -use std::time::Instant; -use PBAR; - -/// Everything required to configure and run the `wasm-pack init` command. -pub struct Init { - crate_path: PathBuf, - scope: Option, - disable_dts: bool, - target: String, -} - -/// `Init` options -#[derive(Debug, StructOpt)] -pub struct InitOptions { - /// The path to the Rust crate. - #[structopt(parse(from_os_str))] - pub path: Option, - - /// The npm scope to use in package.json, if any. - #[structopt(long = "scope", short = "s")] - pub scope: Option, - - #[structopt(long = "no-typescript")] - /// By default a *.d.ts file is generated for the generated JS file, but - /// this flag will disable generating this TypeScript file. - pub disable_dts: bool, - - #[structopt(long = "target", short = "t", default_value = "browser")] - /// Sets the target environment. [possible values: browser, nodejs] - pub target: String, -} - -impl From for Init { - fn from(init_opts: InitOptions) -> Self { - let crate_path = set_crate_path(init_opts.path); - Init { - crate_path, - scope: init_opts.scope, - disable_dts: init_opts.disable_dts, - target: init_opts.target, - } - } -} - -type InitStep = fn(&mut Init, &Step, &Logger) -> Result<(), Error>; - -impl Init { - /// Execute this `Init` command. - pub fn run(&mut self, log: &Logger) -> Result<(), Error> { - let process_steps = Init::set_process_steps(); - - let mut step_counter = Step::new(process_steps.len()); - - let started = Instant::now(); - - for (_, process_step) in process_steps { - process_step(self, &step_counter, log)?; - step_counter.inc(); - } - - let duration = HumanDuration(started.elapsed()); - info!(&log, "Done in {}.", &duration); - info!( - &log, - "Your WASM pkg is ready to publish at {:#?}.", - &self.crate_path.join("pkg") - ); - - PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration)); - - PBAR.message(&format!( - "{} Your WASM pkg is ready to publish at {:#?}.", - emoji::PACKAGE, - &self.crate_path.join("pkg") - )); - Ok(()) - } - - fn set_process_steps() -> Vec<(&'static str, InitStep)> { - macro_rules! steps { - ($($name:ident),+) => { - { - let mut steps: Vec<(&'static str, InitStep)> = Vec::new(); - $(steps.push((stringify!($name), Init::$name));)* - steps - } - }; - ($($name:ident,)*) => (steps![$($name),*]) - } - steps![step_create_dir, step_create_json, step_copy_readme,] - } - - fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Creating a pkg directory..."); - create_pkg_dir(&self.crate_path, step)?; - info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path); - Ok(()) - } - - fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Writing a package.json..."); - manifest::write_package_json( - &self.crate_path, - &self.scope, - self.disable_dts, - &self.target, - step, - )?; - info!( - &log, - "Wrote a package.json at {:#?}.", - &self.crate_path.join("pkg").join("package.json") - ); - Ok(()) - } - - fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Copying readme from crate..."); - readme::copy_from_crate(&self.crate_path, step)?; - info!( - &log, - "Copied readme from crate to {:#?}.", - &self.crate_path.join("pkg") - ); - Ok(()) - } -} diff --git a/src/command/mod.rs b/src/command/mod.rs index 360c7718..9b20672c 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -1,14 +1,12 @@ //! CLI command structures, parsing, and execution. mod build; -pub mod init; mod login; mod pack; mod publish; pub mod utils; use self::build::{Build, BuildMode, BuildOptions}; -use self::init::{Init, InitOptions}; use self::login::login; use self::pack::pack; use self::publish::publish; @@ -21,12 +19,8 @@ use PBAR; /// The various kinds of commands that `wasm-pack` can execute. #[derive(Debug, StructOpt)] pub enum Command { - #[structopt(name = "init")] - /// 🐣 initialize a package.json based on your compiled wasm! - Init(InitOptions), - /// 🏗️ build your npm package! - #[structopt(name = "build")] + #[structopt(name = "build", alias = "init")] Build(BuildOptions), #[structopt(name = "pack")] @@ -83,10 +77,6 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error // Run the correct command based off input and store the result of it so that we can clear // the progress bar then return it let status = match command { - Command::Init(init_opts) => { - info!(&log, "Running init command..."); - Init::from(init_opts).run(&log) - } Command::Build(build_opts) => { info!(&log, "Running build command..."); let build_mode = match build_opts.mode.as_str() { diff --git a/src/logger.rs b/src/logger.rs index 1ceae491..d100ccf7 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -33,7 +33,6 @@ pub fn new(cmd: &Command, verbosity: u8) -> Result { /// Figure out where to stick the log based off the command arguments given fn log_file_path(cmd: &Command) -> PathBuf { let path = match cmd { - Command::Init(init_opts) => &init_opts.path, Command::Build(build_opts) => &build_opts.path, Command::Pack { path } => path, Command::Publish { path } => path, diff --git a/src/main.rs b/src/main.rs index 68e6e4b2..0617f116 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate structopt; extern crate wasm_pack; use failure::Fail; +use std::env; use structopt::StructOpt; use wasm_pack::{command::run_wasm_pack, error::Error, logger, Cli}; @@ -20,6 +21,11 @@ fn main() { } fn run() -> Result<(), Error> { + // Deprecate `init` + if let Some("init") = env::args().nth(1).as_ref().map(|arg| arg.as_str()) { + println!("wasm-pack init is deprecated, consider using wasm-pack build"); + } + let args = Cli::from_args(); let log = logger::new(&args.cmd, args.verbosity)?; run_wasm_pack(args.cmd, &log)?; diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 45f046e0..14cbbf5d 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -55,7 +55,7 @@ fn it_recognizes_a_map_during_depcheck() { fn it_creates_a_package_json_default_path() { let fixture = utils::fixture("."); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -83,7 +83,7 @@ fn it_creates_a_package_json_default_path() { fn it_creates_a_package_json_provided_path() { let fixture = utils::fixture("tests/fixtures/js-hello-world"); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -104,7 +104,7 @@ fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path_with_scope() { let fixture = utils::fixture("tests/fixtures/scopes"); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); assert!( manifest::write_package_json(&fixture.path, &Some("test".to_string()), false, "", &step) .is_ok() @@ -128,7 +128,7 @@ fn it_creates_a_package_json_provided_path_with_scope() { fn it_creates_a_pkg_json_with_correct_files_on_node() { let fixture = utils::fixture("."); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); assert!(manifest::write_package_json(&fixture.path, &None, false, "nodejs", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -157,7 +157,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let fixture = utils::fixture("."); let step = wasm_pack::progressbar::Step::new(1); - wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); assert!(manifest::write_package_json(&fixture.path, &None, true, "", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok());