Skip to content

Commit

Permalink
esbuild: fix some issues with defines.js and add test (#1091)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-mayrgundter authored Apr 4, 2024
1 parent 8ef204e commit 6902431
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bldrs",
"version": "1.0.945",
"version": "1.0.953",
"main": "src/index.jsx",
"license": "MIT",
"homepage": "https://github.com/bldrs-ai/Share",
Expand Down Expand Up @@ -36,6 +36,7 @@
"lint-tools": "yarn eslint cypress tools",
"precommit": "yarn lint && yarn test",
"serve": "SHARE_CONFIG=dev yarn serve-share-conway",
"serve-prod": "SHARE_CONFIG=prod yarn serve-share-conway",
"serve-cosmos": "cosmos --config .cosmos.config.json",
"serve-share-conway": "run-script-os",
"serve-share-conway:win32": "yarn build-share-conway && set USE_WEBIFC_SHIM=true&& node tools/esbuild/serve.js",
Expand Down
15 changes: 10 additions & 5 deletions tools/esbuild/defines.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ import cypress from './vars.cypress.js'
import prod from './vars.prod.js'


// Exported for testing only
/** @return {Object<string,string>} */
function zipEnvWithConfig(config) {
export function zipEnvWithConfig(config) {
const defines = {}
Object.keys(config).forEach((name) => {
let val = parse(process.env[name])
if (val === undefined) {
val = config[name] || null
val = config[name]
if (val === undefined) {
val = null
}
}
defines[`process.env.${name}`] = str(val)
})
return defines
}


// Exported for testing only
/**
* Convert simple env var strings to js types
*
* @return {boolean|number|string}
*/
function parse(envStr) {
export function parse(envStr) {
if (envStr === undefined || envStr === 'undefined') {
return undefined
} else if (envStr === null || envStr === 'null') {
Expand All @@ -31,7 +36,7 @@ function parse(envStr) {
return false
} else if (envStr.toLowerCase() === 'true') {
return true
} else if (isFinite(parseInt(envStr))) {
} else if (isFinite(parseInt(envStr)) && envStr === Number(parseInt(envStr))) {
return parseInt(envStr)
} else if (isFinite(parseFloat(envStr))) {
return parseFloat(envStr)
Expand All @@ -48,7 +53,7 @@ let config
switch (process.env.SHARE_CONFIG) {
case 'dev': config = dev; break
case 'cypress': config = cypress; break
case 'prod': break // fallthru
case 'prod': // fallthru
default: config = prod; break
}

Expand Down
76 changes: 76 additions & 0 deletions tools/esbuild/defines.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {parse, zipEnvWithConfig} from './defines.js'


describe('defines', () => {
it('parses env vars correctly', () => {
expect(parse(undefined)).toBe(undefined)
expect(parse('undefined')).toBe(undefined)
expect(parse('null')).toBe(null)
expect(parse(null)).toBe(null)
expect(parse('true')).toBe(true)
expect(parse('false')).toBe(false)
expect(() => parse(true)).toThrow(TypeError)
expect(() => parse(false)).toThrow(TypeError)
expect(parse('1')).toBe(1)
// eslint-disable-next-line no-magic-numbers
expect(parse('1.1')).toBe(1.1)
expect(parse('str')).toBe('str')
})

describe('with config', () => {
const config = {
isBool1: true,
isBool2: false,
isUndef: undefined,
isNull: null,
isInt: 1,
isFloat: 1.1,
isStr: 'str',
}

const defines = {
'process.env.isBool1': 'true',
'process.env.isBool2': 'false',
'process.env.isUndef': 'null',
'process.env.isNull': 'null',
'process.env.isInt': '1',
'process.env.isFloat': '1.1',
'process.env.isStr': '"str"',
}

beforeEach(() => {
delete process.env['isBool1']
delete process.env['isBool2']
delete process.env['isUndef']
delete process.env['isNull']
delete process.env['isUndef']
delete process.env['isInt']
delete process.env['isFloat']
delete process.env['isStr']
})

it('zipEnvWithConfig, no overrides', () => {
expect(zipEnvWithConfig(config)).toStrictEqual(defines)
})

it('zipEnvWithConfig, with overrides', () => {
// Purposely override with different values/types
process.env.isBool1 = 'false'
process.env.isBool2 = 'true'
process.env.isUndef = 'foo'
process.env.isNull = 'foo'
process.env.isInt = '2'
process.env.isFloat = '2.2'
process.env.isStr = 'str2'
expect(zipEnvWithConfig(config)).toStrictEqual({
'process.env.isBool1': 'false',
'process.env.isBool2': 'true',
'process.env.isUndef': '"foo"',
'process.env.isNull': '"foo"',
'process.env.isInt': '2',
'process.env.isFloat': '2.2',
'process.env.isStr': '"str2"',
})
})
})
})

0 comments on commit 6902431

Please sign in to comment.