port cli/ to ts

This commit is contained in:
Labhansh Agrawal 2019-12-14 16:59:48 +05:30 committed by Benjamin Staneck
parent f05bb265ac
commit f76eae2c52
6 changed files with 201 additions and 149 deletions

View file

@ -1,10 +1,11 @@
const fs = require('fs'); import fs from 'fs';
const os = require('os'); import os from 'os';
const got = require('got'); import got from 'got';
const registryUrl = require('registry-url')(); import registryUrlModule from 'registry-url';
const pify = require('pify'); const registryUrl = registryUrlModule();
const recast = require('recast'); import pify from 'pify';
const path = require('path'); import * as recast from 'recast';
import path from 'path';
// If the user defines XDG_CONFIG_HOME they definitely want their config there, // 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 // otherwise use the home directory in linux/mac and userdata in windows
@ -12,12 +13,12 @@ const applicationDirectory =
process.env.XDG_CONFIG_HOME !== undefined process.env.XDG_CONFIG_HOME !== undefined
? path.join(process.env.XDG_CONFIG_HOME, 'hyper') ? path.join(process.env.XDG_CONFIG_HOME, 'hyper')
: process.platform == 'win32' : process.platform == 'win32'
? path.join(process.env.APPDATA, 'Hyper') ? path.join(process.env.APPDATA!, 'Hyper')
: os.homedir(); : os.homedir();
const devConfigFileName = path.join(__dirname, `../.hyper.js`); const devConfigFileName = path.join(__dirname, `../.hyper.js`);
let fileName = const fileName =
process.env.NODE_ENV !== 'production' && fs.existsSync(devConfigFileName) process.env.NODE_ENV !== 'production' && fs.existsSync(devConfigFileName)
? devConfigFileName ? devConfigFileName
: path.join(applicationDirectory, '.hyper.js'); : path.join(applicationDirectory, '.hyper.js');
@ -27,16 +28,16 @@ let fileName =
* statically analyze the hyper configuration isn't fatal for all kinds of * statically analyze the hyper configuration isn't fatal for all kinds of
* subcommands. We can use memoization to make reading and parsing lazy. * 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 hasResult = false;
let result; let result: any;
return (...args) => { return ((...args: any[]) => {
if (!hasResult) { if (!hasResult) {
result = fn(...args); result = fn(...args);
hasResult = true; hasResult = true;
} }
return result; return result;
}; }) as T;
} }
const getFileContents = memoize(() => { const getFileContents = memoize(() => {
@ -51,18 +52,18 @@ const getFileContents = memoize(() => {
return null; 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 getPlugins = memoize(() => {
const properties = getProperties(); const properties = getProperties();
for (let i = 0; i < properties.length; i++) { 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++) { for (let j = 0; j < rightProperties.length; j++) {
const plugin = rightProperties[j]; const plugin = rightProperties[j];
if (plugin.key.name === 'plugins') { 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 getLocalPlugins = memoize(() => {
const properties = getProperties(); const properties = getProperties();
for (let i = 0; i < properties.length; i++) { 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++) { for (let j = 0; j < rightProperties.length; j++) {
const plugin = rightProperties[j]; const plugin = rightProperties[j];
if (plugin.key.name === 'localPlugins') { if (plugin.key.name === 'localPlugins') {
return plugin.value.elements; return plugin.value.elements as any[];
} }
} }
} }
@ -85,7 +86,7 @@ function exists() {
return getFileContents() !== undefined; return getFileContents() !== undefined;
} }
function isInstalled(plugin, locally) { function isInstalled(plugin: string, locally?: boolean) {
const array = (locally ? getLocalPlugins() : getPlugins()) || []; const array = (locally ? getLocalPlugins() : getPlugins()) || [];
if (array && Array.isArray(array)) { if (array && Array.isArray(array)) {
return array.some(entry => entry.value === plugin); return array.some(entry => entry.value === plugin);
@ -97,32 +98,34 @@ function save() {
return pify(fs.writeFile)(fileName, recast.print(getParsedFile()).code, 'utf8'); return pify(fs.writeFile)(fileName, recast.print(getParsedFile()).code, 'utf8');
} }
function existsOnNpm(plugin) { function getPackageName(plugin: string) {
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) {
const isScoped = plugin[0] === '@'; const isScoped = plugin[0] === '@';
const nameWithoutVersion = plugin.split('#')[0]; const nameWithoutVersion = plugin.split('#')[0];
if (isScoped) { if (isScoped) {
return '@' + nameWithoutVersion.split('@')[1].replace('/', '%2f'); return `@${nameWithoutVersion.split('@')[1].replace('/', '%2f')}`;
} }
return nameWithoutVersion.split('@')[0]; 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()) || []; const array = (locally ? getLocalPlugins() : getPlugins()) || [];
return existsOnNpm(plugin) return existsOnNpm(plugin)
.catch(err => { .catch((err: any) => {
const {statusCode} = err; const {statusCode} = err;
if (statusCode && (statusCode === 404 || statusCode === 200)) { if (statusCode && (statusCode === 404 || statusCode === 200)) {
return Promise.reject(`${plugin} not found on npm`); 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)) { if (!isInstalled(plugin)) {
return Promise.reject(`${plugin} is not installed`); return Promise.reject(`${plugin} is not installed`);
} }
const index = getPlugins().findIndex(entry => entry.value === plugin); const index = getPlugins()!.findIndex(entry => entry.value === plugin);
getPlugins().splice(index, 1); getPlugins()!.splice(index, 1);
return save(); return save();
} }
function list() { function list() {
if (Array.isArray(getPlugins())) { if (Array.isArray(getPlugins())) {
return getPlugins() return getPlugins()!
.map(plugin => plugin.value) .map(plugin => plugin.value)
.join('\n'); .join('\n');
} }
return false; return false;
} }
module.exports.configPath = fileName; export const configPath = fileName;
module.exports.exists = exists; export {exists, existsOnNpm, isInstalled, install, uninstall, list};
module.exports.existsOnNpm = existsOnNpm;
module.exports.isInstalled = isInstalled;
module.exports.install = install;
module.exports.uninstall = uninstall;
module.exports.list = list;

View file

@ -1,23 +1,23 @@
// This is a CLI tool, using console is OK // This is a CLI tool, using console is OK
/* eslint no-console: 0 */ /* eslint no-console: 0 */
const {spawn, exec} = require('child_process'); import {spawn, exec} from 'child_process';
const {isAbsolute, resolve} = require('path'); import {isAbsolute, resolve} from 'path';
const {existsSync} = require('fs'); import {existsSync} from 'fs';
const {version} = require('../app/package'); import {version} from '../app/package.json';
const pify = require('pify'); import pify from 'pify';
const args = require('args'); import args from 'args';
const chalk = require('chalk'); import chalk from 'chalk';
const open = require('open'); import open from 'open';
const columnify = require('columnify'); import columnify from 'columnify';
const got = require('got'); import got from 'got';
const ora = require('ora'); import ora from 'ora';
const api = require('./api'); import * as api from './api';
const PLUGIN_PREFIX = 'hyper-'; const PLUGIN_PREFIX = 'hyper-';
let commandPromise; let commandPromise: Promise<void>;
const assertPluginName = pluginName => { const assertPluginName = (pluginName: string) => {
if (!pluginName) { if (!pluginName) {
console.error(chalk.red('Plugin name is required')); console.error(chalk.red('Plugin name is required'));
process.exit(1); process.exit(1);
@ -34,43 +34,58 @@ const checkConfig = () => {
process.exit(1); process.exit(1);
}; };
args.command(['i', 'install'], 'Install a plugin', (name, args_) => { args.command(
checkConfig(); 'install',
const pluginName = args_[0]; 'Install a plugin',
assertPluginName(pluginName); (name, args_) => {
commandPromise = api checkConfig();
.install(pluginName) const pluginName = args_[0];
.then(() => console.log(chalk.green(`${pluginName} installed successfully!`))) assertPluginName(pluginName);
.catch(err => console.error(chalk.red(err))); 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_) => { args.command(
checkConfig(); 'uninstall',
const pluginName = args_[0]; 'Uninstall a plugin',
assertPluginName(pluginName); (name, args_) => {
commandPromise = api checkConfig();
.uninstall(pluginName) const pluginName = args_[0];
.then(() => console.log(chalk.green(`${pluginName} uninstalled successfully!`))) assertPluginName(pluginName);
.catch(err => console.log(chalk.red(err))); 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', () => { args.command(
checkConfig(); 'list',
let plugins = api.list(); 'List installed plugins',
() => {
checkConfig();
const plugins = api.list();
if (plugins) { if (plugins) {
console.log(plugins); console.log(plugins);
} else { } else {
console.log(chalk.red(`No plugins installed yet.`)); console.log(chalk.red(`No plugins installed yet.`));
} }
process.exit(0); process.exit(0);
}); },
['ls', 'list']
);
const lsRemote = pattern => { const lsRemote = (pattern?: string) => {
// note that no errors are catched by this function // 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`; const URL = `https://api.npms.io/v2/search?q=${(pattern && `${pattern}+`) || ''}keywords:hyper-plugin,hyper-theme`;
return got(URL) 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.map(entry => entry.package))
.then(entries => entries.filter(entry => entry.name.indexOf(PLUGIN_PREFIX) === 0)) .then(entries => entries.filter(entry => entry.name.indexOf(PLUGIN_PREFIX) === 0))
.then(entries => .then(entries =>
@ -86,71 +101,91 @@ const lsRemote = pattern => {
); );
}; };
args.command(['s', 'search'], 'Search for plugins on npm', (name, args_) => { args.command(
const spinner = ora('Searching').start(); 'search',
const query = args_[0] ? args_[0].toLowerCase() : ''; 'Search for plugins on npm',
(name, args_) => {
const spinner = ora('Searching').start();
const query = args_[0] ? args_[0].toLowerCase() : '';
commandPromise = lsRemote(query) commandPromise = lsRemote(query)
.then(entries => { .then(entries => {
if (entries.length === 0) { 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(); spinner.fail();
console.error(chalk.red(`Your search '${query}' did not match any plugins`)); console.error(chalk.red(err)); // TODO
console.error(`${chalk.red('Try')} ${chalk.green('hyper ls-remote')}`); });
process.exit(1); },
} else { ['s', 'search']
);
args.command(
'list-remote',
'List plugins available on npm',
() => {
const spinner = ora('Searching').start();
commandPromise = lsRemote()
.then(entries => {
let msg = columnify(entries); let msg = columnify(entries);
spinner.succeed(); spinner.succeed();
msg = msg.substring(msg.indexOf('\n') + 1); // remove header msg = msg.substring(msg.indexOf('\n') + 1); // remove header
console.log(msg); console.log(msg);
} })
}) .catch(err => {
.catch(err => { spinner.fail();
spinner.fail(); console.error(chalk.red(err)); // TODO
console.error(chalk.red(err)); // TODO });
}); },
}); ['lsr', 'list-remote', 'ls-remote']
);
args.command(['lsr', 'list-remote', 'ls-remote'], 'List plugins available on npm', () => { args.command(
const spinner = ora('Searching').start(); '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() args.command(
.then(entries => { 'version',
let msg = columnify(entries); 'Show the version of hyper',
() => {
console.log(version);
process.exit(0);
},
['version']
);
spinner.succeed(); args.command('<default>', 'Launch Hyper');
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.option(['v', 'verbose'], 'Verbose mode', false); args.option(['v', 'verbose'], 'Verbose mode', false);
const main = argv => { const main = (argv: string[]) => {
const flags = args.parse(argv, { const flags = args.parse(argv, {
name: 'hyper', name: 'hyper',
version: false, version: false,
mri: { mri: {
boolean: ['v', 'verbose'] boolean: ['v', 'verbose']
} }
}); } as any);
if (commandPromise) { if (commandPromise) {
return commandPromise; return commandPromise;
@ -168,7 +203,7 @@ const main = argv => {
env['ELECTRON_ENABLE_LOGGING'] = '1'; env['ELECTRON_ENABLE_LOGGING'] = '1';
} }
const options = { const options: any = {
detached: true, detached: true,
env env
}; };
@ -207,13 +242,13 @@ const main = argv => {
return Promise.resolve(); return Promise.resolve();
}; };
function eventuallyExit(code) { function eventuallyExit(code: number) {
setTimeout(() => process.exit(code), 100); setTimeout(() => process.exit(code), 100);
} }
main(process.argv) main(process.argv)
.then(() => eventuallyExit(0)) .then(() => eventuallyExit(0))
.catch(err => { .catch((err: any) => {
console.error(err.stack ? err.stack : err); console.error(err.stack ? err.stack : err);
eventuallyExit(1); eventuallyExit(1);
}); });

View file

@ -256,7 +256,7 @@
"color": "3.1.2", "color": "3.1.2",
"columnify": "1.5.4", "columnify": "1.5.4",
"css-loader": "3.2.1", "css-loader": "3.2.1",
"got": "10.0.2", "got": "10.0.4",
"json-loader": "0.5.7", "json-loader": "0.5.7",
"mousetrap": "chabou/mousetrap#useCapture", "mousetrap": "chabou/mousetrap#useCapture",
"ms": "2.1.2", "ms": "2.1.2",
@ -294,9 +294,12 @@
"@babel/plugin-proposal-object-rest-spread": "^7.7.4", "@babel/plugin-proposal-object-rest-spread": "^7.7.4",
"@babel/preset-react": "7.7.4", "@babel/preset-react": "7.7.4",
"@babel/preset-typescript": "7.7.4", "@babel/preset-typescript": "7.7.4",
"@types/args": "3.0.0",
"@types/color": "3.0.0", "@types/color": "3.0.0",
"@types/columnify": "^1.5.0",
"@types/mousetrap": "^1.6.3", "@types/mousetrap": "^1.6.3",
"@types/node": "^12.12.15", "@types/node": "^12.12.15",
"@types/pify": "3.0.2",
"@types/plist": "3.0.2", "@types/plist": "3.0.2",
"@types/react": "^16.9.16", "@types/react": "^16.9.16",
"@types/react-dom": "^16.9.4", "@types/react-dom": "^16.9.4",

View file

@ -6,7 +6,8 @@
"include": [ "include": [
"./app/", "./app/",
"./lib/", "./lib/",
"./test/" "./test/",
"./cli/"
], ],
"references": [ "references": [
{ {

View file

@ -107,10 +107,10 @@ module.exports = [
mode: 'none', mode: 'none',
name: 'hyper-cli', name: 'hyper-cli',
resolve: { resolve: {
extensions: ['.js', '.jsx', '.json'] extensions: ['.js', '.jsx', '.ts', '.tsx', '.json']
}, },
devtool: isProd ? 'none' : 'cheap-module-source-map', devtool: isProd ? 'none' : 'cheap-module-source-map',
entry: './cli/index.js', entry: './cli/index.ts',
output: { output: {
path: path.join(__dirname, 'bin'), path: path.join(__dirname, 'bin'),
filename: 'cli.js' filename: 'cli.js'
@ -118,7 +118,7 @@ module.exports = [
module: { module: {
rules: [ rules: [
{ {
test: /\.(js|jsx)$/, test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/, exclude: /node_modules/,
loader: 'babel-loader' loader: 'babel-loader'
}, },

View file

@ -576,6 +576,11 @@
dependencies: dependencies:
defer-to-connect "^1.1.1" defer-to-connect "^1.1.1"
"@types/args@3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/args/-/args-3.0.0.tgz#2fd21cfbdaf5e1ed110bd239de42a82330d6ecf6"
integrity sha512-2A817ZtVj1/nD44MV0/U/R6xe3GM2n1WDdni4ioCuLjay6dE0bLJd5RafHC/ddqwXL1xa2RQUdJhsGZKyr3vpA==
"@types/cacheable-request@^6.0.1": "@types/cacheable-request@^6.0.1":
version "6.0.1" version "6.0.1"
resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.1.tgz#5d22f3dded1fd3a84c0bbeb5039a7419c2c91976" resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.1.tgz#5d22f3dded1fd3a84c0bbeb5039a7419c2c91976"
@ -605,6 +610,11 @@
dependencies: dependencies:
"@types/color-convert" "*" "@types/color-convert" "*"
"@types/columnify@^1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@types/columnify/-/columnify-1.5.0.tgz#94bb31f14c66bab6d968caa455ff13af1f0e7632"
integrity sha512-f39gUbf4NuSTVG9XEUJzYQ8L9e3l91jwywbJdU0fJixCRHKzQnyWxa9KfRPEuC3PhH5TIAi79kX2sAkXXSTB9Q==
"@types/debug@^4.1.4", "@types/debug@^4.1.5": "@types/debug@^4.1.4", "@types/debug@^4.1.5":
version "4.1.5" version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd" resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
@ -674,6 +684,11 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA== integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==
"@types/pify@3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@types/pify/-/pify-3.0.2.tgz#1bc75dac43e31dba981c37e0a08edddc1b49cd39"
integrity sha512-a5AKF1/9pCU3HGMkesgY6LsBdXHUY3WU+I2qgpU0J+I8XuJA1aFr59eS84/HP0+dxsyBSNbt+4yGI2adUpHwSg==
"@types/plist@3.0.2": "@types/plist@3.0.2":
version "3.0.2" version "3.0.2"
resolved "https://registry.yarnpkg.com/@types/plist/-/plist-3.0.2.tgz#61b3727bba0f5c462fe333542534a0c3e19ccb01" resolved "https://registry.yarnpkg.com/@types/plist/-/plist-3.0.2.tgz#61b3727bba0f5c462fe333542534a0c3e19ccb01"
@ -4247,10 +4262,10 @@ globule@^1.0.0:
lodash "~4.17.10" lodash "~4.17.10"
minimatch "~3.0.2" minimatch "~3.0.2"
got@10.0.2: got@10.0.4:
version "10.0.2" version "10.0.4"
resolved "https://registry.yarnpkg.com/got/-/got-10.0.2.tgz#f03040d966e0ff8da516d92e8689a3f9def44a3c" resolved "https://registry.yarnpkg.com/got/-/got-10.0.4.tgz#d3a5a6cafd2f6c342d562513cc2d2d7b6afdcbb4"
integrity sha512-ojjUBCvrhkbEiQRAI8OIcSsCpM+EiVEX5xaoUAS6wFGlnoNa3KnDTiavRfAWXO9x29rA7sl2igh3E7z8glq9Gg== integrity sha512-yMaRLGZJ7iINsDcZ8hso+v44IXVOejz7xrqEabSvUewdHS3zOf57IqU3sWIBYwHlekSrk+CC2PCeLzabOBxnVA==
dependencies: dependencies:
"@sindresorhus/is" "^1.0.0" "@sindresorhus/is" "^1.0.0"
"@szmarczak/http-timer" "^3.1.1" "@szmarczak/http-timer" "^3.1.1"