ts-mdls
Version:
Lightweigth, dependency free, fully typed wrapper of the macOS `mdls` command
41 lines • 2.08 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const util_1 = require("util");
const metadataAttributeParsers_1 = require("./utilities/metadataAttributeParsers");
const exec = (0, util_1.promisify)(child_process_1.exec);
function mdls(path, attributeKeys) {
return __awaiter(this, void 0, void 0, function* () {
if (process.platform !== "darwin")
throw new Error(process.platform + " is not supported.");
const names = attributeKeys === null || attributeKeys === void 0 ? void 0 : attributeKeys.map((a) => `-name ${a}`).join(" ");
const { stdout } = yield exec(`/usr/bin/mdls ${names !== null && names !== void 0 ? names : ""} '${path}'`);
const rawAttributes = stdout
.split(/\s+(?=kMD)/)
.map((a) => a.split("=").map((i) => i.trim()));
const result = {};
for (const rawAttribute of rawAttributes) {
const key = rawAttribute[0];
const value = rawAttribute[1];
const parser = metadataAttributeParsers_1.metadatAttributeParsers[key];
if (parser === undefined) {
continue;
}
else {
result[key] = parser(value);
}
}
return result;
});
}
exports.default = mdls;
//# sourceMappingURL=mdls.js.map