Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hh.ru/js-module-dependency-extrator

Package Overview
Dependencies
Maintainers
9
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hh.ru/js-module-dependency-extrator - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

155

lib/index.js

@@ -1,46 +0,46 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.extractModuleDependenciesFromFile = undefined;
exports["default"] = exports.extractModuleDependenciesFromFile = void 0;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _fs = _interopRequireDefault(require("fs"));
var _fs = require('fs');
var _path = _interopRequireDefault(require("path"));
var _fs2 = _interopRequireDefault(_fs);
var babylon = _interopRequireWildcard(require("@babel/parser"));
var _path = require('path');
var _traverse = _interopRequireDefault(require("@babel/traverse"));
var _path2 = _interopRequireDefault(_path);
var _globAll = _interopRequireDefault(require("glob-all"));
var _babylon = require('babylon');
var _traverser = _interopRequireDefault(require("./traverser"));
var babylon = _interopRequireWildcard(_babylon);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
var _babelTraverse = require('babel-traverse');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
var _globAll = require('glob-all');
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
var _globAll2 = _interopRequireDefault(_globAll);
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
var _traverser = require('./traverser');
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
var _traverser2 = _interopRequireDefault(_traverser);
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { keys.push.apply(keys, Object.getOwnPropertySymbols(object)); } if (enumerableOnly) keys = keys.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); return keys; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var ENCODING = 'utf8';
var BABEL_PARSING_OPTS = {
sourceType: 'module',
plugins: ['jsx', 'flow', 'doExpressions', 'objectRestSpread', 'decorators', 'classProperties', 'exportExtensions', 'asyncGenerators', 'functionBind', 'functionSent', 'dynamicImport']
sourceType: 'module',
plugins: ['jsx', 'flow', 'doExpressions', 'objectRestSpread', 'decorators-legacy', 'classProperties', 'exportExtensions', 'asyncGenerators', 'functionBind', 'functionSent', 'dynamicImport']
};
var noop = function noop() {};
var jsExtLength = '.js'.length;

@@ -51,76 +51,77 @@ var mustacheExt = '.mustache';

