UNPKG

@nx-kz/go

Version:
97 lines (92 loc) 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processProjectGraph = void 0; /* Copyright (c) 2022 KamikazeZirou Released under the MIT license. https://github.com/KamikazeZirou/nx-plugins/blob/main/LICENSE I have modified the file at the following URL so that I can specify the prefix of the go module path from workspace.json or nx.json. https://github.com/nx-go/nx-go/blob/cf139e99bf840d3aaa76ad2cc3b5e73b23c5cb34/packages/nx-go/src/go-package-graph/index.ts Copyright (c) 2020-2021 Bram Borggreve Released under the MIT license. https://github.com/nx-go/nx-go/blob/cf139e99bf840d3aaa76ad2cc3b5e73b23c5cb34/LICENSE */ const devkit_1 = require("@nrwl/devkit"); const path_1 = require("path"); const child_process_1 = require("child_process"); const fs_1 = require("fs"); function processProjectGraph(graph, context) { const workspaceRootPath = findNxWorkspaceRootPath(); const projectRootLookupMap = new Map(); for (const projectName in graph.nodes) { projectRootLookupMap.set(graph.nodes[projectName].data.root, projectName); } const builder = new devkit_1.ProjectGraphBuilder(graph); // Define dependencies using the context of files that were changed to minimize work // between each run. for (const projectName in context.filesToProcess) { context.filesToProcess[projectName] .filter((f) => (0, path_1.extname)(f.file) === '.go') .map(({ file }) => ({ projectName, file, dependencies: getGoDependencies(context, workspaceRootPath, projectRootLookupMap, file), })) .filter((data) => data.dependencies && data.dependencies.length > 0) .forEach(({ projectName, file, dependencies }) => { for (const dependency of dependencies) { builder.addExplicitDependency(projectName, file, dependency); } }); } // We will see how this is used below. return builder.getUpdatedProjectGraph(); } exports.processProjectGraph = processProjectGraph; /** * getGoDependencies will use `go list` to get dependency information from a go file * @param projectRootLookup * @param file * @returns */ const getGoDependencies = (context, workspaceRootPath, projectRootLookup, file) => { var _a, _b, _c, _d; const goModPath = (_c = (_b = (_a = context.workspace.pluginsConfig) === null || _a === void 0 ? void 0 : _a['@nx-kz/go']) === null || _b === void 0 ? void 0 : _b['goModPath']) !== null && _c !== void 0 ? _c : ""; try { const goPackageDataJson = (0, child_process_1.execSync)('go list -json ./' + file, { encoding: 'utf-8', cwd: workspaceRootPath }); const goPackage = JSON.parse(goPackageDataJson); const isTestFile = (0, path_1.basename)(file, '.go').endsWith('_test'); // Use the correct imports list depending if the file is a test file. const listOfImports = (_d = (!isTestFile ? goPackage.Imports : goPackage.TestImports)) !== null && _d !== void 0 ? _d : []; return listOfImports .filter((d) => d.startsWith(goModPath)) .map((d) => d.substring(goModPath.length + 1)) .map((rootDir) => projectRootLookup.get(rootDir)) .filter((projectName) => projectName); } catch (ex) { console.error(`Error processing ${file}`); console.error(ex); return []; // Return an empty array so that we can process other files } }; /** * Finds the absolute path for the root path of the workspace. * * This is useful when modules are executed from Nx processes in the node_modules * folder like when running as a graph plugin */ const findNxWorkspaceRootPath = () => { let workingDirectory = process.cwd(); // eslint-disable-next-line no-constant-condition while (true) { if ((0, fs_1.existsSync)((0, path_1.join)(workingDirectory, 'nx.json'))) { return workingDirectory; } else if (workingDirectory === (0, path_1.dirname)(workingDirectory)) { throw new Error('At the root of the filesystem'); } workingDirectory = (0, path_1.dirname)(workingDirectory); } }; //# sourceMappingURL=index.js.map