Skip to content

Commit

Permalink
Merge branch 'main' into fix/custom-headers-sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Nov 9, 2023
2 parents e155978 + dbf8aaa commit 9a5bcd1
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 deletions.
6 changes: 4 additions & 2 deletions projects/optic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@
"commander": "^11.0.0",
"conf": "^10.2.0",
"crosspath": "^2.0.0",
"dotenv": "^16.0.3",
"dotenv": "^16.3.1",
"exit-hook": "^2.2.1",
"fast-deep-equal": "^3.1.3",
"fast-glob": "^3.2.12",
"fs-extra": "^11.1.0",
"git-url-parse": "^13.1.0",
"handlebars": "^4.7.8",
"har-schema": "^2.0.0",
"is-elevated": "^3.0.0",
"is-url": "^1.2.4",
Expand Down Expand Up @@ -119,7 +120,8 @@
"tunnel": "^0.0.6",
"update-notifier": "^5",
"url-join": "^4.0.1",
"whatwg-mimetype": "^3.0.0"
"whatwg-mimetype": "^3.0.0",
"yaml": "^2.3.4"
},
"pkg": {
"scripts": [
Expand Down
9 changes: 9 additions & 0 deletions projects/optic/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createOpticClient } from '../client';
import {
detectCliConfig,
initializeRules,
RenderTemplate,
validateConfig,
ProjectYmlConfig,
} from '../config';
Expand Down Expand Up @@ -36,6 +37,14 @@ describe('detectConfig', () => {
});
});

describe('templatedConfig', () => {
test('renders template', async () => {
process.env['TOKEN'] = 'my-token';
const renderedTemplate = await RenderTemplate('src/__tests__/optic.yml');
expect(String(renderedTemplate).includes('token my-token'));
});
});

