Skip to content

Commit

Permalink
chore: unified command processing capability (#1017)
Browse files Browse the repository at this point in the history
* chore: unified command processing capability

* chore: handle webpack loader
  • Loading branch information
dqhl76 committed May 16, 2022
1 parent 7e60097 commit 692de31
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 58 deletions.
4 changes: 2 additions & 2 deletions packages/remote-cli/package.json
Expand Up @@ -18,9 +18,9 @@
"url": "git@github.com:opensumi/core.git"
},
"dependencies": {
"commander": "^8.1.0",
"chalk": "^4.1.2",
"got": "^11.8.2",
"chalk": "^4.1.2"
"yargs": "^17.5.0"
},
"devDependencies": {
"@opensumi/ide-dev-tool": "^1.3.1"
Expand Down
106 changes: 50 additions & 56 deletions packages/remote-cli/src/index.ts
Expand Up @@ -2,84 +2,78 @@ import { statSync, existsSync } from 'fs';
import { join } from 'path';

import { green, yellow, red } from 'chalk';
import { Command } from 'commander';
import got from 'got';
import yargs from 'yargs';

const CLI_NAME = process.env.CLI_NAME || 'sumi';
const PRODUCTION_NAME = process.env.PRODUCTION_NAME || 'OpenSumi';
const CLIENT_ID = process.env.CLIENT_ID;
const SUMI_SERVER_HOST = process.env.SUMI_SERVER_HOST || 'http://0.0.0.0:8000';
const OPENER_ROUTE = process.env.OPENER_ROUTE || 'open';

const program = new Command(CLI_NAME);

enum OpenType {
url = 'url',
file = 'file',
}

program.addHelpText(
'beforeAll',
`Help: Open files or website from a shell.
By default, opens each file using the ${PRODUCTION_NAME} for that file.
If the file is in the form of a URL, will be opened the website use internal browser.
Examples:
1. ${green('open https://www.hostname.com')} Will open the website use internal browser.
2. ${green('open ./package.json')} Will open the file use ${PRODUCTION_NAME}.
3. ${green('open /path/to/file')} Will open the file use ${PRODUCTION_NAME}.
`,
);

program
.argument('<URL or FilePath>')
.description('file path or url')
.action((pathOrUrl) => {
if (!CLIENT_ID) {
function openPathOrUrl(pathOrUrl: string): void {
if (!CLIENT_ID) {
// eslint-disable-next-line no-console
console.error(red(`${PRODUCTION_NAME} Client id is undefined!`));
process.exit(0);
}

let type: OpenType = OpenType.file;
let fullPathOrUrl = pathOrUrl;
if (isHttpProtocol(pathOrUrl)) {
type = OpenType.url;
} else if (isRelativePath(pathOrUrl)) {
fullPathOrUrl = join(process.cwd(), pathOrUrl);
}

if (type === OpenType.file) {
if (!existsSync(fullPathOrUrl)) {
// eslint-disable-next-line no-console
console.error(red(`${PRODUCTION_NAME} Client id is undefined!`));
console.error(red(`The file path ${fullPathOrUrl} is not exist!`));
process.exit(0);
}

let type: OpenType = OpenType.file;
let fullPathOrUrl = pathOrUrl;
if (isHttpProtocol(pathOrUrl)) {
type = OpenType.url;
} else if (isRelativePath(pathOrUrl)) {
fullPathOrUrl = join(process.cwd(), pathOrUrl);
}

if (type === OpenType.file) {
if (!existsSync(fullPathOrUrl)) {
// eslint-disable-next-line no-console
console.error(red(`The file path ${fullPathOrUrl} is not exist!`));
process.exit(0);
}

if (statSync(fullPathOrUrl).isDirectory()) {
// eslint-disable-next-line no-console
console.error(red('Directory is unsupported'));
process.exit(0);
}
if (statSync(fullPathOrUrl).isDirectory()) {
// eslint-disable-next-line no-console
console.error(red('Directory is unsupported'));
process.exit(0);
}
}

const query = `?type=${type}&${type}=${encodeURIComponent(fullPathOrUrl)}&clientId=${CLIENT_ID}`;
got(`${SUMI_SERVER_HOST}/${OPENER_ROUTE}${query}`).catch((err) => {
// eslint-disable-next-line no-console
console.error(red(`Open ${type} ${fullPathOrUrl} error: \n ${err.message}`));
process.exit(1);
});
const query = `?type=${type}&${type}=${encodeURIComponent(fullPathOrUrl)}&clientId=${CLIENT_ID}`;
got(`${SUMI_SERVER_HOST}/${OPENER_ROUTE}${query}`).catch((err) => {
// eslint-disable-next-line no-console
console.error(red(`Open ${type} ${fullPathOrUrl} error: \n ${err.message}`));
process.exit(1);
});
}

program.configureOutput({
outputError: (str, write) => write(yellow(str)),
});

program.exitOverride();
const argv = yargs(process.argv)
.usage(
`
Help: Open files or website from a shell.
By default, opens each file using the ${PRODUCTION_NAME} for that file.
If the file is in the form of a URL, will be opened the website use internal browser.
try {
program.parse(process.argv);
} catch (err) {
Examples:
1. ${green('open https://www.hostname.com')} Will open the website use internal browser.
2. ${green('open ./package.json')} Will open the file use ${PRODUCTION_NAME}.
3. ${green('open /path/to/file')} Will open the file use ${PRODUCTION_NAME}.
`,
)
.help()
.string('_').argv;

if (argv._[0] !== undefined) {
openPathOrUrl(argv._[0]);
} else {
// eslint-disable-next-line no-console
console.error(red('The path or url is not defined.'));
process.exit(0);
}

Expand Down
1 change: 1 addition & 0 deletions packages/remote-cli/webpack.config.js
Expand Up @@ -18,6 +18,7 @@ module.exports = {
node: false,
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json', '.less'],
mainFields: ['loader', 'main'],
plugins: [
new TsconfigPathsPlugin({
configFile: tsConfigPath,
Expand Down

0 comments on commit 692de31

Please sign in to comment.