@topoconfig/cmds v0.2.22
@topoconfig/cmds
topoconfig basic cmds preset
Status
Working draft
Install
yarn add @topoconfig/cmds
Usage
import {topoconfig} from 'topoconfig'
import * as cmds from '@topoconfig/cmds'
const config = await topoconfig({
data: {},
sources: {},
cmds
})
get
lodash.get-inspired dot-prop reader.
import {topoconfig} from 'topoconfig'
import {get} from 'topoconfig/cmds'
const config = await topconfig({
cmds: {get},
data: '$b',
sources: {
a: {
data: {
foo: {
bar: 'baz'
}
}
},
b: 'get $a .foo.bar' // gives 'baz'
}
})
file
Reads file.
import {topoconfig} from 'topoconfig'
import {file, json} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {
file,
json
},
data: {
contents: '$contents'
},
sources: {
contents: 'file file.json > json'
}
})
json
Parses value as JSON.
import {topoconfig} from 'topoconfig'
import {json} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {
json
},
data: {
contents: '$contents'
},
sources: {
contents: 'json {"foo":"bar"}'
}
})
yaml
Parses value as YAML.
import {topoconfig} from 'topoconfig'
import {yaml} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {
yaml
},
data: {
contents: '$contents'
},
sources: {
contents: 'yaml "foo: bar"'
}
})
http
Invokes Fetch API to get data from remotes.
import {topoconfig} from 'topoconfig'
import {http, get, json} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {
http,
get,
json
},
data: {
price: '$price',
title: '$title'
},
sources: {
res: 'http https://dummyjson.com/products > get body > json',
title: 'get $res products.0.title',
price: 'get $res products.0.price',
}
})
// {title: 'iPhone 9', price: 549}
conf
Wraps the value with conf API, to bring dot-prop r/w opts, validation, file sync, etc.
import {topoconfig} from 'topoconfig'
import {conf} from 'topoconfig/cmds'
const config = await topoconfig<ReturnType<typeof conf>>({
data: '$b',
sources: {
a: {
data: {
foo: {
bar: 'baz'
}
}
},
schema: {
data: {
foo: {
properties: {
bar: {
type: 'string'
}
}
}
}
},
b: 'conf $a $schema'
},
cmds: {
conf
}
})
config.get('foo.bar') // 'baz'
config.set('foo.bar', 1) // Error: Config schema violation: `foo/bar` must be string
dot
Applies doT to value resolution. Follow the v2 API guides for details.
import {topoconfig} from 'topoconfig'
import {dot} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {dot},
data: '$filename',
sources: {
filename: 'dot {{= "$env.ENVIRONMENT_PROFILE_NAME" || "kube" }}.json',
env: {
data: { ENVIRONMENT_PROFILE_NAME: 'prod' }
}
}
})
// prod.json
cwd
Returns process.cwd()
import {topoconfig} from 'topoconfig'
import {cwd} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {cwd},
data: '$cwd',
sources: {
cwd: 'cwd'
}
})
// /current/working/dir
ip
Resolves current ip.
import {topoconfig} from 'topoconfig'
import {ip} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {ip},
data: '$ip',
sources: {
ip: 'ip'
}
})
// 10.10.0.12
ajv
Validates values by json-schema via ajv. Extra ajv-formats included.
import {topoconfig} from 'topoconfig'
import {ajv} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {ajs},
data: '$output',
sources: {
object: {foo: 'bar'},
schema: {type: 'object', properties: {foo: {type: 'string'}}},
output: 'ajv $object $schema'
}
}) // returns {foo: 'bar'} if it mathes the schema
argv
Returns minimist-parsed argv.
// app.js
import {topoconfig} from 'topoconfig'
import {argv} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {argv},
data: {
param: '$argv.foo'
},
sources: {
argv: 'argv'
}
}) // {param: 'bar'}
// node app.js --foo=bar
env
Refers to process.env
.
// app.js
import {topoconfig} from 'topoconfig'
import {env} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {env},
data: '$env.DEBUG',
sources: {
env: 'env'
}
}) // 'true'
// DEBUG=true node app.js
g
Returns a ref to global object.
import {topoconfig} from 'topoconfig'
import {g} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {g},
data: '$global.foo',
sources: {
global: 'g'
}
})
// refers to this.globalThis.foo
pkg
Reads the closest package.json via read-pkg-up
import {topoconfig} from 'topoconfig'
import {pkg} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {pkg},
data: '$pkg.name',
sources: {
pkg: 'pkg'
}
}) // 'toposource'
xtends
Populate extends
ref in config files via @topoconfig/extends.
import {topoconfig} from 'topoconfig'
import {file, xtends} from 'topoconfig/cmds'
const config = await topoconfig({
cmds: {
xtends
},
data: {
contents: '$contents'
},
sources: {
contents: 'file.json > xtends'
}
})
License
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
4 months ago
6 months ago
6 months ago
6 months ago
6 months ago
7 months ago