Skip to content

Commit

Permalink
experimental
Browse files Browse the repository at this point in the history
Signed-off-by: iswilljr <iswilljr@gmail.com>
  • Loading branch information
iswilljr committed Jul 30, 2022
1 parent a94f7cb commit bcf3100
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 1,629 deletions.
17 changes: 0 additions & 17 deletions .eslintrc.json

This file was deleted.

6 changes: 0 additions & 6 deletions .prettierignore

This file was deleted.

7 changes: 5 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"printWidth": 120,
"useTabs": false,
"useTabs": true,
"tabWidth": 2,
"semi": true
"semi": false,
"singleQuote": true,
"bracketSpacing": false,
"arrowParens": "avoid"
}
123 changes: 13 additions & 110 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,122 +7,25 @@ A command line to generate a .env file based on a Config
```bash
# npm
npm install -g config-env-file

# yarn
yarn global add config-env-file -D
```

## Usage

```bash
cef /path/to/your/config/file [options]
```

## Examples

### Basic example

In this example we have the next config file:

```json
// File path: ./config.json
{
"apiKey": "example",
"authDomain": "example.firebaseapp.com",
"projectId": "example",
"storageBucket": "example.appspot.com",
"messagingSenderId": "example",
"appId": "1:example:web:example"
}
yarn global add config-env-file
```

Then, run the next command:
## Example

```bash
cef ./config.json
# config.json: { "api": 1, "key": 2 }
# output:
# const config = { api: process.env.API, key: process.env.KEY }
# .env.local:
# API=1
# KEY=2
```

This will generate a .env.local file in root of our project, like this:

```env
API_KEY=example
AUTH_DOMAIN=example.firebaseapp.com
PROJECT_ID=example
STORAGE_BUCKET=example.appspot.com
MESSAGING_SENDER_ID=example
APP_ID=1:example:web:example
```
## Options

Output:

```javascript
// Check your .env.local file
// Your config:
const config = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID,
appId: process.env.APP_ID
}
```

### Full example

In this example we have the same config in the previous example, plus we have the next .env.local file:

```env
DOMAIN=http://localhost:3001
```

Then, run the next command:

```bash
cef ./config.json -d . -e development -t next_public -E import -m .env.local
```

Options:

```javascript
./config.json // file path
-d . // destination => current directory
-e development // file extension => .env.development
-t next_public // KEY => NEXT_PUBLIC_KEY
-E import // output => import.meta.env.KEY
-m .env.local // merge with an existing env file
```

This will merge our .env.local file with the config and generate a .env.development file in root of our project, like this:

```env
NEXT_PUBLIC_DOMAIN=http://localhost:3001
NEXT_PUBLIC_API_KEY=example
NEXT_PUBLIC_AUTH_DOMAIN=example.firebaseapp.com
NEXT_PUBLIC_PROJECT_ID=example
NEXT_PUBLIC_STORAGE_BUCKET=example.appspot.com
NEXT_PUBLIC_MESSAGING_SENDER_ID=example
NEXT_PUBLIC_APP_ID=1:example:web:example
```

Output:

```javascript
// Check your .env.development file
// Your config:
const config = {
domain: import.meta.env.NEXT_PUBLIC_DOMAIN,
apiKey: import.meta.env.NEXT_PUBLIC_API_KEY,
authDomain: import.meta.env.NEXT_PUBLIC_AUTH_DOMAIN,
projectId: import.meta.env.NEXT_PUBLIC_PROJECT_ID,
storageBucket: import.meta.env.NEXT_PUBLIC_STORAGE_BUCKET,
messagingSenderId: import.meta.env.NEXT_PUBLIC_MESSAGING_SENDER_ID,
appId: import.meta.env.NEXT_PUBLIC_APP_ID
}
```

## Help

```bash
cef --help
```
- `destination`: destination path to env file. default `.`.
- `extension`: extension to env file name. default `local`.
- `prefix`: prefix to variables name. default `""`.
- `env`: how to access variables. choices `process`, `import`. default `process`.
8 changes: 0 additions & 8 deletions config.json

This file was deleted.

11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { statSync } from "fs"

