Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run example fails to load undefined file #3

Open
drdreo opened this issue Nov 29, 2024 · 5 comments
Open

Run example fails to load undefined file #3

drdreo opened this issue Nov 29, 2024 · 5 comments

Comments

@drdreo
Copy link

drdreo commented Nov 29, 2024

In the docs it refers to run nx day-1-a --data=sample . This command fails on Windows with

 nx run aoc:day-1-a --data=sample

> tsx day-1/a --data=sample

node:internal/fs/promises:638
  return new FileHandle(await PromisePrototypeThen(
                        ^

Error: ENOENT: no such file or directory, open 'D:\DEV\Projects\aoc-24\day-NaN\undefined.data.--data=sample.txt'
    at async open (node:internal/fs/promises:638:25)
    at async readFile (node:internal/fs/promises:1245:14)
    at readData (D:\DEV\Projects\aoc-24\utils.ts:20:17)
    at runSolution (D:\DEV\Projects\aoc-24\utils.ts:6:16)
    at <anonymous> (D:\DEV\Projects\aoc-24\day-1\a.ts:9:1) {
  errno: -4058,
@drdreo drdreo changed the title Run example doesnt work Run example fails to load undefined file Nov 29, 2024
@scarletczen
Copy link

scarletczen commented Dec 1, 2024

Yeah getting the same error on Windows.

@HDv2b
Copy link

HDv2b commented Dec 1, 2024

I know it's not a cross-OS solution, but local fix would be:

import chalk from 'chalk';
import { readFile } from 'fs/promises';
import { join } from 'path';

export async function runSolution(solution: (data: string[]) => any) {
  const data = await readData();
  const answer = await solution(data);
  console.log(chalk.bgGreen('Your Answer:'), chalk.green(answer));
}

export async function readData() {
  const [_, fullPath, dataSet] = process.argv as
    | [string, string, string]
    | [string, string];
  console.log(fullPath);
  const puzzle = fullPath.split('\\').slice(-2).join('\\');
  console.log(puzzle);
  const [day, part] = puzzle
    .split('\\')
    .map((x, i) => (i === 0 ? +x.split('-')[1] : x)) as [number, 'a' | 'b'];
  const fileName = createFileName(day, part, dataSet);
  console.log('filename', fileName);
  const data = (await readFile(fileName)).toString().replaceAll('\r', '').split('\n');
  return data;
}

function createFileName(day: number, part: 'a' | 'b', dataSet?: string) {
  return join(`day-${day}`, `${part}.data${dataSet ? `.${dataSet}` : ''}.txt`);
}

@erikwski
Copy link

erikwski commented Dec 1, 2024

Same error here, @HDv2b your solution is working. Thx

@kongnayeon
Copy link

It seems the same issue exists on macOS. On macOS, updating the code as shown below works well:

export async function readData() {
  const [_, fullPath, ...args] = process.argv;

  const dataSetArg = args.find((arg) => arg.startsWith('--data='));
  const dataSet = dataSetArg ? dataSetArg.split('=')[1] : undefined;

  const puzzle = fullPath.split('/').slice(-2).join('/');
  const [day, part] = puzzle
    .split('/')
    .map((x, i) => (i === 0 ? +x.split('-')[1] : x)) as [number, 'a' | 'b'];
  const fileName = createFileName(day, part, dataSet);
  const data = (await readFile(fileName)).toString().split('\n');
  return data;
}

@TheHolocoder
Copy link

Thanks @kongnayeon your solution is working 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants