cmd(init/build): deprecate init

master
csmoe 7 years ago
parent ddab496f51
commit 022b787a2f
  1. 2
      README.md
  2. 2
      docs/init.md
  3. 138
      src/command/init.rs
  4. 12
      src/command/mod.rs
  5. 1
      src/logger.rs
  6. 6
      src/main.rs
  7. 10
      tests/all/manifest.rs

@ -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

@ -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

@ -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<String>,
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<PathBuf>,
/// The npm scope to use in package.json, if any.
#[structopt(long = "scope", short = "s")]
pub scope: Option<String>,
#[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<InitOptions> 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(())
}
}

@ -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() {

@ -33,7 +33,6 @@ pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, Error> {
/// 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,

@ -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)?;

@ -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());

Loading…
Cancel
Save