UNPKG

1.23 kBJavaScriptView Raw
1var $, Lang, _;
2
3$ = {};
4
5$.formatArgument = require('../dist/formatArgument');
6
7$.info = require('../dist/info');
8
9$.os = require('../dist/os');
10
11$.parseString = require('../dist/parseString');
12
13$.exec_ = require('../dist/exec_');
14
15_ = {};
16
17_.trim = require('lodash/trim');
18
19Lang = {
20 'ja': 'kyoko',
21 'ja-jp': 'kyoko',
22 'zh': 'ting-ting',
23 'zh-cn': 'ting-ting',
24 'zh-hk': 'sin-ji',
25 'zh-tw': 'mei-jia'
26};
27
28module.exports = async function(text, option = {}) {
29 var i, lang, len, listCmd, msg, name, ref, voice;
30 ref = $.formatArgument(text);
31 for (i = 0, len = ref.length; i < len; i++) {
32 msg = ref[i];
33 $.info('say', msg);
34 if (!$.os('macos')) {
35 continue;
36 }
37 msg = $.parseString(msg).replace(/[#\(\)-]/g, '');
38 msg = _.trim(msg);
39 if (!msg.length) {
40 continue;
41 }
42 listCmd = ['say'];
43 if (option.lang) {
44 lang = option.lang.toLowerCase();
45 if (name = Lang[lang]) {
46 lang = name;
47 }
48 listCmd.push(`--voice=${lang}`);
49 }
50 if (option.voice) {
51 voice = option.voice.toLowerCase();
52 listCmd.push(`--voice=${voice}`);
53 }
54 listCmd.push(msg);
55 await $.exec_(listCmd.join(' '), {
56 silent: true
57 });
58 }
59 return this;
60};