wasm_pack/command/mod.rs
1//! CLI command structures, parsing, and execution.
2#![allow(clippy::redundant_closure)]
3
4pub mod build;
5mod generate;
6mod login;
7mod pack;
8/// Data structures and functions for publishing a package.
9pub mod publish;
10pub mod test;
11pub mod utils;
12
13use self::build::{Build, BuildOptions};
14use self::generate::generate;
15use self::login::login;
16use self::pack::pack;
17use self::publish::{access::Access, publish};
18use self::test::{Test, TestOptions};
19use crate::install::InstallMode;
20use anyhow::Result;
21use clap::Subcommand;
22use log::info;
23use std::path::PathBuf;
24/// The various kinds of commands that `wasm-pack` can execute.
25#[derive(Debug, Subcommand)]
26pub enum Command {
27 /// 🏗️ build your npm package!
28 #[clap(name = "build", alias = "init")]
29 Build(BuildOptions),
30
31 #[clap(name = "pack")]
32 /// 🍱 create a tar of your npm package but don't publish!
33 Pack {
34 #[clap(long = "pkg-dir", short = 'd', default_value = "pkg")]
35 /// The name of the output directory where the npm package is stored
36 pkg_directory: PathBuf,
37
38 /// The path to the Rust crate. If not set, searches up the path from the current directory.
39 #[clap()]
40 path: Option<PathBuf>,
41 },
42
43 #[clap(name = "new")]
44 /// 🐑 create a new project with a template
45 Generate {
46 /// The name of the project
47 name: String,
48 /// The URL to the template
49 #[clap(
50 long = "template",
51 default_value = "https://github.com/rustwasm/wasm-pack-template"
52 )]
53 template: String,
54 #[clap(long = "mode", short = 'm', default_value = "normal")]
55 /// Should we install or check the presence of binary tools. [possible values: no-install, normal, force]
56 mode: InstallMode,
57 },
58
59 #[clap(name = "publish")]
60 /// 🎆 pack up your npm package and publish!
61 Publish {
62 #[clap(long = "target", short = 't', default_value = "bundler")]
63 /// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
64 target: String,
65
66 /// The access level for the package to be published
67 #[clap(long = "access", short = 'a')]
68 access: Option<Access>,
69
70 /// The distribution tag being used for publishing.
71 /// See https://docs.npmjs.com/cli/dist-tag
72 #[clap(long = "tag")]
73 tag: Option<String>,
74
75 #[clap(long = "pkg-dir", short = 'd', default_value = "pkg")]
76 /// The name of the output directory where the npm package is stored
77 pkg_directory: PathBuf,
78
79 /// The path to the Rust crate. If not set, searches up the path from the current directory.
80 #[clap()]
81 path: Option<PathBuf>,
82 },
83
84 #[clap(name = "login", alias = "adduser", alias = "add-user")]
85 /// 👤 Add an npm registry user account! (aliases: adduser, add-user)
86 Login {
87 #[clap(long = "registry", short = 'r')]
88 /// Default: 'https://registry.npmjs.org/'.
89 /// The base URL of the npm package registry. If scope is also
90 /// specified, this registry will only be used for packages with that
91 /// scope. scope defaults to the scope of the project directory you're
92 /// currently in, if any.
93 registry: Option<String>,
94
95 #[clap(long = "scope", short = 's')]
96 /// Default: none.
97 /// If specified, the user and login credentials given will be
98 /// associated with the specified scope.
99 scope: Option<String>,
100
101 #[clap(long = "auth-type", short = 't')]
102 /// Default: 'legacy'.
103 /// Type: 'legacy', 'sso', 'saml', 'oauth'.
104 /// What authentication strategy to use with adduser/login. Some npm
105 /// registries (for example, npmE) might support alternative auth
106 /// strategies besides classic username/password entry in legacy npm.
107 auth_type: Option<String>,
108 },
109
110 #[clap(name = "test")]
111 /// 👩🔬 test your wasm!
112 Test(TestOptions),
113}
114
115/// Run a command with the given logger!
116pub fn run_wasm_pack(command: Command) -> Result<()> {
117 // Run the correct command based off input and store the result of it so that we can clear
118 // the progress bar then return it
119 match command {
120 Command::Build(build_opts) => {
121 info!("Running build command...");
122 Build::try_from_opts(build_opts).and_then(|mut b| b.run())
123 }
124 Command::Pack {
125 path,
126 pkg_directory,
127 } => {
128 info!("Running pack command...");
129 info!("Path: {:?}", &path);
130 pack(path, pkg_directory)
131 }
132 Command::Generate {
133 template,
134 name,
135 mode,
136 } => {
137 info!("Running generate command...");
138 info!("Template: {:?}", &template);
139 info!("Name: {:?}", &name);
140 generate(template, name, mode.install_permitted())
141 }
142 Command::Publish {
143 target,
144 path,
145 access,
146 tag,
147 pkg_directory,
148 } => {
149 info!("Running publish command...");
150 info!("Path: {:?}", &path);
151 publish(&target, path, access, tag, pkg_directory)
152 }
153 Command::Login {
154 registry,
155 scope,
156 auth_type,
157 } => {
158 info!("Running login command...");
159 info!(
160 "Registry: {:?}, Scope: {:?}, Auth Type: {:?}",
161 ®istry, &scope, &auth_type
162 );
163 login(registry, &scope, &auth_type)
164 }
165 Command::Test(test_opts) => {
166 info!("Running test command...");
167 Test::try_from_opts(test_opts).and_then(|t| t.run())
168 }
169 }
170}