-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (37 loc) · 1.35 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
const fs = require('fs');
const os = require('os');
const path = require('path');
const isBuffer = require('is-buffer');
const isValidPath = require('is-valid-path');
const revHash = require('rev-hash');
const semver = require('semver');
const { which, exec } = require('shelljs');
const filePath = path.join(__dirname, '_arf.py');
// ensure python installed
if (!which('python3')) throw new Error(`Python v3.5+ is required`);
const silent = process.env.NODE_ENV !== 'test';
// ensure python v3.5+
let version = exec('python3 --version', { silent });
version = semver.coerce(
(version.stdout || version.stderr).split(' ')[1].trim()
);
if (!semver.satisfies(version, '>= 3.5'))
throw new Error(
`Python v3.5+ is required, you currently have v${version} installed`
);
async function arf(str) {
if (!isBuffer(str) && typeof str !== 'string')
throw new Error('str must be a buffer or string/path');
if (isBuffer(str) || (typeof str === 'string' && !isValidPath(str))) {
const tmpPath = path.join(os.tmpdir(), revHash(str));
await fs.promises.writeFile(tmpPath, str);
str = tmpPath;
}
return new Promise((resolve, reject) => {
exec(`python3 ${filePath} ${str}`, { silent }, (code, stdout, stderr) => {
if (code !== 0) return reject(new Error(stderr));
resolve(JSON.parse(stdout.trim()));
});
});
}
module.exports = arf;