-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathuser.rs
More file actions
282 lines (242 loc) · 9.15 KB
/
user.rs
File metadata and controls
282 lines (242 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Configuration structures for user-defined tasks in `vite.config.*`
use std::{collections::HashMap, sync::Arc};
use monostate::MustBe;
use serde::Deserialize;
#[cfg(test)]
use ts_rs::TS;
use vite_path::RelativePathBuf;
use vite_str::Str;
/// Cache-related fields of a task defined by user in `vite.config.*`
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[cfg_attr(test, derive(TS), ts(optional_fields))]
#[serde(untagged, deny_unknown_fields, rename_all = "camelCase")]
pub enum UserCacheConfig {
/// Cache is enabled
Enabled {
/// Whether to cache the task
#[cfg_attr(test, ts(type = "true", optional))]
cache: Option<MustBe!(true)>,
#[serde(flatten)]
enabled_cache_config: EnabledCacheConfig,
},
/// Cache is disabled
Disabled {
/// Whether to cache the task
#[cfg_attr(test, ts(type = "false"))]
cache: MustBe!(false),
},
}
/// Cache configuration fields when caching is enabled
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[cfg_attr(test, derive(TS), ts(optional_fields))]
#[serde(rename_all = "camelCase")]
pub struct EnabledCacheConfig {
/// Environment variable names to be fingerprinted and passed to the task.
pub envs: Option<Box<[Str]>>,
/// Environment variable names to be passed to the task without fingerprinting.
pub pass_through_envs: Option<Vec<Str>>,
}
/// Options for user-defined tasks in `vite.config.*`, excluding the command.
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[cfg_attr(test, derive(TS), ts(optional_fields))]
#[serde(rename_all = "camelCase")]
pub struct UserTaskOptions {
/// The working directory for the task, relative to the package root (not workspace root).
#[serde(rename = "cwd")]
pub cwd_relative_to_package: Option<RelativePathBuf>,
/// Dependencies of this task. Use `package-name#task-name` to refer to tasks in other packages.
pub depends_on: Option<Arc<[Str]>>,
/// Cache-related fields
#[serde(flatten)]
pub cache_config: UserCacheConfig,
}
impl Default for UserTaskOptions {
/// The default user task options for package.json scripts.
fn default() -> Self {
Self {
// Runs in the package root
cwd_relative_to_package: None,
// No dependencies
depends_on: None,
// Caching enabled with no fingerprinted envs
cache_config: UserCacheConfig::Enabled {
cache: None,
enabled_cache_config: EnabledCacheConfig { envs: None, pass_through_envs: None },
},
}
}
}
/// Full user-defined task configuration in `vite.config.*`, including the command and options.
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[cfg_attr(test, derive(TS), ts(optional_fields, rename = "Task"))]
#[serde(rename_all = "camelCase")]
pub struct UserTaskConfig {
/// The command to run for the task.
///
/// If omitted, the script from `package.json` with the same name will be used
pub command: Option<Box<str>>,
/// Fields other than the command
#[serde(flatten)]
pub options: UserTaskOptions,
}
/// User configuration structure for `run` field in `vite.config.*`
#[derive(Debug, Default, Deserialize)]
#[cfg_attr(test, derive(TS), ts(optional_fields, rename = "RunConfig"))]
#[serde(rename_all = "camelCase")]
pub struct UserRunConfig {
/// Enable cache for all scripts from package.json.
///
/// This option can only be set in the workspace root's config file.
/// Setting it in a package's config will result in an error.
pub cache_scripts: Option<bool>,
/// Task definitions
pub tasks: Option<HashMap<Str, UserTaskConfig>>,
}
impl UserRunConfig {
/// TypeScript type definitions for user run configuration.
pub const TS_TYPE: &str = include_str!("../../run-config.ts");
/// Generates TypeScript type definitions for user task configuration.
#[cfg(test)]
pub fn generate_ts_definition() -> String {
use std::{
io::Write,
process::{Command, Stdio},
};
use ts_rs::TypeVisitor;
struct DeclCollector(Vec<String>);
impl TypeVisitor for DeclCollector {
fn visit<T: TS + 'static + ?Sized>(&mut self) {
// Only collect declarations from types that are exportable
// (i.e., have an output path - built-in types like HashMap don't)
if T::output_path().is_some() {
self.0.push(T::decl());
}
}
}
let mut collector = DeclCollector(Vec::new());
Self::visit_dependencies(&mut collector);
// Export all types
let mut types: String = collector
.0
.iter()
.map(|decl| vite_str::format!("export {decl}"))
.collect::<Vec<_>>()
.join("\n\n");
// Export the main type
types.push_str("\n\nexport ");
types.push_str(&Self::decl());
// Format using oxfmt from packages/tools
let workspace_root =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
let tools_path = workspace_root.join("packages/tools/node_modules/.bin");
let oxfmt_path = which::which_in("oxfmt", Some(&tools_path), &tools_path)
.expect("oxfmt not found in packages/tools");
let mut child = Command::new(oxfmt_path)
.arg("--stdin-filepath=run-config.ts")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("failed to spawn oxfmt");
child
.stdin
.take()
.unwrap()
.write_all(types.as_bytes())
.expect("failed to write to oxfmt stdin");
let output = child.wait_with_output().expect("failed to read oxfmt output");
assert!(output.status.success(), "oxfmt failed");
String::from_utf8(output.stdout).expect("oxfmt output is not valid UTF-8")
}
}
#[cfg(test)]
mod ts_tests {
use std::{env, path::PathBuf};
use super::UserRunConfig;
#[test]
fn typescript_generation() {
let file_path =
PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("run-config.ts");
let ts = UserRunConfig::generate_ts_definition().replace("\r", "");
if env::var("VT_UPDATE_TS_TYPES").unwrap_or_default() == "1" {
std::fs::write(&file_path, ts).unwrap();
} else {
let existing_ts =
std::fs::read_to_string(&file_path).unwrap_or_default().replace('\r', "");
pretty_assertions::assert_eq!(
ts,
existing_ts,
"Generated TypeScript types do not match the existing ones. If you made changes to the types, please set VT_UPDATE_TS_TYPES=1 and run the tests again to update the TypeScript definitions."
);
}
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn test_defaults() {
let user_config_json = json!({});
let user_config: UserTaskConfig = serde_json::from_value(user_config_json).unwrap();
assert_eq!(
user_config,
UserTaskConfig {
command: None,
// A empty task config (`{}`) should be equivalent to not specifying any config at all (just package.json script)
options: UserTaskOptions::default(),
}
);
}
#[test]
fn test_cwd_rename() {
let user_config_json = json!({
"cwd": "src"
});
let user_config: UserTaskConfig = serde_json::from_value(user_config_json).unwrap();
assert_eq!(user_config.options.cwd_relative_to_package.as_ref().unwrap().as_str(), "src");
}
#[test]
fn test_cache_disabled() {
let user_config_json = json!({
"cache": false
});
let user_config: UserTaskConfig = serde_json::from_value(user_config_json).unwrap();
assert_eq!(
user_config.options.cache_config,
UserCacheConfig::Disabled { cache: MustBe!(false) }
);
}
#[test]
fn test_cache_explicitly_enabled() {
let user_config_json = json!({
"cache": true,
"envs": ["NODE_ENV"],
"passThroughEnvs": ["FOO"],
});
assert_eq!(
serde_json::from_value::<UserCacheConfig>(user_config_json).unwrap(),
UserCacheConfig::Enabled {
cache: Some(MustBe!(true)),
enabled_cache_config: EnabledCacheConfig {
envs: Some(["NODE_ENV".into()].into_iter().collect()),
pass_through_envs: Some(["FOO".into()].into_iter().collect()),
}
},
);
}
#[test]
fn test_cache_disabled_but_with_fields() {
let user_config_json = json!({
"cache": false,
"envs": ["NODE_ENV"],
});
assert!(serde_json::from_value::<UserCacheConfig>(user_config_json).is_err());
}
#[test]
fn test_deny_unknown_field() {
let user_config_json = json!({
"foo": 42,
});
assert!(serde_json::from_value::<UserCacheConfig>(user_config_json).is_err());
}
}