pub struct PackageJsonManager { /* private fields */ }
Expand description

A manager for manipulating package.json file.

Implementations

Constructs a new, empty PackageJsonManager.

Constructs a new, empty PackageJsonManger with the specified package.json file path.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::with_file_path("/path/to/package.json");

Try to locate the closest package.json file from current working directory to sys root.

Try to locate the closest package.json file from specific directory to sys root.

Specify the package.json file path which is used to read and write.

Get the located file path after locate_closest or locate_closest_from evaluated.

Take the located file path out of PackageJsonManager, leaving a None in its place.

Evaluate package.json parser and return the immutable PackageJson reference.

Note: It always reads the file again. In the most case, you should call as_ref to get a immutable reference if you have read it before.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_some() {
  assert!(manager.read_ref().is_ok());
}

Evaluate package.json parser and return the mutable PackageJson reference.

Note: It always reads the file again. In the most case, you should call as_mut to get a mutable reference if you have read it before.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_some() {
  assert!(manager.read_mut().is_ok());
}

Use the current package.json content to write the target package.json file.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_some() {
  if let Ok(mut json) = manager.read_mut() {
    json.name = "new name".to_string();
    json.version = "1.0.0".to_string();
  }
  manager.write().expect("Couldn't write package.json");
}

Write the current package.json content to the specific package.json file.

use package_json::PackageJsonManager;
use std::path::Path;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_some() {
  if let Ok(mut json) = manager.read_mut() {
    json.name = "new name".to_string();
    json.version = "1.0.0".to_string();
  }
  manager
    .write_to(&Path::new("/path/to/package.json"))
    .expect("Couldn't write package.json");
}

Trait Implementations

Return a mutable reference to the current PackageJson struct.

Return a immutable reference to the current PackageJson struct.

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.