Skip to content

Commit

Permalink
feat: config save relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-p committed Nov 19, 2020
1 parent bf1335a commit e4bf3a8
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 265 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ insert_final_newline = true
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
indent_style = tab
4 changes: 2 additions & 2 deletions .todo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
[x] feat: status view get error only one time
[x] feat: catch get dependency tree error
[x] feat: analyze javascript file and get dependency tree all use babel
[x] feat: use webpack package vsce
[x] feat: config save relative path
[ ] feat: file information view drag to resize
[ ] feat: change get dependency tree as a module
[ ] refactor: change analyze javascript file as plugin to get dependency tree
Expand All @@ -22,9 +24,7 @@
[ ] feat: save file node
[ ] feat: webview svgBox support resize
[ ] feat: node with hash
[ ] feat: use webpack package vsce
[ ] feat: if main of package.json is unequal with config use main
[ ] feat: config save relative path

TODO
[ ] feat: monaco editor highlight(can not use monaco language server in webview https://github.com/microsoft/vscode/issues/87282)
Expand Down
117 changes: 59 additions & 58 deletions src/data-dependencyTree/data-dependencyTree.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,81 @@
import * as path from 'path';
import { DependencyTreeData } from './dependencyTreeData';
import * as dependencyTree from 'dependency-tree';
import { analysesFile } from '../fileAnalysis/javascript/javascriptAnalysis';

import {
statusMsgGetFolderPath,
statusMsgGetPackageJsonPath,
statusMsgGetEntryFile,
statusMsgGetDependencyData,
statusMsgGetDependencyProcessData
statusMsgGetFolderPath,
statusMsgGetPackageJsonPath,
statusMsgGetEntryFile,
statusMsgGetDependencyData,
statusMsgGetDependencyProcessData
} from '../utils/message/messages';

import {
getPackageJsonPath,
getMainFilePath,
getDependencyTree,
processTreeData,
getCurrentFolderPath
getPackageJsonPath,
getMainFilePath,
getDependencyTree,
processTreeData,
getCurrentFolderPath
} from './dependencyTreeMethods';

import { getEntryFilePath } from '../utils/config';
import { getEntryFileRelativePath } from '../utils/config';
import { onError } from '../utils/error/onError';
import {
NO_DEPENDENCY,
NO_FOLDER,
NO_PACKAGE_JSON,
NO_MAIN_FILE,
GET_DEPENDENCY_TREE_FAIL
NO_DEPENDENCY,
NO_FOLDER,
NO_PACKAGE_JSON,
NO_MAIN_FILE,
GET_DEPENDENCY_TREE_FAIL
} from '../utils/error/errorKey';

import { pathExists } from '../utils/utils';

export const getDependencyTreeData = (postMessage?: boolean): DependencyTreeData | undefined => {
// find folder Path catch path sendStatus
const folderPath = getCurrentFolderPath();
if (!folderPath || !pathExists(folderPath)) {
onError(NO_FOLDER);
postMessage ? statusMsgGetFolderPath.postError() : null;
return undefined;
}
postMessage ? statusMsgGetFolderPath.postSuccess() : null;
// find folder Path catch path sendStatus
const folderPath = getCurrentFolderPath();
if (!folderPath || !pathExists(folderPath)) {
onError(NO_FOLDER);
postMessage ? statusMsgGetFolderPath.postError() : null;
return undefined;
}
postMessage ? statusMsgGetFolderPath.postSuccess() : null;

// find cached path sendStatus
let mainFilePath = getEntryFilePath();
if (mainFilePath === undefined) {
// find package.json and main file
const packageJsonPath = getPackageJsonPath(folderPath);
if (!packageJsonPath) {
onError(NO_PACKAGE_JSON);
postMessage ? statusMsgGetPackageJsonPath.postError() : null;
return undefined;
}
postMessage ? statusMsgGetPackageJsonPath.postSuccess() : null;
mainFilePath = getMainFilePath(folderPath, packageJsonPath);
}
if (!mainFilePath || !pathExists(mainFilePath)) {
onError(NO_MAIN_FILE);
postMessage ? statusMsgGetEntryFile.postError() : null;
return undefined;
}
postMessage ? statusMsgGetEntryFile.postSuccess() : null;
// find cached path sendStatus
let mainFilePath = getEntryFileRelativePath();
if (mainFilePath === undefined) {
// find package.json and main file
const packageJsonPath = getPackageJsonPath(folderPath);
if (!packageJsonPath) {
onError(NO_PACKAGE_JSON);
postMessage ? statusMsgGetPackageJsonPath.postError() : null;
return undefined;
}
postMessage ? statusMsgGetPackageJsonPath.postSuccess() : null;
mainFilePath = getMainFilePath(folderPath, packageJsonPath);
}
if (!mainFilePath || !pathExists(path.join(folderPath, mainFilePath))) {
onError(NO_MAIN_FILE);
postMessage ? statusMsgGetEntryFile.postError() : null;
return undefined;
}
postMessage ? statusMsgGetEntryFile.postSuccess() : null;

const { dependencyTree: processedTreeData, dependencyHash } = analysesFile(mainFilePath, folderPath);
// const dependencyTreeData = getDependencyTree(mainFilePath, folderPath);
// if (!dependencyTreeData || !Object.keys(dependencyTreeData as dependencyTree.DependencyObj).length) {
// onError(GET_DEPENDENCY_TREE_FAIL);
// postMessage ? statusMsgGetDependencyData.postError() : null;
// return undefined;
// }
// postMessage ? statusMsgGetDependencyData.postSuccess() : null;
const { dependencyTree: processedTreeData, dependencyHash } = analysesFile(path.join(folderPath, mainFilePath), folderPath);
// const dependencyTreeData = getDependencyTree(mainFilePath, folderPath);
// if (!dependencyTreeData || !Object.keys(dependencyTreeData as dependencyTree.DependencyObj).length) {
// onError(GET_DEPENDENCY_TREE_FAIL);
// postMessage ? statusMsgGetDependencyData.postError() : null;
// return undefined;
// }
// postMessage ? statusMsgGetDependencyData.postSuccess() : null;

// const processedTreeData = processTreeData(dependencyTreeData as dependencyTree.DependencyObj, folderPath);
if (!processedTreeData) {
onError(NO_DEPENDENCY);
postMessage ? statusMsgGetDependencyProcessData.postError() : null;
}
postMessage ? statusMsgGetDependencyProcessData.postSuccess() : null;
// const processedTreeData = processTreeData(dependencyTreeData as dependencyTree.DependencyObj, folderPath);
if (!processedTreeData) {
onError(NO_DEPENDENCY);
postMessage ? statusMsgGetDependencyProcessData.postError() : null;
}
postMessage ? statusMsgGetDependencyProcessData.postSuccess() : null;

return processedTreeData;
return processedTreeData;
};
206 changes: 105 additions & 101 deletions src/data-dependencyTree/dependencyTreeMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ import * as vscode from 'vscode';

import { getFileIconNameByFileName } from '../utils/fileIcons/getFileIcon';
import { DependencyTreeData } from './dependencyTreeData';
import { setEntryFilePath, getCurrentFolderPath as getFolderPathByConfig, setCurrentFolderPath } from '../utils/config';
import { setEntryFileRelativePath } from '../utils/config';

import { analysesFile } from '../fileAnalysis/javascript/javascriptAnalysis';

export const getPackageJsonPath = function(folderPath: string): string | undefined {
const files = fs.readdirSync(folderPath);
if (files.includes('package.json')) {
return folderPath + '/package.json';
} else {
return undefined;
}
export const getPackageJsonPath = function (folderPath: string): string | undefined {
const files = fs.readdirSync(folderPath);
if (files.includes('package.json')) {
return folderPath + '/package.json';
} else {
return undefined;
}
};

export const getMainFilePath = function(folderPath: string, packageJsonPath: string): string | undefined {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
// const packageJson = require(packageJsonPath);
if (packageJson.main) {
const mainFilePath = path.join(folderPath, packageJson.main);
setEntryFilePath(mainFilePath);
return mainFilePath;
} else {
return undefined;
}
export const getMainFilePath = function (folderPath: string, packageJsonPath: string): string | undefined {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
// const packageJson = require(packageJsonPath);
if (packageJson.main) {
const mainFilePath = path.join(packageJson.main);
setEntryFileRelativePath(mainFilePath);
return mainFilePath;
} else {
return undefined;
}
};
export const getDependencyTree = function(
filename: string,
directory: string
export const getDependencyTree = function (
filename: string,
directory: string
): dependencyTree.DependencyObj | undefined {
let tree = undefined;
try {
// tree = getdependencytree({ entry: filename }).tree;
// tree = dependencyTree({
// filter: (path: string) => path.indexOf('node_modules') === -1,
// filename: filename,
// directory: directory
// });
} catch (err) {
return undefined;
}
let tree = undefined;
try {
// tree = getdependencytree({ entry: filename }).tree;
// tree = dependencyTree({
// filter: (path: string) => path.indexOf('node_modules') === -1,
// filename: filename,
// directory: directory
// });
} catch (err) {
return undefined;
}

return tree;
return tree;
};

/**
Expand All @@ -55,74 +55,78 @@ export const getDependencyTree = function(
*
* @returns {(String | undefined)}
*/
export const getCurrentFolderPath = function(): string | undefined {
let currentFolderPath = getFolderPathByConfig();
if (currentFolderPath) {
return currentFolderPath;
}
const ws = vscode.workspace;
let folder = ws.workspaceFolders;
let folderPath = '';
if (folder !== undefined) {
folderPath = folder[0].uri.fsPath;
}
if (folderPath) {
setCurrentFolderPath(folderPath);
return folderPath;
} else {
return undefined;
}
export const getCurrentFolderPath = function (): string | undefined {
// not get or set folderPath in config
// just use script to get path


// let currentFolderPath = getFolderPathByConfig();
// if (currentFolderPath) {
// return currentFolderPath;
// }
const ws = vscode.workspace;
let folder = ws.workspaceFolders;
let folderPath = '';
if (folder !== undefined) {
folderPath = folder[0].uri.fsPath;
}
if (folderPath) {
// setCurrentFolderPath(folderPath);
return folderPath;
} else {
return undefined;
}
};
export const processTreeData = function(
dependencyTree: dependencyTree.DependencyObj,
folderPath: string
export const processTreeData = function (
dependencyTree: dependencyTree.DependencyObj,
folderPath: string
): DependencyTreeData {
let dependencyTreeData: DependencyTreeData = {} as DependencyTreeData;
const nodes = [
{
dependencyTree,
dependencyTreeData,
ancestors: [] as string[],
path: Object.keys(dependencyTree)[0]
}
];
while (nodes.length) {
const node = nodes.pop();
if (node) {
const file = node.path.split('\\').pop() as string;
const fileName = file;
const extension = file.split('.').pop();
const type = getFileIconNameByFileName(fileName);
const analyseData = analysesFile(node.path, folderPath);
if (analyseData.analysed) {
node.dependencyTreeData.analysed = true;
node.dependencyTreeData.lines = analyseData.lines;
node.dependencyTreeData.functions = analyseData.functionsList;
} else {
node.dependencyTreeData.analysed = false;
}
node.dependencyTreeData.fileDescription = analyseData.fileInformation;
node.dependencyTreeData.name = fileName;
node.dependencyTreeData.absolutePath = node.path;
node.dependencyTreeData.ancestors = node.ancestors;
node.dependencyTreeData.relativePath = node.path.replace(folderPath, '');
node.dependencyTreeData.extension = extension as string;
node.dependencyTreeData.type = type;
node.dependencyTreeData.children = [] as Array<DependencyTreeData>;
for (let keys in node.dependencyTree[node.path]) {
let ancestors = [] as string[];
ancestors = ancestors.concat(...node.ancestors);
ancestors.push(node.path);
let subNode = {} as DependencyTreeData;
node.dependencyTreeData.children.push(subNode);
nodes.push({
dependencyTree: node.dependencyTree[node.path],
path: keys,
ancestors: ancestors,
dependencyTreeData: subNode
});
}
}
}
return dependencyTreeData;
let dependencyTreeData: DependencyTreeData = {} as DependencyTreeData;
const nodes = [
{
dependencyTree,
dependencyTreeData,
ancestors: [] as string[],
path: Object.keys(dependencyTree)[0]
}
];
while (nodes.length) {
const node = nodes.pop();
if (node) {
const file = node.path.split('\\').pop() as string;
const fileName = file;
const extension = file.split('.').pop();
const type = getFileIconNameByFileName(fileName);
const analyseData = analysesFile(node.path, folderPath);
if (analyseData.analysed) {
node.dependencyTreeData.analysed = true;
node.dependencyTreeData.lines = analyseData.lines;
node.dependencyTreeData.functions = analyseData.functionsList;
} else {
node.dependencyTreeData.analysed = false;
}
node.dependencyTreeData.fileDescription = analyseData.fileInformation;
node.dependencyTreeData.name = fileName;
node.dependencyTreeData.absolutePath = node.path;
node.dependencyTreeData.ancestors = node.ancestors;
node.dependencyTreeData.relativePath = node.path.replace(folderPath, '');
node.dependencyTreeData.extension = extension as string;
node.dependencyTreeData.type = type;
node.dependencyTreeData.children = [] as Array<DependencyTreeData>;
for (let keys in node.dependencyTree[node.path]) {
let ancestors = [] as string[];
ancestors = ancestors.concat(...node.ancestors);
ancestors.push(node.path);
let subNode = {} as DependencyTreeData;
node.dependencyTreeData.children.push(subNode);
nodes.push({
dependencyTree: node.dependencyTree[node.path],
path: keys,
ancestors: ancestors,
dependencyTreeData: subNode
});
}
}
}
return dependencyTreeData;
};
Loading

0 comments on commit e4bf3a8

Please sign in to comment.