Crate package_json

Source
Expand description

Use the package-json crate to manage your package.json file.

§How to locate the closest package.json file

use package_json::PackageJsonManager;
use std::path::Path;

let mut manager = PackageJsonManager::new();
// based on the current working directory
manager.locate_closest()?;
// based on the given path
manager.locate_closest_from(&Path::new("/path/to/working_dir"))?;

Use with_file_path to create a PackageJsonManager instance with the give file path.

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

Use set_file_path to change file path.

§How to read or write the current package.json file

We can use read_mut or read_ref to read the current package.json file after file located.

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

On the other hand, we should use as_mut and as_ref to get a mutable reference or a immutable reference if we have read it before.

manager.as_mut(); // or manager.as_ref();

Use write to write the current package.json file to the disk. If we want to change the output path, eg. create a new package.json, we should use write_to instead.

use package_json::PackageJsonManager;
use std::path::Path;

let mut manager = PackageJsonManager::new();
assert!(manager.write().is_ok());
assert!(manager.write_to(&Path::new("/path/to/package.json")).is_ok());

Structs§

PackageBugsRecord
see PackageJson::bugs
PackageDirectories
see PackageJson::directories
PackageFundingRecord
see PackageJson::funding
PackageJson
A package.json is a JSON file that exists in the root of a JavaScript/Node.js project. It holds metadata relevant to the project and it’s used for managing the project’s dependencies, scripts, version and a whole lot more.
PackageJsonManager
A manager for manipulating package.json file.
PackagePeopleRecord
PackageRepositoryRecord
WriteOptions
Options used for writing a file.
WriteOptionsBuilder
Builder for WriteOptions.

Enums§

PackageBin
see PackageJson::bin
PackageBugs
see PackageJson::bugs
PackageFunding
see PackageJson::funding
PackageMan
see PackageJson::man
PackagePeople
PackageRepository
see PackageJson::repository

Constants§

PACKAGE_JSON_FILENAME

Type Aliases§

PackageDependencies