Skip to content

Commit 550be4a

Browse files
authored
chore: convert additional server files to TypeScript (#32950)
* chore: convert server files p3 convert fixture file to TS fix types remove only * does deleting this break anything? looks unused * chore: convert tty to TS * convert terminal util to TS * convert status code to TS * chore: convert system util to TS * convert resolve to ts (tsx works inside child process) * empty commit * add comments to ts-expect-error
1 parent cd40ebd commit 550be4a

34 files changed

+592
-583
lines changed

packages/server/lib/cloud/exception.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Bluebird from 'bluebird'
33
import pkg from '@packages/root'
44
import api from './api'
55
import user from './user'
6-
import system from '../util/system'
6+
import * as system from '../util/system'
77
import { stripPath } from './strip_path'
88

99
const { serializeError } = require('serialize-error')

packages/server/lib/controllers/xhrs.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parseContentType } from '@packages/net-stubbing/lib/server/util'
22
import _ from 'lodash'
33
import Promise from 'bluebird'
4-
import fixture from '../fixture'
4+
import { get as fixtureGet } from '../fixture'
55

66
const fixturesRe = /^(fx:|fixture:)/
77

@@ -70,18 +70,19 @@ export = {
7070
return respond()
7171
},
7272

73-
_get (resp: string, config: { fixturesFolder: string }): Promise<{ data: any, encoding?: string }> {
74-
const options: { encoding?: string } = {}
73+
_get (resp: string, config: { fixturesFolder: string }): Promise<{ data: any, encoding?: BufferEncoding }> {
74+
const options: { encoding?: BufferEncoding } = {}
7575

7676
const file = resp.replace(fixturesRe, '')
7777

7878
const [filePath, encoding] = file.split(',')
7979

8080
if (encoding) {
81-
options.encoding = encoding
81+
options.encoding = encoding as BufferEncoding
8282
}
8383

84-
return fixture.get(config.fixturesFolder, filePath, options)
84+
// @ts-expect-error - bluebird to promise type mismatch
85+
return fixtureGet(config.fixturesFolder, filePath, options)
8586
.then((bytes: any) => {
8687
return {
8788
data: bytes,

packages/server/lib/files.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

packages/server/lib/files.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import path from 'path'
2+
import { fs } from './util/fs'
3+
4+
export async function readFile (projectRoot: string, options: { file: string, encoding?: BufferEncoding } = { file: '', encoding: 'utf8' }) {
5+
const filePath = path.resolve(projectRoot, options.file)
6+
7+
// https://github.com/cypress-io/cypress/issues/1558
8+
// If no encoding is specified, then Cypress has historically defaulted
9+
// to `utf8`, because of it's focus on text files. This is in contrast to
10+
// NodeJs, which defaults to binary. We allow users to pass in `null`
11+
// to restore the default node behavior.
12+
try {
13+
let contents
14+
15+
if (path.extname(filePath) === '.json' && options.encoding !== null) {
16+
contents = await fs.readJsonAsync(filePath, options.encoding === undefined ? 'utf8' : options.encoding)
17+
} else {
18+
contents = await fs.readFileAsync(filePath, {
19+
encoding: options.encoding === undefined ? 'utf8' : options.encoding,
20+
})
21+
}
22+
23+
return {
24+
contents,
25+
filePath,
26+
}
27+
} catch (err) {
28+
err.originalFilePath = options.file
29+
err.filePath = filePath
30+
throw err
31+
}
32+
}
33+
34+
export async function readFiles (projectRoot: string, options: { files: { path: string, encoding?: BufferEncoding }[] } = { files: [] }) {
35+
const files = await Promise.all(options.files.map(async (file) => {
36+
const { contents, filePath } = await readFile(projectRoot, {
37+
file: file.path,
38+
encoding: file.encoding,
39+
})
40+
41+
return {
42+
...file,
43+
filePath,
44+
contents,
45+
}
46+
}))
47+
48+
return files
49+
}
50+
51+
export async function writeFile (projectRoot: string, options: { fileName: string, contents: string, encoding?: BufferEncoding, flag?: string } = { fileName: '', contents: '', encoding: 'utf8', flag: 'w' }) {
52+
const filePath = path.resolve(projectRoot, options.fileName)
53+
const writeOptions = {
54+
encoding: options.encoding === undefined ? 'utf8' : options.encoding,
55+
flag: options.flag || 'w',
56+
}
57+
58+
try {
59+
await fs.outputFile(filePath, options.contents, writeOptions)
60+
61+
return {
62+
contents: options.contents,
63+
filePath,
64+
}
65+
} catch (err) {
66+
err.filePath = filePath
67+
throw err
68+
}
69+
}

packages/server/lib/fixture.js

Lines changed: 0 additions & 212 deletions
This file was deleted.

0 commit comments

Comments
 (0)