feat: added ts dependencies support

This commit is contained in:
Jean Baptiste Camaret
2019-04-02 08:33:05 +02:00
parent 2ac23c86ea
commit 662291458f
48 changed files with 937 additions and 48 deletions

View File

@@ -37,7 +37,7 @@ const parseFile = (
item.flows = undefined;
}
}
if (parseImports) {
const importedDependencies = dependenciesParser.getImports(code, itemPath);
if (importedDependencies.length) {

View File

@@ -1,39 +0,0 @@
// TODO: add by config
/**
[
'typescript',
'flow',
'bigInt',
]
*/
const config = {
sourceType: 'module',
plugins: [
'jsx',
'asyncGenerators',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'importMeta',
'logicalAssignment',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding',
'optionalChaining',
'throwExpressions'
]
};
const getNodeLines = node => [node.loc.start.line, node.loc.end.line];
module.exports = {
config,
getNodeLines
};

View File

@@ -1,43 +1,7 @@
const path = require('path');
const madge = require('madge');
const babylon = require('@babel/parser');
const babelTraverse = require('@babel/traverse');
const { config: astParseConfig, getNodeLines } = require('./astParse');
const { convertRelativeToAbsolutePath } = require('../../path');
const getImports = (fileCode, itemPath) => {
let ast = {};
const importedDependencies = [];
//TODO: move to one file in util, to keep config in one place
try {
ast = babylon.parse(fileCode, astParseConfig);
// TODO: combine with codecrumbs babelTraverse, no need to do babelTraverse twice per file
babelTraverse.default(ast, {
enter(path) {
const node = path.node;
if (node.type === 'ImportDeclaration') {
importedDependencies.push({
importNodeLines: getNodeLines(node),
sourceFile: node.source && convertRelativeToAbsolutePath(itemPath, node.source.value),
specifiers: node.specifiers.map(({ type, imported, local }) => ({
type,
name: (imported || local || {}).name
}))
});
}
}
});
return importedDependencies.filter(({ sourceFile }) => !!sourceFile);
} catch (e) {
console.log(itemPath, e);
return importedDependencies;
}
};
const jsDependencies = require('../../../utils/jsDependencies');
const getDependencies = (entryPoint, projectDir, webpackConfigPath) => {
const rootPath = path.resolve();
@@ -67,6 +31,6 @@ const getDependencies = (entryPoint, projectDir, webpackConfigPath) => {
};
module.exports = {
getImports,
getImports: jsDependencies.getImports,
getDependencies
};

View File

@@ -1,7 +1,37 @@
const defaultDependencies = require('../default/dependencies');
const path = require('path');
const madge = require('madge');
// replace with own implementation if needed
module.exports = {
getImports: defaultDependencies.getImports,
getDependencies: defaultDependencies.getDependencies
const jsDependencies = require('../../../utils/jsDependencies');
const getDependencies = (entryPoint, projectDir, webpackConfigPath, tsConfig = {}) => {
// TODO: investigated usage for tsConfig param
const rootPath = path.resolve();
const separator = path.sep;
return madge(entryPoint, {
webpackConfig: webpackConfigPath,
baseDir: projectDir,
// TODO: this filter will be extended based on how much dependencies is needed
dependencyFilter: (depPath, sourcePath) => sourcePath === `${rootPath}${separator}${entryPoint}`
})
.then(res => res.obj())
.then(obj =>
Object.entries(obj).reduce((tree, [key, value]) => {
const moduleName = `${projectDir}${separator}${key.replace(/\//g, separator)}`;
tree[moduleName] = {
moduleName,
importedModuleNames: value.map(
v => `${projectDir}${separator}${v.replace(/\//g, separator)}`
)
};
return tree;
}, {})
);
};
module.exports = {
getImports: jsDependencies.getImports,
getDependencies
};