From 58bb6ffee1c8c26cb360fe0f1107fd6f5b7f5dfb Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 29 Dec 2017 21:14:52 +0100 Subject: Init --- .editorconfig | 12 ++++++++ .gitattributes | 2 ++ .gitignore | 2 ++ .npmrc | 1 + .travis.yml | 4 +++ index.js | 37 +++++++++++++++++++++++++ license | 9 ++++++ package.json | 38 +++++++++++++++++++++++++ readme.md | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 13 +++++++++ 10 files changed, 205 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .npmrc create mode 100644 .travis.yml create mode 100644 index.js create mode 100644 license create mode 100644 package.json create mode 100644 readme.md create mode 100644 test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1c6314a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..391f0a4 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +*.js text eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..239ecff --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ff17e34 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +os: osx +language: node_js +node_js: + - '8' 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()); +}; diff --git a/license b/license new file mode 100644 index 0000000..e7af2f7 --- /dev/null +++ b/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sindre Sorhus (sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..1ec8356 --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "file-metadata", + "version": "0.0.0", + "description": "Get file metadata using `mdls` on macOS", + "license": "MIT", + "repository": "sindresorhus/file-metadata", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "file", + "metadata", + "mdls", + "macos", + "mac", + "spotlight", + "attributes", + "filepath" + ], + "dependencies": { + "plist": "^2.1.0" + }, + "devDependencies": { + "ava": "*", + "xo": "*" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..dec38d6 --- /dev/null +++ b/readme.md @@ -0,0 +1,87 @@ +# file-metadata [![Build Status](https://travis-ci.org/sindresorhus/file-metadata.svg?branch=master)](https://travis-ci.org/sindresorhus/file-metadata) + +> Get file metadata using [`mdls`](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/mdls.1.html) on macOS + + +## Install + +``` +$ npm install file-metadata +``` + + +## Usage + +```js +const fileMetadata = require('file-metadata'); + +(async () => { + console.log(await fileMetadata('index.js')); + /* + { + contentCreationDate: 2016-10-25T18:25:46.000Z, + contentCreationDateRanking: 2016-10-25T00:00:00.000Z, + contentModificationDate: 2017-12-29T19:56:15.000Z, + contentType: 'com.netscape.javascript-source', + contentTypeTree: [ + 'com.netscape.javascript-source', + 'public.script', + 'public.source-code', + 'public.data', + 'public.plain-text', + 'public.item', + 'com.netscape.javascript-source', + 'public.content', + 'public.executable', + 'public.text' + ], + dateAdded: 2017-12-29T18:42:39.000Z, + dateAddedRanking: 2017-12-29T00:00:00.000Z, + displayName: 'index.js', + fsContentChangeDate: 2017-12-29T19:56:15.000Z, + fsCreationDate: 2016-10-25T18:25:46.000Z, + fsCreatorCode: 0, + fsFinderFlags: 0, + fsInvisible: false, + fsIsExtensionHidden: false, + fsLabel: 0, + fsName: 'index.js', + fsOwnerGroupID: 20, + fsOwnerUserID: 501, + fsSize: 860, + fsTypeCode: 0, + interestingDateRanking: 2016-10-25T00:00:00.000Z, + kind: 'JavaScript script', + lastUsedDate: 2017-12-29T18:42:57.000Z, + lastUsedDateRanking: 2017-12-29T00:00:00.000Z, + logicalSize: 860, + physicalSize: 4096, + useCount: 1, + usedDates: [ + 2017-12-28T23:00:00.000Z + ] + } + */ +})(); +``` + + +## API + +### fileMetadata(filePath) + +Returns a `Promise`. + +### fileMetadata.sync(filePath) + +Returns an `Object`. + + +## Related + +- [file-uti](https://github.com/sindresorhus/file-uti) - Get the UTI (Uniform Type Identifier) of a file on macOS + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js new file mode 100644 index 0000000..5ccfad0 --- /dev/null +++ b/test.js @@ -0,0 +1,13 @@ +import test from 'ava'; +import m from '.'; + +test('async', async t => { + const result = await m('index.js'); + t.is(result.contentType, 'com.netscape.javascript-source'); + t.is(result.fsName, 'index.js'); +}); + +test('sync', t => { + const result = m.sync('index.js'); + t.is(result.contentType, 'com.netscape.javascript-source'); +}); -- cgit v1.2.3