Skip to content

A JS library using WebAssembly to patch TOML files, preserving formatting

License

Notifications You must be signed in to change notification settings

Shopify/toml-patch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@shopify/toml-patch

A WebAssembly-powered library to efficiently update TOML files, based on Rust's toml_edit crate.

CI

Usage (JavaScript/TypeScript)

The primary function exported for JavaScript usage is updateTomlValues.

import { updateTomlValues } from "@shopify/toml-patch";

const originalToml = `
[package]
name = "my-package"
version = "0.1.0"

[dependencies]
serde = "1.0"
`;

const pathsToUpdate = "package.version,dependencies.serde,new_table.key";
const correspondingValues = '"0.2.0","$undefined","new value"'; // Use "$undefined" to remove a key

try {
  const updatedToml = updateTomlValues(
    originalToml,
    pathsToUpdate,
    correspondingValues
  );

  console.log(updatedToml);
  /*
  Output:

  [package]
  name = "my-package"
  version = "0.2.0"

  [dependencies]

  [new_table]
  key = "new value"
  */
} catch (error) {
  console.error("Failed to update TOML:", error);
}

Arguments:

  1. toml_content (string): The original TOML content.
  2. paths_str (string): A comma-separated string of dotted paths (e.g., "table.key).
  3. values_str (string): A comma-separated string of values corresponding to the paths. Values are parsed as TOML values (so strings need to be quoted). Use the special string "$undefined" to remove the specified key.

Returns:

  • (string): The updated TOML content as a string.

Development

This project uses Rust and wasm-pack.

  1. Install Rust: https://www.rust-lang.org/tools/install
  2. Install wasm-pack: cargo install wasm-pack
  3. Build: wasm-pack build --target bundler --release --scope="shopify"
  4. Test: wasm-pack test --node

About

A JS library using WebAssembly to patch TOML files, preserving formatting

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages