-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
following example from tj/commander.js#1565 (comment)
- Loading branch information
Showing
5 changed files
with
154 additions
and
82 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env node | ||
|
||
const command = require("../lib/command"); | ||
const program = command.makeProgram(); | ||
|
||
function main() { | ||
try { | ||
program.parse(process.argv); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
// if (program.opts().debug) { | ||
// console.error(`${err.stack}`); | ||
// } | ||
// if (err.message !== util.suppressTerminateExceptionMessage) { | ||
console.log(`caught exception with message ${err.message}`); | ||
// } | ||
} else { | ||
throw err; | ||
} | ||
// Recommended practice for node is set exitcode not force exit | ||
process.exitCode = 7; // Exit with code 7 if an exception occurred | ||
} | ||
} | ||
|
||
main().then(() => {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const { Command, Option } = require("commander"); | ||
const audiomothUtils = require("audiomoth-utils"); | ||
|
||
function makeProgram() { | ||
const program = new Command(); | ||
|
||
program | ||
.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 | ||
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 | ||
} | ||
}); | ||
return program; | ||
} | ||
|
||
function fab(args) { | ||
makeProgram().parse(args, { from: "user" }); | ||
} | ||
|
||
module.exports = { | ||
makeProgram: makeProgram, | ||
fab: fab, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const command = require("../lib/command"); | ||
|
||
const audiomothUtils = require("audiomoth-utils"); | ||
|
||
jest.mock("audiomoth-utils", () => ({ | ||
expand: jest.fn(), | ||
})); | ||
|
||
describe("audiomoth-utils CLI", () => { | ||
beforeEach(() => { | ||
audiomothUtils.expand.mockClear(); | ||
audiomothUtils.expand.mockReturnValue({ success: true }); | ||
// Mock console.log and console.error | ||
console.log = jest.fn(); | ||
console.error = jest.fn(); | ||
// Mock process.exit | ||
process.exit = jest.fn(); | ||
}); | ||
|
||
test("calling with no arguments uses default values", () => { | ||
command.fab(["expand-duration", "input.wav"]); | ||
|
||
expect(audiomothUtils.expand).toHaveBeenCalledWith( | ||
"input.wav", | ||
undefined, | ||
undefined, | ||
"DURATION", | ||
5, | ||
false, | ||
false | ||
); | ||
|
||
expect(console.log).toHaveBeenCalledWith( | ||
"Expansion completed successfully." | ||
); | ||
expect(process.exit).toHaveBeenCalledWith(0); | ||
}); | ||
}); |