pub struct PackageJsonManager { /* private fields */ }
Expand description
A manager for manipulating package.json
file.
Implementations§
Source§impl PackageJsonManager
impl PackageJsonManager
Sourcepub fn with_file_path<FilePath>(path: FilePath) -> Self
pub fn with_file_path<FilePath>(path: FilePath) -> Self
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");
Sourcepub fn with_write_options(options: WriteOptions) -> Self
pub fn with_write_options(options: WriteOptions) -> Self
Construct a new, empty PackageJsonManager
with the specified WriteOptions
.
use package_json::{PackageJsonManager, WriteOptions, WriteOptionsBuilder};
let mut manager = PackageJsonManager::with_write_options(WriteOptions::default());
let mut manager = PackageJsonManager::with_write_options(
WriteOptionsBuilder::default().pretty(false).build().unwrap()
);
Sourcepub fn locate_closest(&mut self) -> Result<PathBuf>
pub fn locate_closest(&mut self) -> Result<PathBuf>
Try to locate the closest package.json
file from current working directory to sys root.
Sourcepub fn locate_closest_from<P: AsRef<Path>>(
&mut self,
from: P,
) -> Result<PathBuf>
pub fn locate_closest_from<P: AsRef<Path>>( &mut self, from: P, ) -> Result<PathBuf>
Try to locate the closest package.json
file from specific directory to sys root.
Sourcepub fn set_file_path<FilePath: AsRef<Path>>(&mut self, file_path: FilePath)
pub fn set_file_path<FilePath: AsRef<Path>>(&mut self, file_path: FilePath)
Specify the package.json
file path which is used to read and write.
Sourcepub fn get_file_path(&mut self) -> Option<&Path>
pub fn get_file_path(&mut self) -> Option<&Path>
Get the located file path after locate_closest
or locate_closest_from
evaluated.
Sourcepub fn take_file_path(&mut self) -> Option<PathBuf>
pub fn take_file_path(&mut self) -> Option<PathBuf>
Take the located file path out of PackageJsonManager
, leaving a None
in its place.
Sourcepub fn read_ref(&mut self) -> Result<&PackageJson>
pub fn read_ref(&mut self) -> Result<&PackageJson>
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_ok() {
assert!(manager.read_ref().is_ok());
}
Sourcepub fn read_mut(&mut self) -> Result<&mut PackageJson>
pub fn read_mut(&mut self) -> Result<&mut PackageJson>
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_ok() {
assert!(manager.read_mut().is_ok());
}
Sourcepub fn write(&mut self) -> Result<()>
pub fn write(&mut self) -> Result<()>
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_ok() {
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");
}
Sourcepub fn write_to(&mut self, file_path: &Path) -> Result<()>
pub fn write_to(&mut self, file_path: &Path) -> Result<()>
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_ok() {
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§
Source§impl AsMut<PackageJson> for PackageJsonManager
impl AsMut<PackageJson> for PackageJsonManager
Source§fn as_mut(&mut self) -> &mut PackageJson
fn as_mut(&mut self) -> &mut PackageJson
Return a mutable reference to the current PackageJson
struct.
Source§impl AsRef<PackageJson> for PackageJsonManager
impl AsRef<PackageJson> for PackageJsonManager
Source§fn as_ref(&self) -> &PackageJson
fn as_ref(&self) -> &PackageJson
Return a immutable reference to the current PackageJson
struct.