npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yaml-import

v3.0.0

Published

Import files and directories in YAML for a modular design

Downloads

17,568

Readme

yaml-import

Version Types License

Import files and directories in YAML for a modular design

Install

  • Local install for programmatic usage: npm install yaml-import.
  • Global install, so the yimp executable is globally available: npm install -g yaml-import.

YAML

With yaml-import, the imports are relative to the current YAML file. For further insight on how each import type behaves, you can check the integration tests root yaml file and its expected result.

!!import/single string

Imports the contents of a single file

foo: !!import/single foo/bar.yml
bar:
  - item
  - !!import/single bar/baz.yml

!!import/sequence string[]

Resolves with an array of the contents of all files passed. If a directory is passed, it will include all files on that directory.

!!import/sequence
  - foo/foo.yml
  - foo/bar.yml
  - baz/

!!import/shallow string[]

Resolves with a shallow merge of the contents of all files passed. If a directory is passed, it will include all files on that directory. If a value is not an object, it will overwrite all previous values for that field.

!!import/shallow
  - foo/foo.yml
  - foo/bar.yml
  - baz/

!!import/merge string[]

Resolves with a deep merge of the contents of all files passed, excluding arrays -which will be overwritten. If a directory is passed, it will include all files on that directory. If a value is not an object, it will overwrite all previous values for that field.

!!import/merge
  - foo/foo.yml
  - foo/bar.yml
  - baz/

!!import/deep string[]

Resolves with a deep merge of the contents of all files passed, including arrays -which will be concatenated. If a directory is passed, it will include all files on that directory. If a value is not an object, it will overwrite all previous values for that field.

!!import/deep
  - foo/foo.yml
  - foo/bar.yml
  - baz/

!!import/payload object

Takes in an object with fields:

  • paths: Required, string | string[]. Paths to import -files or directories.
  • strategy: Optional, string. Any of 'sequence', 'shallow', 'merge', and 'deep'. Default: 'merge'.
  • data: Optional. Any additional data to be treated as coming from an additional last element of paths -that is, the content of paths will be merged with data with the chosen strategy.
  • recursive: Optional, boolean. Whether to recursively traverse directories when passed as paths. Default: false.
# These would be equivalent:
!!import/merge
  - foo/foo.yml
!!import/payload
  paths: foo/foo.yml

# And these would too:
!!import/deep
  - foo/foo.yml
  - foo/bar.yml
!!import/payload
  strategy: deep
  paths:
    - foo/foo.yml
    - foo/bar.yml

!!import/tree object

Creates an object with keys equivalent to the directory tree and files from each path in paths, then merges all of them with strategy.

Takes in an object with fields:

  • paths: Required, string | string[]. Paths to import -files or directories.
  • strategy: Optional, string. Any of 'sequence', 'shallow', 'merge', and 'deep'. Default: 'merge'.
  • data: Optional. Any additional data to be treated as coming from an additional last element of paths -that is, the content of paths will be merged with data with the chosen strategy.
  • recursive: Optional, boolean. Whether to recursively traverse directories when passed as paths. Default: false.
# If we had the following tree, it would resolve with an object with keys 'a'
# and 'b', while the contents of 'a' would be merged with a 'deep' strategy:
#   foo
#     a.yml
#     b.yml
#   bar
#     a.yml
!!import/tree
  strategy: deep
  paths:
    - foo/
    - bar/

Usage

Command Line

If there is no output file, the contents will be written to stdout. The list of ext -file extensions for directory imports, see write- must be comma separated, without spaces.

Usage:
  $ yimp file [options]

Options:
  -o, --output <path>       Path to output file, optional
  -e, --ext <extensions>    Extensions, comma separated, optional
  -h, --help                Show help
  -v, --version             Show version number

Example:
  $ yimp source.yml -o destination.yml -e yml,yaml,raml

Programatic Usage

write(source, destination, options?)

Reads a YAML file and writes the output on a file.

  • source: Required, string. Path of the file to read.
  • destination: Required, string. Path for the output file.
  • options Optional, object:
import path from 'node:path';

import { write } from 'yaml-import';

write(
  path.join(import.meta.dirname, 'foo/root.yml'),
  path.join(import.meta.dirname, 'out/yaml.yml')
);

read(source, options?)

Reads a YAML file and returns the parsed object.

  • input: Required, string. Path of the file to read.
  • options: Optional, object. Same as those taken by write.
import path from 'node:path';

import { read } from 'yaml-import';

const content = read(path.join(import.meta.dirname, 'foo/root.yml'));

We could write it later on with js-yaml and fs:

import fs from 'node:fs';

import { dump } from 'js-yaml';

// To YAML
const text = dump(content);
// Write to file
fs.writeFileSync(path.join(import.meta.dirname, 'out/yaml.yml'), text);

getSchema(cwd, options?)

For flexible usage with js-yaml, getSchema returns a schema you can pass to js-yaml functions.

  • cwd: Required, string. Base directory to read imported files from.
  • options: Optional, object. Same as those taken by write. Used when files to import are loaded.
import path from 'node:path';
import fs from 'node:fs';

import { load } from 'js-yaml';
import { getSchema } from 'yaml-import';

const src = fs.readFileSync(path.join(import.meta.dirname, 'foo/root.yml'), 'utf8');
const cwd = path.join(import.meta.dirname, 'foo');
const schema = getSchema(cwd);
const content = load(src, { schema });