Skip to content

Commit

Permalink
feat: add reverse lookup option (--reverse, -r) feature to CLI
Browse files Browse the repository at this point in the history
* Find extension from mime type using cli

Add an option to lookup extension based on mime from cli

* Update cli.js

fix reuse of mime variable

* Update test.js

* test for reverse lookup

* Use last argument for --reverse lookup in cli.js

* chore: remove '--r' case

Co-authored-by: leevers <leevers@gmail.com>
  • Loading branch information
broofa and leevers authored Apr 19, 2022
1 parent b984421 commit e0da103
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if (args.includes('--version') || args.includes('-v') || args.includes('--v')) {
--help, -h Show this message
--version, -v Display the version
--name, -n Print the name of the program
--reverse, -r Print the extension of the mime type
Note: the command will exit after it executes if a command is specified
The path_or_extension is the path to the file or the extension of the file.
Expand All @@ -32,11 +33,17 @@ if (args.includes('--version') || args.includes('-v') || args.includes('--v')) {
mime --version
mime --name
mime -v
mime --reverse application/text
mime src/log.js
mime new.py
mime foo.sh
`);
process.exit(0);
} else if (args.includes('--reverse') || args.includes('-r')) {
let mimeType = args[args.length-1];
let extension = mime.getExtension(mimeType);
process.stdout.write(extension + '\n');
process.exit(0);
}

let file = args[0];
Expand Down
8 changes: 8 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,13 @@ describe('mime CLI', function() {
done();
});
});

it('returns extension', function(done) {
exec('./cli.js -r video/mpeg', (err, stdout, stderr) => {
if (err) done(err);
assert.equal(stdout, 'mpeg\n');
done();
});
});
});

0 comments on commit e0da103

Please sign in to comment.