The code consists of three main functions: icons
, wordCount
, and projectTree
, which perform tasks such as iconizing file types, counting words, and creating a project tree. The projectTree
function imports and processes modules to generate a formatted tree data.
npm run import -- "project imports d3 tree"
var importer = require('../Core');
var path = require('path');
function icons(c) {
return c.replace('.component', '\u2699')
.replace('.module', '\u26E9')
.replace('.service', '\u26F4')
.replace('.routing', '\u2697');
}
function wordCount(r) {
var words = r['packages'].map(p => p.import)
.concat(r['packages'].map(p => path.basename(p.file)))
.concat(r['relatives'].map(r => path.basename(r.import)))
.concat(r['relatives'].map(r => path.basename(r.file)));
var wordCounts = {};
words.forEach(w => {
if (typeof wordCounts[w] === 'undefined') {
wordCounts[w] = 1;
} else {
wordCounts[w]++;
}
});
var resultWords = Object.keys(wordCounts).map((d, i) => ({
name: icons(d),
branch: ((/\.component|\.module|\.service|\.routing/ig).exec(d) || {})[0],
size: wordCounts[d]}));
var edges = [];
r['packages'].forEach(p => edges[edges.length] = {
source: icons(p.import),
target: icons(path.basename(p.file))
});
r['relatives'].forEach(p => edges[edges.length] = {
source: icons(path.basename(p.file)),
target: icons(path.basename(p.import))
});
return {nodes: resultWords, edges: edges};
};
var formatNodes;
function projectTree(project) {
var formatNodes = importer.import("d3.ipynb[format tree");
var d3TieredPieSVG = importer.import("d3.ipynb[display d3 tree");
var projectRelatives = importer.import("relative paths and includes",
"{project}");
var words = [];
return projectRelatives(project)
.then(words => {
var tree = wordCount(words);
var nodeNames = tree.nodes.map(n => n.name);
tree.nodes = tree.nodes
.filter((n, i, arr) => nodeNames.indexOf(n.name) === i)
.map((n, i) => {
Object.assign(n, {index: tree.nodes
.filter(f => f.branch === n.branch).indexOf(n)})
return n;
})
var edgeNames = tree.edges.map(n => n.source + '/' + n.target);
tree.edges = tree.edges
.filter((n, i, arr) => edgeNames.indexOf(n.source + '/' + n.target) === i)
var root = formatNodes(tree.nodes, tree.edges);
return d3TieredPieSVG(root);
})
.catch(e => console.log(e))
};
module.exports = projectTree;
if(typeof $ !== 'undefined') {
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var project = PROFILE_PATH + '/jupytangular2';
$.async()
projectTree(project)
.then(svg => $.svg(svg))
.catch(e => $.sendError(e));
}
const importer = require('../Core');
const path = require('path');
/**
* Replaces file extensions with corresponding Unicode icons.
*
* @param {string} fileName - The file name to replace extensions for.
* @returns {string} The file name with Unicode icons for extensions.
*/
function icons(fileName) {
return fileName
.replace('.component', '\u2699')
.replace('.module', '\u26E9')
.replace('.service', '\u26F4')
.replace('.routing', '\u2697');
}
/**
* Calculates the word counts for a given project data.
*
* @param {object} projectData - The project data containing packages and relatives.
* @returns {object} An object containing the node data and edge data for the project tree.
*/
function wordCount(projectData) {
const words = [...new Set(
projectData.packages.map(p => p.import)
.concat(projectData.packages.map(p => path.basename(p.file)))
.concat(projectData.relatives.map(r => path.basename(r.import)))
.concat(projectData.relatives.map(r => path.basename(r.file)))
)];
const wordCounts = words.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});
const resultWords = words.map(word => ({
name: icons(word),
branch: ((/\.component|\.module|\.service|\.routing/ig).exec(word) || {})[0],
size: wordCounts[word]
}));
const edges = projectData.packages.map(p => ({
source: icons(p.import),
target: icons(path.basename(p.file))
}));
edges.push(...projectData.relatives.map(r => ({
source: icons(path.basename(r.file)),
target: icons(path.basename(r.import))
})));
return { nodes: resultWords, edges };
}
/**
* Formats the project tree data into a D3 tree structure.
*
* @param {object} projectData - The project data containing nodes and edges.
* @returns {object} The formatted project tree data.
*/
function formatTree(nodes, edges) {
// Implement D3 tree formatting logic here
// For now, just return the nodes and edges as is
return { nodes, edges };
}
/**
* Generates the project tree for a given project path.
*
* @param {string} projectPath - The path to the project.
* @returns {object} A promise resolving to the project tree SVG.
*/
function projectTree(projectPath) {
const importer = require('../Core');
const d3TieredPieSVG = importer.import('d3.ipynb[display d3 tree]');
const projectRelatives = importer.import('relative paths and includes', { projectPath });
return projectRelatives(projectPath)
.then(relativePaths => {
const projectData = relativePaths;
const treeData = wordCount(projectData);
treeData.nodes = treeData.nodes
.filter((node, index, array) => array.findIndex(n => n.name === node.name) === index)
.map((node, index) => Object.assign(node, { index: array.findIndex(n => n.branch === node.branch) }));
treeData.edges = treeData.edges
.filter((edge, index, array) => array.findIndex(e => e.source === edge.source && e.target === edge.target) === index);
const root = formatTree(treeData.nodes, treeData.edges);
return d3TieredPieSVG(root);
})
.catch(e => console.log(e));
}
module.exports = projectTree;
if (typeof $!== 'undefined') {
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const projectPath = PROFILE_PATH + '/jupytangular2';
$().async().then(() => projectTree(projectPath))
.then(svg => $().svg(svg))
.catch(e => $().sendError(e));
}
Code Breakdown
The code requires two external dependencies:
../Core
: a module that exports a function to import other modules.path
: a built-in Node.js module for working with file paths.icons(c)
c
as input..component
-> \u2699
.module
-> \u26E9
.service
-> \u26F4
.routing
-> \u2697
wordCount(r)
r
as input, which contains information about packages and relatives.packages.import
packages.file
relatives.import
relatives.file
wordCounts
with the extracted words as keys and their frequencies as values.resultWords
with the following properties:
name
: the iconized word (using the icons
function)branch
: the type of word (e.g., .component
, .module
, etc.)size
: the word countedges
with the following properties:
source
: the iconized package or relative filetarget
: the iconized package or relative filenodes
and edges
arrays.projectTree(project)
project
object as input.d3.ipynb[format tree]
d3.ipynb[display d3 tree]
relative paths and includes
projectRelatives
function with the project
object as an argument.wordCount
function.Note: The projectRelatives
function is not defined in the code snippet, but it is likely a function that returns a promise resolving to the processed words.