@hh.ru/js-module-dependency-extrator
Advanced tools
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
144099
144
11
1
+ Added@babel/core@^7.5.4
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/code-frame@7.26.2(transitive)
+ Added@babel/compat-data@7.26.2(transitive)
+ Added@babel/core@7.26.0(transitive)
+ Added@babel/generator@7.26.2(transitive)
+ Added@babel/helper-compilation-targets@7.25.9(transitive)
+ Added@babel/helper-module-imports@7.25.9(transitive)
+ Added@babel/helper-module-transforms@7.26.0(transitive)
+ Added@babel/helper-string-parser@7.25.9(transitive)
+ Added@babel/helper-validator-identifier@7.25.9(transitive)
+ Added@babel/helper-validator-option@7.25.9(transitive)
+ Added@babel/helpers@7.26.0(transitive)
+ Added@babel/parser@7.26.2(transitive)
+ Added@babel/template@7.25.9(transitive)
+ Added@babel/traverse@7.25.9(transitive)
+ Added@babel/types@7.26.0(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Addedbrowserslist@4.24.2(transitive)
+ Addedcaniuse-lite@1.0.30001679(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedelectron-to-chromium@1.5.55(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@3.0.2(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-releases@2.0.18(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedupdate-browserslist-db@1.1.1(transitive)
+ Addedyallist@3.1.1(transitive)
- Removedbabel-core@^6.26.3
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedbabel-code-frame@6.26.0(transitive)
- Removedbabel-core@6.26.3(transitive)
- Removedbabel-generator@6.26.1(transitive)
- Removedbabel-helpers@6.24.1(transitive)
- Removedbabel-messages@6.23.0(transitive)
- Removedbabel-register@6.26.0(transitive)
- Removedbabel-runtime@6.26.0(transitive)
- Removedbabel-template@6.26.0(transitive)
- Removedbabel-traverse@6.26.0(transitive)
- Removedbabel-types@6.26.0(transitive)
- Removedbabylon@6.18.0(transitive)
- Removedchalk@1.1.3(transitive)
- Removedconvert-source-map@1.9.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddetect-indent@4.0.0(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesutils@2.0.3(transitive)
- Removedglobals@9.18.0(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhome-or-tmp@2.0.0(transitive)
- Removedinvariant@2.2.4(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedjs-tokens@3.0.2(transitive)
- Removedjsesc@1.3.0(transitive)
- Removedjson5@0.5.1(transitive)
- Removedlodash@4.17.21(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedms@2.0.0(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedos-tmpdir@1.0.2(transitive)
- Removedprivate@0.1.8(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedslash@1.0.0(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedsource-map-support@0.4.18(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedto-fast-properties@1.0.3(transitive)
- Removedtrim-right@1.0.1(transitive)