Skip to content

Latest commit

 

History

History
146 lines (106 loc) · 3.38 KB

File metadata and controls

146 lines (106 loc) · 3.38 KB

Main SYNX site: https://synx.aperturesyndicate.com/

SYNX for JS/TS — @aperturesyndicate/synx-format

The official JavaScript & TypeScript parser for the SYNX format.

Install

npm install @aperturesyndicate/synx-format

Usage

const Synx = require('@aperturesyndicate/synx-format');

// Load from file
const data = Synx.loadSync('config.synx');
console.log(data.server.port); // 8080

// Parse from string
const data2 = Synx.parse('name Wario\nage 30');
console.log(data2.name); // "Wario"

TypeScript

import Synx from '@aperturesyndicate/synx-format';

interface Config {
  server: { port: number; host: string };
  app_name: string;
}

const data = Synx.loadSync<Config>('config.synx');
console.log(data.server.port); // typed as number

API

Method Description
Synx.parse<T>(text, options?) Parse a .synx string ? object
Synx.loadSync<T>(path, options?) Load & parse file (sync)
Synx.load<T>(path, options?) Load & parse file (async, returns Promise)
Synx.stringify(obj, active?) Serialize object ? .synx string

Options

{
  basePath?: string;                 // For :include resolution
  env?: Record<string, string>;      // Override env vars
  region?: string;                   // For :geo ("RU", "US", etc.)
  strict?: boolean;                  // Throw on INCLUDE_ERR/WATCH_ERR/CALC_ERR/CONSTRAINT_ERR
}

strict: true enables fail-fast behavior for production: marker runtime errors throw instead of silently remaining in the output object as error strings.

CLI

Install globally:

npm install -g @aperturesyndicate/synx-format

Commands

# Convert to JSON/YAML/TOML/.env
synx convert config.synx --format json
synx convert config.synx --format yaml > values.yaml
synx convert config.synx --format toml
synx convert config.synx --format env > .env

# Validate (strict mode, for CI/CD)
synx validate config.synx --strict

# Watch for changes
synx watch config.synx --format json
synx watch config.synx --exec "nginx -s reload"

# Extract JSON Schema from constraints
synx schema config.synx

Export Formats

Convert parsed SYNX to other formats programmatically:

const config = Synx.loadSync('config.synx');

Synx.toJSON(config);           // JSON (pretty)
Synx.toJSON(config, false);    // JSON (compact)
Synx.toYAML(config);           // YAML
Synx.toTOML(config);           // TOML
Synx.toEnv(config);            // KEY=VALUE
Synx.toEnv(config, 'PREFIX');  // PREFIX_KEY=VALUE

File Watcher

const handle = Synx.watch('config.synx', (config, error) => {
  if (error) return console.error(error);
  console.log('Config reloaded:', config);
}, { strict: true });

handle.close(); // stop watching

Schema Export

Extract constraints as JSON Schema:

const schema = Synx.schema(`
!active
app_name[required, min:3, max:30] TotalWario
volume[min:0, max:100, type:int] 75
`);
// { "$schema": "...", "properties": { "app_name": { ... } }, "required": ["app_name"] }

Other Languages

  • Pythonsynx-format on PyPI
  • Rustsynx on crates.io
    cargo add synx
    use synx::Synx;
    
    let data = Synx::load("config.synx")?;
    println!("{}", &data["server"]["port"]);

License

MIT — © APERTURESyndicate