Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit a0f9cc4

Browse files
Convert requires to imports (#289)
PR-URL: #289
1 parent e135835 commit a0f9cc4

File tree

10 files changed

+75
-80
lines changed

10 files changed

+75
-80
lines changed

src.ts/agent/debug-assert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
const realAssert = require('assert');
17+
import * as realAssert from 'assert';
1818

1919
const nop = _=>_;
2020
const fakeAssert: any = nop;

src.ts/agent/debuglet.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@
1414
* limitations under the License.
1515
*/
1616

17-
var crypto = require('crypto');
18-
var fs = require('fs');
19-
var path = require('path');
17+
import * as crypto from 'crypto';
18+
import * as fs from 'fs';
19+
import * as path from 'path';
2020
import { EventEmitter } from 'events';
21-
var extend = require('extend');
22-
var util = require('util');
23-
var semver = require('semver');
24-
var _ = require('lodash');
25-
var metadata = require('gcp-metadata');
26-
var common = require('@google-cloud/common');
27-
28-
var v8debugapi = require('./v8debugapi.js');
29-
var Debuggee = require('../debuggee.js').Debuggee;
30-
var DebugletApi = require('../controller.js').Controller;
31-
var defaultConfig = require('./config.js').default;
32-
var scanner = require('./scanner.js');
33-
var StatusMessage = require('../status-message.js').StatusMessage;
34-
var SourceMapper = require('./sourcemapper.js');
35-
var pjson = require('../../package.json');
21+
import * as extend from 'extend';
22+
import * as util from 'util';
23+
import * as semver from 'semver';
24+
import * as _ from 'lodash';
25+
import * as metadata from 'gcp-metadata';
26+
import * as common from '@google-cloud/common';
27+
28+
import * as v8debugapi from './v8debugapi';
29+
import { Debuggee } from '../debuggee';
30+
import { Controller } from '../controller';
31+
// The following import syntax is used because './config' has a default export
32+
import defaultConfig from './config';
33+
import * as scanner from './scanner';
34+
import { StatusMessage } from '../status-message';
35+
import * as SourceMapper from './sourcemapper';
36+
const pjson = require('../../package.json');
3637

3738
var assert = require('assert');
3839

@@ -137,7 +138,7 @@ export class Debuglet extends EventEmitter {
137138
});
138139

139140
/** @private {DebugletApi} */
140-
this.debugletApi_ = new DebugletApi(this.debug_);
141+
this.debugletApi_ = new Controller(this.debug_);
141142

142143
/** @private {Debuggee} */
143144
this.debuggee_ = null;
@@ -352,12 +353,13 @@ export class Debuglet extends EventEmitter {
352353
}
353354

