Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bin/rokid.js as rokid-node with node version #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
node_modules/
build/

Expand Down
193 changes: 193 additions & 0 deletions bin/rokid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#!/usr/bin/env node

// Setup.

const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawn = require('child_process').spawn;
const spawnSync = require('child_process').spawnSync;

const VERSION = "0.10.1"
var ADB = ""
var NODE_PATH = 'NODE_PATH=' + path.posix.join('/', 'usr', 'lib', 'node_models');
var TMP_DIR = path.posix.join('/', 'tmp');

function display_help() {
console.log(`
Usage: rokid [command]

rokid help Get helps
rokid search Search your devices and configuring network
rokid devices List connected devices
rokid run [file] Run scripts on device
rokid shell <command> Wait for an connectable device and shell
rokid test <dir> Run tests on your device
rokid log <filter> Show the logs of connected device by comma-based filter

rokid@${VERSION}`);
}

function display_version() {
console.log(`rokid@${VERSION}`);
}

function get_cli_root() {
return path.resolve(__dirname, '..');
}

// 执行纯属脚本时使用
function exec_go(command, callback) {
let ch = exec(command.join(' '));
process.stdin.pipe(ch.stdin);
ch.stdout.pipe(process.stdout);
ch.stderr.pipe(process.stdout);
}

function execSync_go(command, callback) {
let ch = execSync(command.join(' '));
console.log(ch.toString().trim());
}

function spawn_go(command, callback) {
spawn(command[0], command.slice(1), {
stdio: [
process.stdin,
process.stdout,
process.stderr
]
});
}

// 带有流输出时使用
function spawnSync_go(command, callback) {
let ch = spawnSync(command[0], command.slice(1), {
stdio: [
process.stdin,
process.stdout,
process.stderr
]
});
if (callback) callback();
return ch;
}

function run_exec(command, callback) { // TODO: I don't know which child_process is the best choice or choicing function by command execute file type.
// console.log(command.join(' '));
return spawnSync_go(command, callback);
}

function init_adb() {
if (process.env.hasOwnProperty('ROKIDOS_CLI_ADB_PATH') &&
process.env['ROKIDOS_CLI_ADB_PATH']) {
ADB = process.env['ROKIDOS_CLI_ADB_PATH'];
} else {
ADB = path.join(get_cli_root(), 'tools', 'adb');
}
console.log('use adb:', ADB);
}

function build_rpp() {
let config = path.join(get_cli_root(), 'webpack.config.js');
execSync_go([
path.join(get_cli_root(), 'node_modules', '.bin', 'webpack'),
'--config',
config
]);

execSync_go([
'node',
path.join(get_cli_root(), 'postbuild.js')
]);
}

function log(filter) {
// change the workdir to source dir
process.chdir(get_cli_root());
run_script(path.join('lib', 'log-filter.js'), filter);
}

function install_rpp(rpp_path) {
if (!rpp_path || rpp_path.length <= 0) {
build_rpp();
fs.readdirSync(process.cwd()).forEach(function (file) {
if (file.search(/\.rpp/i) > 0) {
rpp_path = file
}
})
}
run_exec([ADB, 'shell', 'mkdir', '-p', path.posix.join(TMP_DIR, 'installers')]);
run_exec([ADB, 'push', rpp_path, path.posix.join(TMP_DIR, 'installers')]);
run_exec([ADB, 'shell', 'pkgm-install', rpp_path]);
console.log("\033[32m$rpp_path installed\033[0m\n");
}

function run_script(filename, param) {
if (filename) filename = path.normalize(filename);
let filename_remote = path.posix.join(TMP_DIR, ...filename.split(path.sep));
run_exec([ADB, 'push', filename, filename_remote]);
run_exec([ADB, 'shell', NODE_PATH, 'node', filename_remote, param ? param : '']);
run_exec([ADB, 'shell', 'rm', filename_remote]);
}

function run_test(dir) {
let testdir = path.posix.join(TMP_DIR, 'tests');
// create test directory
run_exec([ADB, 'shell', 'mkdir', '-p', testdir]);
run_exec([ADB, 'push', 'tests', testdir]);
// # change the workdir to source dir
run_exec([ADB, 'push', path.join(get_cli_root(), 'lib'), testdir]);
run_exec([ADB, 'shell', 'node', path.posix.join(testdir, 'lib', 'executor.js'), dir ? dir : '']);
run_exec([ADB, 'shell', 'rm', '-r', testdir]); // TODO: not work, I think it should add a key bind.
}