var extractModuleDependenciesFromCode = function extractModuleDependenciesFromCode(code) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
try {
var ast = babylon.parse(code.toString(ENCODING), BABEL_PARSING_OPTS);
var traverser = (0, _traverser2.default)(cb, opts);
try {
var ast = babylon.parse(code.toString(ENCODING), BABEL_PARSING_OPTS);
var traverser = (0, _traverser["default"])(cb, opts);
(0, _traverse["default"])(ast, traverser);
} catch (ignore) {}
};
(0, _babelTraverse2.default)(ast, traverser);
} catch (ignore) {}
var extractModuleDependenciesFromFile = function extractModuleDependenciesFromFile(file) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
extractModuleDependenciesFromCode(_fs["default"].readFileSync(file, ENCODING), _objectSpread({}, opts, {
filename: file
}), cb);
};
var extractModuleDependenciesFromFile = exports.extractModuleDependenciesFromFile = function extractModuleDependenciesFromFile(file) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
exports.extractModuleDependenciesFromFile = extractModuleDependenciesFromFile;
extractModuleDependenciesFromCode(_fs2.default.readFileSync(file, ENCODING), _extends({}, opts, {
filename: file
}), cb);
};
var extractModuleTreeDependenciesFromFile = function extractModuleTreeDependenciesFromFile(file) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cachedFiles = {};
var dependencyList = [];
var cachedFiles = {};
var dependencyList = [];
function _extractModuleTreeDependenciesFromFile(file, opts) {
var moduleDependecies = [];
extractModuleDependenciesFromFile(file, opts, function (_moduleDependecies) {
dependencyList = dependencyList.concat(_moduleDependecies);
moduleDependecies = _moduleDependecies;
});
moduleDependecies.forEach(function (file) {
if (!cachedFiles[file]) {
_extractModuleTreeDependenciesFromFile(file, opts);
function _extractModuleTreeDependenciesFromFile(file, opts) {
var moduleDependecies = [];
cachedFiles[file] = true;
}
});
}
extractModuleDependenciesFromFile(file, opts, function (_moduleDependecies) {
dependencyList = dependencyList.concat(_moduleDependecies);
moduleDependecies = _moduleDependecies;
});
_extractModuleTreeDependenciesFromFile(file, opts);
moduleDependecies.forEach(function (file) {
if (!cachedFiles[file]) {
_extractModuleTreeDependenciesFromFile(file, opts);
cachedFiles[file] = true;
}
});
}
return _toConsumableArray(new Set(dependencyList));
};
_extractModuleTreeDependenciesFromFile(file, opts);
var _default = function _default(globArr) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return [].concat(_toConsumableArray(new Set(dependencyList)));
};
var saveFilePath = _path["default"].resolve(opts.saveFilePath);
exports.default = function (globArr) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var previousContent;
var saveFilePath = _path2.default.resolve(opts.saveFilePath);
var previousContent = void 0;
var staticValues = _globAll["default"].sync(globArr).reduce(function (dependencies, file) {
var staticValues = extractModuleTreeDependenciesFromFile(file, opts);
dependencies = dependencies.concat([file], staticValues);
return dependencies;
}, []);
var staticValues = _globAll2.default.sync(globArr).reduce(function (dependencies, file) {
var staticValues = extractModuleTreeDependenciesFromFile(file, opts);
dependencies = dependencies.concat([file], staticValues);
return dependencies;
}, []);
var modulesPath = _path["default"].resolve(__dirname, opts.modulesPath);
var modulesPath = _path2.default.resolve(__dirname, opts.modulesPath);
staticValues = _toConsumableArray(new Set(staticValues.map(function (item) {
return item.slice(modulesPath.length + backslashLength);
})));
staticValues = [].concat(_toConsumableArray(new Set(staticValues.map(function (item) {
return item.slice(modulesPath.length + backslashLength);
}))));
if (_fs["default"].existsSync("".concat(saveFilePath, "/").concat(opts.saveFileName, ".").concat(opts.saveFileExt))) {
previousContent = _fs["default"].readFileSync("".concat(saveFilePath, "/").concat(opts.saveFileName, ".").concat(opts.saveFileExt), ENCODING).toString();
}
if (_fs2.default.existsSync(saveFilePath + '/' + opts.saveFileName + '.' + opts.saveFileExt)) {
previousContent = _fs2.default.readFileSync(saveFilePath + '/' + opts.saveFileName + '.' + opts.saveFileExt, ENCODING).toString();
}
var content = opts.template ? opts.template(staticValues) : JSON.stringify(staticValues);
var content = opts.template ? opts.template(staticValues) : JSON.stringify(staticValues);
if (content !== previousContent) {
_fs["default"].writeFileSync("".concat(saveFilePath, "/").concat(opts.saveFileName, ".").concat(opts.saveFileExt), content);
}
};
if (content !== previousContent) {
_fs2.default.writeFileSync(saveFilePath + '/' + opts.saveFileName + '.' + opts.saveFileExt, content);
}
};
exports["default"] = _default;

