index.ts 5.3 KB
Newer Older
1 2 3 4
#!/usr/bin/env node

/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
5 6

import ora, {Ora} from 'ora';
P
Peter Pan 已提交
7

8 9 10 11 12
import ecosystem from '@visualdl/server/ecosystem.config';
import pm2 from 'pm2';

const app = ecosystem.apps[0];

13 14 15 16
const argv = require('yargs')
    .usage('Usage: $0 <command> [options]')
    .command('start', 'Start VisualDL server')
    .command('stop', 'Stop VisualDL server')
17 18 19 20
    .example(
        '$0 start --backend="http://172.17.0.82:8040"',
        'Start VisualDL server with backend address http://172.17.0.82:8040'
    )
21 22 23 24 25 26
    .alias('p', 'port')
    .nargs('p', 1)
    .nargs('port', 1)
    .describe('p', 'Port of server')
    .nargs('host', 1)
    .describe('host', 'Host of server')
27 28 29 30
    .alias('b', 'backend')
    .nargs('b', 1)
    .nargs('backend', 1)
    .describe('b', 'Backend API address')
P
Peter Pan 已提交
31 32
    .boolean('demo')
    .describe('demo', 'Run in demo mode')
33 34 35 36
    .boolean('open')
    .describe('open', 'Open browser when server is ready')
    .help('h')
    .alias('h', 'help')
37
    .epilog('Visit https://github.com/PaddlePaddle/VisualDL for more information.').argv;
38 39 40 41 42 43 44 45

const command = argv._[0];

const exit = () => {
    console.log('Command not found, use -h or --help for help');
    process.exit(1);
};

46
const exitIfError = (err?: Error, exitCode = 1, spinner?: Ora) => {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    if (!err) {
        return;
    }
    if (spinner) {
        spinner.fail('Error!');
    }
    console.error(err);
    process.exit(exitCode);
};

if (!command) {
    exit();
}

const banner = `

█████   █████  ███                               ████  ██████████   █████
░░███   ░░███  ░░░                               ░░███ ░░███░░░░███ ░░███
 ░███    ░███  ████   █████  █████ ████  ██████   ░███  ░███   ░░███ ░███
 ░███    ░███ ░░███  ███░░  ░░███ ░███  ░░░░░███  ░███  ░███    ░███ ░███
 ░░███   ███   ░███ ░░█████  ░███ ░███   ███████  ░███  ░███    ░███ ░███
  ░░░█████░    ░███  ░░░░███ ░███ ░███  ███░░███  ░███  ░███    ███  ░███      █
    ░░███      █████ ██████  ░░████████░░████████ █████ ██████████   ███████████
     ░░░      ░░░░░ ░░░░░░    ░░░░░░░░  ░░░░░░░░ ░░░░░ ░░░░░░░░░░   ░░░░░░░░░░░



`;

pm2.connect(err => {
    exitIfError(err, 2);

    pm2.list((err, list) => {
        exitIfError(err, 2);

        const host = argv.host || 'localhost';
        const port = Number.parseInt(argv.port, 10) || 8999;
        const url = `http://${host}:${port}`;

        if (command === 'start') {
            if (list.find(item => item.name === app.name)) {
88
                exitIfError(new Error('VisualDL server is already running'), 1);
89 90 91 92 93 94
            }
            const spinner = ora('Starting VisualDL server...').start();
            pm2.start(
                {
                    ...app,
                    env: {
95
                        ...app.env,
96
                        HOST: host,
97
                        PORT: port + '',
P
Peter Pan 已提交
98 99
                        BACKEND: argv.backend,
                        DEMO: argv.demo ? '1' : ''
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
                    }
                },
                err => {
                    pm2.disconnect();
                    exitIfError(err, 2, spinner);
                    spinner.succeed('Starting VisualDL server... Done');
                    console.log(banner);
                    console.log(`> VisualDL server is running at ${url}`);
                    if (argv.open) {
                        console.log('  Opening your browser for you...');
                        const open = require('open');
                        open(url);
                    }
                }
            );
        } else if (command === 'stop') {
            if (!list.find(item => item.name === app.name)) {
117
                exitIfError(new Error('VisualDL server is not running'), 1);
118 119 120 121
            }
            const spinner = ora('Stopping VisualDL server...').start();
            pm2.delete(app.name, err => {
                exitIfError(err, 2);
122
                const end = (err?: Error) => {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                    pm2.disconnect();
                    exitIfError(err, 2);

                    spinner.succeed('Stopping VisualDL server... Done');
                    console.log('> VisualDL server stopped');
                    console.log('  See you next time');
                    process.exit(0);
                };
                pm2.list((err, newList) => {
                    exitIfError(err, 2);
                    if (!newList.length) {
                        pm2.killDaemon(end);
                    } else {
                        end();
                    }
                });
            });
        } else {
            exit();
        }
    });
});