mirror of
https://github.com/Kong/insomnia.git
synced 2026-10-23 23:29:12 -04:00
CLI: input and output flags should respect workingDir (#2307)
This commit is contained in:
1 parent
1b8c24fbee
commit
e359da5afd
4 files changed
+43
-21
No files matched your search
@@ -55,7 +55,7 @@ describe('generateConfig()', () => {
|
||||
expect(consoleSpy).toHaveBeenCalledWith('a\n---\nb\n');
|
||||
});
|
||||
|
||||
it('should write converted documents to file system', async () => {
|
||||
it('should write generated documents to file system', async () => {
|
||||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
const writeFileSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
|
||||
mock(o2k.generate).mockResolvedValue({ documents: ['a', 'b'] });
|
||||
@@ -63,7 +63,30 @@ describe('generateConfig()', () => {
|
||||
await generateConfig(filePath, { ...base, output: 'output.yaml' });
|
||||
|
||||
expect(o2k.generate).toHaveBeenCalledWith(filePath, ConversionTypeMap[base.type]);
|
||||
expect(writeFileSpy).toHaveBeenCalledWith(path.resolve('output.yaml'), 'a\n---\nb\n');
|
||||
expect(writeFileSpy).toHaveBeenCalledWith('output.yaml', 'a\n---\nb\n');
|
||||
expect(consoleSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should generate documents using workingDir', async () => {
|
||||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
const writeFileSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
|
||||
mock(o2k.generate).mockResolvedValue({ documents: ['a', 'b'] });
|
||||
|
||||
await generateConfig('file.yaml', {
|
||||
...base,
|
||||
workingDir: 'test/dir',
|
||||
output: 'output.yaml',
|
||||
});
|
||||
|
||||
// Read from workingDir
|
||||
expect(o2k.generate).toHaveBeenCalledWith(
|
||||
path.normalize('test/dir/file.yaml'),
|
||||
ConversionTypeMap[base.type],
|
||||
);
|
||||
expect(writeFileSpy).toHaveBeenCalledWith(
|
||||
path.normalize('test/dir/output.yaml'),
|
||||
'a\n---\nb\n',
|
||||
);
|
||||
expect(consoleSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -34,21 +34,26 @@ export async function generateConfig(
|
||||
return;
|
||||
}
|
||||
|
||||
const { type, output, workingDir } = options;
|
||||
const { type, output } = options;
|
||||
|
||||
const workingDir = options.workingDir || '.';
|
||||
|
||||
const db = await gitDataDirDb({ dir: workingDir, filterTypes: ['ApiSpec'] });
|
||||
|
||||
let result: ConversionResult;
|
||||
|
||||
// try get from db
|
||||
const specFromDb = db.ApiSpec.get(identifier);
|
||||
try {
|
||||
if (specFromDb?.contents) {
|
||||
result = await o2k.generateFromString(specFromDb.contents, ConversionTypeMap[type]);
|
||||
} else {
|
||||
result = await o2k.generate(identifier, ConversionTypeMap[type]);
|
||||
// try load as a file
|
||||
const fileName = path.join(workingDir, identifier);
|
||||
result = await o2k.generate(fileName, ConversionTypeMap[type]);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('Config failed to generate', err);
|
||||
console.log('Config failed to generate:', err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -58,7 +63,7 @@ export async function generateConfig(
|
||||
const document = yamlDocs.join('\n---\n').replace(/\n+---\n+/g, '\n---\n');
|
||||
|
||||
if (output) {
|
||||
const fullOutputPath = path.resolve(output);
|
||||
const fullOutputPath = path.join(workingDir, output);
|
||||
fs.writeFileSync(fullOutputPath, document);
|
||||
} else {
|
||||
console.log(document);
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import YAML from 'yaml';
|
||||
import type { ApiSpec } from './types';
|
||||
import type { ApiSpec, BaseModel } from './types';
|
||||
|
||||
type Database = {|
|
||||
export type Database = {|
|
||||
ApiSpec: Map<string, ApiSpec>,
|
||||
Environment: Map<string, Object>,
|
||||
Request: Map<string, Object>,
|
||||
RequestGroup: Map<string, Object>,
|
||||
Workspace: Map<string, Object>,
|
||||
Environment: Map<string, BaseModel>,
|
||||
Request: Map<string, BaseModel>,
|
||||
RequestGroup: Map<string, BaseModel>,
|
||||
Workspace: Map<string, BaseModel>,
|
||||
|};
|
||||
|
||||
export const emptyDb = (): Database => ({
|
||||
@@ -21,13 +21,13 @@ export const emptyDb = (): Database => ({
|
||||
});
|
||||
|
||||
type Options = {
|
||||
dir?: string,
|
||||
dir: string,
|
||||
filterTypes?: Array<$Keys<Database>>,
|
||||
};
|
||||
|
||||
export const gitDataDirDb = async ({ dir, filterTypes }: Options): Promise<Database> => {
|
||||
const db = emptyDb();
|
||||
const insomniaDir = path.normalize(path.join(dir || '.', '.insomnia'));
|
||||
const insomniaDir = path.join(dir, '.insomnia');
|
||||
|
||||
if (!fs.existsSync(insomniaDir)) {
|
||||
// TODO: control logging with verbose flag
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
// @flow
|
||||
|
||||
// These types should come from a shared location (maybe insomnia-importers?)
|
||||
// They represent the models that are read from an Insomnia data source
|
||||
// eg. git data directory, insomnia export format, etc
|
||||
// Simplified and extracted from insomnia-app/app/models/*
|
||||
|
||||
export type BaseModel = {
|
||||
_id: string,
|
||||
type: string,
|
||||
parentId: string,
|
||||
modified: number,
|
||||
created: number,
|
||||
};
|
||||
|
||||
type BaseApiSpec = {
|
||||
@@ -18,6 +14,4 @@ type BaseApiSpec = {
|
||||
contents: string,
|
||||
};
|
||||
|
||||
export const SpecName = 'ApiSpec';
|
||||
|
||||
export type ApiSpec = BaseModel & BaseApiSpec;
|
||||
Reference in new issue
Block a user