port cli/ to ts
This commit is contained in:
parent
f05bb265ac
commit
f76eae2c52
6 changed files with 201 additions and 149 deletions
90
cli/api.ts
90
cli/api.ts
|
|
@ -1,10 +1,11 @@
|
|||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const got = require('got');
|
||||
const registryUrl = require('registry-url')();
|
||||
const pify = require('pify');
|
||||
const recast = require('recast');
|
||||
const path = require('path');
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import got from 'got';
|
||||
import registryUrlModule from 'registry-url';
|
||||
const registryUrl = registryUrlModule();
|
||||
import pify from 'pify';
|
||||
import * as recast from 'recast';
|
||||
import path from 'path';
|
||||
|
||||
// If the user defines XDG_CONFIG_HOME they definitely want their config there,
|
||||
// otherwise use the home directory in linux/mac and userdata in windows
|
||||
|
|
@ -12,12 +13,12 @@ const applicationDirectory =
|
|||
process.env.XDG_CONFIG_HOME !== undefined
|
||||
? path.join(process.env.XDG_CONFIG_HOME, 'hyper')
|
||||
: process.platform == 'win32'
|
||||
? path.join(process.env.APPDATA, 'Hyper')
|
||||
? path.join(process.env.APPDATA!, 'Hyper')
|
||||
: os.homedir();
|
||||
|
||||
const devConfigFileName = path.join(__dirname, `../.hyper.js`);
|
||||
|
||||
let fileName =
|
||||
const fileName =
|
||||
process.env.NODE_ENV !== 'production' && fs.existsSync(devConfigFileName)
|
||||
? devConfigFileName
|
||||
: path.join(applicationDirectory, '.hyper.js');
|
||||
|
|
@ -27,16 +28,16 @@ let fileName =
|
|||
* statically analyze the hyper configuration isn't fatal for all kinds of
|
||||
* subcommands. We can use memoization to make reading and parsing lazy.
|
||||
*/
|
||||
function memoize(fn) {
|
||||
function memoize<T extends (...args: any[]) => any>(fn: T): T {
|
||||
let hasResult = false;
|
||||
let result;
|
||||
return (...args) => {
|
||||
let result: any;
|
||||
return ((...args: any[]) => {
|
||||
if (!hasResult) {
|
||||
result = fn(...args);
|
||||
hasResult = true;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}) as T;
|
||||
}
|
||||
|
||||
const getFileContents = memoize(() => {
|
||||
|
|
@ -51,18 +52,18 @@ const getFileContents = memoize(() => {
|
|||
return null;
|
||||
});
|
||||
|
||||
const getParsedFile = memoize(() => recast.parse(getFileContents()));
|
||||
const getParsedFile = memoize(() => recast.parse(getFileContents()!));
|
||||
|
||||
const getProperties = memoize(() => getParsedFile().program.body.map(obj => obj));
|
||||
const getProperties = memoize(() => (getParsedFile().program.body as any[]).map(obj => obj));
|
||||
|
||||
const getPlugins = memoize(() => {
|
||||
const properties = getProperties();
|
||||
for (let i = 0; i < properties.length; i++) {
|
||||
const rightProperties = Object.values(properties[i].expression.right.properties);
|
||||
const rightProperties = Object.values<any>(properties[i].expression.right.properties);
|
||||
for (let j = 0; j < rightProperties.length; j++) {
|
||||
const plugin = rightProperties[j];
|
||||
if (plugin.key.name === 'plugins') {
|
||||
return plugin.value.elements;
|
||||
return plugin.value.elements as any[];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -71,11 +72,11 @@ const getPlugins = memoize(() => {
|
|||
const getLocalPlugins = memoize(() => {
|
||||
const properties = getProperties();
|
||||
for (let i = 0; i < properties.length; i++) {
|
||||
const rightProperties = Object.values(properties[i].expression.right.properties);
|
||||
const rightProperties = Object.values<any>(properties[i].expression.right.properties);
|
||||
for (let j = 0; j < rightProperties.length; j++) {
|
||||
const plugin = rightProperties[j];
|
||||
if (plugin.key.name === 'localPlugins') {
|
||||
return plugin.value.elements;
|
||||
return plugin.value.elements as any[];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -85,7 +86,7 @@ function exists() {
|
|||
return getFileContents() !== undefined;
|
||||
}
|
||||
|
||||
function isInstalled(plugin, locally) {
|
||||
function isInstalled(plugin: string, locally?: boolean) {
|
||||
const array = (locally ? getLocalPlugins() : getPlugins()) || [];
|
||||
if (array && Array.isArray(array)) {
|
||||
return array.some(entry => entry.value === plugin);
|
||||
|
|
@ -97,32 +98,34 @@ function save() {
|
|||
return pify(fs.writeFile)(fileName, recast.print(getParsedFile()).code, 'utf8');
|
||||
}
|
||||
|
||||
function existsOnNpm(plugin) {
|
||||
const name = getPackageName(plugin);
|
||||
return got.get(registryUrl + name.toLowerCase(), {timeout: 10000, json: true}).then(res => {
|
||||
if (!res.body.versions) {
|
||||
return Promise.reject(res);
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getPackageName(plugin) {
|
||||
function getPackageName(plugin: string) {
|
||||
const isScoped = plugin[0] === '@';
|
||||
const nameWithoutVersion = plugin.split('#')[0];
|
||||
|
||||
if (isScoped) {
|
||||
return '@' + nameWithoutVersion.split('@')[1].replace('/', '%2f');
|
||||
return `@${nameWithoutVersion.split('@')[1].replace('/', '%2f')}`;
|
||||
}
|
||||
|
||||
return nameWithoutVersion.split('@')[0];
|
||||
}
|
||||
|
||||
function install(plugin, locally) {
|
||||
function existsOnNpm(plugin: string) {
|
||||
const name = getPackageName(plugin);
|
||||
return got
|
||||
.get<any>(registryUrl + name.toLowerCase(), {timeout: 10000, responseType: 'json'})
|
||||
.then(res => {
|
||||
if (!res.body.versions) {
|
||||
return Promise.reject(res);
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function install(plugin: string, locally?: boolean) {
|
||||
const array = (locally ? getLocalPlugins() : getPlugins()) || [];
|
||||
return existsOnNpm(plugin)
|
||||
.catch(err => {
|
||||
.catch((err: any) => {
|
||||
const {statusCode} = err;
|
||||
if (statusCode && (statusCode === 404 || statusCode === 200)) {
|
||||
return Promise.reject(`${plugin} not found on npm`);
|
||||
|
|
@ -139,29 +142,24 @@ function install(plugin, locally) {
|
|||
});
|
||||
}
|
||||
|
||||
function uninstall(plugin) {
|
||||
function uninstall(plugin: string) {
|
||||
if (!isInstalled(plugin)) {
|
||||
return Promise.reject(`${plugin} is not installed`);
|
||||
}
|
||||
|
||||
const index = getPlugins().findIndex(entry => entry.value === plugin);
|
||||
getPlugins().splice(index, 1);
|
||||
const index = getPlugins()!.findIndex(entry => entry.value === plugin);
|
||||
getPlugins()!.splice(index, 1);
|
||||
return save();
|
||||
}
|
||||
|
||||
function list() {
|
||||
if (Array.isArray(getPlugins())) {
|
||||
return getPlugins()
|
||||
return getPlugins()!
|
||||
.map(plugin => plugin.value)
|
||||
.join('\n');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
module.exports.configPath = fileName;
|
||||
module.exports.exists = exists;
|
||||
module.exports.existsOnNpm = existsOnNpm;
|
||||
module.exports.isInstalled = isInstalled;
|
||||
module.exports.install = install;
|
||||
module.exports.uninstall = uninstall;
|
||||
module.exports.list = list;
|
||||
export const configPath = fileName;
|
||||
export {exists, existsOnNpm, isInstalled, install, uninstall, list};
|
||||
|
|
|
|||
223
cli/index.ts
223
cli/index.ts
|
|
@ -1,23 +1,23 @@
|
|||
// This is a CLI tool, using console is OK
|
||||
/* eslint no-console: 0 */
|
||||
const {spawn, exec} = require('child_process');
|
||||
const {isAbsolute, resolve} = require('path');
|
||||
const {existsSync} = require('fs');
|
||||
const {version} = require('../app/package');
|
||||
const pify = require('pify');
|
||||
const args = require('args');
|
||||
const chalk = require('chalk');
|
||||
const open = require('open');
|
||||
const columnify = require('columnify');
|
||||
const got = require('got');
|
||||
const ora = require('ora');
|
||||
const api = require('./api');
|
||||
import {spawn, exec} from 'child_process';
|
||||
import {isAbsolute, resolve} from 'path';
|
||||
import {existsSync} from 'fs';
|
||||
import {version} from '../app/package.json';
|
||||
import pify from 'pify';
|
||||
import args from 'args';
|
||||
import chalk from 'chalk';
|
||||
import open from 'open';
|
||||
import columnify from 'columnify';
|
||||
import got from 'got';
|
||||
import ora from 'ora';
|
||||
import * as api from './api';
|
||||
|
||||
const PLUGIN_PREFIX = 'hyper-';
|
||||
|
||||
let commandPromise;
|
||||
let commandPromise: Promise<void>;
|
||||
|
||||
const assertPluginName = pluginName => {
|
||||
const assertPluginName = (pluginName: string) => {
|
||||
if (!pluginName) {
|
||||
console.error(chalk.red('Plugin name is required'));
|
||||
process.exit(1);
|
||||
|
|
@ -34,43 +34,58 @@ const checkConfig = () => {
|
|||
process.exit(1);
|
||||
};
|
||||
|
||||
args.command(['i', 'install'], 'Install a plugin', (name, args_) => {
|
||||
checkConfig();
|
||||
const pluginName = args_[0];
|
||||
assertPluginName(pluginName);
|
||||
commandPromise = api
|
||||
.install(pluginName)
|
||||
.then(() => console.log(chalk.green(`${pluginName} installed successfully!`)))
|
||||
.catch(err => console.error(chalk.red(err)));
|
||||
});
|
||||
args.command(
|
||||
'install',
|
||||
'Install a plugin',
|
||||
(name, args_) => {
|
||||
checkConfig();
|
||||
const pluginName = args_[0];
|
||||
assertPluginName(pluginName);
|
||||
commandPromise = api
|
||||
.install(pluginName)
|
||||
.then(() => console.log(chalk.green(`${pluginName} installed successfully!`)))
|
||||
.catch((err: any) => console.error(chalk.red(err)));
|
||||
},
|
||||
['i', 'install']
|
||||
);
|
||||
|
||||
args.command(['u', 'uninstall', 'rm', 'remove'], 'Uninstall a plugin', (name, args_) => {
|
||||
checkConfig();
|
||||
const pluginName = args_[0];
|
||||
assertPluginName(pluginName);
|
||||
commandPromise = api
|
||||
.uninstall(pluginName)
|
||||
.then(() => console.log(chalk.green(`${pluginName} uninstalled successfully!`)))
|
||||
.catch(err => console.log(chalk.red(err)));
|
||||
});
|
||||
args.command(
|
||||
'uninstall',
|
||||
'Uninstall a plugin',
|
||||
(name, args_) => {
|
||||
checkConfig();
|
||||
const pluginName = args_[0];
|
||||
assertPluginName(pluginName);
|
||||
commandPromise = api
|
||||
.uninstall(pluginName)
|
||||
.then(() => console.log(chalk.green(`${pluginName} uninstalled successfully!`)))
|
||||
.catch(err => console.log(chalk.red(err)));
|
||||
},
|
||||
['u', 'uninstall', 'rm', 'remove']
|
||||
);
|
||||
|
||||
args.command(['ls', 'list'], 'List installed plugins', () => {
|
||||
checkConfig();
|
||||
let plugins = api.list();
|
||||
args.command(
|
||||
'list',
|
||||
'List installed plugins',
|
||||
() => {
|
||||
checkConfig();
|
||||
const plugins = api.list();
|
||||
|
||||
if (plugins) {
|
||||
console.log(plugins);
|
||||
} else {
|
||||
console.log(chalk.red(`No plugins installed yet.`));
|
||||
}
|
||||
process.exit(0);
|
||||
});
|
||||
if (plugins) {
|
||||
console.log(plugins);
|
||||
} else {
|
||||
console.log(chalk.red(`No plugins installed yet.`));
|
||||
}
|
||||
process.exit(0);
|
||||
},
|
||||
['ls', 'list']
|
||||
);
|
||||
|
||||
const lsRemote = pattern => {
|
||||
const lsRemote = (pattern?: string) => {
|
||||
// note that no errors are catched by this function
|
||||
const URL = `https://api.npms.io/v2/search?q=${(pattern && `${pattern}+`) || ''}keywords:hyper-plugin,hyper-theme`;
|
||||
return got(URL)
|
||||
.then(response => JSON.parse(response.body).results)
|
||||
.then(response => JSON.parse(response.body).results as any[])
|
||||
.then(entries => entries.map(entry => entry.package))
|
||||
.then(entries => entries.filter(entry => entry.name.indexOf(PLUGIN_PREFIX) === 0))
|
||||
.then(entries =>
|
||||
|
|
@ -86,71 +101,91 @@ const lsRemote = pattern => {
|
|||
);
|
||||
};
|
||||
|
||||
args.command(['s', 'search'], 'Search for plugins on npm', (name, args_) => {
|
||||
const spinner = ora('Searching').start();
|
||||
const query = args_[0] ? args_[0].toLowerCase() : '';
|
||||
args.command(
|
||||
'search',
|
||||
'Search for plugins on npm',
|
||||
(name, args_) => {
|
||||
const spinner = ora('Searching').start();
|
||||
const query = args_[0] ? args_[0].toLowerCase() : '';
|
||||
|
||||
commandPromise = lsRemote(query)
|
||||
.then(entries => {
|
||||
if (entries.length === 0) {
|
||||
commandPromise = lsRemote(query)
|
||||
.then(entries => {
|
||||
if (entries.length === 0) {
|
||||
spinner.fail();
|
||||
console.error(chalk.red(`Your search '${query}' did not match any plugins`));
|
||||
console.error(`${chalk.red('Try')} ${chalk.green('hyper ls-remote')}`);
|
||||
process.exit(1);
|
||||
} else {
|
||||
let msg = columnify(entries);
|
||||
spinner.succeed();
|
||||
msg = msg.substring(msg.indexOf('\n') + 1); // remove header
|
||||
console.log(msg);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
spinner.fail();
|
||||
console.error(chalk.red(`Your search '${query}' did not match any plugins`));
|
||||
console.error(`${chalk.red('Try')} ${chalk.green('hyper ls-remote')}`);
|
||||
process.exit(1);
|
||||
} else {
|
||||
console.error(chalk.red(err)); // TODO
|
||||
});
|
||||
},
|
||||
['s', 'search']
|
||||
);
|
||||
|
||||
args.command(
|
||||
'list-remote',
|
||||
'List plugins available on npm',
|
||||
() => {
|
||||
const spinner = ora('Searching').start();
|
||||
|
||||
commandPromise = lsRemote()
|
||||
.then(entries => {
|
||||
let msg = columnify(entries);
|
||||
|
||||
spinner.succeed();
|
||||
msg = msg.substring(msg.indexOf('\n') + 1); // remove header
|
||||
console.log(msg);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
spinner.fail();
|
||||
console.error(chalk.red(err)); // TODO
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
spinner.fail();
|
||||
console.error(chalk.red(err)); // TODO
|
||||
});
|
||||
},
|
||||
['lsr', 'list-remote', 'ls-remote']
|
||||
);
|
||||
|
||||
args.command(['lsr', 'list-remote', 'ls-remote'], 'List plugins available on npm', () => {
|
||||
const spinner = ora('Searching').start();
|
||||
args.command(
|
||||
'docs',
|
||||
'Open the npm page of a plugin',
|
||||
(name, args_) => {
|
||||
const pluginName = args_[0];
|
||||
assertPluginName(pluginName);
|
||||
open(`http://ghub.io/${pluginName}`, {wait: false, url: true});
|
||||
process.exit(0);
|
||||
},
|
||||
['d', 'docs', 'h', 'home']
|
||||
);
|
||||
|
||||
commandPromise = lsRemote()
|
||||
.then(entries => {
|
||||
let msg = columnify(entries);
|
||||
args.command(
|
||||
'version',
|
||||
'Show the version of hyper',
|
||||
() => {
|
||||
console.log(version);
|
||||
process.exit(0);
|
||||
},
|
||||
['version']
|
||||
);
|
||||
|
||||
spinner.succeed();
|
||||
msg = msg.substring(msg.indexOf('\n') + 1); // remove header
|
||||
console.log(msg);
|
||||
})
|
||||
.catch(err => {
|
||||
spinner.fail();
|
||||
console.error(chalk.red(err)); // TODO
|
||||
});
|
||||
});
|
||||
|
||||
args.command(['d', 'docs', 'h', 'home'], 'Open the npm page of a plugin', (name, args_) => {
|
||||
const pluginName = args_[0];
|
||||
assertPluginName(pluginName);
|
||||
open(`http://ghub.io/${pluginName}`, {wait: false, url: true});
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
args.command(['version'], 'Show the version of hyper', () => {
|
||||
console.log(version);
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
args.command(['<default>'], 'Launch Hyper');
|
||||
args.command('<default>', 'Launch Hyper');
|
||||
|
||||
args.option(['v', 'verbose'], 'Verbose mode', false);
|
||||
|
||||
const main = argv => {
|
||||
const main = (argv: string[]) => {
|
||||
const flags = args.parse(argv, {
|
||||
name: 'hyper',
|
||||
version: false,
|
||||
mri: {
|
||||
boolean: ['v', 'verbose']
|
||||
}
|
||||
});
|
||||
} as any);
|
||||
|
||||
if (commandPromise) {
|
||||
return commandPromise;
|
||||
|
|
@ -168,7 +203,7 @@ const main = argv => {
|
|||
env['ELECTRON_ENABLE_LOGGING'] = '1';
|
||||
}
|
||||
|
||||
const options = {
|
||||
const options: any = {
|
||||
detached: true,
|
||||
env
|
||||
};
|
||||
|
|
@ -207,13 +242,13 @@ const main = argv => {
|
|||
return Promise.resolve();
|
||||
};
|
||||
|
||||
function eventuallyExit(code) {
|
||||
function eventuallyExit(code: number) {
|
||||
setTimeout(() => process.exit(code), 100);
|
||||
}
|
||||
|
||||
main(process.argv)
|
||||
.then(() => eventuallyExit(0))
|
||||
.catch(err => {
|
||||
.catch((err: any) => {
|
||||
console.error(err.stack ? err.stack : err);
|
||||
eventuallyExit(1);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue