archunit
Version:
ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app
101 lines • 4.43 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.guessNxWorkspaceRoot = exports.extractNxGraph = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const errors_1 = require("../error/errors");
const extractNxGraph = (rootFolder) => {
const workspaceRoot = rootFolder ?? (0, exports.guessNxWorkspaceRoot)();
// The location of the project graph was moved from .nx/cache to .nx/workspace-data in Nx v19.2.0
// If using Nx 19.2.0+ use the new location
let projectGraphCacheDirectory = absolutePath(workspaceRoot, process.env['NX_PROJECT_GRAPH_CACHE_DIRECTORY'] ??
defaultWorkspaceDataDirectory(workspaceRoot));
// If using Nx <19.2.0 use the old location
if (!fs.existsSync(path.join(projectGraphCacheDirectory, 'project-graph.json'))) {
projectGraphCacheDirectory = absolutePath(workspaceRoot, defaultCacheDirectory(workspaceRoot));
}
const depGraph = fs.readFileSync(path.join(projectGraphCacheDirectory, 'project-graph.json'));
const deps = JSON.parse(depGraph.toString('utf-8')).dependencies;
return mapToGraph(deps);
};
exports.extractNxGraph = extractNxGraph;
const mapToGraph = (nodes) => {
return Object.values(nodes).flatMap((edges) => edges.map((edge) => ({
source: edge.source,
target: edge.target,
external: edge.target.startsWith('npm:'),
importKinds: [], // TODO
})));
};
const absolutePath = (root, pathName) => {
return path.isAbsolute(pathName) ? pathName : path.join(root, pathName);
};
const defaultCacheDirectory = (root) => {
if (fs.existsSync(path.join(root, 'lerna.json')) &&
!fs.existsSync(path.join(root, 'nx.json'))) {
return path.join(root, 'node_modules', '.cache', 'nx');
}
return path.join(root, '.nx', 'cache');
};
const defaultWorkspaceDataDirectory = (root) => {
return path.join(root, '.nx', 'workspace-data');
};
const guessNxWorkspaceRoot = () => {
const nxConfigFileName = guessLocationOfNxConfigRecursively('.');
if (!nxConfigFileName) {
throw new errors_1.TechnicalError(`Unable to extract dependency graph: No root folder of nx project was given and no nx config file could be resolved.`);
}
return path.dirname(nxConfigFileName);
};
exports.guessNxWorkspaceRoot = guessNxWorkspaceRoot;
const guessLocationOfNxConfigRecursively = (pathName) => {
const dir = fs.readdirSync(pathName);
// First check if nx.json exists in the current directory
const nxConfigFile = dir.find((fileName) => path.basename(fileName) === 'nx.json');
if (nxConfigFile) {
return path.resolve(pathName, 'nx.json');
}
// If not, go up one level in directory structure
const levelUp = path.resolve(pathName, '..');
const pr = path.relative(levelUp, pathName);
// Stop if we've reached the filesystem root
if (pr === '') {
return undefined;
}
// Continue recursively
return guessLocationOfNxConfigRecursively(levelUp);
};
//# sourceMappingURL=extract-nx-graph.js.map