I am writing a program in node.js
.
I want to create a function isJsFile(file)
that get a file (name or content)
and return true if the file is javascript file
or false
if its not.
How can i create this function?
I am writing a program in node.js
.
I want to create a function isJsFile(file)
that get a file (name or content)
and return true if the file is javascript file
or false
if its not.
How can i create this function?
You can to use node-mime to indentity by mime type: https://github.com/broofa/node-mime
you can try to use file(1) utility that runs a number of tests against the file contents to classify it. Node.js has a module file-type
that provides the functionality equivalent to file(1).
import {fileTypeFromFile} from 'file-type';
console.log(await fileTypeFromFile('test.js'));
Simply use path to get the file extension:
var path = require('path')
path.extname(YOUR_FILENAME) // returns '.js' if file is a js file