Skip to content

Commit

Permalink
Merge pull request #99 from UofGAnalytics/better-error-messages-when-…
Browse files Browse the repository at this point in the history
…finding-files

better error messages for finding files
  • Loading branch information
dmca-glasgow committed Apr 26, 2022
2 parents bdd9518 + f9cd311 commit 025a386
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
23 changes: 11 additions & 12 deletions compiler/src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

import chalk from 'chalk';
import figures from 'figures';
import yargs from 'yargs';

import { Options } from '../context';
Expand Down Expand Up @@ -77,16 +79,13 @@ const options: Options = {
force: argv.force,
};

// async function rMarkdown(dirPath: string, options: Options = {}) {
// try {
// return await run(dirPath, options);
// } catch (err) {
// console.error(err);
// if (err instanceof Error) {
// console.error(err.stack);
// }
// process.exit(1);
// }
// }
async function run() {
try {
await rMarkdown(dirPath, options);
} catch (err: any) {
console.log(chalk.red(figures.cross + ' ' + err.message));
process.exit(1);
}
}

rMarkdown(dirPath, options);
run();
8 changes: 6 additions & 2 deletions compiler/src/course/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import kebabCase from 'lodash/kebabCase.js';
import { toVFile } from 'to-vfile';

import { checkLocalFileExists } from '../utils/utils';
import { loadCourseYaml } from './load-course';
import { loadUnitYaml } from './load-unit';
import {
Expand Down Expand Up @@ -31,8 +32,11 @@ async function collectUnit(
const { content, ...yaml } = await loadUnitYaml(dirPath, unit.src);
const unitPath = path.join(process.cwd(), dirPath, unit.src);
const files = await Promise.all(
content.map((c) => {
const filePath = path.join(dirPath, unit.src, '..', c.src);
content.map(async (c) => {
const filePath = path.resolve(dirPath, unit.src, '..', c.src);
if (!(await checkLocalFileExists(filePath))) {
throw new Error(`No Rmd file exists at ${filePath}`);
}
return toVFile.read(filePath, 'utf-8');
})
);
Expand Down
10 changes: 8 additions & 2 deletions compiler/src/course/load-course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import yaml from 'js-yaml';
import * as yup from 'yup';

import { readFile } from '../utils/utils';
import { checkLocalFileExists, readFile } from '../utils/utils';
import { CourseYaml } from './types';

export const validCatalogValues = [
Expand Down Expand Up @@ -35,7 +35,13 @@ const courseSchema = yup.object().shape({
});

export async function loadCourseYaml(dirPath: string) {
const fileContents = await readFile(path.join(dirPath, 'course.yaml'));
const courseYamlPath = path.join(dirPath, 'course.yaml');
if (!(await checkLocalFileExists(courseYamlPath))) {
throw Error(
`No course.yaml file exists in ${path.join(process.cwd(), dirPath)}`
);
}
const fileContents = await readFile(courseYamlPath);
const course = yaml.load(fileContents);
return courseSchema.validateSync(course) as CourseYaml;
}
10 changes: 8 additions & 2 deletions compiler/src/course/load-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import yaml from 'js-yaml';
import * as yup from 'yup';

import { readFile } from '../utils/utils';
import { checkLocalFileExists, readFile } from '../utils/utils';
import { UnitYaml } from './types';

const unitSchema = yup.object().shape({
Expand All @@ -17,7 +17,13 @@ const unitSchema = yup.object().shape({
});

export async function loadUnitYaml(dirPath: string, src: string) {
const fileContents = await readFile(path.join(dirPath, src));
const contentsPath = path.join(dirPath, src);
if (!(await checkLocalFileExists(contentsPath))) {
throw Error(
`No yaml file exists at ${path.join(process.cwd(), contentsPath)}`
);
}
const fileContents = await readFile(contentsPath);
const unit = yaml.load(fileContents);
return unitSchema.validateSync(unit) as UnitYaml;
}

0 comments on commit 025a386

Please sign in to comment.