-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (49 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
const pify = require('pify');
const child = require('child_process');
const stat = pify(require('fs').stat);
const Path = require('path');
const expandTilde = require('expand-tilde');
const exec = (cmd) =>
new Promise((resolve, reject) => {
const _exec = child.exec(cmd);
_exec.addListener('error', reject);
_exec.addListener('exit', (code) =>
(code === 0) ? resolve(code) : reject(`cmd failed with code: ${code}`)
);
});
const colorMap = {
clear: '01',
gray: '03',
green: '04',
purple: '06',
blue: '09',
yellow: '0A',
red: '0C',
orange: '0E'
};
module.exports = function (path, color) {
if (process.platform !== 'darwin') {
return Promise.reject(new Error('Only OS X systems are currently supported'));
}
if (typeof path !== 'string') {
return Promise.reject(new TypeError('path should be a string'));
}
if (!(color in colorMap)) {
return Promise.reject(new TypeError('color must be one of: clear, gray, green, purple, blue, yellow, red or orange'));
}
path = expandTilde(path);
if (!Path.isAbsolute(path)) {
path = Path.resolve(path);
}
const hexString = '0'.repeat(18) + colorMap[color] + '0'.repeat(44);
const command = `xattr -wx com.apple.FinderInfo ${hexString} ${path.replace(/ /g, '\\ ')}`;
return stat(path).then(
() => exec(command)
).then(code => ({
code: code,
command: command,
path: path,
tag: color
}));
};