├── .npmrc ├── .gitattributes ├── .gitignore ├── .editorconfig ├── test.js ├── .github └── workflows │ └── main.yml ├── package.json ├── index.js ├── license └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {fileMetadata, fileMetadataSync} from './index.js'; 3 | 4 | test('async', async t => { 5 | const result = await fileMetadata('index.js'); 6 | t.is(result.contentType, 'com.netscape.javascript-source'); 7 | t.is(result.fsName, 'index.js'); 8 | }); 9 | 10 | test('sync', t => { 11 | const result = fileMetadataSync('index.js'); 12 | t.is(result.contentType, 'com.netscape.javascript-source'); 13 | }); 14 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: macos-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "file-metadata", 3 | "version": "3.0.0", 4 | "description": "Get file metadata using `mdls` on macOS", 5 | "license": "MIT", 6 | "repository": "sindresorhus/file-metadata", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "keywords": [ 25 | "file", 26 | "metadata", 27 | "mdls", 28 | "macos", 29 | "mac", 30 | "spotlight", 31 | "attributes", 32 | "filepath" 33 | ], 34 | "dependencies": { 35 | "plist": "^3.0.4" 36 | }, 37 | "devDependencies": { 38 | "ava": "^3.15.0", 39 | "xo": "^0.45.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {promisify} from 'node:util'; 2 | import childProcess from 'node:child_process'; 3 | import plist from 'plist'; 4 | 5 | const execFileP = promisify(childProcess.execFile); 6 | 7 | const parse = data => { 8 | const object = plist.parse(data); 9 | const returnValue = {}; 10 | 11 | for (let [key, value] of Object.entries(object)) { 12 | key = key.replace(/^kMDItem/, '').replace(/_/g, ''); 13 | key = key.startsWith('FS') ? key.replace(/^FS/, 'fs') : key[0].toLowerCase() + key.slice(1); 14 | returnValue[key] = value; 15 | } 16 | 17 | return returnValue; 18 | }; 19 | 20 | export async function fileMetadata(filePath) { 21 | const {stdout} = await execFileP('mdls', ['-plist', '-', filePath]); 22 | return parse(stdout.trim()); 23 | } 24 | 25 | export function fileMetadataSync(filePath) { 26 | const stdout = childProcess.execFileSync('mdls', ['-plist', '-', filePath], { 27 | encoding: 'utf8', 28 | stdio: ['ignore', 'pipe', 'ignore'], 29 | }); 30 | 31 | return parse(stdout.trim()); 32 | } 33 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # file-metadata 2 | 3 | > Get file metadata using [`mdls`](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/mdls.1.html) on macOS 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install file-metadata 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import {fileMetadata} from 'file-metadata'; 15 | 16 | console.log(await fileMetadata('index.js')); 17 | /* 18 | { 19 | contentCreationDate: 2016-10-25T18:25:46.000Z, 20 | contentCreationDateRanking: 2016-10-25T00:00:00.000Z, 21 | contentModificationDate: 2017-12-29T19:56:15.000Z, 22 | contentType: 'com.netscape.javascript-source', 23 | contentTypeTree: [ 24 | 'com.netscape.javascript-source', 25 | 'public.script', 26 | 'public.source-code', 27 | 'public.data', 28 | 'public.plain-text', 29 | 'public.item', 30 | 'com.netscape.javascript-source', 31 | 'public.content', 32 | 'public.executable', 33 | 'public.text' 34 | ], 35 | dateAdded: 2017-12-29T18:42:39.000Z, 36 | dateAddedRanking: 2017-12-29T00:00:00.000Z, 37 | displayName: 'index.js', 38 | fsContentChangeDate: 2017-12-29T19:56:15.000Z, 39 | fsCreationDate: 2016-10-25T18:25:46.000Z, 40 | fsCreatorCode: 0, 41 | fsFinderFlags: 0, 42 | fsInvisible: false, 43 | fsIsExtensionHidden: false, 44 | fsLabel: 0, 45 | fsName: 'index.js', 46 | fsOwnerGroupID: 20, 47 | fsOwnerUserID: 501, 48 | fsSize: 860, 49 | fsTypeCode: 0, 50 | interestingDateRanking: 2016-10-25T00:00:00.000Z, 51 | kind: 'JavaScript script', 52 | lastUsedDate: 2017-12-29T18:42:57.000Z, 53 | lastUsedDateRanking: 2017-12-29T00:00:00.000Z, 54 | logicalSize: 860, 55 | physicalSize: 4096, 56 | useCount: 1, 57 | usedDates: [ 58 | 2017-12-28T23:00:00.000Z 59 | ] 60 | } 61 | */ 62 | ``` 63 | 64 | ## API 65 | 66 | ### fileMetadata(filePath) 67 | 68 | Returns a `Promise` with the properties seen in the above example. 69 | 70 | ### fileMetadataSync(filePath) 71 | 72 | Returns an `object` with the properties seen in the above example. 73 | 74 | ## Related 75 | 76 | - [file-uti](https://github.com/sindresorhus/file-uti) - Get the UTI (Uniform Type Identifier) of a file on macOS 77 | --------------------------------------------------------------------------------