-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathhelp.js
61 lines (54 loc) · 1.75 KB
/
help.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
60
61
'use strict';
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const readdirAsync = promisify(fs.readdir);
const accessAsync = promisify(fs.access);
const userHome = require('user-home');
const getLocale = require('os-locale');
module.exports = help;
async function help(opts) {
const command = opts.argv[0];
if (!command) {
await showBasicHelp();
} else {
return new Promise(async (resolve, reject) => {
const locale = (await getLocale()).toLowerCase();
const localeFn = path.join(__dirname, `help-${command}-${locale}.txt`);
const defaultFn = path.join(__dirname, `help-${command}-en_us.txt`);
let fn = localeFn;
try {
await accessAsync(localeFn);
} catch (err) {
console.log(
`Could not find a help file for locale ${locale}, defaulting to English`
);
console.log(`You can contribute a translation for this help file!`);
fn = defaultFn;
}
fs.createReadStream(fn)
.on('error', async err => {
if (err.code === 'ENOENT') {
console.log(
`help has not been implemented yet for ${command}. You could build it!`
);
await showBasicHelp();
return resolve();
}
reject(err);
})
.on('end', () => resolve())
.pipe(process.stdout);
});
}
}
async function showBasicHelp() {
const commands = (await readdirAsync(__dirname))
.filter(cmd => cmd.endsWith('.js'))
.map(cmd => `\t${cmd.split('.')[0]}`)
.join('\n');
console.log('Usage: ds <command>');
console.log('\nAvailable commands:');
console.log(commands);
console.log(`\nThe configuration is located at ${userHome}/.entropicrc `);
}