@@ -1,71 +0,70 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports["default"] = void 0;
var _fs = require('fs');
var _fs = _interopRequireDefault(require("fs"));
var _fs2 = _interopRequireDefault(_fs);
var _path = _interopRequireDefault(require("path"));
var _path = require('path');
var _core = require("@babel/core");
var _path2 = _interopRequireDefault(_path);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _babelCore = require('babel-core');
var _default = function _default(cb) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var modulesPath = opts.modulesPath,
filename = opts.filename;
var importDeclarationPaths = [];
return {
Program: {
exit: function exit() {
cb(importDeclarationPaths);
}
},
CallExpression: {
enter: function enter(_ref) {
var node = _ref.node;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
if (_core.types.isCallExpression(node) && node.callee.name === 'define') {
var dependencies = node.arguments[0].elements.reduce(function (result, node) {
var ext = node.value.endsWith('mustache') ? '' : '.js';
exports.default = function (cb) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var modulesPath = opts.modulesPath,
filename = opts.filename;
var pathModule = _path["default"].join(modulesPath, "".concat(node.value).concat(ext));
var importDeclarationPaths = [];
return {
Program: {
exit: function exit() {
cb(importDeclarationPaths);
if (_fs["default"].existsSync(pathModule)) {
result.push(pathModule);
}
},
CallExpression: {
enter: function enter(_ref) {
var node = _ref.node;
return result;
}, []);
importDeclarationPaths = importDeclarationPaths.concat(dependencies);
}
}
},
ImportDeclaration: {
enter: function enter(_ref2) {
var node = _ref2.node;
if (_babelCore.types.isCallExpression(node) && node.callee.name === 'define') {
var dependencies = node.arguments[0].elements.reduce(function (result, node) {
var ext = node.value.endsWith('mustache') ? '' : '.js';
var pathModule = _path2.default.join(modulesPath, '' + node.value + ext);
if (_fs2.default.existsSync(pathModule)) {
result.push(pathModule);
}
if (node && node.source && node.source.value) {
var moduleDir = node.source.value[0] === '.' ? filename.slice(modulesPath.length, filename.lastIndexOf('/')) : '';
var ext = node.source.value.endsWith('mustache') ? '' : '.js';
return result;
}, []);
var jsModule = _path["default"].join(modulesPath, moduleDir, "".concat(node.source.value).concat(ext));
importDeclarationPaths = importDeclarationPaths.concat(dependencies);
}
}
},
var jsIndexModule = _path["default"].join(modulesPath, moduleDir, "".concat(node.source.value, "/index").concat(ext));
ImportDeclaration: {
enter: function enter(_ref2) {
var node = _ref2.node;
if (_fs["default"].existsSync(jsModule)) {
importDeclarationPaths = importDeclarationPaths.concat(jsModule);
} else if (_fs["default"].existsSync(jsIndexModule)) {
importDeclarationPaths = importDeclarationPaths.concat(jsIndexModule);
}
}
}
}
};
};
if (node && node.source && node.source.value) {
var moduleDir = node.source.value[0] === '.' ? filename.slice(modulesPath.length, filename.lastIndexOf('/')) : '';
var ext = node.source.value.endsWith('mustache') ? '' : '.js';
var jsModule = _path2.default.join(modulesPath, moduleDir, '' + node.source.value + ext);
var jsIndexModule = _path2.default.join(modulesPath, moduleDir, node.source.value + '/index' + ext);
if (_fs2.default.existsSync(jsModule)) {
importDeclarationPaths = importDeclarationPaths.concat(jsModule);
} else if (_fs2.default.existsSync(jsIndexModule)) {
importDeclarationPaths = importDeclarationPaths.concat(jsIndexModule);
}
}
}
}
};
};
exports["default"] = _default;
{
"name": "@hh.ru/js-module-dependency-extrator",
"version": "0.1.0",
"version": "0.1.1",
"main": "lib/index.js",
"devDependencies": {
"@babel/cli": "^7.5.0",
"@babel/plugin-proposal-function-bind": "^7.2.0",
"@babel/preset-env": "^7.5.4",
"@babel/register": "^7.5.5",
"@hh.ru/eslint-config": "^3.0.1",
"babel-cli": "^6.26.0",
"babel-eslint": "^9.0.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-0": "^6.24.1",
"eslint": "~4.19.1",

@@ -25,3 +26,3 @@ "eslint-config-prettier": "^3.0.1",

"build": "babel src -d lib",
"test": "mocha --compilers js:babel-register",
"test": "mocha --compilers js:@babel/register",
"test:watch": "yarn run test -- --watch",

@@ -34,3 +35,3 @@ "prepublish": "yarn clean && yarn build"

"dependencies": {
"babel-core": "^6.26.3",
"@babel/core": "^7.5.4",
"del": "^3.0.0",

@@ -37,0 +38,0 @@ "glob-all": "^3.1.0"

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc