Skip to content

Commit

Permalink
Adjusting error messsages and messaging of CLI tool to be friendlier
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhang03 committed Aug 1, 2024
1 parent 6b99365 commit 0b10725
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jspsych-metadata-cli",
"version": "1.0.2",
"version": "1.0.3",
"description": "This directory contains tools for interacting and generating CLI through the terminal.",
"main": "dist/cjs/index.cjs",
"author": "Victor Zhang",
Expand Down
25 changes: 17 additions & 8 deletions packages/cli/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const copyFileWithStructure = async (sourceFilePath, targetDirectoryPath) => {

// Copy the file
await fs.promises.copyFile(sourceFilePath, targetFilePath);
console.log(`File copied from ${sourceFilePath} to ${targetFilePath}`);
// console.log(`File copied from ${sourceFilePath} to ${targetFilePath}`); can delete
} catch (error) {
console.error(`Failed to copy file from ${sourceFilePath} to ${targetDirectoryPath}:`, error);
}
Expand All @@ -34,7 +34,7 @@ const copyFileWithStructure = async (sourceFilePath, targetDirectoryPath) => {
// processing single file, need to refactor this into a seperate call
const processFile = async (metadata, directoryPath, file, targetDirectoryPath?) => {
const filePath = path.join(directoryPath, file);
console.log("Reading file:", filePath);
// console.log("Reading file:", filePath); -> does not need to count the number of files that read

try {
const content = await fs.promises.readFile(filePath, "utf8");
Expand All @@ -49,17 +49,24 @@ const processFile = async (metadata, directoryPath, file, targetDirectoryPath?)
await metadata.generate(content, {}, 'csv');
break;
default:
console.error("File is not .csv or .json", file);
console.error(file, "is not .csv or .json format.");
return false;
}

if (targetDirectoryPath) await copyFileWithStructure(filePath, targetDirectoryPath); // error catching to create backwards compability with CLI and old cli prompting
} catch (err) {
console.error(`Error reading file ${file}:`, err);
console.error(`Error reading file ${file}: ${err} Please ensure this is data generated by JsPsych.`);
return false;
}

return true;
}

// Processing directory recursively up to one level
export const processDirectory = async (metadata, directoryPath, targetDirectoryPath?) => {
let total = 0;
let failed = 0;

const processDirectoryRecursive = async (currentPath, level) => {
if (level > 1){
console.warn("Can only read subdirectories one level deep:", directoryPath);
Expand All @@ -81,18 +88,20 @@ export const processDirectory = async (metadata, directoryPath, targetDirectoryP
if (item.isDirectory()) {
await processDirectoryRecursive(itemPath, level + 1);
} else {
await processFile(metadata, currentPath, item.name, targetDirectoryPath);
total += 1;
if (!await processFile(metadata, currentPath, item.name, targetDirectoryPath)) failed += 1; // returns false if failed
}
}

return true; // might not work when doesn't have a csv/json
} catch (err) {
console.error(`Error reading directory ${currentPath}:`, err);
return false;
}
failed += 1; }
};

return await processDirectoryRecursive(directoryPath, 0);
await processDirectoryRecursive(directoryPath, 0);
console.log("Files read with rate:", `${(total - failed)}/${total}`);
return { total, failed };
};

// Processing metadata options json
Expand Down
4 changes: 0 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ const promptProjectStructure = async (metadata) => {
try {
if (await validateDirectory(input) && await validateJson(input + "/dataset_description.json", "dataset_description.json")){ // and will need to check that contains dataset_description.json -> validate that this is existing psych-DS dataset, validateJson with inpput
return true;
// read through the entire directory checking dataset_json (will assume it has already been made) -> only thing that is reallly important
// maybe call the validator function but will instead just write to this
// -> will not actually read, will assume there is a directory
}
return "Please enter a valid path to the project directory. Be sure it includes a dataset_description.json file in the root otherwise it will not work.";
} catch (err) {
Expand Down Expand Up @@ -142,7 +139,6 @@ const main = async () => {
await metadataOptionsPrompt(metadata); // passing in options file to overwite existing file

const metadataString = JSON.stringify(metadata.getMetadata(), null, 2); // Assuming getMetadata() is the function that retrieves your metadata
console.log(metadataString); // Pretty print with 2 spaces for indentation
saveTextToPath(metadataString,`${project_path}/dataset_description.json`);
};

Expand Down
3 changes: 2 additions & 1 deletion packages/metadata/src/PluginCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export class PluginCache {

private async fetchScript(pluginType: string, version: string, extension?: boolean) {
const unpkgUrl = this.generateUnpkg(pluginType, version, extension);
console.log("-> fetching information for [", pluginType, "] from ->", unpkgUrl);

// console.log("-> fetching information for [", pluginType, "] from ->", unpkgUrl); should figure out verbose mode to display

try {
const response = await fetch(unpkgUrl);
Expand Down

0 comments on commit 0b10725

Please sign in to comment.