Skip to content

Commit

Permalink
feat(writers): gesture directory
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Mar 30, 2020
1 parent 60810aa commit 621265f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 34 deletions.
14 changes: 12 additions & 2 deletions src/core/writers/singleMode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {writeFileSync} from 'fs';
import {camel} from 'case';
import {existsSync, mkdirSync, writeFileSync} from 'fs';
import {OutputOptions} from '../../types';
import {WriteSpecsProps} from '../../types/writers';
import {getFileInfo} from '../../utils/file';
import {isObject, isString} from '../../utils/is';
import {getFilesHeader} from '../../utils/messages/inline';
import {generateImports} from '../generators/imports';
Expand All @@ -14,7 +16,15 @@ export const writeSingleMode = ({
info,
output
}: WriteSpecsProps & {output: string | OutputOptions}) => {
const path = (isString(output) ? output : output?.target) || '';
const {path, dirname} = getFileInfo(
isString(output) ? output : output.target,
camel(info.title)
);

if (!existsSync(dirname)) {
mkdirSync(dirname);
}

const {
definition,
imports,
Expand Down
7 changes: 5 additions & 2 deletions src/core/writers/specs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Options, OutputMode} from '../../types';
import {Options, OutputMode, OutputOptions} from '../../types';
import {WriteSpecsProps} from '../../types/writers';
import {isObject, isString} from '../../utils/is';
import {createSuccessMessage} from '../../utils/messages/logs';
Expand All @@ -7,6 +7,9 @@ import {writeSingleMode} from './singleMode';
import {writeSplitMode} from './splitMode';
import {writeTagsMode} from './tagsMode';

const isSingleMode = (output: string | OutputOptions): output is string =>
isString(output) || !output.mode || output.mode === OutputMode.SINGLE;

export const writeSpecs = (options: Options, backend?: string) => ({
operations,
schemas,
Expand All @@ -27,7 +30,7 @@ export const writeSpecs = (options: Options, backend?: string) => ({
return;
}

if (isString(output) || !output.mode || output.mode === OutputMode.SINGLE) {
if (isSingleMode(output)) {
writeSingleMode({operations, output, info, schemas});
} else if (output.mode === OutputMode.SPLIT) {
writeSplitMode({operations, output, info, schemas});
Expand Down
28 changes: 17 additions & 11 deletions src/core/writers/splitMode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {pascal} from 'case';
import {writeFileSync} from 'fs';
import {basename, dirname, join} from 'path';
import {camel, pascal} from 'case';
import {existsSync, mkdirSync, writeFileSync} from 'fs';
import {join} from 'path';
import {OutputOptions} from '../../types';
import {WriteSpecsProps} from '../../types/writers';
import {getFileInfo} from '../../utils/file';
import {getFilesHeader} from '../../utils/messages/inline';
import {generateImports} from '../generators/imports';
import {generateModelsInline} from '../generators/modelsInline';
Expand All @@ -15,7 +16,15 @@ export const writeSplitMode = ({
info,
output
}: WriteSpecsProps & {output: OutputOptions}) => {
const path = output.target!;
const {path, filename, dirname, extension} = getFileInfo(
output.target,
camel(info.title)
);

if (!existsSync(dirname)) {
mkdirSync(dirname);
}

const {
definition,
imports,
Expand All @@ -29,9 +38,6 @@ export const writeSplitMode = ({
let implementationData = header;
let mockData = header;

const filename = basename(path, '.ts');
const dir = dirname(path);

definitionData += "import { AxiosPromise } from 'axios'\n";

const definitionPath = './' + filename + '.definition';
Expand All @@ -54,7 +60,7 @@ export const writeSplitMode = ({
const schemasPath = './' + filename + '.schemas';
const schemasData = header + generateModelsInline(schemas);

writeFileSync(join(dir, schemasPath + '.ts'), schemasData);
writeFileSync(join(dirname, schemasPath + extension), schemasData);

definitionData += generateImports(imports, schemasPath, true);
implementationData += generateImports(imports, schemasPath, true);
Expand All @@ -66,11 +72,11 @@ export const writeSplitMode = ({
mockData += implementationMocks;

if (path) {
writeFileSync(join(dir, definitionPath + '.ts'), definitionData);
writeFileSync(join(dir, filename + '.ts'), implementationData);
writeFileSync(join(dirname, definitionPath + extension), definitionData);
writeFileSync(join(dirname, filename + extension), implementationData);

if (output.mock) {
writeFileSync(join(dir, filename + '.mock.ts'), mockData);
writeFileSync(join(dirname, filename + '.mock' + extension), mockData);
}
}
};
24 changes: 15 additions & 9 deletions src/core/writers/tagsMode.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {kebab, pascal} from 'case';
import {writeFileSync} from 'fs';
import {camel, kebab, pascal} from 'case';
import {existsSync, mkdirSync, writeFileSync} from 'fs';
import {InfoObject} from 'openapi3-ts';
import {basename, dirname, join} from 'path';
import {join} from 'path';
import {OutputOptions} from '../../types';
import {
GeneratorOperation,
GeneratorOperations,
GeneratorTarget
} from '../../types/generator';
import {WriteSpecsProps} from '../../types/writers';
import {getFileInfo} from '../../utils/file';
import {generalTypesFilter} from '../../utils/filters';
import {isObject} from '../../utils/is';
import {getFilesHeader} from '../../utils/messages/inline';
Expand Down Expand Up @@ -89,11 +90,16 @@ export const writeTagsMode = ({
info,
output
}: WriteSpecsProps & {output: OutputOptions}) => {
const path = output.target!;
const target = generateTarget(operations, info);
const {path, filename, dirname, extension} = getFileInfo(
output.target,
camel(info.title)
);

if (!existsSync(dirname)) {
mkdirSync(dirname);
}

const filename = basename(path, '.ts');
const dir = dirname(path);
const target = generateTarget(operations, info);

Object.entries(target).forEach(([tag, target]) => {
const {definition, imports, implementation, implementationMocks} = target;
Expand All @@ -110,7 +116,7 @@ export const writeTagsMode = ({
const schemasPath = './' + filename + '.schemas';
const schemasData = header + generateModelsInline(schemas);

writeFileSync(join(dir, schemasPath + '.ts'), schemasData);
writeFileSync(join(dirname, schemasPath + extension), schemasData);

data += generateImports(imports, schemasPath, true);
}
Expand All @@ -125,6 +131,6 @@ export const writeTagsMode = ({
data += implementationMocks;
}

writeFileSync(join(dir, `${filename}.${kebab(tag)}.ts`), data);
writeFileSync(join(dirname, `${filename}.${kebab(tag)}${extension}`), data);
});
};
18 changes: 18 additions & 0 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {basename, dirname, join} from 'path';
import {isDirectory} from './is';

export const getFileInfo = (target: string = '', backupFilename: string) => {
const isDir = isDirectory(target);
const extension = '.ts';
const path = isDir ? join(target, backupFilename + extension) : target;
const dir = dirname(path);
const filename = basename(path, '.ts');

return {
path,
extension,
isDirectory: isDir,
dirname: dir,
filename
};
};
11 changes: 1 addition & 10 deletions src/utils/is.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {existsSync, lstatSync} from 'fs';
import {ReferenceObject} from 'openapi3-ts';
import {extname} from 'path';

Expand All @@ -11,15 +10,7 @@ export const isReference = (property: any): property is ReferenceObject => {
return Boolean(property.$ref);
};

export const isDirectory = async (path: string) => {
if (path.endsWith('/')) {
return true;
}

if (existsSync(path)) {
return lstatSync(path).isDirectory();
}

export const isDirectory = (path: string) => {
return !extname(path);
};

Expand Down

0 comments on commit 621265f

Please sign in to comment.