From f554e073e98aa5fd0a29bd3e377576fc0bd9df18 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Tue, 6 Mar 2018 08:33:59 +0200 Subject: [PATCH] initial commit --- .eslintrc | 15 +++++++++++++++ .gitignore | 1 + bin/qnm.js | 9 +++++++++ package.json | 26 ++++++++++++++++++++++++++ src/index.js | 24 ++++++++++++++++++++++++ src/modules-map.js | 41 +++++++++++++++++++++++++++++++++++++++++ src/node-module.js | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100755 bin/qnm.js create mode 100644 package.json create mode 100644 src/index.js create mode 100644 src/modules-map.js create mode 100644 src/node-module.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..13df110 --- /dev/null +++ b/.eslintrc @@ -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" + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/bin/qnm.js b/bin/qnm.js new file mode 100755 index 0000000..ba2ff38 --- /dev/null +++ b/bin/qnm.js @@ -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); diff --git a/package.json b/package.json new file mode 100644 index 0000000..4f145d8 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..128e82b --- /dev/null +++ b/src/index.js @@ -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 }); + } +}; diff --git a/src/modules-map.js b/src/modules-map.js new file mode 100644 index 0000000..601bfe0 --- /dev/null +++ b/src/modules-map.js @@ -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; + } + } +}; diff --git a/src/node-module.js b/src/node-module.js new file mode 100644 index 0000000..9fed481 --- /dev/null +++ b/src/node-module.js @@ -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; + } + } +}; +