const paths = ["lib/index.js", "lib/bin.js", "lib/index.d.ts", "package.json", "README.md"]
let size = 0;
for (const path of paths) {
const stats = statSync(path)
console.log(`${path}: ${stats.size < 1000 ? stats.size : stats.size / 1000}${stats.size<1000?"B":"KB"}`)
size += stats.size
}
console.log()
console.log(`Total: ${size} -> ${size < 1000 ? size : size / 1000}${size<1000?"B":"KB"}`)
94 changes: 35 additions & 59 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,37 @@
{
"name": "config-env-file",
"version": "3.1.0",
"description": "A command line to generate a .env file based on a Config",
"main": "lib/index.js",
"bin": {
"cef": "lib/bin.js"
},
"scripts": {
"build": "yarn typecheck && rimraf lib && tsc",
"typecheck": "tsc --noEmit",
"prepare": "yarn build"
},
"keywords": [
"cli",
"commander",
"dotenv",
"env",
"config",
"env-file"
],
"author": "iswilljr",
"license": "ISC",
"devDependencies": {
"@types/node": "^17.0.29",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.2.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.0.0",
"prettier": "^2.6.2",
"rimraf": "^3.0.2",
"typescript": "^4.7.4"
},
"dependencies": {
"camel-case": "^4.1.2",
"commander": "^9.2.0",
"constant-case": "^3.0.4",
"dotenv": "^16.0.1"
},
"files": [
"lib",
"README.md"
],
"types": "./lib/index.d.ts",
"directories": {
"lib": "lib"
},
"repository": {
"type": "git",
"url": "git+https://github.com/iswilljr/config-env-file.git"
},
"bugs": {
"url": "https://github.com/iswilljr/config-env-file/issues"
},
"homepage": "https://github.com/iswilljr/config-env-file#readme"
"name": "config-env-file",
"version": "0.0.2-experimental",
"description": "A command line to generate a .env file based on a Config",
"license": "ISC",
"main": "lib/index.js",
"author": "iswilljr",
"type": "module",
"types": "./lib/index.d.ts",
"files": [
"lib",
"README.md"
],
"bin": {
"cef": "lib/bin.js"
},
"scripts": {
"build": "tsc && rimraf lib/bin.d.ts && prettier lib --write",
"prepublishOnly": "yarn build"
},
"keywords": [
"config",
"env"
],
"dependencies": {
"no-case": "^3.0.4",
"sade": "^1.8.1"
},
"devDependencies": {
"@types/node": "17.0.29",
"prettier": "2.6.2",
"rimraf": "3.0.2",
"typescript": "4.7.4"
},
"repository": "git+https://github.com/iswilljr/config-env-file.git",
"bugs": "https://github.com/iswilljr/config-env-file/issues"
}
30 changes: 13 additions & 17 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#!/usr/bin/env node
import { Option, program } from "commander";
import configEnvFile from ".";

program.version("3.0.1").description("A command line to generate a .env.local based on a Config");

program.argument("<file>", "The config file to use");
program.option("-d, --destination <destination>", "Destination path to env file");
program.option("-e, --extension <extension>", "extension to env file name (example: env.prod, env.local)");
program.option("-t, --template <template>", "Template to vars' name (example: REACT_APP_KEY, VITE_APP_API)");
program.addOption(
new Option("-E, --env <type>", "Env (example: process.env, import.meta.env)").choices(["process", "import"])
);
program.option("-m, --merge <file>", "merge a given env file with the new config");

program.action(configEnvFile);

program.parse(process.argv);
import {createRequire} from 'module'
import sade from 'sade'
import {configEnvFile} from './index.js'
const pkg = createRequire(import.meta.url)('../package.json')
sade('cef <config-file>')
.version(pkg.version)
.describe(pkg.description)
.option('-d, --destination <d>', 'destination path to env file')
.option('-e, --extension <e>', 'extension to env file name')
.option('-p, --prefix <p>', 'prefix to variables name')
.option('-E, --env <e>', 'how to access variables')
.action(configEnvFile)
.parse(process.argv)
Loading

0 comments on commit bcf3100

Please sign in to comment.