Skip to content

Commit

Permalink
built a simple cli using caporal
Browse files Browse the repository at this point in the history
  • Loading branch information
ranyitz committed Mar 6, 2018
1 parent f554e07 commit 6830fd0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
8 changes: 1 addition & 7 deletions bin/qnm.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#! /usr/bin/env node

const NodeModules = require('../src/index');

const nm = NodeModules.loadSync();

const v = nm.getVersion(process.argv[2]);

console.log(v);
require('../src/cli');
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"description": "query node_modules directory",
"bin": "bin/qnm.js",
"main": "index.js",
"main": "workspace.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -20,7 +20,6 @@
"eslint-plugin-import": "~2.9.0"
},
"dependencies": {
"glob": "~7.1.2",
"lodash": "~4.17.5"
"caporal": "~0.10.0"
}
}
20 changes: 20 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const prog = require('caporal');
const pkg = require('../package.json');
const Workspace = require('../src/workspace');

prog
.version(pkg.version)
.argument('[module]', 'prints module version from the node_modules')
.action((args, options, logger) => {
if (!args.module) {
console.log(prog._helper.get()); // eslint-disable-line no-console
return;
}

const nm = Workspace.loadSync({ logger });
const version = nm.getVersion(args.module);
console.log(version); // eslint-disable-line no-console
});

prog.parse(process.argv);

9 changes: 5 additions & 4 deletions src/index.js → src/workspace.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const ModulesMap = require('./modules-map');

module.exports = class NodeModules {
constructor({ root, modulesMap }) {
module.exports = class Workspace {
constructor({ root, modulesMap, logger }) {
this.root = root;
this.modulesMap = modulesMap;
this.logger = logger;
}

getVersion(packageName) {
Expand All @@ -15,10 +16,10 @@ module.exports = class NodeModules {
// implement
}

static loadSync(cwd = process.cwd()) {
static loadSync({ cwd = process.cwd(), logger }) {
// TODO
// identify nodeModules recursivly
const modulesMap = ModulesMap.loadSync(cwd);
return new NodeModules({ cwd, modulesMap });
return new Workspace({ cwd, modulesMap, logger });
}
};

0 comments on commit 6830fd0

Please sign in to comment.