Skip to content

Commit

Permalink
added support for Webpack Config file using --webpack-config option
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Jun 24, 2018
1 parent 3775188 commit 48562dd
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 49 deletions.
67 changes: 53 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ Usage: deps-report [options] [command]
Options:
-V, --version output the version number
--json Output results in JSON format
--pretty Pretty-print JSON output (implies --json)
--abs-path Print absolute path of dependencies/dependents
--exclude-node-modules Don't consider node_modules dependencies
-h, --help output usage information
-V, --version output the version number
-j, --json Output results in JSON format
-p, --pretty Pretty-print JSON output (implies --json)
-a, --abs-path Print absolute path of dependencies/dependents
-e, --exclude-node-modules Don't consider node_modules dependencies
-w, --webpack-config [webpackConfigFile] Webpack config file for resolving aliased modules
-h, --help output usage information
Commands:
Expand Down Expand Up @@ -64,40 +65,67 @@ react
./App.css
$ deps-report --json --pretty find-dependencies tests/project-react-js-test/src/App.js
$ deps-report -jp find-dependencies tests/project-react-js-test/src/App.js
[
"react",
"./logo.svg",
"./App.css"
]
$ deps-report --json --pretty --exclude-node-modules find-dependencies tests/project-react-js-test/src/App.js
$ deps-report -jpe find-dependencies tests/project-react-js-test/src/App.js
[
"./logo.svg",
"./App.css"
]
$ deps-report --json --pretty --abs-path find-dependencies tests/project-react-js-test/src/App.js
$ deps-report -jpa find-dependencies tests/project-react-js-test/src/App.js
[
"react",
"/Users/lorenzo/Desktop/deps-report/tests/project-react-js-test/src/logo.svg",
"/Users/lorenzo/Desktop/deps-report/tests/project-react-js-test/src/App.css"
]
$ deps-report --json --pretty --exclude-node-modules --abs-path find-dependencies tests/project-react-js-test/src/App.js
$ deps-report -jpea find-dependencies tests/project-react-js-test/src/App.js
[
"/Users/lorenzo/Desktop/deps-report/tests/project-react-js-test/src/logo.svg",
"/Users/lorenzo/Desktop/deps-report/tests/project-react-js-test/src/App.css"
]
$ deps-report -jp -w tests/project-test/webpack.config.js find-dependencies tests/project-test/a.js
[
"fs",
"./c/d.js",
"Utilities/index.js",
"UtilitiesRelativePath",
"Utilities/utilityA.js",
"Templates/main.js",
"TemplatesMain",
"MyPath",
"fs",
"./e/b.js",
"./c/d.js"
]
$ deps-report -jpea -w tests/project-test/webpack.config.js find-dependencies tests/project-test/a.js
[
"/Users/lorenzo/Desktop/deps-report/tests/project-test/c/d.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-test/src/utilities/index.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-test/src/utilities/relative.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-test/src/utilities/utilityA.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-test/src/templates/main.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-test/src/templates/main.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-test/e/b.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-test/c/d.js"
]
$ deps-report find-dependencies tests/project-test/a1.ts
fs
./b.ts
./e/b.js
$ deps-report lorenzo$ deps-report --json --pretty find-dependencies tests/project-test/a1.ts
$ deps-report -jp find-dependencies tests/project-test/a1.ts
[
"fs",
"fs",
Expand All @@ -115,13 +143,13 @@ tests/project-react-js-test/src/App.test.js
tests/project-react-js-test/src/index.js
$ deps-report --json --pretty find-dependents tests/project-react-js-test/src/App.js
$ deps-report -jp find-dependents tests/project-react-js-test/src/App.js
[
"tests/project-react-js-test/src/App.test.js",
"tests/project-react-js-test/src/index.js"
]
$ deps-report --json --pretty --abs-path find-dependents tests/project-react-js-test/src/App.js
$ deps-report -jpa find-dependents tests/project-react-js-test/src/App.js
[
"/Users/lorenzo/Desktop/deps-report/tests/project-react-js-test/src/App.test.js",
"/Users/lorenzo/Desktop/deps-report/tests/project-react-js-test/src/index.js"
Expand All @@ -132,12 +160,23 @@ $ deps-report find-dependents tests/project-test/a1.ts
No dependents found!
$ deps-report --json --pretty find-dependents --root tests/project-test tests/project-test/c/d.js
$ deps-report -jp find-dependents -r tests/project-test tests/project-test/c/d.js
[
"tests/project-test/a.js",
"tests/project-test/e/b.js",
"tests/project-test/b.ts"
]
$ deps-report -w tests/project-test/webpack.config.js -jpe find-dependents -r tests/project-test/ tests/project-test/src/utilities/index.js
[
"tests/project-test/a.js"
]
$ deps-report -w tests/project-test/webpack.config.js -jpe find-dependents -r tests/project-test/ tests/project-test/src/templates/main.js
[
"tests/project-test/a.js",
"tests/project-test/src/utilities/index.js"
]
```

## License
Expand Down
14 changes: 6 additions & 8 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ commander

let dependencies = findDependencies(inputFile, options)

if (dependencies.length == 0) {
console.log('No dependencies found!')
}

if (options.parent.json) {
let jsonOutput = (options.parent.pretty) ? JSON.stringify(dependencies, null, 2) : JSON.stringify(dependencies)
console.log(jsonOutput)
}
else if (dependencies.length == 0) {
console.log('No dependencies found!')
}
else {
Array.from(new Set(dependencies)).map((filename, index) => {
console.log(filename)
Expand All @@ -45,14 +44,13 @@ commander

let dependents = findDependents(inputFile, options)

if (dependents.length == 0) {
console.log('No dependents found!')
}

if (options.parent.json) {
let jsonOutput = (options.parent.pretty) ? JSON.stringify(dependents, null, 2) : JSON.stringify(dependents)
console.log(jsonOutput)
}
else if (dependents.length == 0) {
console.log('No dependents found!')
}
else {
Array.from(new Set(dependents)).map((filename, index) => {
console.log(filename)
Expand Down
67 changes: 54 additions & 13 deletions lib/find-dependents.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function findDependents(inputFile/*: string*/, options/*: {root: string, parent:
const baseNameNoExt = path.basename(inputFile, path.extname(inputFile))
const absPath = path.resolve(inputFile)
const isTS = baseName.endsWith(".ts")
let aliasName = ""
if (options.parent.webpackConfig) {
aliasName = util.webpackFindAlias(inputFile, options.parent.webpackConfig)
}
const entries = globby.sync([
path.join(dirname, "**", "*.js"),
path.join(dirname, "**", "*.ts"),
Expand All @@ -26,14 +30,18 @@ function findDependents(inputFile/*: string*/, options/*: {root: string, parent:
for (let entry of entries) {

let data /*: string */ = fs.readFileSync(entry, 'utf8')


// Apply a first filter to exclude some files:
// Don't consider js files where there is no import/require of inputFile
if (data.indexOf(baseName+'\"') == -1 && data.indexOf(baseNameNoExt+'\"') == -1 &&
if ( (data.indexOf(baseName+'\"') == -1 && data.indexOf(baseNameNoExt+'\"') == -1 &&
data.indexOf(baseName+'\'') == -1 && data.indexOf(baseNameNoExt+'\'') == -1 &&
data.indexOf(baseName+'\`') == -1 && data.indexOf(baseNameNoExt+'\`') == -1) {
data.indexOf(baseName+'\`') == -1 && data.indexOf(baseNameNoExt+'\`') == -1) &&
(aliasName && (data.indexOf('\"'+aliasName) == -1 &&
data.indexOf('\''+aliasName) == -1 &&
data.indexOf('\`'+aliasName) == -1)) ) {
continue
}

let ast = {}
let imports = []
let isEntryTS = false
Expand All @@ -60,6 +68,9 @@ function findDependents(inputFile/*: string*/, options/*: {root: string, parent:

for (let imp /*: Object*/ of imports) {
let dependent = ""
let dependentExt = ""
let dependentAbsPath = ""
let dependentAbsPathExt = ""

if (isEntryTS) {
if (imp.kind == typescript.SyntaxKind.VariableDeclaration &&
Expand All @@ -85,16 +96,46 @@ function findDependents(inputFile/*: string*/, options/*: {root: string, parent:
dependent = imp.source.value
}
}
let dependentAbsPath = path.resolve(path.join(path.dirname(entry), dependent))
dependentAbsPath = (dependentAbsPath.endsWith( (isTS) ? '.ts' : '.js' ) ) ? dependentAbsPath : (dependentAbsPath.replace(/(\.js|\.ts)/, '') + ((isTS) ? '.ts' : '.js'))
if (!fs.existsSync(dependentAbsPath))
continue

if (dependent &&
( dependent.startsWith(".") || dependent.startsWith("/") || dependent.startsWith(":", 1) ) &&
( dependentAbsPath == absPath) &&
( dependent.endsWith(path.basename(inputFile)) || dependent.endsWith(baseNameNoExt) ) )
{
if (dependent) {
let webpackAliasResolved = {}

if (options.parent.webpackConfig) {
webpackAliasResolved = util.webpackAliasResolver(dependent, options.parent.webpackConfig)
dependent = webpackAliasResolved.module
dependentAbsPath = webpackAliasResolved.moduleAbsPath
}

if (webpackAliasResolved.isWebpackError)
continue

dependentAbsPath = (!dependentAbsPath) ? path.resolve(path.join(path.dirname(entry), dependent)) : dependentAbsPath
dependentAbsPathExt = path.extname(dependentAbsPath)
if (!dependentAbsPathExt) {
if ( isTS && fs.existsSync( dependentAbsPath + '.ts' ) ) {
dependentAbsPath += '.ts'
dependentAbsPathExt = 'ts'
}
else if (fs.existsSync(dependentAbsPath + '.js')) {
dependentAbsPath += '.js'
dependentAbsPathExt = 'js'
}
}

dependentExt = path.extname(dependent)
if (!webpackAliasResolved.keepRelative && !dependentExt) {
if ( isTS && dependentAbsPathExt == 'ts' && fs.existsSync(dependentAbsPath) ) {
dependent += '.ts'
dependentExt = 'ts'
}
else if (dependentAbsPathExt == 'js' && fs.existsSync(dependentAbsPath)) {
dependent += '.js'
dependentExt = 'js'
}
}
}

if (dependentAbsPath == absPath) {
dependents.push( (options.parent.absPath) ? path.resolve(entry) : entry )
break
}
Expand Down
68 changes: 64 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ function webpackAliasResolver(mod/*: string */, webpackConfigFile/*: string*/)/*

if (result.webpackConfig.resolve && result.webpackConfig.resolve.alias) {
for (let alias /*: string*/ in result.webpackConfig.resolve.alias) {
let modulePath /*: string*/ = result.webpackConfig.resolve.alias[alias]
let aliasAbsPath = webpackAliasResolveAbsPath(alias, webpackConfigFile)
let normalizedAliasName = (alias[alias.length - 1] == "$") ? alias.substring(0, alias.length - 1) : alias
let isExactMatch = mod == alias.substring(0, alias.length - 1)

if (isExactMatch || mod == alias || (mod+path.sep).indexOf(normalizedAliasName+path.sep) >= 0 ) {
result.moduleAbsPath = path.normalize(mod.replace(normalizedAliasName, modulePath))
if (mod == alias && path.extname(modulePath)) {
result.moduleAbsPath = path.normalize(mod.replace(normalizedAliasName, aliasAbsPath))
if (mod == alias && path.extname(aliasAbsPath)) {
result.keepRelative = true
}

Expand Down Expand Up @@ -110,8 +110,68 @@ function webpackAliasResolver(mod/*: string */, webpackConfigFile/*: string*/)/*
return result
}

function webpackFindAlias(modulePath/*: string*/, webpackConfigFile/*: string*/)/*: string*/ {

if (!fs.existsSync(webpackConfigFile)) {
throw new Error(webpackConfigFile + ' webpack config file doesn\'t exists.')
}

let moduleAbsPath = path.resolve(modulePath)

// $FlowFixMe
let result = require(path.resolve(webpackConfigFile))
if (result.resolve && result.resolve.alias) {
for (let alias /*: string*/ in result.resolve.alias) {
let aliasAbsPath = webpackAliasResolveAbsPath(alias, webpackConfigFile)

if (fs.existsSync(aliasAbsPath) && fs.lstatSync(aliasAbsPath).isDirectory()) {
// points to main attribute of package.json, default is index.js
let mainAttrPackageJson = 'index.js'
let packageJsonModule = path.join(aliasAbsPath, 'package.json')
packageJsonModule = (fs.existsSync(packageJsonModule)) ? JSON.parse(fs.readFileSync(packageJsonModule, 'utf8')) : null
if (packageJsonModule && packageJsonModule.main) {
mainAttrPackageJson = packageJsonModule.main
}
aliasAbsPath = path.join(aliasAbsPath, mainAttrPackageJson)
}

if (moduleAbsPath == aliasAbsPath) {
let normalizedAliasName = (alias[alias.length - 1] == "$") ? alias.substring(0, alias.length - 1) : alias
return normalizedAliasName
}
}
}

return ""
}

function webpackAliasResolveAbsPath(alias/*: string*/, webpackConfigFile/*: string*/)/*: string*/ {
let aliasAbsPath = ""
// $FlowFixMe
let webpackConfig = require(path.resolve(webpackConfigFile))
if (path.isAbsolute(webpackConfig.resolve.alias[alias])) {
aliasAbsPath = webpackConfig.resolve.alias[alias]
}
else {
if (webpackConfig.resolve.modules) {
for (let mod /*: string*/ of webpackConfig.resolve.modules) {
let modPath = (path.isAbsolute(mod)) ? mod : path.resolve(mod)
if (fs.existsSync(modPath)) {
aliasAbsPath = path.join( modPath, webpackConfig.resolve.alias[alias] )
break
}
}
}
else {
aliasAbsPath = path.join(path.dirname(path.resolve(webpackConfigFile)), webpackConfig.resolve.alias[alias])
}
}
return aliasAbsPath
}

module.exports = {
flowTraverseAST,
typescriptTraverseAST,
webpackAliasResolver
webpackAliasResolver,
webpackFindAlias
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deps-report",
"version": "0.1.2",
"version": "0.2.0",
"description": "Generate reports about dependencies and dependents of your JavaScript/TypeScript files through an AST. It supports import and require statements.",
"keywords": [
"dependencies",
Expand Down
2 changes: 2 additions & 0 deletions tests/project-test/a.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import d from './c/d.js'
// $FlowFixMe
import Utilities from 'Utilities'
// $FlowFixMe
import UtilitiesRelativePath from 'UtilitiesRelativePath'
// $FlowFixMe
import utilityA from 'Utilities/utilityA'
// $FlowFixMe
import templates from 'Templates'
Expand Down
3 changes: 3 additions & 0 deletions tests/project-test/src/utilities/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @flow

// $FlowFixMe
import templates from 'Templates'

module.exports = {}
3 changes: 3 additions & 0 deletions tests/project-test/src/utilities/relative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow

module.exports = {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
resolve: {
alias: {
Utilities: path.resolve(__dirname, path.join('src','utilities')),
UtilitiesRelativePath: path.join('src','utilities', 'relative.js'),
Templates: path.resolve(__dirname, path.join('src','templates')),
TemplatesMain: path.resolve(__dirname, path.join('src','templates', 'main.js')),
TemplatesMain$: path.resolve(__dirname, path.join('src','templates', 'main.js')),
Expand Down
Loading

0 comments on commit 48562dd

Please sign in to comment.