mirror of
https://github.com/placeholder-soft/libs.git
synced 2026-04-28 20:45:50 +08:00
refactor: diff env/env name
This commit is contained in:
20
README.md
20
README.md
@@ -6,8 +6,8 @@ typed-env can help us better handle environment variables
|
||||
|
||||
### [commands](#usage-typed-env-cli)
|
||||
|
||||
- [diff-env](#diff-env)
|
||||
- [diff-env-name](#diff-env-name)
|
||||
- [gen-diff-env](#gen-diff-env)
|
||||
- [gen-diff-env-name-type](#gen-diff-env-name-type)
|
||||
- [gen-env](#gen-env)
|
||||
- [gen-env-name-type](#gen-env-name-type)
|
||||
- [gen-typed-env-call-usage-report](#gen-typed-env-call-usage-report)
|
||||
@@ -33,13 +33,13 @@ $ npm install @placeholdersoft/typed-env
|
||||
|
||||
## Usage typed-env-cli
|
||||
|
||||
### `diff-env`
|
||||
### `gen-diff-env`
|
||||
|
||||
```
|
||||
Output diff env
|
||||
Generate diff env
|
||||
|
||||
USAGE
|
||||
$ typed-env diff-env -p <value> -a <value>
|
||||
$ typed-env gen-diff-env -p <value> -a <value>
|
||||
|
||||
FLAGS
|
||||
-a, --after-env=<value> (required) enter change env json
|
||||
@@ -49,16 +49,16 @@ DESCRIPTION
|
||||
Output diff env
|
||||
|
||||
EXAMPLES
|
||||
$ typed-env diff-env -a "$(env)" -p "$(env)"
|
||||
$ typed-env diff-env -p "$(env)" -a "$(env)"
|
||||
```
|
||||
|
||||
### `diff-env-name`
|
||||
### `gen-diff-env-name-type`
|
||||
|
||||
```
|
||||
Output diff env name
|
||||
Generate diff env name type definition
|
||||
|
||||
USAGE
|
||||
$ typed-env diff-env-name -p <value> -a <value>
|
||||
$ typed-env gen-diff-env-name-type -p <value> -a <value>
|
||||
|
||||
FLAGS
|
||||
-a, --after-env=<value> (required) enter change env json
|
||||
@@ -68,7 +68,7 @@ DESCRIPTION
|
||||
Output diff env name
|
||||
|
||||
EXAMPLES
|
||||
$ typed-env diff-env-name -a "$(env)" -p "$(env)"
|
||||
$ typed-env gen-diff-env-name-type -p "$(env)" -a "$(env)"
|
||||
```
|
||||
|
||||
### `gen-env`
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@placeholdersoft/typed-env-cli",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.6",
|
||||
"type": "commonjs",
|
||||
"bin": {
|
||||
"typed-env": "./bin/run"
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
import { Command, Flags } from '@oclif/core';
|
||||
import { parseEnv } from '../lib/utils/util';
|
||||
|
||||
export default class DiffEnvName extends Command {
|
||||
static override description = 'Output diff env name';
|
||||
|
||||
static override examples = [
|
||||
`$ typed-env diff-env-name -a "$(env)" -p "$(env)"`,
|
||||
];
|
||||
|
||||
static override flags = {
|
||||
'prev-env': Flags.string({
|
||||
char: 'p',
|
||||
description: 'enter current env json',
|
||||
required: true,
|
||||
}),
|
||||
|
||||
'after-env': Flags.string({
|
||||
char: 'a',
|
||||
description: 'enter change env json',
|
||||
required: true,
|
||||
}),
|
||||
};
|
||||
|
||||
async run(): Promise<void> {
|
||||
const { flags } = await this.parse(DiffEnvName);
|
||||
|
||||
const prev = parseEnv(flags['prev-env']);
|
||||
const after = parseEnv(flags['after-env']);
|
||||
|
||||
const names: { [key in string]: string } = {};
|
||||
for (const [k] of Object.entries(after)) {
|
||||
// only print changed vars
|
||||
if (prev[k] !== after[k]) {
|
||||
names[k] = k;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(names);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Command, Flags } from '@oclif/core';
|
||||
import { parseEnv } from '../lib/utils/util';
|
||||
|
||||
export default class DiffEnv extends Command {
|
||||
static override description = 'Output diff env';
|
||||
|
||||
static override examples = [`$ typed-env diff-env -a "$(env)" -p "$(env)"`];
|
||||
|
||||
static override flags = {
|
||||
'prev-env': Flags.string({
|
||||
char: 'p',
|
||||
description: 'enter current env json',
|
||||
required: true,
|
||||
}),
|
||||
|
||||
'after-env': Flags.string({
|
||||
char: 'a',
|
||||
description: 'enter change env json',
|
||||
required: true,
|
||||
}),
|
||||
};
|
||||
|
||||
async run(): Promise<void> {
|
||||
const { flags } = await this.parse(DiffEnv);
|
||||
|
||||
const prev = parseEnv(flags['prev-env']);
|
||||
const after = parseEnv(flags['after-env']);
|
||||
|
||||
const env: string[] = [];
|
||||
|
||||
for (const [k, v] of Object.entries(after)) {
|
||||
if (prev[k] !== after[k]) {
|
||||
env.push(`${k}=${v}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(env);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { Command, Flags } from '@oclif/core';
|
||||
import { parseEnv } from '../lib/utils/util';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { ensureDirSync } from '../lib/utils/fs';
|
||||
|
||||
export default class GenDiffEnvNameType extends Command {
|
||||
static override description = 'Generate diff env name type definition';
|
||||
|
||||
static override examples = [
|
||||
`$ typed-env diff-env-name -p "$(env)" -a "$(env)"`,
|
||||
];
|
||||
|
||||
static override flags = {
|
||||
'prev-env': Flags.string({
|
||||
char: 'p',
|
||||
description: 'enter current env json',
|
||||
required: true,
|
||||
}),
|
||||
|
||||
'after-env': Flags.string({
|
||||
char: 'a',
|
||||
description: 'enter change env json',
|
||||
required: true,
|
||||
}),
|
||||
|
||||
output: Flags.string({
|
||||
char: 'o',
|
||||
description: 'enter the output file path',
|
||||
}),
|
||||
};
|
||||
|
||||
async run(): Promise<void> {
|
||||
const { flags } = await this.parse(GenDiffEnvNameType);
|
||||
|
||||
const prev = parseEnv(flags['prev-env']);
|
||||
const after = parseEnv(flags['after-env']);
|
||||
|
||||
const envNames: { [key in string]: string } = {};
|
||||
for (const [k] of Object.entries(after)) {
|
||||
// only print changed vars
|
||||
if (prev[k] !== after[k]) {
|
||||
envNames[k] = k;
|
||||
}
|
||||
}
|
||||
|
||||
const fileContent = `export const AllProjectDiffEnvNames = ${JSON.stringify(
|
||||
envNames,
|
||||
null,
|
||||
2
|
||||
)} as const;
|
||||
export type ProjectDiffEnvName = keyof typeof AllProjectDiffEnvNames;`;
|
||||
|
||||
if (flags.output) {
|
||||
const tempstats = fs.statSync(flags.output);
|
||||
|
||||
if (tempstats.isDirectory()) {
|
||||
ensureDirSync(flags.output);
|
||||
const outputFilePath = path.join(flags.output, 'env.d.ts');
|
||||
fs.writeFileSync(outputFilePath, fileContent);
|
||||
|
||||
console.log(`output file: ${outputFilePath}`);
|
||||
} else {
|
||||
const folderPath = path.parse(flags.output);
|
||||
ensureDirSync(folderPath.dir);
|
||||
fs.writeFileSync(flags.output, fileContent);
|
||||
|
||||
console.log(`output file: ${flags.output}`);
|
||||
}
|
||||
} else {
|
||||
console.log(fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
65
packages/typed-env-cli/src/commands/gen-diff-env.ts
Normal file
65
packages/typed-env-cli/src/commands/gen-diff-env.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Command, Flags } from '@oclif/core';
|
||||
import { parseEnv } from '../lib/utils/util';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { ensureDirSync } from '../lib/utils/fs';
|
||||
|
||||
export default class GenDiffEnv extends Command {
|
||||
static override description = 'Generate diff env';
|
||||
|
||||
static override examples = [`$ typed-env diff-env -p "$(env)" -a "$(env)"`];
|
||||
|
||||
static override flags = {
|
||||
'prev-env': Flags.string({
|
||||
char: 'p',
|
||||
description: 'enter current env json',
|
||||
required: true,
|
||||
}),
|
||||
|
||||
'after-env': Flags.string({
|
||||
char: 'a',
|
||||
description: 'enter change env json',
|
||||
required: true,
|
||||
}),
|
||||
|
||||
output: Flags.string({
|
||||
char: 'o',
|
||||
description: 'enter the output file path',
|
||||
}),
|
||||
};
|
||||
|
||||
async run(): Promise<void> {
|
||||
const { flags } = await this.parse(GenDiffEnv);
|
||||
|
||||
const prev = parseEnv(flags['prev-env']);
|
||||
const after = parseEnv(flags['after-env']);
|
||||
|
||||
const env: string[] = [];
|
||||
|
||||
for (const [k, v] of Object.entries(after)) {
|
||||
if (prev[k] !== after[k]) {
|
||||
env.push(`${k}=${v}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (flags.output) {
|
||||
const tempstats = fs.statSync(flags.output);
|
||||
|
||||
if (tempstats.isDirectory()) {
|
||||
ensureDirSync(flags.output);
|
||||
const outputFilePath = path.join(flags.output, '.env');
|
||||
fs.writeFileSync(outputFilePath, env.join('\n'));
|
||||
|
||||
console.log(`output file: ${outputFilePath}`);
|
||||
} else {
|
||||
const folderPath = path.parse(flags.output);
|
||||
ensureDirSync(folderPath.dir);
|
||||
fs.writeFileSync(flags.output, env.join('\n'));
|
||||
|
||||
console.log(`output file: ${flags.output}`);
|
||||
}
|
||||
} else {
|
||||
console.log(env.join('\n'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user