diff options
author | Sindre Sorhus <sindresorhus@gmail.com> | 2017-12-29 21:14:52 +0100 |
---|---|---|
committer | Sindre Sorhus <sindresorhus@gmail.com> | 2017-12-29 22:15:18 +0100 |
commit | 58bb6ffee1c8c26cb360fe0f1107fd6f5b7f5dfb (patch) | |
tree | 2ee6317a8e897e10047c2f1cce6a75b20ac11dc2 /index.js | |
download | file-metadata-58bb6ffee1c8c26cb360fe0f1107fd6f5b7f5dfb.tar.gz file-metadata-58bb6ffee1c8c26cb360fe0f1107fd6f5b7f5dfb.tar.bz2 file-metadata-58bb6ffee1c8c26cb360fe0f1107fd6f5b7f5dfb.zip |
Init
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()); +}; |