-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Convert codebase to TypeScript and add some types
- Loading branch information
Showing
14 changed files
with
303 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import {DepTree} from './types'; | ||
import * as _ from 'lodash'; | ||
|
||
const newline = /[\r\n]+/g; | ||
const logLabel = /^\[\w+\]\s*/gm; | ||
const digraph = /digraph([\s\S]*?)\}/g; | ||
const errorLabel = /^\[ERROR\]/gm; | ||
|
||
// Parse the output from 'mvn dependency:tree -DoutputType=dot' | ||
export function parseTree(text, withDev) { | ||
// check for errors in mvn output | ||
if (errorLabel.test(text)) { | ||
throw new Error('Failed to execute an `mvn` command.'); | ||
} | ||
|
||
// clear all labels | ||
text = text.replace(logLabel, ''); | ||
|
||
// extract deps | ||
const data = getRootProject(text, withDev); | ||
|
||
return {ok: true, data}; | ||
} | ||
|
||
function getRootProject(text, withDev) { | ||
const projects = text.match(digraph); | ||
if (!projects) { | ||
throw new Error('Cannot find dependency information.'); | ||
} | ||
const rootProject = getProject(projects[0], withDev); | ||
const defaultRoot: DepTree = { | ||
name: 'no-name', | ||
version: '0.0.0', | ||
dependencies: {}, | ||
packageFormatVersion: 'mvn:0.0.1', | ||
}; | ||
|
||
const root = { | ||
...defaultRoot, | ||
...rootProject, | ||
}; | ||
|
||
// Add any subsequent projects to the root as dependencies | ||
for (let i = 1; i < projects.length; i++) { | ||
const project: DepTree | null = getProject(projects[i], withDev); | ||
if (project && project.name) { | ||
root.dependencies = {}; | ||
root.dependencies[project.name] = project; | ||
} | ||
} | ||
return root; | ||
} | ||
|
||
function getProject(projectDump, withDev) { | ||
const lines = projectDump.split(newline); | ||
const identity = dequote(lines[0]); | ||
const deps = {}; | ||
for (let i = 1; i < lines.length - 1; i++) { | ||
const line = lines[i]; | ||
const nodes = line.trim().split('->'); | ||
const source = dequote(nodes[0]); | ||
const target = dequote(nodes[1]); | ||
deps[source] = deps[source] || []; | ||
deps[source].push(target); | ||
} | ||
return assemblePackage(identity, deps, withDev); | ||
} | ||
|
||
function assemblePackage(source, projectDeps, withDev): DepTree | null { | ||
const sourcePackage: DepTree = createPackage(source); | ||
if (sourcePackage.scope === 'test' && !withDev) { | ||
// skip a test dependency if it's not asked for | ||
return null; | ||
} | ||
const sourceDeps = projectDeps[source]; | ||
if (sourceDeps) { | ||
sourcePackage.dependencies = {}; | ||
for (const dep of sourceDeps) { | ||
const pkg: DepTree | null = assemblePackage( | ||
dep, projectDeps, withDev); | ||
if (pkg) { | ||
sourcePackage.dependencies[pkg.name] = pkg; | ||
} | ||
} | ||
} | ||
return sourcePackage; | ||
} | ||
|
||
function createPackage(pkgStr) { | ||
const range = getConstraint(pkgStr); | ||
|
||
if (range) { | ||
pkgStr = pkgStr.substring(0, pkgStr.indexOf(' ')); | ||
} | ||
|
||
const parts = pkgStr.split(':'); | ||
const result: DepTree = { | ||
version: parts[3], | ||
name: parts[0] + ':' + parts[1], | ||
dependencies: {}, | ||
}; | ||
|
||
if (parts.length >= 5) { | ||
result.scope = parts[parts.length - 1]; | ||
result.version = parts[parts.length - 2]; | ||
} | ||
|
||
// TODO: This is likely obsolete, remove | ||
if (range) { | ||
result.dep = range; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function dequote(str) { | ||
return str.slice(str.indexOf('"') + 1, str.lastIndexOf('"')); | ||
} | ||
|
||
function getConstraint(str) { | ||
const index = str.indexOf('selected from constraint'); | ||
if (index === -1) { | ||
return null; | ||
} | ||
return str.slice(index + 25, str.lastIndexOf(')')); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as childProcess from 'child_process'; | ||
|
||
export function execute(command, args, options) { | ||
const spawnOptions: { | ||
shell: boolean; | ||
cwd?: string; | ||
} = {shell: true}; | ||
if (options && options.cwd) { | ||
spawnOptions.cwd = options.cwd; | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
let stdout = ''; | ||
let stderr = ''; | ||
|
||
const proc = childProcess.spawn(command, args, spawnOptions); | ||
proc.stdout.on('data', (data) => { | ||
stdout = stdout + data; | ||
}); | ||
proc.stderr.on('data', (data) => { | ||
stderr = stderr + data; | ||
}); | ||
|
||
proc.on('close', (code) => { | ||
if (code !== 0) { | ||
return reject(new Error(stdout || stderr)); | ||
} | ||
resolve(stdout || stderr); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
export interface PluginMetadata { | ||
name: string; | ||
runtime: string; | ||
targetFile?: string; | ||
} | ||
|
||
export interface DepDict { | ||
[name: string]: DepTree; | ||
} | ||
|
||
export interface DepRoot { | ||
depTree: DepTree; | ||
targetFile?: string; | ||
|
||
meta?: any; | ||
} | ||
export interface DepTree { | ||
name: string; | ||
version: string; | ||
dependencies?: DepDict; | ||
packageFormatVersion?: string; | ||
scope?: string; // TODO: is it needed on the Tree? | ||
dep?: string; // TODO: is it needed on the Tree? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.