function debug_mode() {
run_exec([ADB, 'shell', 'killall', '-9', 'ams']);
run_exec([ADB, 'shell', 'killall', '-9', 'node']);
run_exec([ADB, 'shell', NODE_PATH, 'ams']);
}

function adb_commands(action, param) {
init_adb();
run_exec([ADB, 'wait-for-device'], function () {});
switch (action) {
case 'devices':
case 'shell':
run_exec([ADB, action]);
break;
case 'node':
run_exec([ADB, 'shell', NODE_PATH, 'node']);
break;
case 'search':
exec_go(['rokid-search']);
break;
case 'log':
log();
break;
case 'install':
install_rpp(param);
break;
case 'build':
build_rpp(param);
break;
case 'run':
run_script(param);
break;
case 'test':
run_test(param);
break;
case 'debug':
debug_mode();
break;
case '-V':
case '--version':
display_version();
break;
default:
display_help();
break;
}
}

adb_commands(process.argv[2], process.argv[3]);
2 changes: 1 addition & 1 deletion example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const app = require('@rokid/ams')();
app.on('request', (data) => {
// handler
});
app.start();
app.start();
24 changes: 24 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@rokidapp/rokidos-cli_example",
"version": "1.0.0",
"description": "example app for rokidos",
"main": "app.js",
"scripts": {},
"repository": {
"type": "git",
"url": "[email protected]/rokid/rokidos-cli/example"
},
"keywords": [
"nodejs",
"rokid"
],
"author": "example<[email protected]>",
"license": "MIT",
"metadata": {
"type": "cut",
"skills": [
"example"
],
"native": true
}
}
24 changes: 14 additions & 10 deletions example/tests/cloud-confirm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,40 @@ test('test 7 play', (t) => {
'intent': 'play_random',
'pattern': '^$iwant?$play$one?$keyword$',
'slots': {}
}, {
'appId':'RCAC74F187F34C94B93EE3BAECFCE2E3',
'response': {
}, {
'appId': 'RCAC74F187F34C94B93EE3BAECFCE2E3',
'response': {
'action': {
'version': '2.0.0',
'type': 'NORMAL',
'form': 'scene',
'shouldEndSession': true,
'directives': [{
'type':'voice',
'type': 'voice',
'action': 'PLAY',
'disableEvent':false,
'disableEvent': false,
'item': {
'itemId':'newstestitemid',
'itemId': 'newstestitemid',
'tts': '晚上好,若琪为您播放晚间新闻摘要,首先我们来看看社会新闻。'
}
}, {
'type':'confirm',
'type': 'confirm',
'confirmIntent': 'nlp intent to confirm',
'confirmSlot': 'nlp slot to confirm',
'optionWords': ['word1', 'word2']
}]
}
},
'startWithActiveWord':false,
'version':'2.0.0'
'startWithActiveWord': false,
'version': '2.0.0'
});

Promise.all([
t.assert('Voice.FINISHED', {voice: {item: 'newstestitemid'}}),
t.assert('Voice.FINISHED', {
voice: {
item: 'newstestitemid'
}
}),
t.assert('siren.statechange', 'open'),
]).then(() => {
t.done();
Expand Down
5 changes: 3 additions & 2 deletions lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const asserts = [];
const filter = process.argv[2];
const redify = (text) => '\033[31m' + text + '\033[0m';
const greyify = (text) => '\033[1;30m' + text + '\033[0m';
const yellowify = (text) => '\033[33m' + text + '\033[0m';
const greenify = (text) => '\033[32m' + text + '\033[0m';

class TestCase {
Expand Down Expand Up @@ -149,11 +150,11 @@ dispatcher.on('line', (line) => {
}
const item = asserts[0];
if (!item) {
console.info(greyify(` .skip ${name} ${JSON.stringify(val)}`));
console.info(yellowify(` .skip ${name} ${JSON.stringify(val)}`));
return;
}
if (item.name !== name) {
console.info(greyify(` .skip ${name} ${JSON.stringify(val)}`));
console.info(yellowify(` .skip ${name} ${JSON.stringify(val)}`));
return;
}
asserts.shift();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"bin": {
"ro": "bin/rokid",
"rokid": "bin/rokid",
"rokid-node": "bin/rokid.js",
"rokid-search": "bin/rokid-search"
},
"repository": {
Expand Down