diff --git a/packages/ng-openapi-gen/src/lib/generator.ts b/packages/ng-openapi-gen/src/lib/generator.ts index 1da16c9..2aeed00 100644 --- a/packages/ng-openapi-gen/src/lib/generator.ts +++ b/packages/ng-openapi-gen/src/lib/generator.ts @@ -1,3 +1,4 @@ +import { EOL } from 'node:os'; import { dirname, join, resolve } from 'node:path'; import fse from 'fs-extra'; @@ -25,10 +26,12 @@ export class Generator { protected operations = new Map(); 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 { @@ -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 { diff --git a/packages/ng-openapi-gen/src/lib/options.ts b/packages/ng-openapi-gen/src/lib/options.ts index 10095b3..e582b12 100644 --- a/packages/ng-openapi-gen/src/lib/options.ts +++ b/packages/ng-openapi-gen/src/lib/options.ts @@ -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]: { diff --git a/packages/ng-openapi-gen/src/lib/utils/file-system.ts b/packages/ng-openapi-gen/src/lib/utils/file-system.ts index a8cdce7..0e79d16 100644 --- a/packages/ng-openapi-gen/src/lib/utils/file-system.ts +++ b/packages/ng-openapi-gen/src/lib/utils/file-system.ts @@ -32,7 +32,7 @@ export function syncDirs(srcDir: string, destDir: string, removeStale: boolean, const destContent = existsSync(destFile) ? fileRead(destFile) : null; if (srcContent !== destContent) { fileWrite(destFile, srcContent); - verbose && console.debug('Created file ' + destFile); + verbose && console.debug(destContent ? `Updated file ${destFile}` : `Created file ${destFile}`); } } } @@ -44,7 +44,7 @@ export function syncDirs(srcDir: string, destDir: string, removeStale: boolean, const destFile = join(destDir, file); if (!existsSync(srcFile) && lstatSync(destFile).isFile()) { unlinkSync(destFile); - verbose && console.debug('Removed stale file ' + destFile); + verbose && console.debug(`Removed stale file ${destFile}`); } } } diff --git a/packages/ng-openapi-gen/src/schema.json b/packages/ng-openapi-gen/src/schema.json index 3f81dcc..cb6a8e8 100644 --- a/packages/ng-openapi-gen/src/schema.json +++ b/packages/ng-openapi-gen/src/schema.json @@ -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", diff --git a/tools/executors/schema-generator/schema-descriptor.ts b/tools/executors/schema-generator/schema-descriptor.ts index aaf2cb1..a31e8f7 100644 --- a/tools/executors/schema-generator/schema-descriptor.ts +++ b/tools/executors/schema-generator/schema-descriptor.ts @@ -155,6 +155,12 @@ export const schemaDescriptor: { properties: Record } = { 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.",