-
Notifications
You must be signed in to change notification settings - Fork 26k
/
writeAppTypeDeclarations.ts
47 lines (41 loc) · 1.22 KB
/
writeAppTypeDeclarations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os from 'os'
import path from 'path'
import { promises as fs } from 'fs'
export async function writeAppTypeDeclarations(
baseDir: string,
imageImportsEnabled: boolean
): Promise<void> {
// Reference `next` types
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 =
'/// <reference types="next" />' +
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
// Avoids an un-necessary write on read-only fs
if (currentContent === content) {
return
}
await fs.writeFile(appTypeDeclarations, content)
}