Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add EOL config #8

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"private": true,
"dependencies": {
"@apidevtools/json-schema-ref-parser": "~11.1.0",
"eol": "~0.9.0",
"fs-extra": "^11.0.0",
"handlebars": "~4.7.0",
"jsesc": "~3.0.0",
Expand Down
21 changes: 16 additions & 5 deletions packages/ng-openapi-gen/src/lib/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EOL } from 'node:os';
import { dirname, join, resolve } from 'node:path';

import eol from 'eol';
import fse from 'fs-extra';
import { OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from 'openapi3-ts';

Expand All @@ -26,12 +26,10 @@ 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,10 +104,10 @@ export class Generator {
}

protected write(template: string, model: object | null | undefined, baseName: string, subDir?: string) {
const tsContent = this.templates.apply(template, model);
const tsContent = this.setEndOfLine(this.templates.apply(template, model));
const filePath = join(this.tempDir, subDir || '.', `${baseName}.ts`);
fse.ensureDirSync(dirname(filePath));
fileWrite(filePath, this.replaceEol ? tsContent.replace(EOL, this.replaceEol) : tsContent);
fileWrite(filePath, tsContent);
}

protected async collectTemplates(): Promise<void> {
Expand Down Expand Up @@ -302,4 +300,17 @@ export class Generator {
model.assumedName = tryName;
}
}

protected setEndOfLine(text: string): string {
switch (this.options.endOfLineStyle) {
case 'cr':
return eol.cr(text);
case 'lf':
return eol.lf(text);
case 'crlf':
return eol.crlf(text);
default:
return eol.auto(text);
}
}
}
13 changes: 11 additions & 2 deletions packages/ng-openapi-gen/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ interface OptionsInput {
fallbackPropertyType?: string;
/** Determines how bigint will be generated. Can be either `number` (default) or `bigint`. Defaults to 'number'. */
bigintStyle?: 'number' | 'bigint';
/** When specified, overrides default system line separators when writing files. Possible values are: `\n` and `\r\n`. */
lineSeparator?: '\n' | '\r\n';
/**
* Determines how to normalize line endings. Possible values are:
* - `lf` to force LF (\n) line endings (Unix, OS X);
* - `cr` to force CR (\r) line endings (Mac OS);
* - `crlf` to force CRLF (\r\n) line endings (Windows, DOS);
* - `auto` to normalize line endings for the current operating system (default).
* Defaults to 'auto'.
*/
endOfLineStyle?: 'lf' | 'cr' | 'crlf' | 'auto';
/** Defines responseType for specific paths to use. Commonly used when built-in deduction can't fulfill your needs. */
customizedResponseType?: {
[key: string]: {
Expand Down Expand Up @@ -108,6 +115,7 @@ type DefaultedOptions =
| 'skipJsonSuffix'
| 'fallbackPropertyType'
| 'bigintStyle'
| 'endOfLineStyle'
| 'responseMethodDescription'
| 'bodyMethodDescription'
| 'modelsDir'
Expand Down Expand Up @@ -141,6 +149,7 @@ export const defaultOptions: Required<Pick<OptionsInput, DefaultedOptions>> = {
skipJsonSuffix: false,
fallbackPropertyType: 'any',
bigintStyle: 'number',
endOfLineStyle: 'auto',
responseMethodDescription:
'{{descriptionPrefix}}This method provides access to the full `HttpResponse`, allowing access to response headers.\nTo access only the response body, use `{{methodName}}()` instead.{{descriptionSuffix}}',
bodyMethodDescription:
Expand Down
3 changes: 2 additions & 1 deletion packages/ng-openapi-gen/src/lib/templates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readdirSync } from 'node:fs';

import eol from 'eol';
import Handlebars from 'handlebars';

import { Globals } from './globals.js';
Expand Down Expand Up @@ -41,7 +42,7 @@ export class Templates {
protected loadTemplate(dir: string, file: string): void {
const partialName = getFileNameIfExt(file, '.handlebars');
if (partialName && !this.templates[partialName]) {
const contents = fileRead(dir, file);
const contents = eol.auto(fileRead(dir, file));
const compiled = this.handlebars.compile(normalizeLineBreaks(contents));
this.templates[partialName] = compiled;
this.handlebars.registerPartial(partialName, compiled);
Expand Down
7 changes: 4 additions & 3 deletions packages/ng-openapi-gen/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@
"enum": ["number", "bigint"],
"default": "number"
},
"lineSeparator": {
"description": "When specified, overrides default system line separators when writing files. Possible values are: `\\n` and `\\r\\n`.",
"enum": ["\\n", "\\r\\n"]
"endOfLineStyle": {
"description": "Determines how to normalize line endings. Possible values are:\n- `lf` to force LF (\\n) line endings (Unix, OS X);\n- `cr` to force CR (\\r) line endings (Mac OS);\n- `crlf` to force CRLF (\\r\\n) line endings (Windows, DOS);\n- `auto` to normalize line endings for the current operating system (default).\nDefaults to 'auto'.",
"enum": ["lf", "cr", "crlf", "auto"],
"default": "auto"
},
"customizedResponseType": {
"description": "Defines responseType for specific paths to use. Commonly used when built-in deduction can't fulfill your needs.",
Expand Down
15 changes: 11 additions & 4 deletions tools/executors/schema-generator/schema-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,18 @@ export const schemaDescriptor: { properties: Record<string, Property> } = {
default: 'number',
values: ['number', 'bigint'],
},
lineSeparator: {
description:
'When specified, overrides default system line separators when writing files. Possible values are: `\\n` and `\\r\\n`.',
endOfLineStyle: {
description: [
'Determines how to normalize line endings. Possible values are:',
'- `lf` to force LF (\\n) line endings (Unix, OS X);',
'- `cr` to force CR (\\r) line endings (Mac OS);',
'- `crlf` to force CRLF (\\r\\n) line endings (Windows, DOS);',
'- `auto` to normalize line endings for the current operating system (default).',
'',
].join('\n'),
type: 'enum',
values: ['\\n', '\\r\\n'],
default: 'auto',
values: ['lf', 'cr', 'crlf', 'auto'],
},
customizedResponseType: {
description:
Expand Down
Loading