Skip to content

Commit

Permalink
refactor: remove T symbol from types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `T` symbol was removed from types
  • Loading branch information
raveclassic committed Oct 8, 2019
1 parent 78d6513 commit b4ef46c
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 356 deletions.
8 changes: 4 additions & 4 deletions src/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as yaml from 'js-yaml';

export type TJSON = {
export type JSON = {
[key: string]: unknown;
};
export type TFileReader = (buffer: Buffer) => TJSON;
export type FileReader = (buffer: Buffer) => JSON;

export const fromJSON: TFileReader = buffer => JSON.parse(buffer.toString());
export const fromYaml: TFileReader = buffer => yaml.safeLoad(buffer.toString());
export const fromJSON: FileReader = buffer => JSON.parse(buffer.toString());
export const fromYaml: FileReader = buffer => yaml.safeLoad(buffer.toString());
20 changes: 10 additions & 10 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import * as fs from 'fs-extra';
import * as path from 'path';

export type TFile = {
export type File = {
type: 'FILE';
name: string;
content: string;
};

export const file = (name: string, content: string): TFile => ({
export const file = (name: string, content: string): File => ({
type: 'FILE',
name,
content,
});

export type TDirectory = {
export type Directory = {
type: 'DIRECTORY';
name: string;
content: TFSEntity[];
content: FSEntity[];
};

export const directory = (name: string, content: TFSEntity[]): TDirectory => ({
export const directory = (name: string, content: FSEntity[]): Directory => ({
type: 'DIRECTORY',
name,
content,
});

export type TFSEntity = TFile | TDirectory;
export type FSEntity = File | Directory;

export type TBuffer = {
export type BufferWithName = {
buffer: Buffer;
fileName: string;
};

export const write = async (destination: string, entity: TFSEntity): Promise<void> => {
export const write = async (destination: string, entity: FSEntity): Promise<void> => {
switch (entity.type) {
case 'FILE': {
const filePath = path.resolve(destination, entity.name);
Expand All @@ -50,7 +50,7 @@ export const write = async (destination: string, entity: TFSEntity): Promise<voi
}
};

export const map = (entity: TFSEntity, f: (content: string) => string): TFSEntity => {
export const map = (entity: FSEntity, f: (content: string) => string): FSEntity => {
switch (entity.type) {
case 'FILE': {
return file(entity.name, f(entity.content));
Expand All @@ -61,7 +61,7 @@ export const map = (entity: TFSEntity, f: (content: string) => string): TFSEntit
}
};

export const read = async (_pathToFile: string, cwd: string): Promise<TBuffer> => {
export const read = async (_pathToFile: string, cwd: string): Promise<BufferWithName> => {
const pathToFile = path.isAbsolute(_pathToFile) ? _pathToFile : path.resolve(cwd, _pathToFile);
return {
buffer: await fs.readFile(pathToFile),
Expand Down
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SwaggerObject, TSwaggerObject } from './schema/2.0/swagger';
import { SwaggerObject } from './schema/2.0/swagger';
import * as prettier from 'prettier';
import { map, read, TFSEntity, write } from './fs';
import { TSerializer } from './utils';
import { map, read, FSEntity, write } from './fs';
import { Serializer } from './utils';
import * as fs from 'fs-extra';
import { fromNullable, getOrElse, Option, map as mapOption } from 'fp-ts/lib/Option';
import * as path from 'path';
import { head, last } from 'fp-ts/lib/Array';
import { TFileReader } from './fileReader';
import { FileReader } from './fileReader';
import { isLeft, Right } from 'fp-ts/lib/Either';
import * as del from 'del';
import { pipe } from 'fp-ts/lib/pipeable';
Expand All @@ -15,7 +15,7 @@ import { PathReporter } from 'io-ts/lib/PathReporter';

const log = console.log.bind(console, '[SWAGGER-CODEGEN-TS]:');

export type TGenerateOptions = {
export type GenerateOptions = {
/**
* Paths to spec files
*/
Expand All @@ -27,7 +27,7 @@ export type TGenerateOptions = {
/**
* Spec serializer
*/
serialize: TSerializer;
serialize: Serializer;
/**
* Path to prettier config
*/
Expand All @@ -36,16 +36,16 @@ export type TGenerateOptions = {
* Buffer to JSON converter
* @param buffer - File Buffer
*/
fileReader: TFileReader;
fileReader: FileReader;
};

const cwd = process.cwd();
const resolvePath = (p: string) => (path.isAbsolute(p) ? p : path.resolve(cwd, p));

const serializeDecode = (serializer: TSerializer) => async (
decoded: Right<TSwaggerObject>,
const serializeDecode = (serializer: Serializer) => async (
decoded: Right<SwaggerObject>,
out: string,
): Promise<TFSEntity> => serializer(path.basename(out), decoded.right);
): Promise<FSEntity> => serializer(path.basename(out), decoded.right);

const getPrettierConfig = async (pathToPrettierConfig?: string): Promise<Option<prettier.Options>> =>
fromNullable(
Expand All @@ -58,16 +58,16 @@ const getPrettierConfig = async (pathToPrettierConfig?: string): Promise<Option<
),
);

const formatSerialized = (serialized: TFSEntity, prettierConfig: Option<prettier.Options>): TFSEntity =>
const formatSerialized = (serialized: FSEntity, prettierConfig: Option<prettier.Options>): FSEntity =>
pipe(
prettierConfig,
mapOption(config => map(serialized, content => prettier.format(content, config))),
getOrElse(() => serialized),
);

const writeFormatted = (out: string, formatted: TFSEntity) => write(path.dirname(out), formatted);
const writeFormatted = (out: string, formatted: FSEntity) => write(path.dirname(out), formatted);

export const generate = async (options: TGenerateOptions): Promise<void> => {
export const generate = async (options: GenerateOptions): Promise<void> => {
const out = resolvePath(options.out);
const isPathExist = await fs.pathExists(out);

Expand Down
Loading

0 comments on commit b4ef46c

Please sign in to comment.