@@ -8,6 +8,10 @@ const { rm, isString, isBoolean, isObject, isNumber, isUndefined, match } = requ
88const utf8 = require ( 'is-utf8' )
99const Ware = require ( 'ware' )
1010
11+ const symbol = {
12+ env : Symbol ( 'env' )
13+ }
14+
1115/**
1216 * Metalsmith representation of the files in `metalsmith.source()`.
1317 * The keys represent the file paths and the values are {@link File} objects
@@ -97,7 +101,7 @@ function Metalsmith(directory) {
97101 this . metadata ( { } )
98102 this . source ( 'src' )
99103 this . destination ( 'build' )
100- this . env ( { } )
104+ this [ symbol . env ] = Object . create ( null )
101105 this . concurrency ( Infinity )
102106 this . clean ( true )
103107 this . frontmatter ( true )
@@ -297,7 +301,7 @@ Metalsmith.prototype.match = function (patterns, input, options) {
297301}
298302
299303/**
300- * Get or set one or multiple metalsmith environment variables.
304+ * Get or set one or multiple metalsmith environment variables. Metalsmith env vars are case-insensitive.
301305 * @param {string|Object } [vars] name of the environment variable, or an object with `{ name: 'value' }` pairs
302306 * @param {string|number|boolean } [value] value of the environment variable
303307 * @example
@@ -317,29 +321,21 @@ Metalsmith.prototype.match = function (patterns, input, options) {
317321 * })
318322 */
319323Metalsmith . prototype . env = function ( vars , value ) {
320- if ( ! arguments . length ) return this . _env
321-
322- if ( arguments . length === 1 ) {
323- assert (
324- isString ( vars ) || isObject ( vars ) ,
325- 'You must pass an environment variable name, or an object with name:value pairs'
326- )
327- if ( isString ( vars ) ) {
328- return this . _env [ vars ]
324+ if ( isString ( vars ) ) {
325+ if ( arguments . length === 1 ) {
326+ return this [ symbol . env ] [ vars . toUpperCase ( ) ]
329327 }
330- if ( isObject ( vars ) ) {
331- this . _env = Object . assign ( this . _env || Object . create ( null ) , vars )
328+ if ( ! ( isFunction ( value ) || isObject ( value ) ) ) {
329+ this [ symbol . env ] [ vars . toUpperCase ( ) ] = value
332330 return this
333331 }
332+ throw new TypeError ( 'Environment variable values can only be primitive: Number, Boolean, String or null' )
334333 }
335- if ( arguments . length === 2 ) {
336- assert (
337- isString ( vars ) && ( isString ( value ) || isNumber ( value ) || isBoolean ( value ) ) ,
338- 'Environment variable values can only be primitives, no objects or functions'
339- )
340- this . _env [ vars ] = value
334+ if ( isObject ( vars ) ) {
335+ Object . entries ( vars ) . forEach ( ( [ key , value ] ) => this . env ( key , value ) )
341336 return this
342337 }
338+ if ( isUndefined ( vars ) ) return Object . assign ( Object . create ( null ) , this [ symbol . env ] )
343339}
344340
345341/**
0 commit comments