Skip to content

Commit

Permalink
feat: add ability to override lineSeparator
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael M committed Jul 21, 2022
1 parent 3fbb30d commit fde1f64
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/ng-openapi-gen/src/lib/generator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EOL } from 'node:os';
import { dirname, join, resolve } from 'node:path';

import fse from 'fs-extra';
Expand Down Expand Up @@ -25,10 +26,12 @@ export class Generator {
protected operations = new Map<string, OaOperation>();
protected outDir: string;
protected tempDir: string;
protected replaceEol?: string;

constructor(public openApi: OpenAPIObject, public options: Options) {
this.outDir = trimTrailingSlash(options.output);
this.tempDir = this.outDir + '$';
this.replaceEol = options.lineSeparator && options.lineSeparator !== EOL ? options.lineSeparator : undefined;
}

public async generate(): Promise<void> {
Expand Down Expand Up @@ -106,7 +109,7 @@ export class Generator {
const tsContent = this.templates.apply(template, model);
const filePath = join(this.tempDir, subDir || '.', `${baseName}.ts`);
fse.ensureDirSync(dirname(filePath));
fileWrite(filePath, tsContent);
fileWrite(filePath, this.replaceEol ? tsContent.replace(EOL, this.replaceEol) : tsContent);
}

protected async collectTemplates(): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions packages/ng-openapi-gen/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ interface OptionsInput {
skipJsonSuffix?: boolean;
/** Fallback property type when type can not be determined for any reason. Defaults to 'any'. */
fallbackPropertyType?: string;
/** When specified, overrides default system line separators when writing files. Possible values are: `\n` and `\r\n`. */
lineSeparator?: '\n' | '\r\n';
/** Defines responseType for specific paths to use. Commonly used when built-in deduction can't fulfill your needs. */
customizedResponseType?: {
[key: string]: {
Expand Down
4 changes: 4 additions & 0 deletions packages/ng-openapi-gen/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@
"type": "string",
"default": "any"
},
"lineSeparator": {
"description": "When specified, overrides default system line separators when writing files. Possible values are: `\\n` and `\\r\\n`.",
"enum": ["\\n", "\\r\\n"]
},
"customizedResponseType": {
"description": "Defines responseType for specific paths to use. Commonly used when built-in deduction can't fulfill your needs.",
"type": "object",
Expand Down
6 changes: 6 additions & 0 deletions tools/executors/schema-generator/schema-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ export const schemaDescriptor: { properties: Record<string, Property> } = {
type: 'string',
default: 'any',
},
lineSeparator: {
description:
'When specified, overrides default system line separators when writing files. Possible values are: `\\n` and `\\r\\n`.',
type: 'enum',
values: ['\\n', '\\r\\n'],
},
customizedResponseType: {
description:
"Defines responseType for specific paths to use. Commonly used when built-in deduction can't fulfill your needs.",
Expand Down

0 comments on commit fde1f64

Please sign in to comment.