Skip to content

Commit d320e2b

Browse files
committed
Troubleshoot windows failure with new URL
Must use `fileURLToPath` to convert properly back from URL into a path.
1 parent e02b20c commit d320e2b

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/bin.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { existsSync } from 'fs';
2+
import { fileURLToPath } from 'url';
23
import execa from 'execa';
34
import { join } from 'path';
45
import { createTempDir, TempDir } from 'broccoli-test-helper';
56
import slash from 'slash';
67

7-
const COMPILED_BIN_PATH = new URL('./bin.js', import.meta.url).pathname;
8+
const COMPILED_BIN_PATH = fileURLToPath(new URL('./bin.js', import.meta.url));
9+
810
if (!existsSync(COMPILED_BIN_PATH)) {
9-
throw new Error('Missing compiled output, run `yarn build`!');
11+
throw new Error(
12+
`Missing compiled output, run \`yarn build\`! Looked at ${COMPILED_BIN_PATH} (based on ${
13+
import.meta.url
14+
})`
15+
);
1016
}
1117

1218
function run(args: string[], cwd: string) {

src/bin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as os from 'os';
44
import { readFileSync } from 'fs';
55
import { program } from 'commander';
66
import run from './runner.js';
7+
import { pathToFileURL } from 'url';
78

89
const version = JSON.parse(
910
readFileSync(new URL('../package.json', import.meta.url), { encoding: 'utf-8' })
@@ -36,5 +37,6 @@ if (program.args.length < 1 || !programOptions.transform) {
3637
silent: programOptions.silent,
3738
};
3839

39-
run(programOptions.transform, program.args, options);
40+
const transformPath = pathToFileURL(programOptions.transform);
41+
run(transformPath, program.args, options);
4042
}

src/runner.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import workerpool from 'workerpool';
1212

1313
tmp.setGracefulCleanup();
1414

15-
const WORKER_URL = new URL('./worker.js', import.meta.url);
15+
import { fileURLToPath } from 'url';
16+
const WORKER_PATH = fileURLToPath(new URL('./worker.js', import.meta.url));
1617

1718
class NoFilesError extends Error {}
1819

@@ -121,7 +122,7 @@ class StatsCollector {
121122
}
122123

123124
export default async function run(
124-
transformFile: string,
125+
transformFile: URL,
125126
filePaths: string[],
126127
options: { silent?: boolean; cpus: number }
127128
): Promise<void> {
@@ -151,11 +152,11 @@ export default async function run(
151152
/**
152153
* Returns the location of the transform module on disk.
153154
*/
154-
async function loadTransform(transformFile: string): Promise<string> {
155-
const isRemote = transformFile.startsWith('http');
155+
async function loadTransform(transformFile: URL): Promise<string> {
156+
const isLocal = transformFile.protocol === 'file:';
156157

157-
if (!isRemote) {
158-
return resolve(process.cwd(), transformFile);
158+
if (isLocal) {
159+
return fileURLToPath(transformFile);
159160
}
160161

161162
const contents = await downloadFile(transformFile);
@@ -166,9 +167,9 @@ async function loadTransform(transformFile: string): Promise<string> {
166167
return filePath.name;
167168
}
168169

169-
function downloadFile(url: string): Promise<string> {
170+
function downloadFile(url: URL): Promise<string> {
170171
return new Promise((resolve, reject) => {
171-
const transport = url.startsWith('https') ? https : http;
172+
const transport = url.protocol === 'https:' ? https : http;
172173

173174
let contents = '';
174175
transport
@@ -221,7 +222,7 @@ async function spawnWorkers(
221222

222223
logger.spin('Processed 0 files');
223224

224-
const pool = workerpool.pool(WORKER_URL.pathname, { maxWorkers: cpus });
225+
const pool = workerpool.pool(WORKER_PATH, { maxWorkers: cpus });
225226

226227
let i = 0;
227228
const worker = (queue as any).async.asyncify(async (file: string) => {
@@ -240,13 +241,14 @@ async function spawnWorkers(
240241

241242
function handleError(err: any, logger: Logger): void {
242243
if (err.code === 'MODULE_NOT_FOUND') {
243-
logger.error('Transform plugin not found');
244+
logger.error(`Transform plugin not found`);
244245
} else if (err instanceof NoFilesError) {
245246
logger.error('No files matched');
246247
} else {
247248
logger.error(err);
248-
if (err.stack) {
249-
logger.error(err.stack);
250-
}
249+
}
250+
251+
if (err.stack) {
252+
logger.error(err.stack);
251253
}
252254
}

0 commit comments

Comments
 (0)