Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: Simplify dependencies, support node 14+ #2

Merged
merged 4 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

32 changes: 32 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
node-version: [14.x, 16.x, 18.x, 19.x]
arch: [ARM64, X64]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"
- run: yarn install
- run: yarn test
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

33 changes: 0 additions & 33 deletions appveyor.yml

This file was deleted.

168 changes: 168 additions & 0 deletions bin/unienv
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#!/usr/bin/env node
IanVS marked this conversation as resolved.
Show resolved Hide resolved

/*! lazy-universal-dotenv v3.0.1 by Storybook Team */
'use strict';

var crossEnv = require('cross-env');
var yargs = require('yargs');
require('core-js/modules/es.error.cause.js');
var fs = require('fs');
var path = require('path');
var appRoot = require('app-root-dir');
var dotenv = require('dotenv');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }

var crossEnv__default = /*#__PURE__*/_interopDefaultLegacy(crossEnv);
var yargs__default = /*#__PURE__*/_interopDefaultLegacy(yargs);
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var appRoot__default = /*#__PURE__*/_interopDefaultLegacy(appRoot);

/* eslint-disable unicorn/no-unsafe-regex */
// It should be fine to have unsafe regex (with maybe exponential time) here as this is
// only triggered once on start and is defined by admin or devops

/**
* Expand environment variables
* Based upon https://github.com/motdotla/dotenv-expand
* but does not change process.env itself
*/
function expandSingleValue(envValue, environment, processEnvironment) {
const matches = envValue && envValue.match(/(.?\${?(?:\w+)?}?)/g) || [];
return matches.reduce((prev, match) => {
const matchParts = /(.?)\${?(\w+)?}?/g.exec(match);
const prefix = matchParts[1];
const key = matchParts[2];
if (key === undefined) {
// Looks like a matcher but has no key, so no change
return prev;
}
let value;
let replacePart;
if (prefix === "\\") {
// Dollar sign is prefixed so no expansion
replacePart = matchParts[0];
value = replacePart.replace("\\$", "$");
} else {
// Found expansion matcher, so expand
replacePart = matchParts[0].slice(prefix.length);

// processEnvironment wins over environment
value = (processEnvironment == null ? void 0 : processEnvironment[key]) ?? environment[key] ?? "";

// Resolve recursive interpolations
value = expandSingleValue(value, environment, processEnvironment);
}
return prev.replace(replacePart, value);
}, envValue);
}

/**
* Expands environment map
*
* - ${VARNAME}
* - $VARNAME
*
* @param environment Environment map to expand
* @param processEnvironment usually process.env to let command line ENV win over .env files
*/
function expandEnvironment(environment, processEnvironment = process.env) {
return Object.entries(environment).reduce((prev, [envKey, envValue]) => ({
...prev,
[envKey]: expandSingleValue(envValue, environment, processEnvironment)
}), {});
}

function flattenAndUnique(keys) {
const unique = new Set([].concat(...keys));
return [...unique];
}
function init() {
const dotEnvBase = path__default.join(appRoot__default.get(), ".env");

// Cache Node environment at load time. We have to do it to make
// sure that the serialization, which might happen later, is in sync
// with the parsing of the conditional NODE_ENV files now.
const NODE_ENV = process.env.NODE_ENV;

// Can be use for values like e.g. "client" or "server". Or `docker`. Or `staging`.
const ENV_CONTEXT = process.env.ENV_CONTEXT;

// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
const dotenvFiles = [ENV_CONTEXT && NODE_ENV && `${dotEnvBase}.${ENV_CONTEXT}.${NODE_ENV}.local`, ENV_CONTEXT && NODE_ENV && `${dotEnvBase}.${ENV_CONTEXT}.${NODE_ENV}`, ENV_CONTEXT && NODE_ENV !== "test" && `${dotEnvBase}.${ENV_CONTEXT}.local`, ENV_CONTEXT && `${dotEnvBase}.${ENV_CONTEXT}`, NODE_ENV && `${dotEnvBase}.${NODE_ENV}.local`, NODE_ENV && `${dotEnvBase}.${NODE_ENV}`, NODE_ENV !== "test" && `${dotEnvBase}.local`, dotEnvBase].filter(Boolean).filter(fs__default.existsSync);
const resultKeys = [];
// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set. Variable expansion is supported in .env files.
// https://github.com/motdotla/dotenv
dotenvFiles.forEach(dotenvFile => {
resultKeys.push(Object.keys(dotenv.config({
path: dotenvFile
}).parsed));
});

// Catch all keys from any .env file processed and merge with environment value from
// process.env. This is set by dotenv package.
const environmentItems = flattenAndUnique(resultKeys).reduce((prev, envKey) => ({
...prev,
[envKey]: process.env[envKey]
}), {});
const expandedEnvironment = expandEnvironment(environmentItems);
for (const [envKey, envValue] of Object.entries(expandedEnvironment)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
process.env[envKey] = envValue;
}
if (process.env.APP_ROOT == null) {
const detectedRoot = appRoot__default.get();
try {
process.env.APP_ROOT = detectedRoot;
} catch {
throw new Error("Universal-DotEnv requires a writable process.env! " + "Please make sure that this code is not transpiled with Webpack.");
}
}
if (process.env.APP_SOURCE == null) {
const sourceFolder = path__default.join(process.env.APP_ROOT || "", "src");
process.env.APP_SOURCE = fs__default.existsSync(sourceFolder) ? sourceFolder : process.env.APP_ROOT;
}
}

function getParameter() {
const args = yargs__default.option("context", {
alias: "c",
type: "string"
}).option("verbose", {
alias: "v",
default: false,
type: "boolean"
}).argv;
const {
context,
verbose,
_
} = args;
return {
context,
verbose,
command: _
};
}
function executeCommand() {
const args = getParameter();
if (args.context) {
process.env.ENV_CONTEXT = args.context;
}
if (args.verbose) {
process.stderr.write("Load .env files...\n");
}
init();
if (args.verbose) {
process.stderr.write(`Execute "${args.command.join(" ")}"...\n`);
}
crossEnv__default(args.command);
}
executeCommand();
//# sourceMappingURL=unienv.map
Loading