describe('validateConfig', () => {
test('success', () => {
const config = {};
Expand Down
12 changes: 12 additions & 0 deletions projects/optic/src/__tests__/optic.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
files:
- id: spec
path: openapi-spec.yml

capture:
openapi.yml:
server:
url: https://echo.o3c.org
requests:
send:
- path: /users
headers:
x-response-json: '[{"id":0, "name":"aidan"}]'
authorization: "token {{TOKEN}}"

26 changes: 26 additions & 0 deletions projects/optic/src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Command } from 'commander';
import { OpticCliConfig, RenderTemplate } from '../config';
import * as yaml from 'yaml';

export const registerConfigCommand = (
cli: Command,
cliConfig: OpticCliConfig
) => {
const config = cli.command('config');
config
.helpOption('-h, --help', 'Display help for the command')
.addHelpCommand(false)
.configureHelp({ commandUsage: () => `\n optic config <command>` })
.description('Commands to manage the Optic configuration');

config
.command('show')
.description('Display the rendered Optic configuration from optic.yml')
.configureHelp({ commandUsage: () => `\n optic config show` })
.action(async () => {
console.log(
// rendering the template a second time isn't awesome (it first happens when the CLI is initialized), but an OpticCliConfig contains more than just whats in optic.yml and doesn't have a simple way to convert to yaml anyway.
yaml.stringify(await RenderTemplate(cliConfig.configPath!), null, 2)
);
});
};
15 changes: 14 additions & 1 deletion projects/optic/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { createOpticClient, OpticBackendClient } from './client';
import * as Git from './utils/git-utils';
import { logger } from './logger';
import { Static, Type } from '@sinclair/typebox';
import Handlebars from 'handlebars';
import * as dotenv from 'dotenv';

export enum VCS {
Git = 'git',
Expand Down Expand Up @@ -181,11 +183,22 @@ export async function detectCliConfig(
return undefined;
}

export async function RenderTemplate(configPath: string): Promise<unknown> {
// if present, expect the .optic.env is in the same dir as the config file
const dir = path.dirname(configPath);
dotenv.config({ path: path.join(dir, '.optic.env') });

const template = Handlebars.compile(await fs.readFile(configPath, 'utf-8'));
const result = template(process.env);
return yaml.load(result);
}

export async function loadCliConfig(
configPath: string,
client: OpticBackendClient
): Promise<OpticCliConfig> {
const config = yaml.load(await fs.readFile(configPath, 'utf-8'));
const config = await RenderTemplate(configPath);

validateConfig(config, configPath);
await initializeRules(config as ProjectYmlConfig, client);

Expand Down
2 changes: 2 additions & 0 deletions projects/optic/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { registerRulesetInit } from './commands/ruleset/init';
import { registerApiAdd } from './commands/api/add';
import { registerApiCreate } from './commands/api/create';
import { registerCaptureCommand } from './commands/capture/capture';
import { registerConfigCommand } from './commands/config';
import { captureCommand as captureV1Command } from './commands/oas/capture';
import { newCommand } from './commands/oas/new';
import { setupTlsCommand } from './commands/oas/setup-tls';
Expand Down Expand Up @@ -127,6 +128,7 @@ export const initCli = async (

const betaSubcommands = cli.command('beta', { hidden: true });
registerCaptureCommand(cli, cliConfig);
registerConfigCommand(cli, cliConfig);

//@todo by 2023/5/10
const oas = new Command('oas').description(
Expand Down
56 changes: 53 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3751,12 +3751,13 @@ __metadata:
commander: "npm:^11.0.0"
conf: "npm:^10.2.0"
crosspath: "npm:^2.0.0"
dotenv: "npm:^16.0.3"
dotenv: "npm:^16.3.1"
exit-hook: "npm:^2.2.1"
fast-deep-equal: "npm:^3.1.3"
fast-glob: "npm:^3.2.12"
fs-extra: "npm:^11.1.0"
git-url-parse: "npm:^13.1.0"
handlebars: "npm:^4.7.8"
har-schema: "npm:^2.0.0"
is-elevated: "npm:^3.0.0"
is-url: "npm:^1.2.4"
Expand Down Expand Up @@ -3799,6 +3800,7 @@ __metadata:
update-notifier: "npm:^5"
url-join: "npm:^4.0.1"
whatwg-mimetype: "npm:^3.0.0"
yaml: "npm:^2.3.4"
bin:
optic: build/index.js
languageName: unknown
Expand Down Expand Up @@ -5562,7 +5564,7 @@ __metadata:
languageName: node
linkType: hard

"dotenv@npm:^16.0.3":
"dotenv@npm:^16.3.1":
version: 16.3.1
resolution: "dotenv@npm:16.3.1"
checksum: b95ff1bbe624ead85a3cd70dbd827e8e06d5f05f716f2d0cbc476532d54c7c9469c3bc4dd93ea519f6ad711cb522c00ac9a62b6eb340d5affae8008facc3fbd7
Expand Down Expand Up @@ -6704,6 +6706,24 @@ __metadata:
languageName: node
linkType: hard

"handlebars@npm:^4.7.8":
version: 4.7.8
resolution: "handlebars@npm:4.7.8"
dependencies:
minimist: "npm:^1.2.5"
neo-async: "npm:^2.6.2"
source-map: "npm:^0.6.1"
uglify-js: "npm:^3.1.4"
wordwrap: "npm:^1.0.0"
dependenciesMeta:
uglify-js:
optional: true
bin:
handlebars: bin/handlebars
checksum: 7aff423ea38a14bb379316f3857fe0df3c5d66119270944247f155ba1f08e07a92b340c58edaa00cfe985c21508870ee5183e0634dcb53dd405f35c93ef7f10d
languageName: node
linkType: hard

"har-schema@npm:^2.0.0":
version: 2.0.0
resolution: "har-schema@npm:2.0.0"
Expand Down Expand Up @@ -8759,6 +8779,13 @@ __metadata:
languageName: node
linkType: hard

"minimist@npm:^1.2.5":
version: 1.2.8
resolution: "minimist@npm:1.2.8"
checksum: 19d3fcdca050087b84c2029841a093691a91259a47def2f18222f41e7645a0b7c44ef4b40e88a1e58a40c84d2ef0ee6047c55594d298146d0eb3f6b737c20ce6
languageName: node
linkType: hard

"minipass-collect@npm:^1.0.2":
version: 1.0.2
resolution: "minipass-collect@npm:1.0.2"
Expand Down Expand Up @@ -8961,6 +8988,13 @@ __metadata:
languageName: node
linkType: hard

"neo-async@npm:^2.6.2":
version: 2.6.2
resolution: "neo-async@npm:2.6.2"
checksum: c2f5a604a54a8ec5438a342e1f356dff4bc33ccccdb6dc668d94fe8e5eccfc9d2c2eea6064b0967a767ba63b33763f51ccf2cd2441b461a7322656c1f06b3f5d
languageName: node
linkType: hard

"netmask@npm:^2.0.2":
version: 2.0.2
resolution: "netmask@npm:2.0.2"
Expand Down Expand Up @@ -11171,6 +11205,15 @@ __metadata:
languageName: node
linkType: hard

"uglify-js@npm:^3.1.4":
version: 3.17.4
resolution: "uglify-js@npm:3.17.4"
bin:
uglifyjs: bin/uglifyjs
checksum: 8b7fcdca69deb284fed7d2025b73eb747ce37f9aca6af53422844f46427152d5440601b6e2a033e77856a2f0591e4167153d5a21b68674ad11f662034ec13ced
languageName: node
linkType: hard

"unbox-primitive@npm:^1.0.2":
version: 1.0.2
resolution: "unbox-primitive@npm:1.0.2"
Expand Down Expand Up @@ -11559,6 +11602,13 @@ __metadata:
languageName: node
linkType: hard

"wordwrap@npm:^1.0.0":
version: 1.0.0
resolution: "wordwrap@npm:1.0.0"
checksum: 7ed2e44f3c33c5c3e3771134d2b0aee4314c9e49c749e37f464bf69f2bcdf0cbf9419ca638098e2717cff4875c47f56a007532f6111c3319f557a2ca91278e92
languageName: node
linkType: hard

"wrap-ansi@npm:^7.0.0":
version: 7.0.0
resolution: "wrap-ansi@npm:7.0.0"
Expand Down Expand Up @@ -11689,7 +11739,7 @@ __metadata:
languageName: node
linkType: hard

"yaml@npm:^2.3.2":
"yaml@npm:^2.3.2, yaml@npm:^2.3.4":
version: 2.3.4
resolution: "yaml@npm:2.3.4"
checksum: cf03b68f8fef5e8516b0f0b54edaf2459f1648317fc6210391cf606d247e678b449382f4bd01f77392538429e306c7cba8ff46ff6b37cac4de9a76aff33bd9e1
Expand Down

0 comments on commit 9a5bcd1

Please sign in to comment.