Skip to content

Commit

Permalink
replace dotenv with custom solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelvesavuori committed Sep 8, 2021
1 parent 67c619e commit ef03104
Show file tree
Hide file tree
Showing 156 changed files with 1,423 additions and 1,259 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -36,7 +36,7 @@ hint-report/
# Dependency directories
/**/node_modules

# dotenv environment variables file(s)
# Environment variables file(s)
.env
.env.*

Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -80,7 +80,7 @@ Here are a few reasons you'd want to use Figmagic rather than anything similar:

- In Figmagic, design tokens are a first-class concept since day 1
- Figmagic is designer-driven, since Figma is seen as the source of truth
- Figmagic is automatable and very lightweight (~40kb compressed), with only a single external dependency (`dotenv`)
- Figmagic is automatable and very lightweight (~40kb compressed), with no external dependencies
- Figmagic is developer-friendly and makes very few assumptions on your use and/or setup and supports a range of output formats plus that it's extensible through custom templates if you need something completely different
- Figmagic is open-sourced under the MIT license and has a long track record of very short implementation cycles for new features/fixes
- Generated React components bind to any token values you've defined so these are equally useful as starter code or as code you continually output
Expand Down Expand Up @@ -286,7 +286,7 @@ There are several ways in which you can provide Figmagic with knowledge about ho
You can combine them, but beware of the below prioritization chart (from lowest to highest):

1. User-provided configuration from `figmagic.json`/`.figmagicrc` file
2. Environment variables (also loaded from `.env` file using `dotenv`)
2. Environment variables (also loaded from `.env` file)
3. Command-line arguments and flags

If possible, stick to one way of providing settings.
Expand Down
5 changes: 2 additions & 3 deletions __tests__/frameworks/network/getData.test.ts
@@ -1,8 +1,7 @@
import dotenv from 'dotenv';

import { loadEnv } from '../../../bin/frameworks/system/loadEnv';
import { getData } from '../../../bin/frameworks/network/getData';

dotenv.config();
loadEnv();

describe('Failure cases', () => {
test('It should throw an error if no argument is provided', async () => {
Expand Down
5 changes: 2 additions & 3 deletions __tests__/frameworks/network/getDataRemote.test.ts
@@ -1,8 +1,7 @@
import dotenv from 'dotenv';

import { loadEnv } from '../../../bin/frameworks/system/loadEnv';
import { getDataRemote } from '../../../bin/frameworks/network/getDataRemote';

dotenv.config();
loadEnv();

describe('Failure cases', () => {
test('It should throw an error if no argument is provided', async () => {
Expand Down
5 changes: 2 additions & 3 deletions __tests__/frameworks/network/getFromApi.test.ts
@@ -1,8 +1,7 @@
import dotenv from 'dotenv';

import { loadEnv } from '../../../bin/frameworks/system/loadEnv';
import { getFromApi } from '../../../bin/frameworks/network/getFromApi';

dotenv.config();
loadEnv();

describe('Failure cases', () => {
test('It should throw an error when receiving invalid token and/or URL', async () => {
Expand Down
5 changes: 3 additions & 2 deletions __tests__/usecases/createElements.test.ts
@@ -1,14 +1,15 @@
import * as fs from 'fs';
import trash from 'trash';
import dotenv from 'dotenv';

import { createElements } from '../../bin/usecases/createElements';

import { loadEnv } from '../../bin/frameworks/system/loadEnv';

import { testConfig } from '../../testdata/testConfig';
import { figmaTestResponse } from '../../testdata/figmaTestResponse';
import { figmaCompleteCleaned } from '../../testdata/figma-complete-cleaned';

dotenv.config();
loadEnv();

describe('Failure cases', () => {
test('It should throw an error if no argument is provided', async () => {
Expand Down
5 changes: 3 additions & 2 deletions __tests__/usecases/createGraphics.test.ts
@@ -1,13 +1,14 @@
import * as fs from 'fs';
import trash from 'trash';
import dotenv from 'dotenv';

import { createGraphics } from '../../bin/usecases/createGraphics';

import { loadEnv } from '../../bin/frameworks/system/loadEnv';

import { testConfig } from '../../testdata/testConfig';
import { figmaTestResponse } from '../../testdata/figmaTestResponse';

dotenv.config();
loadEnv();

describe('Failure cases', () => {
test('It should throw an error if no argument is provided', async () => {
Expand Down
@@ -1,12 +1,12 @@
import dotenv from 'dotenv';

import { Config } from '../../../../bin/contracts/Config';

import { processGraphics } from '../../../../bin/usecases/interactors/graphics/processGraphics';

import { loadEnv } from '../../../../bin/frameworks/system/loadEnv';

import { graphicsPage } from '../../../../testdata/graphicsPage';

dotenv.config();
loadEnv();

// Re-ordered success and failure cases because the "normal" order will leak variables or something, causing tests to fail

Expand Down
25 changes: 25 additions & 0 deletions bin/frameworks/system/loadEnv.ts
@@ -0,0 +1,25 @@
import fs from 'fs';

/**
* @description Loads Figmagic environment variables if an `.env` file exists.
*/
export function loadEnv(): void {
try {
const filePath = `${process.cwd()}/.env`;
if (fs.existsSync(filePath)) {
const file = fs.readFileSync(filePath, 'utf-8');

const variables = file
.toString()
.split('\n')
.filter((i: string) => i.startsWith('FIGMA_TOKEN') || i.startsWith('FIGMA_URL'));

variables.forEach((variable: string) => {
const [key, value] = variable.split('=');
process.env[key] = value;
});
}
} catch (error: any) {
throw Error(error);
}
}
1 change: 1 addition & 0 deletions build/bin/frameworks/system/loadEnv.d.ts
@@ -0,0 +1 @@
export declare function loadEnv(): void;
2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions index.ts
@@ -1,7 +1,6 @@
#!/usr/bin/env node

import * as path from 'path';
import * as dotenv from 'dotenv';

import { makeConfiguration } from './bin/entities/Config/index';

Expand All @@ -10,6 +9,7 @@ import { Config } from './bin/contracts/Config';

import { FigmagicController } from './bin/controllers/FigmagicController';

import { loadEnv } from './bin/frameworks/system/loadEnv';
import { write } from './bin/frameworks/filesystem/write';
import { getData } from './bin/frameworks/network/getData';
import { writeBaseJson } from './bin/frameworks/filesystem/writeBaseJson';
Expand All @@ -27,7 +27,7 @@ const RC_FILES = ['figmagic.json', '.figmagicrc'];
async function main(): Promise<void> {
try {
// Setup environment and user configuration
dotenv.config();
loadEnv();

// Use first valid match for configuration file
const configFilePath: any = RC_FILES.filter((configFile: string) => {
Expand Down

0 comments on commit ef03104

Please sign in to comment.