@env-spec/parser 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +191 -0
- package/dist/chunk-FWOHTEQS.mjs +492 -0
- package/dist/chunk-FWOHTEQS.mjs.map +1 -0
- package/dist/chunk-JU4IPIOA.js +507 -0
- package/dist/chunk-JU4IPIOA.js.map +1 -0
- package/dist/classes-BGrn6GBW.d.mts +185 -0
- package/dist/classes-BGrn6GBW.d.ts +185 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +3796 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3742 -0
- package/dist/index.mjs.map +1 -0
- package/dist/simple-resolver.d.mts +12 -0
- package/dist/simple-resolver.d.ts +12 -0
- package/dist/simple-resolver.js +79 -0
- package/dist/simple-resolver.js.map +1 -0
- package/dist/simple-resolver.mjs +77 -0
- package/dist/simple-resolver.mjs.map +1 -0
- package/package.json +40 -7
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# `@env-spec`
|
|
2
|
+
|
|
3
|
+
> We're actively seeking feedback on @env-spec. Please share your thoughts and ideas in the [RFC](https://github.com/dmno-dev/varlock/discussions/17).
|
|
4
|
+
|
|
5
|
+
**@env-spec** is a simple DSL/language that extends normal dotenv syntax, allowing you to:
|
|
6
|
+
- add structured metadata using `@decorator` style comments similar to [JSDoc](https://jsdoc.app/)
|
|
7
|
+
- formalizes a syntax for setting values via function calls
|
|
8
|
+
|
|
9
|
+
_Here is a short illustrative example:_
|
|
10
|
+
```dotenv
|
|
11
|
+
# Stripe secret api key
|
|
12
|
+
# @required @sensitive @type=string(startsWith="sk_")
|
|
13
|
+
# @docsUrl=https://docs.stripe.com/keys
|
|
14
|
+
STRIPE_SECRET_KEY=encrypted("asdfqwerqwe2374298374lksdjflksdjf981273948okjdfksdl")
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
This structured data can be used by libraries to provide:
|
|
18
|
+
- additional validation, coercion, type-safety on your env vars
|
|
19
|
+
- extra guard-rails around handling `@sensitive` data
|
|
20
|
+
- more flexible loading logic without additional application code or config files
|
|
21
|
+
|
|
22
|
+
## How can schema data be used?
|
|
23
|
+
|
|
24
|
+
This schema information is most valuable when it is shared across team members and machines - so it is intended to be used within a file which is comitted to git.
|
|
25
|
+
In most cases, that will mean creating a `.env.schema`, committed to source control, which contains all schema info and possibly some default values.
|
|
26
|
+
It's not very different than a having a `.env.example` file - it's just more useful and actually involving it in the env loading process.
|
|
27
|
+
|
|
28
|
+
Then you could use additional files which set values - and of course they could add additional items or overriding properties of existing ones.
|
|
29
|
+
Whether you want to use a single git-ignored `.env` file, or apply a cascade of environment-specific files (e.g., `.env`, `.env.local`, `.env.test`, etc) is up to you.
|
|
30
|
+
However the new ability to use function calls to safely decrypt data, or load values from external sources, means you'll likely be tempted to use committed `.env` files much more.
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
An env-spec enabled tool would load all env files appropriately, merging together both schema and values, as well as additional values read from the shell/process.
|
|
34
|
+
Then the schema would be applied which could transform/fill values, for example decrypting or fetching from an external source, as well as applying coercion and validation.
|
|
35
|
+
|
|
36
|
+
| In a very simple project, you can also imagine using a single committed .env file which contains both schema and values, and takes advantage of function calls to securely load sensitive info.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## Backwards compatibility
|
|
40
|
+
|
|
41
|
+
This is designed to be mostly backwards compatible with traditional .env files, however there is no standard .env spec and various tools have slightly different rules and features, so we make some decisions to try to standardize things.
|
|
42
|
+
Tools may support additional compatibility flags if users want to opt in/out of specific behaviours that match other tools.
|
|
43
|
+
|
|
44
|
+
The extended feature set means an env-spec enabled parser will successfully parse env files that other tools may not.
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
## What is _this_ package?
|
|
48
|
+
This package defines a parser and related tools for parsing an **@env-spec** enabled .env file.
|
|
49
|
+
It does not provide anything past this parsing step - like actually loading environment variables.
|
|
50
|
+
For a usable tool which lets you actually use it in your .env files, check out https://varlock.com
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
-----
|
|
54
|
+
|
|
55
|
+
# Language Syntax Reference
|
|
56
|
+
|
|
57
|
+
This is a reference of the details of the env-spec language itself.
|
|
58
|
+
Here we don't make and assumptions about the meaning of specific decorators, or function calls.
|
|
59
|
+
|
|
60
|
+
## Comments
|
|
61
|
+
|
|
62
|
+
Comments in env-spec (like dotenv) start with a `#`. Comments can be either on their own line, or at the end of a line after something else.
|
|
63
|
+
- `# this is a comment`
|
|
64
|
+
- `KEY=val # so is this`
|
|
65
|
+
- ` # but this is invalid`
|
|
66
|
+
|
|
67
|
+
We give these comments additional meaning by letting them contain `@decorators` and attaching them to specific config items.
|
|
68
|
+
|
|
69
|
+
### `Comment`
|
|
70
|
+
A regular comment is a comment that does not start with `@`
|
|
71
|
+
- Leading whitespace is optional -- `#these`, `# are`, `# all valid `
|
|
72
|
+
- Decorators within are ignored -- `# this @decorator is ignored`
|
|
73
|
+
|
|
74
|
+
### `DecoratorComment`
|
|
75
|
+
A decorator comment is a comment that starts with an `@` and contains decorators
|
|
76
|
+
- It may contain one or multiple decorators -- `# @type=integer`, `# @sensitive @required`
|
|
77
|
+
- There may be an additional regular comment after the decorator(s) -- `# @sensitive=false # key is published in final build`
|
|
78
|
+
|
|
79
|
+
### `Divider`
|
|
80
|
+
A divider is a comment that serves as a separator, like a horizontal line
|
|
81
|
+
- A comment starting with `---` or `===` is considered a divider -- `# ---`, `# ===`
|
|
82
|
+
- A _single_ leading whitespace is optional -- `# ---`, `#---`
|
|
83
|
+
- Anything after that is ignored and valid -- `# --- some info`, `# ------------`
|
|
84
|
+
|
|
85
|
+
### `CommentBlock`
|
|
86
|
+
A comment block is a group of continuous comments that is not attached to a specific config item.
|
|
87
|
+
- The comment block is ended by an empty line, a `Divider`, or the end of the file.
|
|
88
|
+
- Both `DecoratorComment`s and `RegularComment`s may be interpersed
|
|
89
|
+
|
|
90
|
+
### `DocumentHeader`
|
|
91
|
+
If a `CommentBlock` ends with a `Divider` and is the first element of the document, it will be considered the Header.
|
|
92
|
+
- Decorators from this header can be used to configure all contained elements, or the loading process itself
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
## Decorators
|
|
96
|
+
|
|
97
|
+
A `Decorator` is used within comments to attach structured data to specific config items or to the entire document and loading process.
|
|
98
|
+
|
|
99
|
+
- Each decorator has a name and optional value -- `@name=value`
|
|
100
|
+
- Using the name only is equivalent to setting the value to true -- `@required` === `@required=true`
|
|
101
|
+
- Decorator values will be parsed using the common value-handling rules (see below)
|
|
102
|
+
|
|
103
|
+
**Valid decorator examples:**
|
|
104
|
+
```dotenv
|
|
105
|
+
# @willBeTrue @willBeFalse=false @explicitTrue=true @undef=undefined
|
|
106
|
+
# @int=123 @float=123.456 @willBeString=123.456.789
|
|
107
|
+
# @quoted="with spaces" @trueString="true"
|
|
108
|
+
# @singleQuote='hi' @backTickQuote=`hi`
|
|
109
|
+
# @withNewline="new\nline"`
|
|
110
|
+
# @funcCallNoArgs=func() @dec=funcCallArray(val1, "val2") @dec=funcCallObj(k1=v1, k2="v2")
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Invalid decorator examples:**
|
|
114
|
+
```dotenv
|
|
115
|
+
# @
|
|
116
|
+
# @int=
|
|
117
|
+
# @spaceNeedsQuotes=spaces without quotes
|
|
118
|
+
# @noNewLines="new
|
|
119
|
+
# laksdjf"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Config Items
|
|
123
|
+
|
|
124
|
+
Config items define individual env vars.
|
|
125
|
+
Each has a key, an optional value, and optional attached comments.
|
|
126
|
+
|
|
127
|
+
- Keys must start with [a-ZA-Z_], followed by any of [a-ZA-Z0-9_] -- ✅ `SOME_ITEM`, ❌ `BAD-KEY`, ❌ `2BAD_KEY`
|
|
128
|
+
- Setting no value is allowed and will be treated as `undefined` -- `UNDEF_VAR=`
|
|
129
|
+
- An empty string is allowed -- `EMPTY_STRING_VAR=""`
|
|
130
|
+
- Single-line values may be wrapped in quotes or not, and will follow the common value-handling rules (see below)
|
|
131
|
+
- Multi-line values may be wrapped in either ``( " | """ | ``` )``
|
|
132
|
+
- Only comments _directly_ preceeding the item will be attached to the item
|
|
133
|
+
- However a `Divider` will break the above comments into a `CommentBlock` that is not attached to the item
|
|
134
|
+
- An additional post-comment can appear after the item `ITEM1=foo # post comment`
|
|
135
|
+
- This post-comment can contain decorators `ITEM1=foo # @required`
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
## Common Value-Handling Rules
|
|
140
|
+
Values are interpreted similarly for config item values, decorator values, and values within function call arguments. Values may be wrapped in quotes or not, but handling varies slightly.
|
|
141
|
+
|
|
142
|
+
- Values may never contain actual newlines, but may contain the string `\n`
|
|
143
|
+
- Values do not have to be wrapped in quotes if they do not contain spaces
|
|
144
|
+
- Unquoted values also may not contain other characters depending on the context:
|
|
145
|
+
- `ConfigItem` values may not contain `[ #]`
|
|
146
|
+
- `Decorator` values may not contain `[ #]`
|
|
147
|
+
- `FunctionCall` args may not contain `[ ,)]`
|
|
148
|
+
- Unquoted values will coerce `true`, `false`, `undefined` -- `@foo=false`
|
|
149
|
+
- Unquoted values will coerce numeric values -- `@int=123 @float=123.456`
|
|
150
|
+
- Otherwise unquoted values will be treated as a string
|
|
151
|
+
- A value in quotes is _always_ be treated as a string -- `@d1="with spaces" @trueString="true"`, `@numStr="123"`
|
|
152
|
+
- All quote styles ``[`'"]`` are ok -- ``@dq="c" @bt=`b` @sq='a'``
|
|
153
|
+
- Escaped quotes matching the wrapping quote style are ok -- `@ok="escaped\"quote"`
|
|
154
|
+
- In quote-wrapped values, the string `\n` will be converted to an actual newline
|
|
155
|
+
|
|
156
|
+
### Function calls
|
|
157
|
+
|
|
158
|
+
If a value is not wrapped in quotes and looks like a function call - for example `encrypted(ASDF123...)` - we will interpret it as a `FunctionCall`. This is relevant both for config item values and decorator values.
|
|
159
|
+
|
|
160
|
+
- function names must start with a letter, and can then contain letters, numbers, and underscores `/[a-ZA-Z][a-ZA-Z0-9_]*/`
|
|
161
|
+
- function args are always interpreted as either an array or object
|
|
162
|
+
- you can pass no args, a single value, or multiple
|
|
163
|
+
- or you may pass key value pairs, and the args will be interpreted as an object
|
|
164
|
+
- each value will be interpreted using common value-handling rules (see above)
|
|
165
|
+
|
|
166
|
+
Examples:
|
|
167
|
+
```dotenv
|
|
168
|
+
# @noArgs=fn()
|
|
169
|
+
# @oneArg=fn(asdf) @oneArgQuoted=fn("with quotes")
|
|
170
|
+
# @multipleArgs=fn(one, "two", three, 123.456)
|
|
171
|
+
# @objArgs=fn(key1=v1, key2="v2", key3=true)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
------------------
|
|
175
|
+
## Notable differences with dotenv/dotenv-expand/dotenvx
|
|
176
|
+
- we do not support _nested_ default expansion (ex: `VAR=${FOO:-${BAR}}`
|
|
177
|
+
- instead you can use the `fallback` function directly `VAR=fallback(ref(FOO), ref(BAR))`
|
|
178
|
+
- we do not support _unescaped_ quotes within exec expansion (ex: `VAR="$(echo "foo")"`)
|
|
179
|
+
- we support backtick quotes, escaped quotes, or you can use `exec` function directly `VAR=exec(echo "foo")`
|
|
180
|
+
------------------
|
|
181
|
+
|
|
182
|
+
## Local dev and testing workflow
|
|
183
|
+
|
|
184
|
+
- `pnpm dev` - builds and watches everything
|
|
185
|
+
- `pnpm test` builds everything, run tests, and watches for changes to re-run
|
|
186
|
+
- `pnpm test:ci` will just build and run the tests once
|
|
187
|
+
|
|
188
|
+
If you need to pass extra flags to vitest (for example to run specific tests/files)
|
|
189
|
+
run `pnpm dev:grammar` in one terminal and `pnpm exec vitest ...` in another
|
|
190
|
+
|
|
191
|
+
Setting `PEGGY_TRACE=1` will enable tracing in the _built grammar file_.
|
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/helpers.ts
|
|
5
|
+
var VALID_NUMBER_REGEX = /^(0|([1-9][0-9]*))?(\.[0-9]+)?$/;
|
|
6
|
+
function autoCoerce(valStr) {
|
|
7
|
+
if (valStr === "true") return true;
|
|
8
|
+
if (valStr === "false") return false;
|
|
9
|
+
if (valStr === "undefined") return void 0;
|
|
10
|
+
if (VALID_NUMBER_REGEX.test(valStr)) return Number(valStr);
|
|
11
|
+
return valStr;
|
|
12
|
+
}
|
|
13
|
+
__name(autoCoerce, "autoCoerce");
|
|
14
|
+
|
|
15
|
+
// src/classes.ts
|
|
16
|
+
var ParsedEnvSpecDivider = class {
|
|
17
|
+
constructor(data) {
|
|
18
|
+
this.data = data;
|
|
19
|
+
}
|
|
20
|
+
static {
|
|
21
|
+
__name(this, "ParsedEnvSpecDivider");
|
|
22
|
+
}
|
|
23
|
+
toString() {
|
|
24
|
+
return `#${this.data.leadingSpace || ""}${this.data.contents}`;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var ParsedEnvSpecStaticValue = class {
|
|
28
|
+
constructor(data) {
|
|
29
|
+
this.data = data;
|
|
30
|
+
if (!data.quote) {
|
|
31
|
+
if (typeof data.rawValue === "string") {
|
|
32
|
+
const trimmed = data.rawValue.trim();
|
|
33
|
+
if (trimmed === "") this.value = void 0;
|
|
34
|
+
else this.value = autoCoerce(trimmed);
|
|
35
|
+
} else {
|
|
36
|
+
this.value = autoCoerce(data.rawValue);
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
const quoteChar = data.quote.substring(0, 1);
|
|
40
|
+
this.value = data.rawValue.slice(data.quote.length, -1 * data.quote.length).replaceAll(`\\${quoteChar}`, quoteChar);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
static {
|
|
44
|
+
__name(this, "ParsedEnvSpecStaticValue");
|
|
45
|
+
}
|
|
46
|
+
value;
|
|
47
|
+
get unescapedValue() {
|
|
48
|
+
if (typeof this.value !== "string") return this.value;
|
|
49
|
+
let unescaped = this.value;
|
|
50
|
+
if (this.data.quote !== "'") {
|
|
51
|
+
unescaped = unescaped.replaceAll("\\$", "$");
|
|
52
|
+
}
|
|
53
|
+
if (this.data.quote === '"' || this.data.quote === "`") {
|
|
54
|
+
unescaped = unescaped.replaceAll("\\n", "\n");
|
|
55
|
+
}
|
|
56
|
+
return unescaped;
|
|
57
|
+
}
|
|
58
|
+
toString() {
|
|
59
|
+
let strVal = String(this.value);
|
|
60
|
+
if (this.data.quote) {
|
|
61
|
+
strVal = strVal.replaceAll(this.data.quote, `\\${this.data.quote}`);
|
|
62
|
+
}
|
|
63
|
+
return `${this.data.quote || ""}${strVal}${this.data.quote || ""}`;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var ParsedEnvSpecKeyValuePair = class {
|
|
67
|
+
constructor(data) {
|
|
68
|
+
this.data = data;
|
|
69
|
+
}
|
|
70
|
+
static {
|
|
71
|
+
__name(this, "ParsedEnvSpecKeyValuePair");
|
|
72
|
+
}
|
|
73
|
+
get key() {
|
|
74
|
+
return this.data.key;
|
|
75
|
+
}
|
|
76
|
+
get value() {
|
|
77
|
+
return this.data.val;
|
|
78
|
+
}
|
|
79
|
+
toString() {
|
|
80
|
+
return `${this.key}=${this.data.val.toString()}`;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var ParsedEnvSpecFunctionArgs = class {
|
|
84
|
+
constructor(data) {
|
|
85
|
+
this.data = data;
|
|
86
|
+
}
|
|
87
|
+
static {
|
|
88
|
+
__name(this, "ParsedEnvSpecFunctionArgs");
|
|
89
|
+
}
|
|
90
|
+
get values() {
|
|
91
|
+
return this.data.values;
|
|
92
|
+
}
|
|
93
|
+
get simplifiedValues() {
|
|
94
|
+
if (this.data.values.length === 0) return [];
|
|
95
|
+
const vals = this.data.values;
|
|
96
|
+
if (vals.every((i) => i instanceof ParsedEnvSpecStaticValue)) {
|
|
97
|
+
return vals.map((val) => val.value);
|
|
98
|
+
} else if (vals.every((i) => i instanceof ParsedEnvSpecKeyValuePair)) {
|
|
99
|
+
const obj = {};
|
|
100
|
+
vals.forEach((val) => {
|
|
101
|
+
if (val.value instanceof ParsedEnvSpecStaticValue) {
|
|
102
|
+
obj[val.key] = val.value.value;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return obj;
|
|
106
|
+
} else {
|
|
107
|
+
throw new Error("Invalid function args");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
toString() {
|
|
111
|
+
let s = "(";
|
|
112
|
+
s += this.data.values.map((val) => val.toString()).join(", ");
|
|
113
|
+
s += ")";
|
|
114
|
+
return s;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var ParsedEnvSpecFunctionCall = class {
|
|
118
|
+
constructor(data) {
|
|
119
|
+
this.data = data;
|
|
120
|
+
}
|
|
121
|
+
static {
|
|
122
|
+
__name(this, "ParsedEnvSpecFunctionCall");
|
|
123
|
+
}
|
|
124
|
+
get name() {
|
|
125
|
+
return this.data.name;
|
|
126
|
+
}
|
|
127
|
+
get simplifiedArgs() {
|
|
128
|
+
return this.data.args.simplifiedValues;
|
|
129
|
+
}
|
|
130
|
+
toString() {
|
|
131
|
+
return this.data.name + this.data.args.toString();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
var ParsedEnvSpecDecorator = class {
|
|
135
|
+
constructor(data) {
|
|
136
|
+
this.data = data;
|
|
137
|
+
}
|
|
138
|
+
static {
|
|
139
|
+
__name(this, "ParsedEnvSpecDecorator");
|
|
140
|
+
}
|
|
141
|
+
get name() {
|
|
142
|
+
return this.data.name;
|
|
143
|
+
}
|
|
144
|
+
get bareFnArgs() {
|
|
145
|
+
if (this.data.valueOrFnArgs && this.data.valueOrFnArgs instanceof ParsedEnvSpecFunctionArgs) {
|
|
146
|
+
return this.data.valueOrFnArgs;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
get value() {
|
|
150
|
+
if (!this.data.valueOrFnArgs) {
|
|
151
|
+
return new ParsedEnvSpecStaticValue({ rawValue: true, isImplicit: true });
|
|
152
|
+
} else if (!(this.data.valueOrFnArgs instanceof ParsedEnvSpecFunctionArgs)) {
|
|
153
|
+
return this.data.valueOrFnArgs;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
get simplifiedValue() {
|
|
157
|
+
if (this.value instanceof ParsedEnvSpecStaticValue) {
|
|
158
|
+
return this.value.value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
toString() {
|
|
162
|
+
let s = `@${this.name}`;
|
|
163
|
+
if (!this.data.valueOrFnArgs) return s;
|
|
164
|
+
if (!(this.data.valueOrFnArgs instanceof ParsedEnvSpecFunctionArgs)) s += "=";
|
|
165
|
+
s += this.data.valueOrFnArgs.toString();
|
|
166
|
+
return s;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var ParsedEnvSpecComment = class {
|
|
170
|
+
constructor(data) {
|
|
171
|
+
this.data = data;
|
|
172
|
+
}
|
|
173
|
+
static {
|
|
174
|
+
__name(this, "ParsedEnvSpecComment");
|
|
175
|
+
}
|
|
176
|
+
get contents() {
|
|
177
|
+
return this.data.contents;
|
|
178
|
+
}
|
|
179
|
+
toString() {
|
|
180
|
+
return `#${this.data.leadingSpace || ""}${this.data.contents}`;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
var ParsedEnvSpecDecoratorComment = class {
|
|
184
|
+
constructor(data) {
|
|
185
|
+
this.data = data;
|
|
186
|
+
}
|
|
187
|
+
static {
|
|
188
|
+
__name(this, "ParsedEnvSpecDecoratorComment");
|
|
189
|
+
}
|
|
190
|
+
get decorators() {
|
|
191
|
+
return this.data.decorators;
|
|
192
|
+
}
|
|
193
|
+
get postComment() {
|
|
194
|
+
return this.data.postComment;
|
|
195
|
+
}
|
|
196
|
+
toString() {
|
|
197
|
+
let s = "#";
|
|
198
|
+
s += this.data.leadingSpace || "";
|
|
199
|
+
s += this.data.decorators.map((d) => d.toString()).join(" ");
|
|
200
|
+
if (this.data.postComment) s += ` ${this.data.postComment.toString()}`;
|
|
201
|
+
return s;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
function getDecoratorsObject(comments) {
|
|
205
|
+
const decObj = {};
|
|
206
|
+
comments.forEach((comment) => {
|
|
207
|
+
if (comment instanceof ParsedEnvSpecDecoratorComment) {
|
|
208
|
+
comment.decorators.forEach((decorator) => {
|
|
209
|
+
decObj[decorator.name] = decorator;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
return decObj;
|
|
214
|
+
}
|
|
215
|
+
__name(getDecoratorsObject, "getDecoratorsObject");
|
|
216
|
+
var ParsedEnvSpecCommentBlock = class {
|
|
217
|
+
constructor(data) {
|
|
218
|
+
this.data = data;
|
|
219
|
+
}
|
|
220
|
+
static {
|
|
221
|
+
__name(this, "ParsedEnvSpecCommentBlock");
|
|
222
|
+
}
|
|
223
|
+
get comments() {
|
|
224
|
+
return this.data.comments;
|
|
225
|
+
}
|
|
226
|
+
get divider() {
|
|
227
|
+
return this.data.divider || void 0;
|
|
228
|
+
}
|
|
229
|
+
get decoratorsObject() {
|
|
230
|
+
return getDecoratorsObject(this.data.comments);
|
|
231
|
+
}
|
|
232
|
+
toString() {
|
|
233
|
+
return [
|
|
234
|
+
...this.data.comments.map((comment) => comment.toString()),
|
|
235
|
+
...this.data.divider ? [this.data.divider.toString()] : []
|
|
236
|
+
].join("\n");
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
var ParsedEnvSpecBlankLine = class {
|
|
240
|
+
constructor(data) {
|
|
241
|
+
this.data = data;
|
|
242
|
+
}
|
|
243
|
+
static {
|
|
244
|
+
__name(this, "ParsedEnvSpecBlankLine");
|
|
245
|
+
}
|
|
246
|
+
toString() {
|
|
247
|
+
return "";
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
var ParsedEnvSpecConfigItem = class {
|
|
251
|
+
constructor(data) {
|
|
252
|
+
this.data = data;
|
|
253
|
+
}
|
|
254
|
+
static {
|
|
255
|
+
__name(this, "ParsedEnvSpecConfigItem");
|
|
256
|
+
}
|
|
257
|
+
expandedValue;
|
|
258
|
+
get key() {
|
|
259
|
+
return this.data.key;
|
|
260
|
+
}
|
|
261
|
+
get value() {
|
|
262
|
+
if (!this.data.value) {
|
|
263
|
+
return new ParsedEnvSpecStaticValue({ rawValue: void 0, isImplicit: true });
|
|
264
|
+
}
|
|
265
|
+
return this.data.value;
|
|
266
|
+
}
|
|
267
|
+
get decoratorsObject() {
|
|
268
|
+
return getDecoratorsObject([...this.data.preComments, this.data.postComment]);
|
|
269
|
+
}
|
|
270
|
+
get description() {
|
|
271
|
+
const regularComments = this.data.preComments.filter((comment) => comment instanceof ParsedEnvSpecComment);
|
|
272
|
+
return regularComments.map((comment) => comment.contents).join("\n");
|
|
273
|
+
}
|
|
274
|
+
processExpansion(_opts) {
|
|
275
|
+
if (this.data.value) {
|
|
276
|
+
const expanded = expand(this.data.value);
|
|
277
|
+
if (expanded instanceof ParsedEnvSpecKeyValuePair) throw new Error("Nested key-value pair found in config item");
|
|
278
|
+
this.expandedValue = expanded;
|
|
279
|
+
} else {
|
|
280
|
+
this.expandedValue = void 0;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
get resolverDef() {
|
|
284
|
+
if (!this.data.value) {
|
|
285
|
+
return {
|
|
286
|
+
type: "static",
|
|
287
|
+
value: void 0
|
|
288
|
+
};
|
|
289
|
+
} else if (this.data.value instanceof ParsedEnvSpecStaticValue) {
|
|
290
|
+
return {
|
|
291
|
+
type: "static",
|
|
292
|
+
value: this.data.value.value
|
|
293
|
+
};
|
|
294
|
+
} else if (this.data.value instanceof ParsedEnvSpecFunctionCall) {
|
|
295
|
+
return {
|
|
296
|
+
type: "function",
|
|
297
|
+
functionName: this.data.value.name,
|
|
298
|
+
functionArgs: this.data.value.simplifiedArgs
|
|
299
|
+
};
|
|
300
|
+
} else {
|
|
301
|
+
throw new Error("Unknown value resolver type");
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
toString() {
|
|
305
|
+
let s = "";
|
|
306
|
+
for (const comment of this.data.preComments) s += `${comment.toString()}
|
|
307
|
+
`;
|
|
308
|
+
s += `${this.key}=`;
|
|
309
|
+
if (this.data.value) s += `${this.data.value.toString()}`;
|
|
310
|
+
if (this.data.postComment) s += ` ${this.data.postComment.toString()}`;
|
|
311
|
+
return s;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
var ParsedEnvSpecFile = class {
|
|
315
|
+
static {
|
|
316
|
+
__name(this, "ParsedEnvSpecFile");
|
|
317
|
+
}
|
|
318
|
+
contents;
|
|
319
|
+
constructor(_contents) {
|
|
320
|
+
this.contents = _contents;
|
|
321
|
+
}
|
|
322
|
+
get configItems() {
|
|
323
|
+
return this.contents.filter((item) => item instanceof ParsedEnvSpecConfigItem);
|
|
324
|
+
}
|
|
325
|
+
get header() {
|
|
326
|
+
for (const item of this.contents) {
|
|
327
|
+
if (item instanceof ParsedEnvSpecCommentBlock && item.divider) {
|
|
328
|
+
return item;
|
|
329
|
+
} else if (!(item instanceof ParsedEnvSpecBlankLine)) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
get decoratorsObject() {
|
|
335
|
+
return this.header?.decoratorsObject ?? {};
|
|
336
|
+
}
|
|
337
|
+
toString() {
|
|
338
|
+
return this.contents.map((item) => item.toString()).join("\n");
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* simple helper to convert an object in a basic case
|
|
342
|
+
* mostly useful for comparison with other env parsers
|
|
343
|
+
* */
|
|
344
|
+
toSimpleObj() {
|
|
345
|
+
const obj = {};
|
|
346
|
+
for (const item of this.contents) {
|
|
347
|
+
if (item instanceof ParsedEnvSpecConfigItem) {
|
|
348
|
+
if (item.value instanceof ParsedEnvSpecStaticValue) {
|
|
349
|
+
obj[item.key] = item.value.value ?? "";
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return obj;
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
// src/expand.ts
|
|
358
|
+
var EXPAND_VAR_BRACKETED_REGEX = /(?<!\\)\${([a-zA-Z_][a-zA-Z0-9_.]*)((:?-)([^}]+))?}/g;
|
|
359
|
+
var EXPAND_VAR_SIMPLE_REGEX = /(?<!\\)\$([a-zA-Z_][a-zA-Z0-9_]*)/g;
|
|
360
|
+
var EXPAND_EXEC_REGEX = /\$\(([^)]+)\)/g;
|
|
361
|
+
function expandExecs(staticVal, _opts) {
|
|
362
|
+
if (typeof staticVal.value !== "string") return staticVal;
|
|
363
|
+
const quote = staticVal.data.quote;
|
|
364
|
+
const quoteStr = quote ?? "";
|
|
365
|
+
if (quote === "'") return staticVal;
|
|
366
|
+
const execMatches = Array.from(staticVal.value.matchAll(EXPAND_EXEC_REGEX));
|
|
367
|
+
if (execMatches.length === 0) return staticVal;
|
|
368
|
+
let lastIndex = 0;
|
|
369
|
+
const parts = [];
|
|
370
|
+
for (const match of execMatches) {
|
|
371
|
+
if (lastIndex < match.index) {
|
|
372
|
+
const preText = staticVal.value.slice(lastIndex, match.index);
|
|
373
|
+
parts.push(new ParsedEnvSpecStaticValue({ rawValue: `${quoteStr}${preText}${quoteStr}`, quote }));
|
|
374
|
+
}
|
|
375
|
+
const shellCmd = match[1];
|
|
376
|
+
parts.push(new ParsedEnvSpecFunctionCall({
|
|
377
|
+
name: "exec",
|
|
378
|
+
args: new ParsedEnvSpecFunctionArgs({
|
|
379
|
+
values: [new ParsedEnvSpecStaticValue({ rawValue: `${quoteStr}${shellCmd}${quoteStr}`, quote })]
|
|
380
|
+
})
|
|
381
|
+
}));
|
|
382
|
+
lastIndex = match.index + match[0].length;
|
|
383
|
+
}
|
|
384
|
+
if (lastIndex < staticVal.value.length) {
|
|
385
|
+
const postText = staticVal.value.slice(lastIndex);
|
|
386
|
+
parts.push(new ParsedEnvSpecStaticValue({ rawValue: `${quoteStr}${postText}${quoteStr}`, quote }));
|
|
387
|
+
}
|
|
388
|
+
if (parts.length === 1) return parts[0];
|
|
389
|
+
return new ParsedEnvSpecFunctionCall({
|
|
390
|
+
name: "concat",
|
|
391
|
+
args: new ParsedEnvSpecFunctionArgs({
|
|
392
|
+
values: parts
|
|
393
|
+
})
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
__name(expandExecs, "expandExecs");
|
|
397
|
+
function expandRefs(staticVal, mode) {
|
|
398
|
+
if (typeof staticVal.value !== "string") return staticVal;
|
|
399
|
+
const quote = staticVal.data.quote;
|
|
400
|
+
const quoteStr = quote ?? "";
|
|
401
|
+
if (quote === "'") return staticVal;
|
|
402
|
+
const varMatches = Array.from(staticVal.value.matchAll(mode === "simple" ? EXPAND_VAR_SIMPLE_REGEX : EXPAND_VAR_BRACKETED_REGEX));
|
|
403
|
+
if (varMatches.length === 0) return staticVal;
|
|
404
|
+
let lastIndex = 0;
|
|
405
|
+
const parts = [];
|
|
406
|
+
for (const match of varMatches) {
|
|
407
|
+
if (lastIndex < match.index) {
|
|
408
|
+
const preText = staticVal.value.slice(lastIndex, match.index);
|
|
409
|
+
parts.push(new ParsedEnvSpecStaticValue({ rawValue: `${quoteStr}${preText}${quoteStr}`, quote }));
|
|
410
|
+
}
|
|
411
|
+
const varName = match[1];
|
|
412
|
+
let defaultVal;
|
|
413
|
+
if (mode === "bracketed") {
|
|
414
|
+
match[3];
|
|
415
|
+
defaultVal = match[4];
|
|
416
|
+
}
|
|
417
|
+
const refFnCall = new ParsedEnvSpecFunctionCall({
|
|
418
|
+
name: "ref",
|
|
419
|
+
args: new ParsedEnvSpecFunctionArgs({
|
|
420
|
+
values: [new ParsedEnvSpecStaticValue({ rawValue: varName })]
|
|
421
|
+
})
|
|
422
|
+
});
|
|
423
|
+
if (defaultVal) {
|
|
424
|
+
parts.push(new ParsedEnvSpecFunctionCall({
|
|
425
|
+
name: "fallback",
|
|
426
|
+
args: new ParsedEnvSpecFunctionArgs({
|
|
427
|
+
values: [refFnCall, new ParsedEnvSpecStaticValue({ rawValue: `${quoteStr}${defaultVal}${quoteStr}`, quote })]
|
|
428
|
+
})
|
|
429
|
+
}));
|
|
430
|
+
} else {
|
|
431
|
+
parts.push(refFnCall);
|
|
432
|
+
}
|
|
433
|
+
lastIndex = match.index + match[0].length;
|
|
434
|
+
}
|
|
435
|
+
if (lastIndex < staticVal.value.length) {
|
|
436
|
+
const postText = staticVal.value.slice(lastIndex);
|
|
437
|
+
parts.push(new ParsedEnvSpecStaticValue({ rawValue: `${quoteStr}${postText}${quoteStr}`, quote }));
|
|
438
|
+
}
|
|
439
|
+
if (parts.length === 1) return parts[0];
|
|
440
|
+
return new ParsedEnvSpecFunctionCall({
|
|
441
|
+
name: "concat",
|
|
442
|
+
args: new ParsedEnvSpecFunctionArgs({
|
|
443
|
+
values: parts
|
|
444
|
+
})
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
__name(expandRefs, "expandRefs");
|
|
448
|
+
function expandHelper(val, expandStaticFn) {
|
|
449
|
+
if (val instanceof ParsedEnvSpecFunctionCall) {
|
|
450
|
+
const fnName = val.name;
|
|
451
|
+
const newConcatArgs = [];
|
|
452
|
+
val.data.args.values.forEach((v) => {
|
|
453
|
+
const expandedArg = expandHelper(v, expandStaticFn);
|
|
454
|
+
if (fnName === "concat" && expandedArg instanceof ParsedEnvSpecFunctionCall && expandedArg.name === "concat") {
|
|
455
|
+
newConcatArgs.push(...expandedArg.data.args.values);
|
|
456
|
+
} else {
|
|
457
|
+
newConcatArgs.push(expandedArg);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
return new ParsedEnvSpecFunctionCall({
|
|
461
|
+
name: fnName,
|
|
462
|
+
args: new ParsedEnvSpecFunctionArgs({
|
|
463
|
+
values: newConcatArgs
|
|
464
|
+
})
|
|
465
|
+
});
|
|
466
|
+
} else if (val instanceof ParsedEnvSpecKeyValuePair) {
|
|
467
|
+
const expandedVal = expandHelper(val.value, expandStaticFn);
|
|
468
|
+
if (expandedVal instanceof ParsedEnvSpecKeyValuePair) throw new Error("Nested key-value pair found in concat");
|
|
469
|
+
return new ParsedEnvSpecKeyValuePair({
|
|
470
|
+
key: val.key,
|
|
471
|
+
val: expandedVal
|
|
472
|
+
});
|
|
473
|
+
} else if (val instanceof ParsedEnvSpecStaticValue) {
|
|
474
|
+
if (typeof val.value !== "string") return val;
|
|
475
|
+
if (val.data.quote === "'") return val;
|
|
476
|
+
return expandStaticFn(val);
|
|
477
|
+
}
|
|
478
|
+
throw new Error("Unknown value type");
|
|
479
|
+
}
|
|
480
|
+
__name(expandHelper, "expandHelper");
|
|
481
|
+
function expand(val, _opts) {
|
|
482
|
+
let expandedVal = val;
|
|
483
|
+
expandedVal = expandHelper(expandedVal, (v) => expandExecs(v));
|
|
484
|
+
expandedVal = expandHelper(expandedVal, (v) => expandRefs(v, "simple"));
|
|
485
|
+
expandedVal = expandHelper(expandedVal, (v) => expandRefs(v, "bracketed"));
|
|
486
|
+
return expandedVal;
|
|
487
|
+
}
|
|
488
|
+
__name(expand, "expand");
|
|
489
|
+
|
|
490
|
+
export { ParsedEnvSpecBlankLine, ParsedEnvSpecComment, ParsedEnvSpecCommentBlock, ParsedEnvSpecConfigItem, ParsedEnvSpecDecorator, ParsedEnvSpecDecoratorComment, ParsedEnvSpecDivider, ParsedEnvSpecFile, ParsedEnvSpecFunctionArgs, ParsedEnvSpecFunctionCall, ParsedEnvSpecKeyValuePair, ParsedEnvSpecStaticValue, __name, expand };
|
|
491
|
+
//# sourceMappingURL=chunk-FWOHTEQS.mjs.map
|
|
492
|
+
//# sourceMappingURL=chunk-FWOHTEQS.mjs.map
|