Skip to content

Commit

Permalink
fix: convert __dirname to dirname(fileURLToPath(import.meta.url))
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdra committed Dec 28, 2021
1 parent 69595d7 commit 6db3b65
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'path';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { Worker } from 'worker_threads';
import { wrap } from 'comlink';
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand Down Expand Up @@ -33,7 +34,7 @@ export async function run(options: Options) {

// Directly executing the Core API will hog the main thread and halt the spinner.
// So we wrap it with comlink and run it on the Worker.
const worker = new Worker(join(__dirname, 'core-worker.js'));
const worker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'core-worker.js'));
const ProxiedCore = wrap<typeof SerializableCore>(nodeEndpoint.default(worker));
const core = await new ProxiedCore(config);

Expand Down
5 changes: 3 additions & 2 deletions src/util/filter-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { exec as execOriginal } from 'child_process';
import { mkdir, appendFile, readFile, access } from 'fs/promises';
import { tmpdir } from 'os';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { promisify } from 'util';

const exec = promisify(execOriginal);
Expand Down Expand Up @@ -36,9 +37,9 @@ export function generateFixableMakerScriptFilePath(ruleIds: string[]): string {
}

export function generateExampleFilterScriptFilePath(): string {
return join(__dirname, '..', '..', 'static', 'example-filter-script.js');
return join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'static', 'example-filter-script.js');
}

export function generateExampleFixableMakerScriptFilePath(): string {
return join(__dirname, '..', '..', 'static', 'example-fixable-maker-script.js');
return join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'static', 'example-fixable-maker-script.js');
}
12 changes: 7 additions & 5 deletions test/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { exec } from 'child_process';
import { join } from 'path';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { promisify } from 'util';
import { ESLint } from 'eslint';
import { mockConsoleLog } from 'jest-mock-process';
import { Core } from '../src/core.js';

const execPromise = promisify(exec);
const cwd = join(dirname(fileURLToPath(import.meta.url)), '..');

/**
* Returns a string containing the stitched together contents of the file modified by transform.
* To make the snapshot easier to read, the name of the file is inserted at the beginning of the contents of each file.
*/
async function getSnapshotOfChangedFiles(): Promise<string> {
const { stdout } = await execPromise(`diff -qr fixtures fixtures-tmp | cut -d " " -f 4 | xargs tail -n +1`, {
cwd: join(__dirname, '..'),
cwd,
});
return stdout.toString();
}
Expand All @@ -32,11 +34,11 @@ function normalize(results: ESLint.LintResult[]): ESLint.LintResult[] {
}

beforeEach(async () => {
await execPromise(`rm -rf fixtures-tmp && cp -r fixtures fixtures-tmp`, { cwd: join(__dirname, '..') });
await execPromise(`rm -rf fixtures-tmp && cp -r fixtures fixtures-tmp`, { cwd });
});

afterEach(async () => {
await execPromise(`rm -rf fixtures-tmp`, { cwd: join(__dirname, '..') });
await execPromise(`rm -rf fixtures-tmp`, { cwd });
});

describe('Core', () => {
Expand All @@ -45,7 +47,7 @@ describe('Core', () => {
rulePaths: ['fixtures-tmp/rules'],
extensions: ['.js', '.jsx', '.mjs'],
formatterName: 'codeframe',
cwd: join(__dirname, '..'),
cwd,
});
test('lint', async () => {
const results = await core.lint();
Expand Down

0 comments on commit 6db3b65

Please sign in to comment.