diff options
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100644 index 0000000..19779c0 --- /dev/null +++ b/index.js @@ -0,0 +1,37 @@ +'use strict'; +const util = require('util'); +const childProcess = require('child_process'); +const plist = require('plist'); + +const execFileP = util.promisify(childProcess.execFile); + +const parse = data => { + const object = plist.parse(data); + const ret = {}; + + for (let key of Object.keys(object)) { + const value = object[key]; + + key = key.replace(/^kMDItem/, '').replace(/_/g, ''); + + if (key.startsWith('FS')) { + key = key.replace(/^FS/, 'fs'); + } else { + key = key[0].toLowerCase() + key.slice(1); + } + + ret[key] = value; + } + + return ret; +}; + +module.exports = async filePath => { + const {stdout} = await execFileP('mdls', ['-plist', '-', filePath]); + return parse(stdout.trim()); +}; + +module.exports.sync = filePath => { + const stdout = childProcess.execFileSync('mdls', ['-plist', '-', filePath], {encoding: 'utf8'}); + return parse(stdout.trim()); +}; |