@ibm-adw/skill-toolkit
Version:
Developing your own skills with IBM Automation Digital Worker Skill Toolkit
156 lines (126 loc) • 5.3 kB
JavaScript
/*
Licensed Materials - Property of IBM
5737-I23
Copyright IBM Corp. 2019, 2020. All Rights Reserved.
U.S. Government Users Restricted Rights:
Use, duplication or disclosure restricted by GSA ADP Schedule
Contract with IBM Corp.
*/
;
const chalk = require('chalk');
const chokidar = require('chokidar');
const fs = require('fs');
const path = require('path');
const DEFAULT_PORT = 8888;
const SKILL_DIR_1 = process.argv[2];
let LISTENER_PORT = Number(process.argv[3]);
if (!LISTENER_PORT) {
if (process.argv[3]) {
console.log(chalk.blue('\ninfo '), 'Invalid port:', process.argv[3], 'is not a valid number');
console.log(chalk.blue('info '), 'Fallback to default port', DEFAULT_PORT);
}
LISTENER_PORT = DEFAULT_PORT;
}
if (!process.env.PWD) {
if (process.env.ComSpec.endsWith('cmd.exe')) {
const { execSync } = require('child_process');
const output = execSync('cd');
process.env.PWD = output.toString().trim();
}
}
const SKILL_DIR = path.resolve(process.env.PWD, SKILL_DIR_1);
const skill_dir_valid = fs.existsSync(SKILL_DIR);
if (skill_dir_valid) {
console.log('\nSKILL DIRECTORY: ', chalk.cyan(SKILL_DIR));
console.log('SERVER PORT: ', chalk.cyan(LISTENER_PORT));
console.log('');
console.log(chalk.magentaBright('Starting server...\n'));
console.log('Press ^C at any time to quit.\n');
// check port available otherwise suggest other one.
const portfinder = require('portfinder');
portfinder.getPortPromise({
port: LISTENER_PORT, // minimum port
stopPort: ( LISTENER_PORT + 5 <= 65535 ? LISTENER_PORT + 5 : 65537) // maximum port
})
.then(async (port) => {
//
// `port` is guaranteed to be a free port
// in this scope.
//
const launchServer = () => {
// Launching server
const app = require('./app');
const state = {};
// watch tested skill directory
const watcher = chokidar.watch(SKILL_DIR, {
ignored: [ /(^|[/\\])\../, '**/tests/**', '**/coverage/**', '**/node_modules/**' ]// ignore dotfiles
});
const listenerCallback = () => {
console.log(chalk.green(`server is up, listening on port ${LISTENER_PORT}.\n`));
};
const start = () => {
state.server = app.listen(LISTENER_PORT, listenerCallback);
};
const restart = () => {
// clean the cache
Object.keys(require.cache).forEach((id) => {
//TODO filter on specific files to clean from cache
delete require.cache[id];
});
state.server.close(() => {
start();
});
};
start();
watcher.on('ready', function () {
watcher.on('all', function (event, path) {
console.log('---> File', chalk.cyan(path), 'has changed.');
console.log('Restarting server...');
// console.log(chalk.green('Restarting server...'));
restart();
});
});
};
if (LISTENER_PORT === port) {
launchServer();
} else {
for (let i = LISTENER_PORT; i < port; i++) {
console.log(chalk.blue('info '), 'Port', i, 'is not available');
}
console.log(chalk.blue('info '), 'The first available port after', LISTENER_PORT, 'is:', port);
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const askQuestion = (readline, question) => {
return new Promise(resolve => {
readline.question(question, async (answer) => {
resolve(answer);
});
});
};
const agree = await askQuestion(readline, '\nDo you want to launch the server on port ' + port + '? [yes]') || 'yes';
readline.close();
if (agree === 'yes' || agree === 'y') {
LISTENER_PORT = port;
launchServer();
} else {
console.log(chalk.red('\nquit '), 'You can run the same command with your desired port');
console.log(chalk.cyan(` npx launch-server ${SKILL_DIR_1} YOUR_PORT\n`));
}
}
})
.catch((err) => {
//
// Could not get a free port, `err` contains the reason.
//
if (err.message) {
console.log(chalk.red('\nerror '), err.message);
} else {
console.log(chalk.red('\nerror '), err);
}
});
} else {
console.log(chalk.red('\nerror '), 'SKILL DIRECTORY: ', SKILL_DIR, 'is invalid.\n');
}