Skip to content

Commit 3cec483

Browse files
committed
fix: fix '..' and '.' in spec names
BREAKING CHANGE: generate doesn't include `out` path in spec output BREAKING CHANGE: language doesn't accept `out` path and can return `Fragment` to write to `out` directly
1 parent 68c1c30 commit 3cec483

File tree

7 files changed

+24
-31
lines changed

7 files changed

+24
-31
lines changed

src/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface GenerateOptions<A> {
1515
readonly spec: string;
1616
readonly decoder: Decoder<unknown, A>;
1717
readonly language: (
18-
out: string,
1918
documents: Record<string, A>,
2019
resolveRef: (ref: string) => Either<unknown, unknown>,
2120
) => Either<unknown, FSEntity>;
@@ -65,10 +64,7 @@ export const generate = <A>(options: GenerateOptions<A>): TaskEither<unknown, vo
6564

6665
log('Writing to', out);
6766

68-
await write(
69-
out,
70-
getUnsafe(options.language(out, specs, ref => either.tryCatch(() => $refs.get(ref), identity))),
71-
);
67+
await write(out, getUnsafe(options.language(specs, ref => either.tryCatch(() => $refs.get(ref), identity))));
7268

7369
log('Done');
7470
}, identity);

src/language/typescript/2.0/index.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SwaggerObject } from '../../../schema/2.0/swagger-object';
22
import { defaultPrettierConfig, SerializeOptions } from '../common/utils';
3-
import { directory, FSEntity, map } from '../../../utils/fs';
3+
import { fragment, FSEntity, map } from '../../../utils/fs';
44
import { Dictionary } from '../../../utils/types';
55
import { either, record } from 'fp-ts';
66
import { pipe } from 'fp-ts/lib/pipeable';
@@ -13,17 +13,15 @@ import { combineReader } from '@devexperts/utils/dist/adt/reader.utils';
1313
export const serialize = combineReader(
1414
serializeSwaggerObject,
1515
serializeSwaggerObject => (
16-
out: string,
1716
documents: Dictionary<SwaggerObject>,
1817
options: SerializeOptions = {},
1918
): Either<Error, FSEntity> =>
2019
pipe(
2120
documents,
2221
record.collect(serializeSwaggerObject),
2322
sequenceEither,
24-
either.map(serialized => directory(out, serialized)),
2523
either.map(serialized =>
26-
map(serialized, content => format(content, options.prettierConfig || defaultPrettierConfig)),
24+
map(fragment(serialized), content => format(content, options.prettierConfig || defaultPrettierConfig)),
2725
),
2826
),
2927
);

src/language/typescript/3.0/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { serializeDocument } from './serializers/document';
22
import { format } from 'prettier';
3-
import { directory, FSEntity, map as mapFS } from '../../../utils/fs';
3+
import { fragment, FSEntity, map as mapFS } from '../../../utils/fs';
44
import { Either } from 'fp-ts/lib/Either';
55
import { pipe } from 'fp-ts/lib/pipeable';
66
import { combineReader } from '@devexperts/utils/dist/adt/reader.utils';
@@ -15,15 +15,15 @@ export { serializeDocument } from './serializers/document';
1515
export const serialize = combineReader(
1616
serializeDocument,
1717
serializeDocument => (
18-
out: string,
1918
documents: Dictionary<OpenapiObject>,
2019
options: SerializeOptions = {},
2120
): Either<Error, FSEntity> =>
2221
pipe(
2322
documents,
2423
record.collect(serializeDocument),
2524
sequenceEither,
26-
either.map(serialized => directory(out, serialized)),
27-
either.map(e => mapFS(e, content => format(content, options.prettierConfig || defaultPrettierConfig))),
25+
either.map(e =>
26+
mapFS(fragment(e), content => format(content, options.prettierConfig || defaultPrettierConfig)),
27+
),
2828
),
2929
);

src/language/typescript/asyncapi-2.0.0/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { combineReader } from '@devexperts/utils/dist/adt/reader.utils';
22
import { AsyncAPIObject } from '../../../schema/asyncapi-2.0.0/asyncapi-object';
33
import { defaultPrettierConfig, SerializeOptions } from '../common/utils';
44
import { Either } from 'fp-ts/lib/Either';
5-
import { directory, FSEntity, map as mapFS } from '../../../utils/fs';
5+
import { fragment, FSEntity, map as mapFS } from '../../../utils/fs';
66
import { pipe } from 'fp-ts/lib/pipeable';
77
import { either, record } from 'fp-ts';
88
import { format } from 'prettier';
@@ -12,15 +12,15 @@ import { sequenceEither } from '@devexperts/utils/dist/adt/either.utils';
1212
export const serialize = combineReader(
1313
serializeAsyncAPIObject,
1414
serializeAsyncAPIObject => (
15-
out: string,
1615
documents: Record<string, AsyncAPIObject>,
1716
options: SerializeOptions = {},
1817
): Either<Error, FSEntity> =>
1918
pipe(
2019
documents,
2120
record.collect(serializeAsyncAPIObject),
2221
sequenceEither,
23-
either.map(serialized => directory(out, serialized)),
24-
either.map(e => mapFS(e, content => format(content, options.prettierConfig || defaultPrettierConfig))),
22+
either.map(e =>
23+
mapFS(fragment(e), content => format(content, options.prettierConfig || defaultPrettierConfig)),
24+
),
2525
),
2626
);

