Skip to content

Commit

Permalink
feat: add package-manager for packages and enhence remove command (#60)
Browse files Browse the repository at this point in the history
* refactor: update util to 1.1.0, use package-manager for installation

* feat: import package-manager for svrx load

* build: remove dependencies

* refactor: change remove help info

* build(deps-update): update @svrx/util and etc

* fix: refactor remove error message
  • Loading branch information
xuchaoying authored Nov 28, 2019
1 parent ae6da8c commit bedf04f
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 378 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"devDependencies": {
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@svrx/eslint-config": "0.0.2",
"@svrx/eslint-config": "^1.0.0",
"coveralls": "^3.0.0",
"eslint": "^6.2.0",
"expect.js": "^0.3.1",
"husky": "^1.3.1",
"husky": "^3.1.0",
"lerna": "^3.13.2",
"lerna-changelog": "^0.8.2",
"mocha": "^6.1.4",
Expand Down
111 changes: 64 additions & 47 deletions packages/svrx-cli/bin/svrx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
/* eslint-disable no-console */
const os = require('os');
const parse = require('yargs-parser');
const { logger } = require('@svrx/util');
const { PackageManagerCreator, logger, rcFileRead } = require('@svrx/util');
const updateNotifier = require('update-notifier');
const pkg = require('../package.json');
const Manager = require('../lib');

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

Expand All @@ -16,94 +15,119 @@ const printErrorAndExit = (error) => {
process.exit(1);
};

// option parse
// cli option parse
const options = parse(process.argv.slice(2));
const cmds = options._;
delete options._;

// svrx config file option read
const rcOptions = rcFileRead();

// command prepare
const manager = new Manager();
const pm = PackageManagerCreator({
version: options.svrx || rcOptions.svrx,
path: options.path || rcOptions.path,
});
const prepareSvrx = async () => {
const spinner = logger.spin('Loading svrx...');
try {
logger.debug('Loading svrx...');
manager.loadConfigFile(); // load user config file
const svrx = await Manager.loadSvrx(options);
logger.debug('Successfully loaded svrx');

return svrx;
const svrxPkg = await pm.load();
const Svrx = svrxPkg.module;
if (spinner) spinner();
return new Svrx({}, options);
} catch (e) {
if (spinner) spinner();
printErrorAndExit(e);
return null;
}
};
const commands = {
ls: {
description: 'List svrx versions installed locally',
description: 'List versions of svrx core and plugins installed locally',
exec: async () => {
try {
const versions = Manager.getLocalVersions();
const tags = await Manager.getRemoteTags();

const versions = pm.getLocalPackages();
if (versions && versions.length > 0) {
console.log('Svrx Versions Installed:\n');
console.log(versions.join(', '), '\n');
if (tags.latest.indexOf('-') === -1 && tags.latest
!== versions[versions.length - 1]) {
console.log('There is a new version of svrx, run "svrx install" to install the latest one.');
}
const versionStrs = versions.map((v) => v.version);
console.log('svrx Versions Installed:\n');
console.log(versionStrs.join(', '), '\n');
} else {
console.log('There is no svrx installed.\n');
console.log('You can install the latest version using: "svrx install".');
console.log('You can install the latest version through: "svrx install".\n');
}

const plugins = pm.getLocalPlugins();
if (plugins && plugins.length > 0) {
console.log('svrx Plugins Installed:\n');
plugins.forEach((p) => {
console.log(`${p.name}: ${p.versions.join(', ')}`);
});
}
} catch (e) {
printErrorAndExit(e);
}
},
},
'ls-remote': {
description: 'List remote svrx versions available for install',
description: 'List remote svrx core versions available for install',
exec: async () => {
const spinner = logger.spin('Searching for available versions...');
try {
const versions = await Manager.getRemoteVersions();
const tags = await Manager.getRemoteTags();
const versions = await pm.getRemotePackages();
if (spinner) spinner();

console.log('Available Svrx Versions:\n');
console.log(versions.join(', '));
console.log('\nTags:\n');
Object.keys(tags).forEach((tag) => {
console.log(`${tag}: ${tags[tag]}`);
});
console.log(versions.map((v) => v.version).join(', '));
} catch (e) {
if (spinner) spinner();
printErrorAndExit(e);
}
},
},
install: {
description: 'Download and install a specific < version > of svrx ',
description: 'Download and install a specific < version > of svrx core ',
exec: async (params = []) => {
const version = cmds.length > 0 ? params[0] : undefined;

const spinner = logger.spin('Installing svrx core package...');
try {
await Manager.install(version);
logger.notify(`Successfully installed svrx@${version || 'latest'}`);
pm.set('version', version);
const installedPkg = await pm.load();
if (spinner) spinner();
logger.notify(`Successfully installed svrx@${installedPkg.version}`);
} catch (e) {
if (spinner) spinner();
printErrorAndExit(e);
}
},
},
remove: {
description: 'Remove a specific < version > of svrx from local',
description: `Remove local packages of svrx core or plugins. eg:
svrx remove 1.0.0 (remove a core package)
svrx remove webpack (remove a plugin)
svrx remove webpack/1.0.0
svrx remove ALL (to remove all packages of svrx core and plugins)
svrx remove CORE (to remove all packages of svrx core)
svrx remove PLUGIN (to remove all packages of svrx plugins)`,
exec: async (params = []) => {
const version = cmds.length > 0 ? params[0] : undefined;
const packageToRemove = cmds.length > 0 ? params[0] : undefined;

if (!version) {
logger.notify('Please specific a version to remove, eg: svrx remove 1.0.0');
if (!packageToRemove) {
logger.notify('Please specific a package to remove, eg: svrx remove 1.0.0, svrx remove webpack');
return;
}

const name = packageToRemove === '*' ? 'all packages' : packageToRemove;
const spinner = logger.spin(`Removing ${name}...`);
try {
await Manager.remove(version);
logger.notify(`Successfully removed svrx@${version} from local`);
const isSuccess = await pm.remove(packageToRemove);
if (spinner) spinner();
if (isSuccess) {
logger.notify(`Successfully removed ${name} from local`);
} else {
logger.error(`Failed to remove ${name}`);
}
} catch (e) {
if (spinner) spinner();
printErrorAndExit(e);
}
},
Expand All @@ -112,14 +136,7 @@ const commands = {
description: 'Start a develop server. This is the default command',
exec: async () => {
const svrx = await prepareSvrx();
svrx.start(() => {
// check and install latest version @background
Manager.install(null, {
silent: true,
autoClean: true,
current: svrx.Svrx.getCurrentVersion(),
});
});
svrx.start();
},
},
};
Expand Down
53 changes: 0 additions & 53 deletions packages/svrx-cli/lib/config.js

This file was deleted.

62 changes: 0 additions & 62 deletions packages/svrx-cli/lib/index.js

This file was deleted.

59 changes: 0 additions & 59 deletions packages/svrx-cli/lib/local.js

This file was deleted.

Loading

0 comments on commit bedf04f

Please sign in to comment.