mirror of
https://github.com/fofolee/uTools-ProcessKiller.git
synced 2025-06-09 06:54:33 +08:00
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
'use strict';
|
|
const path = require('path');
|
|
const util = require('util');
|
|
const { execFile } = require('child_process');
|
|
const fs = require('fs');
|
|
|
|
const execFileP = util.promisify(execFile);
|
|
// const bin = path.join(__dirname, 'file-icon');
|
|
const bin = path.join(__dirname.replace(/(unsafe-\w+\.asar)/, '$1.unpacked'), 'file-icon');
|
|
fs.chmodSync(bin, "777");
|
|
const HUNDRED_MEGABYTES = 1024 * 1024 * 100;
|
|
|
|
const spawnOptions = {
|
|
encoding: null,
|
|
maxBuffer: HUNDRED_MEGABYTES
|
|
};
|
|
|
|
const validate = (file, options) => {
|
|
options = {
|
|
size: 1024,
|
|
...options
|
|
};
|
|
|
|
if (process.platform !== 'darwin') {
|
|
throw new Error('macOS only');
|
|
}
|
|
|
|
if (!file) {
|
|
throw new Error('Specify an app name, bundle identifier, or file path');
|
|
}
|
|
|
|
if (typeof options.size !== 'number') {
|
|
options.size = 1024;
|
|
}
|
|
|
|
if (options.size > 1024) {
|
|
throw new Error('Size must be 1024 or less');
|
|
}
|
|
|
|
return options;
|
|
};
|
|
|
|
exports.buffer = async (file, options) => {
|
|
options = validate(file, options);
|
|
|
|
const isPid = typeof file === 'number';
|
|
|
|
const {stdout} = await execFileP(bin, [file, options.size, isPid], spawnOptions);
|
|
|
|
return stdout;
|
|
};
|
|
|
|
exports.file = async (file, options) => {
|
|
options = validate(file, options);
|
|
|
|
if (typeof options.destination !== 'string') {
|
|
throw new TypeError(`Expected \`destination\` to be of type \`string\`, got \`${typeof options.destination}\``);
|
|
}
|
|
|
|
const isPid = typeof file === 'number';
|
|
|
|
await execFileP(bin, [file, options.size, isPid, options.destination], spawnOptions);
|
|
};
|