Skip to content

Commit

Permalink
Fully switch to ESM (scripts, tests, etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 9, 2022
1 parent 97c9512 commit 0449966
Show file tree
Hide file tree
Showing 243 changed files with 1,649 additions and 1,606 deletions.
11 changes: 3 additions & 8 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ rules:
node/no-exports-assign: error
node/no-extraneous-import: error
node/no-extraneous-require: error
node/no-missing-import: [error, { allowModules: ['graphql'] }]
node/no-missing-import: off # Blocked by https://github.com/mysticatea/eslint-plugin-node/issues/248
node/no-missing-require: error
node/no-new-require: error
node/no-path-concat: error
Expand Down Expand Up @@ -81,7 +81,7 @@ rules:

# Static analysis
# https://github.com/benmosher/eslint-plugin-import#static-analysis
import/no-unresolved: [error, { ignore: ['graphql'] }]
import/no-unresolved: off # blocked by https://github.com/import-js/eslint-plugin-import/issues/2170
import/named: error
import/default: error
import/namespace: error
Expand Down Expand Up @@ -127,7 +127,6 @@ rules:
import/extensions:
- error
- ignorePackages
- ts: never # TODO: remove once TS supports extensions
import/order: [error, { newlines-between: always-and-inside-groups }]
import/newline-after-import: error
import/prefer-default-export: off
Expand Down Expand Up @@ -691,13 +690,9 @@ overrides:
env:
node: true
rules:
internal-rules/only-ascii: [error, { allowEmoji: true }]
node/no-sync: off
import/no-namespace: off
import/no-unresolved: off
import/no-nodejs-modules: off
no-console: off
no-await-in-loop: off
- files: 'resources/**'
env:
node: true
Expand All @@ -709,6 +704,7 @@ overrides:
import/no-extraneous-dependencies: [error, { devDependencies: true }]
import/no-nodejs-modules: off
no-console: off
no-await-in-loop: off
- files: 'resources/eslint-internal-rules/**'
env:
node: true
Expand All @@ -735,7 +731,6 @@ overrides:
version: detect
rules:
node/no-unpublished-require: off
node/no-missing-import: off
import/no-default-export: off
import/no-commonjs: off
import/no-nodejs-modules: off
Expand Down
6 changes: 3 additions & 3 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fail-zero: true
throw-deprecation: true
check-leaks: true
require:
- 'ts-node/register/transpile-only'
extension:
- 'ts'
- ts
node-option:
- 'loader=ts-node/esm/transpile-only'
10 changes: 5 additions & 5 deletions integrationTests/integration-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as childProcess from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import childProcess from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { describe, it } from 'mocha';

Expand All @@ -26,7 +26,7 @@ describe('Integration Tests', () => {
);

function testOnNodeProject(projectName: string) {
const projectPath = path.join(__dirname, projectName);
const projectPath = new URL(projectName, import.meta.url).pathname;

const packageJSONPath = path.join(projectPath, 'package.json');
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'));
Expand Down
1 change: 0 additions & 1 deletion integrationTests/webpack/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from 'assert';

// eslint-disable-next-line import/no-unresolved, node/no-missing-import
import mainCJS from './dist/main.cjs';

assert.deepStrictEqual(mainCJS.result, {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A Query Language and Runtime which can target any service.",
"license": "MIT",
"private": true,
"type": "module",
"typesVersions": {
">=4.4.0": {
"*": [
Expand Down Expand Up @@ -32,7 +33,7 @@
"version": "ts-node resources/gen-version.ts && npm test && git add src/version.ts",
"fuzzonly": "mocha --full-trace src/**/__tests__/**/*-fuzz.ts",
"changelog": "ts-node resources/gen-changelog.ts",
"benchmark": "ts-node benchmark/benchmark.ts",
"benchmark": "ts-node resources/benchmark.ts",
"test": "npm run lint && npm run check && npm run testonly:cover && npm run prettier:check && npm run check:spelling && npm run check:integrations",
"lint": "eslint --cache --max-warnings 0 .",
"check": "tsc --pretty",
Expand Down
68 changes: 0 additions & 68 deletions resources/add-extension-to-import-paths.ts

This file was deleted.

94 changes: 42 additions & 52 deletions benchmark/benchmark.ts → resources/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as assert from 'node:assert';
import * as cp from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import assert from 'node:assert';
import cp from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { exec, execOutput, localRepoPath } from './utils.js';

const NS_PER_SEC = 1e9;
const LOCAL = 'local';
Expand All @@ -12,28 +14,14 @@ const maxTime = 5;
// The minimum sample size required to perform statistical analysis.
const minSamples = 5;

// Get the revisions and make things happen!
if (require.main === module) {
async function runBenchmarks() {
// Get the revisions and make things happen!
const { benchmarks, revisions } = getArguments(process.argv.slice(2));
const benchmarkProjects = prepareBenchmarkProjects(revisions);

runBenchmarks(benchmarks, benchmarkProjects).catch((error) => {
console.error(error);
process.exit(1);
});
}

function localDir(...paths: ReadonlyArray<string>) {
return path.join(__dirname, '..', ...paths);
}

function exec(command: string, options = {}) {
const result = cp.execSync(command, {
encoding: 'utf-8',
stdio: ['inherit', 'pipe', 'inherit'],
...options,
});
return result?.trimEnd();
for (const benchmark of benchmarks) {
await runBenchmark(benchmark, benchmarkProjects);
}
}

interface BenchmarkProject {
Expand All @@ -59,7 +47,7 @@ function prepareBenchmarkProjects(
fs.rmSync(projectPath, { recursive: true, force: true });
fs.mkdirSync(projectPath);

fs.cpSync(localDir('benchmark'), path.join(projectPath, 'benchmark'), {
fs.cpSync(localRepoPath('benchmark'), path.join(projectPath, 'benchmark'), {
recursive: true,
});

Expand All @@ -81,14 +69,14 @@ function prepareBenchmarkProjects(

function prepareNPMPackage(revision: string) {
if (revision === LOCAL) {
const repoDir = localDir();
const repoDir = localRepoPath();
const archivePath = path.join(tmpDir, 'graphql-local.tgz');
fs.renameSync(buildNPMArchive(repoDir), archivePath);
return archivePath;
}

// Returns the complete git hash for a given git revision reference.
const hash = exec(`git rev-parse "${revision}"`);
const hash = execOutput(`git rev-parse "${revision}"`);

const archivePath = path.join(tmpDir, `graphql-${hash}.tgz`);
if (fs.existsSync(archivePath)) {
Expand All @@ -109,7 +97,9 @@ function prepareBenchmarkProjects(
exec('npm --quiet run build:npm', { cwd: repoDir });

const distDir = path.join(repoDir, 'npmDist');
const archiveName = exec(`npm --quiet pack ${distDir}`, { cwd: repoDir });
const archiveName = execOutput(`npm --quiet pack ${distDir}`, {
cwd: repoDir,
});
return path.join(repoDir, archiveName);
}
}
Expand Down Expand Up @@ -266,35 +256,33 @@ function maxBy<T>(array: ReadonlyArray<T>, fn: (obj: T) => number) {
}

// Prepare all revisions and run benchmarks matching a pattern against them.
async function runBenchmarks(
benchmarks: ReadonlyArray<string>,
async function runBenchmark(
benchmark: string,
benchmarkProjects: ReadonlyArray<BenchmarkProject>,
) {
for (const benchmark of benchmarks) {
const results = [];
for (let i = 0; i < benchmarkProjects.length; ++i) {
const { revision, projectPath } = benchmarkProjects[i];
const modulePath = path.join(projectPath, benchmark);

if (i === 0) {
const { name } = await sampleModule(modulePath);
console.log('⏱ ' + name);
}
const results = [];
for (let i = 0; i < benchmarkProjects.length; ++i) {
const { revision, projectPath } = benchmarkProjects[i];
const modulePath = path.join(projectPath, benchmark);

if (i === 0) {
const { name } = await sampleModule(modulePath);
console.log('⏱ ' + name);
}

try {
const samples = await collectSamples(modulePath);
try {
const samples = await collectSamples(modulePath);

results.push(computeStats(revision, samples));
process.stdout.write(' ' + cyan(i + 1) + ' tests completed.\u000D');
} catch (error) {
console.log(' ' + revision + ': ' + red(String(error)));
}
results.push(computeStats(revision, samples));
process.stdout.write(' ' + cyan(i + 1) + ' tests completed.\u000D');
} catch (error) {
console.log(' ' + revision + ': ' + red(String(error)));
}
console.log('\n');

beautifyBenchmark(results);
console.log('');
}
console.log('\n');

beautifyBenchmark(results);
console.log('');
}

function getArguments(argv: ReadonlyArray<string>) {
Expand Down Expand Up @@ -324,7 +312,7 @@ function getArguments(argv: ReadonlyArray<string>) {

function findAllBenchmarks() {
return fs
.readdirSync(localDir('benchmark'), { withFileTypes: true })
.readdirSync(localRepoPath('benchmark'), { withFileTypes: true })
.filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name)
.filter((name) => name.endsWith('-benchmark.js'))
Expand Down Expand Up @@ -421,3 +409,5 @@ function sampleModule(modulePath: string): Promise<BenchmarkSample> {
});
});
}

await runBenchmarks();
Loading

0 comments on commit 0449966

Please sign in to comment.