From 5e94f3f9c9671fbbb9ff31a9ae2f747a8ccdab42 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Tue, 30 Jun 2020 20:54:35 +0100 Subject: [PATCH] Add typing info to requires wherever possible Change-type: patch --- automation/deploy-bin.ts | 8 ++++++-- automation/run.ts | 2 +- lib/app-common.ts | 7 +++---- lib/utils/compose_ts.ts | 4 ++-- lib/utils/helpers.ts | 2 +- lib/utils/remote-build.ts | 2 +- npm-shrinkwrap.json | 24 +++++++++++++++++++++--- package.json | 3 ++- tests/docker-build.ts | 4 +++- tests/helpers.ts | 3 +-- 10 files changed, 41 insertions(+), 18 deletions(-) diff --git a/automation/deploy-bin.ts b/automation/deploy-bin.ts index 626a77f1..0338e9b2 100644 --- a/automation/deploy-bin.ts +++ b/automation/deploy-bin.ts @@ -63,7 +63,8 @@ export async function release() { /** Return a cached Octokit instance, creating a new one as needed. */ const getOctokit = _.once(function () { const Octokit = (require('@octokit/rest') as typeof import('@octokit/rest')).Octokit.plugin( - require('@octokit/plugin-throttling'), + (require('@octokit/plugin-throttling') as typeof import('@octokit/plugin-throttling')) + .throttling, ); return new Octokit({ auth: GITHUB_TOKEN, @@ -109,8 +110,11 @@ function getPageNumbers( if (!response.headers.link) { return res; } - const parse = require('parse-link-header'); + const parse = require('parse-link-header') as typeof import('parse-link-header'); const parsed = parse(response.headers.link); + if (parsed == null) { + throw new Error(`Failed to parse link header: '${response.headers.link}'`); + } let perPage = perPageDefault; if (parsed.next) { if (parsed.next.per_page) { diff --git a/automation/run.ts b/automation/run.ts index bb054277..4c163674 100644 --- a/automation/run.ts +++ b/automation/run.ts @@ -83,7 +83,7 @@ export async function run(args?: string[]) { // limitation of some library used by NSIS), as the "current working dir" // provided by balena CI is a rather long path to start with. if (process.platform === 'win32' && !process.env.BUILD_TMP) { - const randID = require('crypto') + const randID = (await import('crypto')) .randomBytes(6) .toString('base64') .replace(/\+/g, '-') diff --git a/lib/app-common.ts b/lib/app-common.ts index 204bdd7b..356a8c9e 100644 --- a/lib/app-common.ts +++ b/lib/app-common.ts @@ -211,11 +211,10 @@ export function makeUrlFromTunnelNgConfig(cfg: GlobalTunnelNgConfig): string { function setupBalenaSdkSharedOptions(settings: CliSettings) { // We don't yet use balena-sdk directly everywhere, but we set up shared // options correctly so we can do safely in submodules - const BalenaSdk = require('balena-sdk'); + const BalenaSdk = require('balena-sdk') as typeof import('balena-sdk'); BalenaSdk.setSharedOptions({ apiUrl: settings.get('apiUrl'), dataDirectory: settings.get('dataDirectory'), - retries: 2, }); } @@ -232,7 +231,7 @@ export function configureBluebird() { return; } BluebirdConfigured = true; - const Bluebird = require('bluebird'); + const Bluebird = require('bluebird') as typeof import('bluebird'); Bluebird.config({ longStackTraces: process.env.DEBUG ? true : false, }); @@ -263,5 +262,5 @@ export async function globalInit() { setupBalenaSdkSharedOptions(settings); // check for CLI updates once a day - require('./utils/update').notify(); + (await import('./utils/update')).notify(); } diff --git a/lib/utils/compose_ts.ts b/lib/utils/compose_ts.ts index cd8a6548..41fd7419 100644 --- a/lib/utils/compose_ts.ts +++ b/lib/utils/compose_ts.ts @@ -544,7 +544,7 @@ async function performResolution( releaseHash: string, preprocessHook?: (dockerfile: string) => string, ): Promise { - const { cloneTarStream } = require('tar-utils'); + const { cloneTarStream } = await import('tar-utils'); return await new Promise((resolve, reject) => { const buildTasks = MultiBuild.performResolution( @@ -796,7 +796,7 @@ export async function deployProject( apiEndpoint: string, skipLogUpload: boolean, ): Promise> { - const releaseMod = require('balena-release'); + const releaseMod = await import('balena-release'); const { createRelease, tagServiceImages } = await import('./compose'); const tty = (await import('./tty'))(process.stdout); diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index 3671a039..e75ced0b 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -20,7 +20,7 @@ import * as Bluebird from 'bluebird'; import { spawn, SpawnOptions } from 'child_process'; import * as _ from 'lodash'; import * as os from 'os'; -import * as ShellEscape from 'shell-escape'; +import type * as ShellEscape from 'shell-escape'; import type { Device, PineOptionsFor } from 'balena-sdk'; import { ExpectedError } from '../errors'; diff --git a/lib/utils/remote-build.ts b/lib/utils/remote-build.ts index c4a55b54..e7f9731e 100644 --- a/lib/utils/remote-build.ts +++ b/lib/utils/remote-build.ts @@ -331,7 +331,7 @@ function createRemoteBuildRequest( builderUrl: string, onError: (error: Error) => void, ): request.Request { - const zlib = require('zlib'); + const zlib = require('zlib') as typeof import('zlib'); if (DEBUG_MODE) { console.error(`[debug] Connecting to builder at ${builderUrl}`); } diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index ac798dac..e80f56d9 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -755,12 +755,24 @@ } }, "@octokit/plugin-throttling": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/@octokit/plugin-throttling/-/plugin-throttling-2.7.1.tgz", - "integrity": "sha512-08CKNFCpSpmOEAQBn6/MR8zbJgjP4+bplNUJbKlqJSNBHTO1NdsDHzBD4VeFYopOo7rBEySng4WifxNVaQ5bVw==", + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/@octokit/plugin-throttling/-/plugin-throttling-3.3.0.tgz", + "integrity": "sha512-0hMtpqTAwAvo3XkNoaPQFqX2+csfbuR0bYpruHq3anrBrhQFPt9Gn01RVqqoDla/qBYmIIY2MRirxlaLO21Apg==", "dev": true, "requires": { + "@octokit/types": "^5.0.0", "bottleneck": "^2.15.3" + }, + "dependencies": { + "@octokit/types": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-5.0.1.tgz", + "integrity": "sha512-GorvORVwp244fGKEt3cgt/P+M0MGy4xEDbckw+K5ojEezxyMDgCaYPKVct+/eWQfZXOT7uq0xRpmrl/+hliabA==", + "dev": true, + "requires": { + "@types/node": ">= 8" + } + } } }, "@octokit/request": { @@ -1389,6 +1401,12 @@ "integrity": "sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==", "dev": true }, + "@types/parse-link-header": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@types/parse-link-header/-/parse-link-header-1.0.0.tgz", + "integrity": "sha512-fCA3btjE7QFeRLfcD0Sjg+6/CnmC66HpMBoRfRzd2raTaWMJV21CCZ0LO8MOqf8onl5n0EPfjq4zDhbyX8SVwA==", + "dev": true + }, "@types/prettier": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.0.1.tgz", diff --git a/package.json b/package.json index 46be928f..5ad20201 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "@oclif/config": "^1.14.0", "@oclif/dev-cli": "1.22.0", "@oclif/parser": "^3.8.4", - "@octokit/plugin-throttling": "^2.7.1", + "@octokit/plugin-throttling": "^3.3.0", "@octokit/rest": "^16.43.1", "@types/archiver": "^2.1.3", "@types/bluebird": "^3.5.31", @@ -137,6 +137,7 @@ "@types/nock": "^11.0.7", "@types/node": "^10.17.20", "@types/node-cleanup": "^2.1.1", + "@types/parse-link-header": "^1.0.0", "@types/prettyjson": "0.0.29", "@types/progress-stream": "^2.0.0", "@types/request": "^2.48.4", diff --git a/tests/docker-build.ts b/tests/docker-build.ts index 99fda0e1..16fccb9a 100644 --- a/tests/docker-build.ts +++ b/tests/docker-build.ts @@ -94,7 +94,9 @@ export async function inspectTarStream( try { expect($expected).to.deep.equal(found); } catch (e) { - const { diff } = require('deep-object-diff'); + const { + diff, + } = require('deep-object-diff') as typeof import('deep-object-diff'); const diffStr = JSON.stringify( diff($expected, found), (_k, v) => (v === undefined ? 'undefined' : v), diff --git a/tests/helpers.ts b/tests/helpers.ts index 34c4888c..88793be1 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -15,8 +15,7 @@ * limitations under the License. */ -// tslint:disable-next-line:no-var-requires -require('./config-tests'); // required for side effects +import './config-tests'; // required for side effects import { execFile } from 'child_process'; import intercept = require('intercept-stdout');