@nx-go/nx-go
Advanced tools
Comparing version 2.5.0 to 2.6.0
{ | ||
"name": "@nx-go/nx-go", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"main": "src/index.js", | ||
@@ -5,0 +5,0 @@ "generators": "./generators.json", |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const utils_1 = require("../../utils"); | ||
const child_process_1 = require("child_process"); | ||
function runExecutor(options, context) { | ||
var _a, _b; | ||
var _a, _b, _c, _d, _e, _f; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
const projectName = context === null || context === void 0 ? void 0 : context.projectName; | ||
const sourceRoot = (_b = (_a = context === null || context === void 0 ? void 0 : context.workspace) === null || _a === void 0 ? void 0 : _a.projects[projectName]) === null || _b === void 0 ? void 0 : _b.root; | ||
const cwd = `${sourceRoot}`; | ||
const projectName = context.projectName; | ||
const cwd = (_b = (_a = context.workspace.projects[projectName]) === null || _a === void 0 ? void 0 : _a.root) !== null && _b !== void 0 ? _b : ''; | ||
const isVerbose = (_c = context.isVerbose) !== null && _c !== void 0 ? _c : false; | ||
const sources = `./...`; | ||
return (0, utils_1.runGoCommand)(context, 'fmt', [sources], { cwd }); | ||
const linter = (_d = options.linter) !== null && _d !== void 0 ? _d : 'go fmt'; // Keeping default from initial version of linter | ||
const args = (_f = (_e = options.args) === null || _e === void 0 ? void 0 : _e.trim()) !== null && _f !== void 0 ? _f : ''; | ||
const command = [linter, args, sources].filter((a) => a).join(' '); | ||
if (isVerbose) { | ||
console.log(`Executing Lint linter: '${linter}', arguments: '${args}', cwd: '${cwd}', command: '${command}'`); | ||
} | ||
try { | ||
(0, child_process_1.execSync)(command, { cwd, stdio: [0, 1, 2] }); | ||
return { success: true }; | ||
} | ||
catch (ex) { | ||
console.error(ex); | ||
return { success: false }; | ||
} | ||
}); | ||
@@ -14,0 +27,0 @@ } |
@@ -1,1 +0,4 @@ | ||
export interface LintExecutorSchema {} // eslint-disable-line | ||
export interface LintExecutorSchema { | ||
linter?: string | ||
args?: string | ||
} // eslint-disable-line |
@@ -5,6 +5,15 @@ { | ||
"title": "Lint executor", | ||
"description": "", | ||
"description": "Lints the code for the Go project.", | ||
"type": "object", | ||
"properties": {}, | ||
"properties": { | ||
"linter": { | ||
"type": "string", | ||
"description": "The command to execute when running the lint executor." | ||
}, | ||
"args": { | ||
"type": "string", | ||
"description": "Additional arguments to the linter" | ||
} | ||
}, | ||
"required": [] | ||
} |
@@ -10,2 +10,3 @@ "use strict"; | ||
const workspaceRootPath = (0, find_workspace_root_path_1.findNxWorkspaceRootPath)(); | ||
const goModules = getGoModules(workspaceRootPath); | ||
const projectRootLookupMap = new Map(); | ||
@@ -24,3 +25,3 @@ for (const projectName in graph.nodes) { | ||
file, | ||
dependencies: getGoDependencies(workspaceRootPath, projectRootLookupMap, file), | ||
dependencies: getGoDependencies(workspaceRootPath, goModules, projectRootLookupMap, file), | ||
})) | ||
@@ -39,2 +40,4 @@ .filter((data) => data.dependencies && data.dependencies.length > 0) | ||
* getGoDependencies will use `go list` to get dependency information from a go file | ||
* @param workspaceRootPath | ||
* @param goModules | ||
* @param projectRootLookup | ||
@@ -44,16 +47,14 @@ * @param file | ||
*/ | ||
const getGoDependencies = (workspaceRootPath, projectRootLookup, file) => { | ||
const getGoDependencies = (workspaceRootPath, goModules, projectRootLookup, file) => { | ||
var _a; | ||
try { | ||
const goModuleJSON = (0, child_process_1.execSync)('go list -m -json', { encoding: 'utf-8', cwd: workspaceRootPath }); | ||
const goModule = JSON.parse(goModuleJSON); | ||
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. | ||
// Use the correct imports list even if the file is a test file. | ||
const listOfImports = (_a = (!isTestFile ? goPackage.Imports : goPackage.TestImports)) !== null && _a !== void 0 ? _a : []; | ||
return listOfImports | ||
.filter((d) => d.startsWith(goModule.Path)) | ||
.map((d) => d.substring(goModule.Path.length + 1)) | ||
.map((rootDir) => projectRootLookup.get(rootDir)) | ||
.map((goImport) => ({ goImport, goModule: goModules.find((m) => goImport.startsWith(m.Path)) })) | ||
.filter((importInfo) => importInfo.goModule) | ||
.map(({ goImport, goModule }) => getProjectNameForGoImport(workspaceRootPath, goImport, goModule, projectRootLookup)) | ||
.filter((projectName) => projectName); | ||
@@ -67,2 +68,46 @@ } | ||
}; | ||
/** | ||
* Parses go modules in a way that work with Go workspaces if they are used. | ||
* @param workspaceRootPath | ||
*/ | ||
const getGoModules = (workspaceRootPath) => { | ||
const goModuleJSON = (0, child_process_1.execSync)('go list -m -json', { encoding: 'utf-8', cwd: workspaceRootPath }); | ||
const modules = []; | ||
const exp = /}\r?\n{/; | ||
let jsonString = goModuleJSON; | ||
let idx = jsonString.search(exp); | ||
while (idx !== -1) { | ||
const toParse = jsonString.substring(0, idx + 1); | ||
modules.push(JSON.parse(toParse)); | ||
jsonString = jsonString.substring(idx + 1).trimStart(); | ||
idx = jsonString.search(exp); | ||
} | ||
modules.push(JSON.parse(jsonString)); | ||
// Sort and reverse the modules so when looking up a go import we will encounter the most specific path first | ||
modules.sort((a, b) => a.Path.localeCompare(b.Path)); | ||
modules.reverse(); | ||
return modules; | ||
}; | ||
/** | ||
* Gets the project name for the go import by getting the relative path for the import with in the go module system | ||
* then uses that to calculate the relative path on disk and looks up which project in the workspace the import is a part | ||
* of. | ||
* @param workspaceRootPath | ||
* @param importPath | ||
* @param module | ||
* @param projectRootLookup | ||
*/ | ||
const getProjectNameForGoImport = (workspaceRootPath, importPath, module, projectRootLookup) => { | ||
const relativeImportPath = importPath.substring(module.Path.length + 1); | ||
const relativeModuleDir = module.Dir.substring(workspaceRootPath.length + 1); | ||
let projectPath = relativeModuleDir ? relativeModuleDir + '/' + relativeImportPath : relativeImportPath; | ||
while (projectPath !== '.') { | ||
const projectName = projectRootLookup.get(projectPath); | ||
if (projectName) { | ||
return projectName; | ||
} | ||
projectPath = (0, path_1.dirname)(projectPath); | ||
} | ||
return null; | ||
}; | ||
//# sourceMappingURL=index.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
48394
727
3