Skip to content

Commit

Permalink
Troubleshoot windows failure with new URL
Browse files Browse the repository at this point in the history
Must use `fileURLToPath` to convert properly back from URL into a path.
  • Loading branch information
rwjblue committed Nov 4, 2021
1 parent e02b20c commit d320e2b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/bin.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { existsSync } from 'fs';
import { fileURLToPath } from 'url';
import execa from 'execa';
import { join } from 'path';
import { createTempDir, TempDir } from 'broccoli-test-helper';
import slash from 'slash';

const COMPILED_BIN_PATH = new URL('./bin.js', import.meta.url).pathname;
const COMPILED_BIN_PATH = fileURLToPath(new URL('./bin.js', import.meta.url));

if (!existsSync(COMPILED_BIN_PATH)) {
throw new Error('Missing compiled output, run `yarn build`!');
throw new Error(
`Missing compiled output, run \`yarn build\`! Looked at ${COMPILED_BIN_PATH} (based on ${
import.meta.url
})`
);
}

function run(args: string[], cwd: string) {
Expand Down
4 changes: 3 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as os from 'os';
import { readFileSync } from 'fs';
import { program } from 'commander';
import run from './runner.js';
import { pathToFileURL } from 'url';

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

run(programOptions.transform, program.args, options);
const transformPath = pathToFileURL(programOptions.transform);
run(transformPath, program.args, options);
}
28 changes: 15 additions & 13 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import workerpool from 'workerpool';

tmp.setGracefulCleanup();

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

class NoFilesError extends Error {}

Expand Down Expand Up @@ -121,7 +122,7 @@ class StatsCollector {
}

export default async function run(
transformFile: string,
transformFile: URL,
filePaths: string[],
options: { silent?: boolean; cpus: number }
): Promise<void> {
Expand Down Expand Up @@ -151,11 +152,11 @@ export default async function run(
/**
* Returns the location of the transform module on disk.
*/
async function loadTransform(transformFile: string): Promise<string> {
const isRemote = transformFile.startsWith('http');
async function loadTransform(transformFile: URL): Promise<string> {
const isLocal = transformFile.protocol === 'file:';

if (!isRemote) {
return resolve(process.cwd(), transformFile);
if (isLocal) {
return fileURLToPath(transformFile);
}

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

function downloadFile(url: string): Promise<string> {
function downloadFile(url: URL): Promise<string> {
return new Promise((resolve, reject) => {
const transport = url.startsWith('https') ? https : http;
const transport = url.protocol === 'https:' ? https : http;

let contents = '';
transport
Expand Down Expand Up @@ -221,7 +222,7 @@ async function spawnWorkers(

logger.spin('Processed 0 files');

const pool = workerpool.pool(WORKER_URL.pathname, { maxWorkers: cpus });
const pool = workerpool.pool(WORKER_PATH, { maxWorkers: cpus });

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

function handleError(err: any, logger: Logger): void {
if (err.code === 'MODULE_NOT_FOUND') {
logger.error('Transform plugin not found');
logger.error(`Transform plugin not found`);
} else if (err instanceof NoFilesError) {
logger.error('No files matched');
} else {
logger.error(err);
if (err.stack) {
logger.error(err.stack);
}
}

if (err.stack) {
logger.error(err.stack);
}
}

0 comments on commit d320e2b

Please sign in to comment.