Skip to content

Commit

Permalink
Publish as ESM (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed May 18, 2023
1 parent f9e4369 commit dacd357
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 375 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "chrome-launcher",
"main": "./dist/index.js",
"type": "module",
"engines": {
"node": ">=12.13.0"
},
"scripts": {
"build": "tsc",
"dev": "tsc -w",
"test": "mocha --require ts-node/register --reporter=dot test/**/*-test.ts --timeout=10000",
"test": "test/run-tests.sh",
"test-formatting": "test/check-formatting.sh",
"format": "scripts/format.sh",
"type-check": "tsc --allowJs --checkJs --noEmit --target es2019 *.js",
Expand All @@ -21,9 +22,9 @@
"@types/mocha": "^8.0.4",
"@types/sinon": "^9.0.1",
"clang-format": "^1.0.50",
"mocha": "^8.2.1",
"mocha": "^10.2.0",
"sinon": "^9.0.1",
"ts-node": "^9.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.1.2"
},
"dependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/chrome-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/
'use strict';

import fs = require('fs');
import path = require('path');
import fs from 'fs';
import path from 'path';
import {homedir} from 'os';
import {execSync, execFileSync} from 'child_process';
import escapeRegExp = require('escape-string-regexp');
const log = require('lighthouse-logger');
import escapeRegExp from 'escape-string-regexp';
import log from 'lighthouse-logger';

import {getWSLLocalAppDataPath, toWSLPath, ChromePathNotSetError} from './utils';
import {getWSLLocalAppDataPath, toWSLPath, ChromePathNotSetError} from './utils.js';

const newLineRegex = /\r?\n/;

Expand Down
15 changes: 8 additions & 7 deletions src/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as net from 'net';
import * as chromeFinder from './chrome-finder';
import {getRandomPort} from './random-port';
import {DEFAULT_FLAGS} from './flags';
import {makeTmpDir, defaults, delay, getPlatform, toWin32Path, InvalidUserDataDirectoryError, UnsupportedPlatformError, ChromeNotInstalledError} from './utils';
import * as chromeFinder from './chrome-finder.js';
import {getRandomPort} from './random-port.js';
import {DEFAULT_FLAGS} from './flags.js';
import {makeTmpDir, defaults, delay, getPlatform, toWin32Path, InvalidUserDataDirectoryError, UnsupportedPlatformError, ChromeNotInstalledError} from './utils.js';
import {ChildProcess} from 'child_process';
import {spawn, spawnSync} from 'child_process';
const log = require('lighthouse-logger');
import log from 'lighthouse-logger';

const isWsl = getPlatform() === 'wsl';
const isWindows = getPlatform() === 'win32';
const _SIGINT = 'SIGINT';
Expand Down Expand Up @@ -253,8 +254,8 @@ class Launcher {
try {
await this.isDebuggerReady();
log.log(
'ChromeLauncher',
`Found existing Chrome already running using port ${this.port}, using that.`);
'ChromeLauncher',
`Found existing Chrome already running using port ${this.port}, using that.`);
return;
} catch (err) {
log.log(
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './chrome-launcher';
export * from './chrome-launcher.js';
12 changes: 7 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
'use strict';

import {join} from 'path';
import {execSync, execFileSync} from 'child_process';
import childProcess from 'child_process';
import {mkdirSync} from 'fs';
import isWsl = require('is-wsl');
import isWsl from 'is-wsl';

export const enum LaunchErrorCodes {
ERR_LAUNCHER_PATH_NOT_SET = 'ERR_LAUNCHER_PATH_NOT_SET',
Expand Down Expand Up @@ -91,15 +91,15 @@ export function toWin32Path(dir: string = ''): string {
}

try {
return execFileSync('wslpath', ['-w', dir]).toString().trim();
return childProcess.execFileSync('wslpath', ['-w', dir]).toString().trim();
} catch {
return toWinDirFormat(dir);
}
}

export function toWSLPath(dir: string, fallback: string): string {
try {
return execFileSync('wslpath', ['-u', dir]).toString().trim();
return childProcess.execFileSync('wslpath', ['-u', dir]).toString().trim();
} catch {
return fallback;
}
Expand All @@ -121,7 +121,7 @@ export function getWSLLocalAppDataPath(path: string): string {
}

function makeUnixTmpDir() {
return execSync('mktemp -d -t lighthouse.XXXXXXX').toString().trim();
return childProcess.execSync('mktemp -d -t lighthouse.XXXXXXX').toString().trim();
}

function makeWin32TmpDir() {
Expand All @@ -133,3 +133,5 @@ function makeWin32TmpDir() {
mkdirSync(tmpdir, {recursive: true});
return tmpdir;
}

export {childProcess as _childProcessForTesting};
13 changes: 8 additions & 5 deletions test/chrome-launcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
*/
'use strict';

import {Launcher, launch, killAll, Options, getChromePath} from '../src/chrome-launcher';
import {DEFAULT_FLAGS} from '../src/flags';
import {Launcher, launch, killAll, Options, getChromePath} from '../src/chrome-launcher.js';
import {DEFAULT_FLAGS} from '../src/flags.js';

import {spy, stub} from 'sinon';
import sinon from 'sinon';
import * as assert from 'assert';
import * as fs from 'fs';
import fs from 'fs';

import log from 'lighthouse-logger';

const {spy, stub} = sinon;

const log = require('lighthouse-logger');
const fsMock = {
openSync: () => {},
closeSync: () => {},
Expand Down
5 changes: 3 additions & 2 deletions test/launch-signature-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

'use strict';

import {launch} from '../src/';
import {launch} from '../src/index.js';
import * as assert from 'assert';

const log = require('lighthouse-logger');
import log from 'lighthouse-logger';

describe('Launcher', () => {

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/random-port-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

import * as assert from 'assert';
import {getRandomPort} from '../src/random-port';
import {getRandomPort} from '../src/random-port.js';

describe('Random port generation', () => {
it('generates a valid random port number', async () => {
Expand Down
6 changes: 6 additions & 0 deletions test/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euxo pipefail

export TS_NODE_PROJECT="test/tsconfig.json"
mocha --loader=ts-node/esm --reporter=dot test/**/*-test.ts --timeout=10000
10 changes: 10 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"ts-node": {
"files": true,
},
"include": [
"*-test.ts",
"../types"
]
}
7 changes: 3 additions & 4 deletions test/utils-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
'use strict';

import * as assert from 'assert';
import { toWin32Path, toWSLPath, getWSLLocalAppDataPath } from '../src/utils';
import * as sinon from 'sinon';
import * as child_process from 'child_process';
import { toWin32Path, toWSLPath, getWSLLocalAppDataPath, _childProcessForTesting } from '../src/utils.js';
import sinon from 'sinon';

const execFileSyncStub = sinon.stub(child_process, 'execFileSync').callThrough();
const execFileSyncStub = sinon.stub(_childProcessForTesting, 'execFileSync').callThrough();

const asBuffer = (str: string): Buffer => Buffer.from(str, 'utf-8');

Expand Down
9 changes: 6 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "esnext",
"moduleResolution": "node",
"outDir": "dist",
"target": "es2019",
"declaration": true,
Expand All @@ -10,12 +11,14 @@
"strictNullChecks": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true
"noUnusedParameters": true,
"allowSyntheticDefaultImports": true,
},
"exclude": [
"node_modules"
],
"include": [
"src"
"src",
"types"
]
}
34 changes: 34 additions & 0 deletions types/lighthouse-logger.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license Copyright 2023 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

declare module 'lighthouse-logger' {
interface Status {
msg: string;
id: string;
args?: any[];
}
export function setLevel(level: string): void;
export function isVerbose(): boolean;
export function formatProtocol(prefix: string, data: Object, level?: string): void;
export function log(title: string, ...args: any[]): void;
export function warn(title: string, ...args: any[]): void;
export function error(title: string, ...args: any[]): void;
export function verbose(title: string, ...args: any[]): void;
export function time(status: Status, level?: string): void;
export function timeEnd(status: Status, level?: string): void;
export function reset(): string;
export const cross: string;
export const dim: string;
export const tick: string;
export const bold: string;
export const purple: string;
export function redify(message: any): string;
export function greenify(message: any): string;
/** Retrieves and clears all stored time entries */
export function takeTimeEntries(): PerformanceEntry[];
export function getTimeEntries(): PerformanceEntry[];
export const events: import('events').EventEmitter;
}

0 comments on commit dacd357

Please sign in to comment.