354355
getSourceContext_(callback) {
355-
fs.readFile('source-context.json', 'utf8', function(err, data) {
356+
fs.readFile('source-context.json', 'utf8', function(err: any, data) {
356357
var sourceContext;
357358
if (!err) {
358359
try {
359360
sourceContext = JSON.parse(data);
360361
} catch (e) {
362+
// TODO: Fix casting `err` from an ErrnoException to a string
361363
err = 'Malformed source-context.json file: ' + e;
362364
}
363365
}

src.ts/agent/scanner.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
var _ = require('lodash');
18-
var fs = require('fs');
19-
var path = require('path');
20-
var crypto = require('crypto');
21-
var findit = require('findit2');
22-
var split = require('split');
23-
24-
module.exports = {
25-
scan: scan
26-
};
17+
import * as _ from 'lodash';
18+
import * as fs from 'fs';
19+
import * as path from 'path';
20+
import * as crypto from 'crypto';
21+
import * as findit from 'findit2';
22+
import * as split from 'split';
2723

2824
class ScanResults {
2925
private stats_;
@@ -213,7 +209,8 @@ function stats(filename, shouldHash, cb: (err, stats?: FileStats) => void) {
213209
if (shouldHash) {
214210
shasum = crypto.createHash('sha1');
215211
}
216-
var s = fs.ReadStream(filename);
212+
// TODO: Determine why property 'ReadStream' does not exist on type 'fs'
213+
var s = (fs as any).ReadStream(filename);
217214
var lines = 0;
218215
var byLine = s.pipe(split());
219216
byLine.on('error', function(e) {

src.ts/agent/sourcemapper.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
/** @const */ var _ = require('lodash');
18-
/** @const */ var async = require('async');
19-
/** @const */ var fs = require('fs');
20-
/** @const */ var path = require('path');
17+
import * as _ from 'lodash';
18+
import * as async from 'async';
19+
import * as fs from 'fs';
20+
import * as path from 'path';
2121

22-
/** @const */ var sourceMap = require('source-map');
22+
import * as sourceMap from 'source-map';
2323

2424
/** @define {string} */ var MAP_EXT = '.map';
2525

src.ts/agent/state.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var ScopeType = require('vm').runInDebugContext('ScopeType');
17-
var assert = require('./debug-assert').debugAssert(process.env.CLOUD_DEBUG_ASSERTIONS);
18-
var util = require('util');
19-
var lodash = require('lodash');
20-
var transform = lodash.transform;
21-
var flatten = lodash.flatten;
22-
var isEmpty = lodash.isEmpty;
16+
import * as vm from 'vm';
17+
import { debugAssert } from './debug-assert';
18+
import * as util from 'util';
19+
import * as lodash from 'lodash';
20+
const transform = lodash.transform;
21+
const flatten = lodash.flatten;
22+
const isEmpty = lodash.isEmpty;
2323

24-
var StatusMessage = require('../status-message.js').StatusMessage;
24+
import { StatusMessage } from '../status-message';
25+
26+
const ScopeType = vm.runInDebugContext('ScopeType');
27+
const assert = debugAssert(process.env.CLOUD_DEBUG_ASSERTIONS);
2528

2629
// Error message indices into the resolved variable table.
2730
var BUFFER_FULL_MESSAGE_INDEX = 0;
@@ -117,7 +120,8 @@ class StateResolver {
117120
'` stack frames.',
118121
true) };
119122

120-
this.resolvedVariableTable_ = util._extend([], this.messageTable_);
123+
// TODO: Determine why _extend is used here
124+
this.resolvedVariableTable_ = (util as any)._extend([], this.messageTable_);
121125
this.rawVariableTable_ = this.messageTable_.map(function() { return null; });
122126
}
123127

src.ts/agent/v8debugapi.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
'use strict';
17+
import * as _ from 'lodash';
18+
import * as vm from 'vm';
19+
import * as path from 'path';
20+
import * as semver from 'semver';
1821

19-
/** @const */ var _ = require('lodash');
20-
/** @const */ var vm = require('vm');
21-
/** @const */ var path = require('path');
22-
/** @const */ var semver = require('semver');
23-
24-
/** @const */ var state = require('./state.js');
25-
/** @const */ var StatusMessage = require('../status-message.js').StatusMessage;
22+
import * as state from './state';
23+
import { StatusMessage } from '../status-message';
2624

2725
/** @const */ var messages = {
2826
INVALID_BREAKPOINT: 'invalid snapshot - id or location missing',
@@ -38,7 +36,7 @@
3836
V8_BREAKPOINT_DISABLED: 'Internal error: V8 breakpoint externally disabled',
3937
CAPTURE_BREAKPOINT_DATA: 'Error trying to capture snapshot data: ',
4038
INVALID_LINE_NUMBER: 'Invalid snapshot position: ',
41-
COULD_NOT_FIND_OUTPUT_FILE:
39+
COULD_NOT_FIND_OUTPUT_FILE:
4240
'Could not determine the output file associated with the transpiled input file'
4341
};
4442

@@ -58,7 +56,7 @@ var formatInterval = function(msg, interval) {
5856
};
5957

6058
var singleton;
61-
function create(logger_, config_, jsFiles_, sourcemapper_) {
59+
export function create(logger_, config_, jsFiles_, sourcemapper_) {
6260
if (singleton && !config_.forceNewAgent_) {
6361
return singleton;
6462
}
@@ -314,7 +312,7 @@ function create(logger_, config_, jsFiles_, sourcemapper_) {
314312

315313
// The breakpoint protobuf message presently doesn't have a column property
316314
// but it may have one in the future.
317-
var column = mapInfo && mapInfo.column ? mapInfo.column :
315+
var column = mapInfo && mapInfo.column ? mapInfo.column :
318316
(breakpoint.location.column || 1);
319317
var line = mapInfo ? mapInfo.line : breakpoint.location.line;
320318

@@ -566,7 +564,8 @@ function pathToRegExp(scriptPath) {
566564
return new RegExp(scriptPath + '$');
567565
}
568566

569-
function findScripts(scriptPath, config, fileStats) {
567+
// Exposed for unit testing.
568+
export function findScripts(scriptPath, config, fileStats) {
570569
// Use repository relative mapping if present.
571570
if (config.appPathRelativeToRepository) {
572571
var candidate = scriptPath.replace(config.appPathRelativeToRepository,
@@ -589,7 +588,7 @@ function findScripts(scriptPath, config, fileStats) {
589588
* Given an list of available files and a script path to match, this function
590589
* tries to resolve the script to a (hopefully unique) match in the file list
591590
* disregarding the full path to the script. This can be useful because repo
592-
* file paths (that the UI has) may not necessarily be suffixes of the absolute
591+
* file paths (that the UI has) may not necessarily be suffixes of the absolute
593592
* paths of the deployed files. This happens when the user deploys a
594593
* subdirectory of the repo.
595594
*
@@ -610,7 +609,8 @@ function findScripts(scriptPath, config, fileStats) {
610609
* available.
611610
* @return {array<string>} list of files that match.
612611
*/
613-
function findScriptsFuzzy(scriptPath, fileList) {
612+
// Exposed for unit testing.
613+
export function findScriptsFuzzy(scriptPath, fileList) {
614614
var matches = fileList;
615615
var components = scriptPath.split(path.sep);
616616
for (var i = components.length - 1; i >= 0; i--) {
@@ -622,10 +622,3 @@ function findScriptsFuzzy(scriptPath, fileList) {
622622
}
623623
return matches;
624624
}
625-
626-
module.exports = {
627-
create: create,
628-
// Exposed for unit testing.
629-
findScripts: findScripts,
630-
findScriptsFuzzy: findScriptsFuzzy
631-
};

src.ts/controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
'use strict';
18-
1917
/*!
2018
* @module debug/controller
2119
*/
2220

23-
var assert = require('assert');
24-
var common: { ServiceObject: new (any) => any } = require('@google-cloud/common');
25-
var qs = require('querystring');
21+
import * as _common from '@google-cloud/common';
22+
import * as assert from 'assert';
23+
import * as qs from 'querystring';
24+
25+
const common: { ServiceObject: new (any) => any } = _common;
2626

2727
/** @const {string} Cloud Debug API endpoint */
2828
var API = 'https://clouddebugger.googleapis.com/v2/controller';

src.ts/debug.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
var common: { Service: new (any) => any,
18-
util: any } = require('@google-cloud/common');
19-
var util = require('util');
17+
import * as _common from '@google-cloud/common';
18+
const common: { Service: new (any) => any, util: any } = _common;
19+
import * as util from 'util';
2020

2121
export class Debug {
2222
private options;

src.ts/debuggee.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
'use strict';
1716

18-
var pjson = require('../package.json');
19-
var _ = require('lodash');
17+
const pjson = require('../package.json');
18+
import * as _ from 'lodash';
2019

2120
export class Debuggee {
2221
private project;

src.ts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
var Debug = require('./debug.js').Debug;
18-
var Debuglet = require('./agent/debuglet.js').Debuglet;
17+
import { Debug } from './debug';
18+
import { Debuglet } from './agent/debuglet';
1919

2020
// Singleton.
2121
var debuglet;

0 commit comments

Comments
 (0)