src/language/typescript/sketch-121/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defaultPrettierConfig, SerializeOptions } from '../common/utils';
2-
import { directory, FSEntity, map as mapFS } from '../../../utils/fs';
2+
import { directory, fragment, FSEntity, map as mapFS } from '../../../utils/fs';
33
import { Either } from 'fp-ts/lib/Either';
44
import { pipe } from 'fp-ts/lib/pipeable';
55
import { array, either, option, record } from 'fp-ts';
@@ -12,7 +12,6 @@ import { serializeFileFormat } from './serializers/file-format';
1212
export const serialize = combineReader(
1313
serializeFileFormat,
1414
serializeFileFormat => (
15-
out: string,
1615
files: Record<string, FileFormat>,
1716
options: SerializeOptions = {},
1817
): Either<Error, FSEntity> =>
@@ -26,7 +25,7 @@ export const serialize = combineReader(
2625
),
2726
sequenceEither,
2827
either.map(serialized =>
29-
mapFS(directory(out, array.compact(serialized)), content =>
28+
mapFS(fragment(array.compact(serialized)), content =>
3029
format(content, options.prettierConfig || defaultPrettierConfig),
3130
),
3231
),

src/utils/fs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ export const write = async (destination: string, entity: FSEntity): Promise<void
4646
switch (entity.type) {
4747
case 'FILE': {
4848
const normalizedEntityName = normalizeFilePath(entity.name);
49-
const filePath = path.resolve(destination, normalizedEntityName);
49+
const filePath = path.join(destination, normalizedEntityName);
5050
await fs.outputFile(filePath, entity.content);
5151
break;
5252
}
5353
case 'DIRECTORY': {
54-
const directoryPath = path.resolve(destination, entity.name);
54+
const directoryPath = path.join(destination, entity.name);
5555
await fs.mkdirp(directoryPath);
5656
for (const contentEntity of entity.content) {
5757
await write(directoryPath, contentEntity);

test/index.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,47 @@ const out = path.resolve(cwd, 'out');
2222
const test1 = generate({
2323
spec: path.resolve(__dirname, 'specs/2.0/json/swagger.json'),
2424
out,
25-
language: (out, documents, resolveRef) =>
25+
language: (documents, resolveRef) =>
2626
serializeSwagger2({
2727
resolveRef: referenceObject => option.toUndefined(option.fromEither(resolveRef(referenceObject.$ref))),
28-
})(out, documents),
28+
})(documents),
2929
decoder: SwaggerObject,
3030
});
3131

3232
const test2 = generate({
3333
spec: path.resolve(__dirname, 'specs/2.0/yaml/demo.yml'),
3434
out,
35-
language: (out, documents, resolveRef) =>
35+
language: (documents, resolveRef) =>
3636
serializeSwagger2({
3737
resolveRef: referenceObject => option.toUndefined(option.fromEither(resolveRef(referenceObject.$ref))),
38-
})(out, documents),
38+
})(documents),
3939
decoder: SwaggerObject,
4040
});
4141

4242
const test3 = generate({
4343
spec: path.resolve(__dirname, 'specs/3.0/demo.yml'),
4444
out,
45-
language: (out, documents, resolveRef) =>
45+
language: (documents, resolveRef) =>
4646
serializeOpenAPI3({
4747
resolveRef: referenceObject => option.toUndefined(option.fromEither(resolveRef(referenceObject.$ref))),
48-
})(out, documents),
48+
})(documents),
4949
decoder: OpenapiObjectCodec,
5050
});
5151

5252
const test4 = generate({
5353
spec: path.resolve(__dirname, 'specs/asyncapi-2.0.0/streetlights-api.yml'),
5454
out,
55-
language: (out, documents, resolveRef) =>
55+
language: (documents, resolveRef) =>
5656
serializeAsyncAPI({
5757
resolveRef: referenceObject => option.toUndefined(option.fromEither(resolveRef(referenceObject.$ref))),
58-
})(out, documents),
58+
})(documents),
5959
decoder: AsyncAPIObjectCodec,
6060
});
6161

6262
const test5 = generate({
6363
spec: path.resolve(__dirname, 'specs/sketch/demo.sketch'),
6464
out,
65-
language: (out1, documents) => serializeSketch({ nameStorage: createNameStorage() })(out, documents),
65+
language: documents => serializeSketch({ nameStorage: createNameStorage() })(documents),
6666
decoder: FileFormatCodec,
6767
});
6868

0 commit comments

Comments
 (0)