Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

22 changes: 0 additions & 22 deletions .eslintrc.js

This file was deleted.

2 changes: 2 additions & 0 deletions dist/index.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import mdls from './index.js';
export = mdls;
15 changes: 15 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default mdls;
export type MdlsReady = (err?: null | import("node:child_process").ExecException, deserialized?: {}) => void;
export type MdlsValue = string | number | Date | null | (string | number | Date | null)[];
/**
* @typedef {(
* err?: null|import('node:child_process').ExecException,
* deserialized?: {}
* ) => void} MdlsReady
*/
/**
* @param {string} file
* @param {string|MdlsReady} [args]
* @param {MdlsReady} [ready]
*/
declare function mdls(file: string, args?: string | MdlsReady, ready?: MdlsReady): Promise<any>;
21 changes: 21 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import js from '@eslint/js';
import globals from 'globals';

export default [
js.configs.recommended,
{
languageOptions: {
globals: globals.node
}
},
{
files: ['test.js'],
rules: {
'no-console': 'off'
}
},
{
rules: {
}
}
];
123 changes: 123 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Do not edit this file; it was auto-generated from index.js
'use strict';

var path = require('node:path');
var node_child_process = require('node:child_process');

/**
* @typedef {(
* err?: null|import('node:child_process').ExecException,
* deserialized?: {}
* ) => void} MdlsReady
*/

/**
* @param {string} file
* @param {string|MdlsReady} [args]
* @param {MdlsReady} [ready]
*/
function mdls(file, args, ready) {
if (typeof args !== 'string') {
ready = args;
}
file = path.resolve(file).replace(/ /g, '\\ ');

return new Promise((resolve, reject) => {
node_child_process.exec('mdls ' + (args ? args + ' ' : '') + file, function(err, raw_data) {
if(err) {
if (ready) ready(err);
reject(err);
return;
}

const deserialized = deserialize(raw_data);
if (ready) ready(null, deserialized);
resolve(deserialized);
});
});
}

/**
* @typedef {string | number | Date | null |
* (string | number | Date | null)[]} MdlsValue
*/

/**
* @param {string} raw_data
*/
function deserialize(raw_data) {
const splits = raw_data.split('\n') // only targets osx
, lines = []
, /** @type {Record<string, MdlsValue>} */ data = {};

for (const split of splits) {
if(!split.includes('=')) {
lines[lines.length - 1] += split.trim();

continue
}

lines[lines.length] = split.trim();
}

for (const line of lines) {
const kv = line.split('=');
const key = kv[0].trim().replace('kMD', '');

/** @type {MdlsValue} */
let value = kv[1].trim();

if(value === '(null)') {
value = null;
} else if (value[0] === '(' && value[value.length - 1] === ')') {
value = value.slice(1, -1).split(',').map(to_js_type(key));
} else {
value = to_js_type(key)(value);
}

data[key] = value;
}

return data
}

/**
* @param {string} key
*/
function to_js_type(key) {
/**
* @param {string|null} value
*/
return function(value) {
if (value === null) {
return null;
}
if(value[0] === '"' && value[value.length - 1] === '"') {
return value.slice(1, -1)
}

const as_num = +value;

if(!isNaN(as_num)) {
return as_num
}

const as_date = new Date(value);

if(isNaN(as_date.getTime())) {
bad_value(key, value);
}

return as_date
}
}

/**
* @param {string} key
* @param {string} value
*/
function bad_value(key, value) {
throw new Error('invalid value: ' + value + ' for key: ' + key)
}

module.exports = mdls;
2 changes: 2 additions & 0 deletions index.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import mdls from './index.js';
export = mdls;
46 changes: 38 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
'use strict';

const path = require('path')
, {exec} = require('child_process')

module.exports = mdls

import path from 'node:path';
import {exec} from 'node:child_process';

/**
* @typedef {(
* err?: null|import('node:child_process').ExecException,
* deserialized?: {}
* ) => void} MdlsReady
*/

/**
* @param {string} file
* @param {string|MdlsReady} [args]
* @param {MdlsReady} [ready]
*/
function mdls(file, args, ready) {
if (typeof args !== 'string') {
ready = args;
Expand All @@ -26,10 +34,18 @@ function mdls(file, args, ready) {
});
}

/**
* @typedef {string | number | Date | null |
* (string | number | Date | null)[]} MdlsValue
*/

/**
* @param {string} raw_data
*/
function deserialize(raw_data) {
const splits = raw_data.split('\n') // only targets osx
, lines = []
, data = {}
, /** @type {Record<string, MdlsValue>} */ data = {}

for (const split of splits) {
if(!split.includes('=')) {
Expand All @@ -44,6 +60,8 @@ function deserialize(raw_data) {
for (const line of lines) {
const kv = line.split('=')
const key = kv[0].trim().replace('kMD', '')

/** @type {MdlsValue} */
let value = kv[1].trim()

if(value === '(null)') {
Expand All @@ -60,7 +78,13 @@ function deserialize(raw_data) {
return data
}

/**
* @param {string} key
*/
function to_js_type(key) {
/**
* @param {string|null} value
*/
return function(value) {
if (value === null) {
return null;
Expand All @@ -85,6 +109,12 @@ function to_js_type(key) {
}
}

/**
* @param {string} key
* @param {string} value
*/
function bad_value(key, value) {
throw new Error('invalid value: ' + value + ' for key: ' + key)
}

export default mdls;
Loading