-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f554e07
Showing
7 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "airbnb-base", | ||
"env": { | ||
"jest": true, | ||
"node": true | ||
}, | ||
"rules": { | ||
"no-console": "warn", | ||
"no-plusplus": "off", | ||
"no-restricted-syntax": "off", | ||
"no-underscore-dangle": "off", | ||
"guard-for-in": "off", | ||
"no-await-in-loop": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#! /usr/bin/env node | ||
|
||
const NodeModules = require('../src/index'); | ||
|
||
const nm = NodeModules.loadSync(); | ||
|
||
const v = nm.getVersion(process.argv[2]); | ||
|
||
console.log(v); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "qnm", | ||
"version": "0.1.0", | ||
"description": "query node_modules directory", | ||
"bin": "bin/qnm.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"query", | ||
"node_modules", | ||
"version" | ||
], | ||
"author": "Ran Yitzhaki", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"eslint": "~4.18.2", | ||
"eslint-config-airbnb-base": "~12.1.0", | ||
"eslint-plugin-import": "~2.9.0" | ||
}, | ||
"dependencies": { | ||
"glob": "~7.1.2", | ||
"lodash": "~4.17.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const ModulesMap = require('./modules-map'); | ||
|
||
module.exports = class NodeModules { | ||
constructor({ root, modulesMap }) { | ||
this.root = root; | ||
this.modulesMap = modulesMap; | ||
} | ||
|
||
getVersion(packageName) { | ||
return this.modulesMap.getModule(packageName).version; | ||
} | ||
|
||
static load(from) { | ||
// TODO | ||
// implement | ||
} | ||
|
||
static loadSync(cwd = process.cwd()) { | ||
// TODO | ||
// identify nodeModules recursivly | ||
const modulesMap = ModulesMap.loadSync(cwd); | ||
return new NodeModules({ cwd, modulesMap }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const NodeModule = require('./node-module'); | ||
|
||
const isNotHiddenDirectory = dirname => !dirname.startsWith('.'); | ||
|
||
module.exports = class ModulesMap extends Map { | ||
constructor({ rawModulesMap, root }) { | ||
super(rawModulesMap); | ||
this.root = root; | ||
} | ||
|
||
getModule(name) { | ||
const m = this.get(name); | ||
|
||
if (!m) { | ||
throw new Error(`The node module "${name}" does not exist in ${this.root}`); | ||
} | ||
|
||
return m; | ||
} | ||
|
||
static loadSync(root) { | ||
const nodeModulesRoot = path.resolve(root, 'node_modules'); | ||
|
||
try { | ||
const modulesNames = fs.readdirSync(nodeModulesRoot).filter(isNotHiddenDirectory); | ||
|
||
const rawModulesMap = modulesNames.map(name => | ||
[name, NodeModule.loadSync(nodeModulesRoot, name)]); | ||
|
||
return new ModulesMap({ rawModulesMap, root: nodeModulesRoot }); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
throw new Error(`couldn't find node_modules directory in path ${root}`); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = class NodeModule { | ||
constructor({ packageJson, nodeModulesRoot, name }) { | ||
this.name = name; | ||
this.packageJson = packageJson; | ||
this.nodeModulesRoot = nodeModulesRoot; | ||
} | ||
|
||
get version() { | ||
return this.packageJson.version; | ||
} | ||
|
||
static load() { | ||
// TODO | ||
} | ||
|
||
static loadSync(nodeModulesRoot, name) { | ||
const packageJsonPath = path.resolve(nodeModulesRoot, name, 'package.json'); | ||
try { | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); | ||
return new NodeModule({ packageJson, nodeModulesRoot, name }); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
throw new Error(`Couldn't find "package.json" in path ${packageJsonPath}`); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
}; | ||
|