@@ -75,11 +75,17 @@ fn deps_fields_equal(pkg_field: Option<&Value>, lock_field: Option<&Value>) -> b
75
75
76
76
pub async fn ensure_package_lock ( root_path : & Path ) -> Result < ( ) > {
77
77
// check package.json exists in cwd
78
- if fs:: metadata ( root_path. join ( "package.json" ) ) . is_err ( ) {
78
+ if tokio:: fs:: metadata ( root_path. join ( "package.json" ) )
79
+ . await
80
+ . is_err ( )
81
+ {
79
82
return Err ( anyhow ! ( "package.json not found" ) ) ;
80
83
}
81
84
// check package-lock.json exists in cwd
82
- if fs:: metadata ( root_path. join ( "package-lock.json" ) ) . is_err ( ) {
85
+ if tokio:: fs:: metadata ( root_path. join ( "package-lock.json" ) )
86
+ . await
87
+ . is_err ( )
88
+ {
83
89
log_info ( "Resolving dependencies" ) ;
84
90
build_deps ( root_path) . await ?;
85
91
Ok ( ( ) )
@@ -124,7 +130,8 @@ pub async fn update_package_json(
124
130
125
131
// 3. Read package.json once
126
132
let package_json_path = target_dir. join ( "package.json" ) ;
127
- let mut package_json: Value = serde_json:: from_reader ( fs:: File :: open ( & package_json_path) ?) ?;
133
+ let package_json_content = tokio:: fs:: read_to_string ( & package_json_path) . await ?;
134
+ let mut package_json: Value = serde_json:: from_str ( & package_json_content) ?;
128
135
129
136
let dep_field = match save_type {
130
137
SaveType :: Dev => "devDependencies" ,
@@ -161,10 +168,11 @@ pub async fn update_package_json(
161
168
}
162
169
163
170
// 6. Write back to package.json once
164
- fs:: write (
171
+ tokio :: fs:: write (
165
172
& package_json_path,
166
173
serde_json:: to_string_pretty ( & package_json) ?,
167
- ) ?;
174
+ )
175
+ . await ?;
168
176
169
177
Ok ( ( ) )
170
178
}
@@ -212,7 +220,7 @@ pub async fn prepare_global_package_json(npm_spec: &str, prefix: Option<&str>) -
212
220
let cache_flag_path = cache_dir. join ( format ! ( "{}/{}/_resolved" , name, resolved. version) ) ;
213
221
214
222
// Download if not cached
215
- if !cache_flag_path . exists ( ) {
223
+ if !tokio :: fs :: try_exists ( & cache_flag_path ) . await ? {
216
224
log_verbose ( & format ! (
217
225
"Downloading {} to {}" ,
218
226
tarball_url,
@@ -227,7 +235,7 @@ pub async fn prepare_global_package_json(npm_spec: &str, prefix: Option<&str>) -
227
235
// so we need to copy the file to the package directory to avoid effect other packages
228
236
if resolved. manifest . get ( "hasInstallScript" ) == Some ( & json ! ( true ) ) {
229
237
let has_install_script_flag_path = cache_path. join ( "_hasInstallScript" ) ;
230
- fs:: write ( has_install_script_flag_path, "" ) ?;
238
+ tokio :: fs:: write ( has_install_script_flag_path, "" ) . await ?;
231
239
}
232
240
}
233
241
@@ -243,7 +251,8 @@ pub async fn prepare_global_package_json(npm_spec: &str, prefix: Option<&str>) -
243
251
244
252
// Remove devDependencies from package.json
245
253
let package_json_path = package_path. join ( "package.json" ) ;
246
- let mut package_json: Value = serde_json:: from_reader ( fs:: File :: open ( & package_json_path) ?) ?;
254
+ let package_json_content = tokio:: fs:: read_to_string ( & package_json_path) . await ?;
255
+ let mut package_json: Value = serde_json:: from_str ( & package_json_content) ?;
247
256
248
257
// Remove specified dependency fields and scripts.prepare
249
258
let package_obj = package_json. as_object_mut ( ) . unwrap ( ) ;
@@ -258,10 +267,11 @@ pub async fn prepare_global_package_json(npm_spec: &str, prefix: Option<&str>) -
258
267
}
259
268
260
269
// Write back the modified package.json
261
- fs:: write (
270
+ tokio :: fs:: write (
262
271
& package_json_path,
263
272
serde_json:: to_string_pretty ( & package_json) ?,
264
- ) ?;
273
+ )
274
+ . await ?;
265
275
266
276
log_verbose ( & format ! ( "package_path: {}" , package_path. to_string_lossy( ) ) ) ;
267
277
Ok ( package_path)
@@ -290,8 +300,8 @@ pub struct InvalidDependency {
290
300
}
291
301
292
302
pub async fn is_pkg_lock_outdated ( root_path : & Path ) -> Result < bool > {
293
- let pkg_file = load_package_json_from_path ( root_path) ?;
294
- let lock_file = load_package_lock_json_from_path ( root_path) ?;
303
+ let pkg_file = load_package_json_from_path ( root_path) . await ?;
304
+ let lock_file = load_package_lock_json_from_path ( root_path) . await ?;
295
305
296
306
// get packages in package-lock.json
297
307
let packages = lock_file
@@ -323,7 +333,7 @@ pub async fn is_pkg_lock_outdated(root_path: &Path) -> Result<bool> {
323
333
} ;
324
334
325
335
// check dependencies whether changed
326
- for ( dep_field, _is_optional) in get_dep_types ( ) {
336
+ for ( dep_field, _is_optional) in get_dep_types ( ) . await {
327
337
if !deps_fields_equal ( pkg. get ( dep_field) , lock. get ( dep_field) ) {
328
338
let name = if path. is_empty ( ) { "root" } else { & path } ;
329
339
log_warning ( & format ! (
@@ -353,7 +363,7 @@ pub async fn validate_deps(
353
363
354
364
if let Some ( packages) = pkgs_in_pkg_lock. as_object ( ) {
355
365
for ( pkg_path, pkg_info) in packages {
356
- for ( dep_field, is_optional) in get_dep_types ( ) {
366
+ for ( dep_field, is_optional) in get_dep_types ( ) . await {
357
367
if let Some ( dependencies) = pkg_info. get ( dep_field) . and_then ( |d| d. as_object ( ) ) {
358
368
for ( dep_name, req_version) in dependencies {
359
369
let req_version_str = req_version. as_str ( ) . unwrap_or_default ( ) ;
@@ -474,8 +484,8 @@ pub async fn validate_deps(
474
484
Ok ( invalid_deps)
475
485
}
476
486
477
- fn get_dep_types ( ) -> Vec < ( & ' static str , bool ) > {
478
- let legacy_peer_deps = get_legacy_peer_deps ( ) ;
487
+ async fn get_dep_types ( ) -> Vec < ( & ' static str , bool ) > {
488
+ let legacy_peer_deps = get_legacy_peer_deps ( ) . await ;
479
489
480
490
if legacy_peer_deps {
481
491
vec ! [
0 commit comments