Skip to content

Commit

Permalink
Exposes Twav expansion via CLI script
Browse files Browse the repository at this point in the history
THe plan is to eventually expose more sub-commands,
and I am aware I'm currently misusing Commander for that end goal.
  • Loading branch information
IamJeffG committed Jun 25, 2024
1 parent 08c61ef commit 0bc57de
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
79 changes: 70 additions & 9 deletions bin/audiomoth-utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,79 @@
#!/usr/bin/env node

const { Command } = require("commander");
const { Command, Option } = require("commander");
const audiomothUtils = require("audiomoth-utils");

const program = new Command();

program
.version("1.0.0")
.description("A simple hello-world CLI")
.argument("<name>", "name to greet")
.option("-u, --uppercase", "convert name to uppercase")
.action((name, options) => {
if (options.uppercase) {
name = name.toUpperCase();
.description(
"Expand an AudioMoth T.WAV recording (a recording with amplitude thresholding or frequency triggering applied)"
) // TODO use addCommand to handle the subcommands
.arguments("<subcommand> <inputFile>")
.option(
"-d, --destination [outputPath]",
"output directory to write expansions. If omitted, dump expanded files in the same folder as inputFile."
)
.option(
"--prefix [prefix]",
"optional string (not including '_') to prefix to expanded filenames that will be created."
)
.addOption(
new Option(
"--max-file-duration [seconds]",
"max duration of expanded file to write, in seconds"
)
.argParser(parseInt)
.default(5)
)
.option("--generate-silent-files", "generate silent files", false)
.option("--align-to-second-transitions", "align to second transitions", false)
.action((subcommand, inputFile, options) => {
const {
destination,
prefix,
maxFileDuration,
generateSilentFiles,
alignToSecondTransitions,
} = options;

// Determine expansionType based on command
let expansionType;
switch (subcommand) {
case "expand-duration":
expansionType = "DURATION";
break;
case "expand-event":
expansionType = "EVENT";
break;
default:
console.error("Invalid subcommand");
process.exit(9);
}

// Call the expand function with the provided arguments and options
try {
const result = audiomothUtils.expand(
inputFile,
destination, // optional
prefix, // optional
expansionType,
maxFileDuration,
generateSilentFiles,
alignToSecondTransitions
);

if (result?.success) {
console.log("Expansion completed successfully.");
process.exit(0); // Exit with code 0 if success is true
} else {
console.error("Expansion failed:", result?.error);
process.exit(1); // Exit with code 1 if success is false
}
} catch (err) {
console.error("Error:", err);
process.exit(7); // Exit with code 7 if an exception occurred
}
console.log(`Hello, ${name}!`);
});

program.parse(process.argv);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build": "pkg . --targets node14-linux-x64,node14-win-x64 --out-path dist"
},
"dependencies": {
"audiomoth-utils": "1.5.0",
"commander": "^12.1.0"
},
"devDependencies": {
Expand Down

0 comments on commit 0bc57de

Please sign in to comment.