Unverified Commit 23ce41e2 authored by Luan Devecchi's avatar Luan Devecchi Committed by GitHub

Preserve `next-env.d.ts` line ending (#28100)

* Preserve `next-env.d.ts` line ending

Prevent next from changing already existing line ending on
`next-env.d.ts` for no good reason

* Update comparison
Co-authored-by: 's avatarSteven <steven@ceriously.com>

* Update checks and add tests

* update test
Co-authored-by: 's avatarSteven <steven@ceriously.com>
Co-authored-by: 's avatarjj@jjsweb.site <jj@jjsweb.site>
parent 05a732eb
import { promises as fs } from 'fs'
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import { fileExists } from '../file-exists' import { promises as fs } from 'fs'
export async function writeAppTypeDeclarations( export async function writeAppTypeDeclarations(
baseDir: string, baseDir: string,
...@@ -10,27 +9,41 @@ export async function writeAppTypeDeclarations( ...@@ -10,27 +9,41 @@ export async function writeAppTypeDeclarations(
// Reference `next` types // Reference `next` types
const appTypeDeclarations = path.join(baseDir, 'next-env.d.ts') const appTypeDeclarations = path.join(baseDir, 'next-env.d.ts')
// Defaults EOL to system default
let eol = os.EOL
let currentContent: string | undefined
try {
currentContent = await fs.readFile(appTypeDeclarations, 'utf8')
// If file already exists then preserve its line ending
const lf = currentContent.indexOf('\n', /* skip first so we can lf - 1 */ 1)
if (lf !== -1) {
if (currentContent[lf - 1] === '\r') {
eol = '\r\n'
} else {
eol = '\n'
}
}
} catch (err) {}
const content = const content =
'/// <reference types="next" />' + '/// <reference types="next" />' +
os.EOL + eol +
'/// <reference types="next/types/global" />' + '/// <reference types="next/types/global" />' +
os.EOL + eol +
(imageImportsEnabled (imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + os.EOL ? '/// <reference types="next/image-types/global" />' + eol
: '') + : '') +
os.EOL + eol +
'// NOTE: This file should not be edited' + '// NOTE: This file should not be edited' +
os.EOL + eol +
'// see https://nextjs.org/docs/basic-features/typescript for more information.' + '// see https://nextjs.org/docs/basic-features/typescript for more information.' +
os.EOL eol
// Avoids a write for read-only filesystems // Avoids an un-necessary write on read-only fs
if ( if (currentContent === content) {
(await fileExists(appTypeDeclarations)) &&
(await fs.readFile(appTypeDeclarations, 'utf8')) === content
) {
return return
} }
await fs.writeFile(appTypeDeclarations, content) await fs.writeFile(appTypeDeclarations, content)
} }
/* eslint-env jest */
import os from 'os'
import fs from 'fs-extra'
import { join } from 'path'
import { writeAppTypeDeclarations } from 'next/dist/lib/typescript/writeAppTypeDeclarations'
const fixtureDir = join(__dirname, 'fixtures/app-declarations')
const declarationFile = join(fixtureDir, 'next-env.d.ts')
const imageImportsEnabled = false
describe('find config', () => {
afterEach(() => fs.remove(declarationFile))
it('should preserve CRLF EOL', async () => {
const eol = '\r\n'
const content =
'/// <reference types="next" />' +
eol +
'/// <reference types="next/types/global" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
eol
await fs.ensureDir(fixtureDir)
await fs.writeFile(declarationFile, content)
await writeAppTypeDeclarations(fixtureDir, imageImportsEnabled)
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should preserve LF EOL', async () => {
const eol = '\n'
const content =
'/// <reference types="next" />' +
eol +
'/// <reference types="next/types/global" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
eol
await fs.ensureDir(fixtureDir)
await fs.writeFile(declarationFile, content)
await writeAppTypeDeclarations(fixtureDir, imageImportsEnabled)
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should use OS EOL by default', async () => {
const eol = os.EOL
const content =
'/// <reference types="next" />' +
eol +
'/// <reference types="next/types/global" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
eol
await fs.ensureDir(fixtureDir)
await writeAppTypeDeclarations(fixtureDir, imageImportsEnabled)
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment