Skip to content

Commit 18c1405

Browse files
committed
convert a handful of files to TypeScript
1 parent 4ad9c28 commit 18c1405

File tree

14 files changed

+86
-81
lines changed

14 files changed

+86
-81
lines changed

packages/server/lib/cache.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Promise from 'bluebird'
33
import { globalPubSub } from '@packages/data-context'
44
import { fs } from './util/fs'
55
import appData from './util/app_data'
6-
import FileUtil from './util/file'
6+
import { File as FileUtil } from './util/file'
77
import type { Cache, CachedUser, Preferences, Cohort } from '@packages/types'
88

99
interface Transaction {
@@ -55,6 +55,7 @@ export const cache = {
5555
},
5656

5757
getProjectRoots (): Promise<string[]> {
58+
// @ts-expect-error - transaction is untyped currently
5859
return fileUtil.transaction((tx: Transaction) => {
5960
return this._getProjects(tx).then((projects) => {
6061
const pathsToRemove = Promise.reduce(projects, (memo: string[], path: string) => {
@@ -76,6 +77,7 @@ export const cache = {
7677
},
7778

7879
removeProject (path: string): Promise<void> {
80+
// @ts-expect-error - transaction is untyped currently
7981
return fileUtil.transaction((tx: Transaction) => {
8082
return this._getProjects(tx).then((projects) => {
8183
return this._removeProjects(tx, projects, path)
@@ -84,6 +86,7 @@ export const cache = {
8486
},
8587

8688
insertProject (path: string): Promise<void> {
89+
// @ts-expect-error - transaction is untyped currently
8790
return fileUtil.transaction((tx: Transaction) => {
8891
return this._getProjects(tx).then((projects) => {
8992
// projects are sorted by most recently used, so add a project to
@@ -124,6 +127,7 @@ export const cache = {
124127
},
125128

126129
insertProjectPreferences (projectTitle: string, projectPreferences: Preferences): Promise<void> {
130+
// @ts-expect-error - transaction is untyped currently
127131
return fileUtil.transaction((tx: Transaction) => {
128132
return tx.get('PROJECT_PREFERENCES', {}).then((preferences) => {
129133
return tx.set('PROJECT_PREFERENCES', {
@@ -163,6 +167,7 @@ export const cache = {
163167
},
164168

165169
insertCohort (cohort: Cohort): Promise<void> {
170+
// @ts-expect-error - transaction is untyped currently
166171
return fileUtil.transaction((tx: Transaction) => {
167172
return tx.get('COHORTS', {}).then((cohorts) => {
168173
return tx.set('COHORTS', {

packages/server/lib/cloud/protocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import os from 'os'
88
import path from 'path'
99
import { strictAgent } from '@packages/network'
1010
import pkg from '@packages/root'
11-
import env from '../util/env'
11+
import * as env from '../util/env'
1212
import { putProtocolArtifact } from './api/put_protocol_artifact'
1313
import { requireScript } from './require_script'
1414
import * as routes from './routes'

packages/server/lib/modes/record.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { AllCypressErrorNames } from '@packages/errors'
1616
import { get as getErrors, warning as errorsWarning, throwErr } from '../errors'
1717
import * as capture from '../capture'
1818
import { getResolvedRuntimeConfig } from '../config'
19-
import env from '../util/env'
19+
import * as env from '../util/env'
2020
import ciProvider from '../util/ci_provider'
2121
import { flattenSuiteIntoRunnables } from '../util/tests_utils'
2222
import { countStudioUsage } from '../util/spec_writer'

packages/server/lib/modes/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { openProject } from '../open_project'
1515
import * as videoCapture from '../video_capture'
1616
import { fs, getPath } from '../util/fs'
1717
import runEvents from '../plugins/run_events'
18-
import env from '../util/env'
18+
import * as env from '../util/env'
1919
import trash from '../util/trash'
2020
import { id as randomId } from '../util/random'
2121
import system from '../util/system'

packages/server/lib/saved_state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Debug from 'debug'
44
import Bluebird from 'bluebird'
55
import appData from './util/app_data'
66
import { getCwd } from './cwd'
7-
import FileUtil from './util/file'
7+
import { File as FileUtil } from './util/file'
88
import { fs } from './util/fs'
99
import { AllowedState, allowedKeys } from '@packages/types'
1010
import { globalPubSub } from '@packages/data-context'

packages/server/lib/util/env.js

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

packages/server/lib/util/env.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const set = (key: string, val: string): string | undefined => {
2+
return process.env[key] = val
3+
}
4+
5+
export const get = (key: string): string | undefined => {
6+
return process.env[key]
7+
}

packages/server/lib/util/file.js renamed to packages/server/lib/util/file.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
const _ = require('lodash')
2-
const os = require('os')
3-
const md5 = require('md5')
4-
const path = require('path')
5-
const debugVerbose = require('debug')('cypress-verbose:server:util:file')
6-
const Promise = require('bluebird')
7-
const lockFile = Promise.promisifyAll(require('lockfile'))
8-
const { fs } = require('./fs')
9-
const env = require('./env')
10-
const exit = require('./exit')
11-
const { default: pQueue } = require('p-queue')
1+
import _ from 'lodash'
2+
import os from 'os'
3+
import md5 from 'md5'
4+
import path from 'path'
5+
import debugModule from 'debug'
6+
import Promise from 'bluebird'
7+
import lockFileModule from 'lockfile'
8+
import { fs } from './fs'
9+
import * as env from './env'
10+
import exit from './exit'
11+
import pQueue from 'p-queue'
12+
const lockFile = Promise.promisifyAll(lockFileModule)
13+
14+
const debugVerbose = debugModule('cypress-verbose:server:util:file')
1215

1316
const DEBOUNCE_LIMIT = 1000
1417
const LOCK_TIMEOUT = 2000
1518

1619
function getUid () {
1720
try {
18-
// eslint-disable-next-line no-restricted-properties
21+
// @ts-expect-error - process.geteuid is defined
1922
return process.geteuid()
2023
} catch (err) {
2124
// process.geteuid() can fail, return a constant
@@ -24,8 +27,28 @@ function getUid () {
2427
}
2528
}
2629

27-
class File {
28-
constructor (options = {}) {
30+
export class File {
31+
_lockFileDir!: string
32+
_lockFilePath!: string
33+
_queue!: pQueue
34+
_cache!: Record<string, any>
35+
_lastRead!: number
36+
path: string
37+
38+
static noopFile = {
39+
get () {
40+
return Promise.resolve({})
41+
},
42+
set () {
43+
return Promise.resolve()
44+
},
45+
transaction () {},
46+
remove () {
47+
return Promise.resolve()
48+
},
49+
}
50+
51+
constructor (options: { path?: string } = {}) {
2952
if (!options.path) {
3053
throw new Error('Must specify path to file when creating new FileUtil()')
3154
}
@@ -70,13 +93,13 @@ class File {
7093
get (...args) {
7194
debugVerbose('get values from %s', this.path)
7295

73-
return this._get(false, ...args)
96+
return this._get(false, ...(args as [string, any]))
7497
}
7598

7699
set (...args) {
77100
debugVerbose('set values in %s', this.path)
78101

79-
return this._set(false, ...args)
102+
return this._set(false, ...(args as [string, any]))
80103
}
81104

82105
remove () {
@@ -136,6 +159,7 @@ class File {
136159
.then(() => {
137160
debugVerbose('read %s', this.path)
138161

162+
// @ts-expect-error
139163
return fs.readJsonAsync(this.path, 'utf8')
140164
})
141165
.catch((err) => {
@@ -208,6 +232,7 @@ class File {
208232
.then(() => {
209233
debugVerbose('write %s', this.path)
210234

235+
// @ts-expect-error
211236
return fs.outputJsonAsync(this.path, this._cache, { spaces: 2 })
212237
})
213238
.finally(() => {
@@ -221,6 +246,7 @@ class File {
221246
debugVerbose('attempt to get lock on %s', this.path)
222247

223248
return fs
249+
// @ts-expect-error
224250
.ensureDirAsync(this._lockFileDir)
225251
.then(() => {
226252
// polls every 100ms up to 2000ms to obtain lock, otherwise rejects
@@ -245,18 +271,3 @@ class File {
245271
})
246272
}
247273
}
248-
249-
File.noopFile = {
250-
get () {
251-
return Promise.resolve({})
252-
},
253-
set () {
254-
return Promise.resolve()
255-
},
256-
transaction () {},
257-
remove () {
258-
return Promise.resolve()
259-
},
260-
}
261-
262-
module.exports = File

packages/server/lib/util/print-run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import pkg from '@packages/root'
88
import humanTime from './human_time'
99
import duration from './duration'
1010
import newlines from './newlines'
11-
import env from './env'
11+
import * as env from './env'
1212
import terminal from './terminal'
1313
import { getIsCi } from './ci_provider'
1414
import * as experiments from '../experiments'
@@ -704,7 +704,7 @@ export const beginUploadActivityOutput = () => {
704704
process.stdout.write(chalk.bold.blue('. '))
705705
const uploadActivityInterval = setInterval(() => {
706706
process.stdout.write(chalk.bold.blue('. '))
707-
}, UPLOAD_ACTIVITY_INTERVAL)
707+
}, Number(UPLOAD_ACTIVITY_INTERVAL))
708708

709709
return () => {
710710
if (uploadActivityInterval) {

packages/server/lib/util/resolve.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const env = require('./env')
21
const debug = require('debug')('cypress:server:plugins')
32

43
module.exports = {
@@ -9,7 +8,7 @@ module.exports = {
98
* @returns {string|null} path if typescript exists, otherwise null
109
*/
1110
typescript: (projectRoot) => {
12-
if (env.get('CYPRESS_INTERNAL_NO_TYPESCRIPT') === '1' || !projectRoot) {
11+
if (process.env['CYPRESS_INTERNAL_NO_TYPESCRIPT'] === '1' || !projectRoot) {
1312
return null
1413
}
1514

0 commit comments

Comments
 (0)