Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions indexer/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import {writeFileSync} from 'fs'
import path from 'path'
import {homedir} from 'os'
import * as yargs from 'yargs'
import * as Types from './types'

import {ts} from 'ts-morph'
import * as TsMorph from 'ts-morph'

import extractFunctions from './index'
import * as Tsearch from './index'

// .option('directory', {type: 'positional'})

Expand All @@ -19,20 +18,23 @@ const cli = yargs.option('out', {

const normalize = (p: string) => path.normalize(p.replace(/^~/, homedir()))

function main() {
const main = () => {
const {_: directories, out} = cli.argv

const results: Types.FunctionRecord[] = directories.flatMap(directory =>
extractFunctions(normalize(directory), {
const results: {
name: string
type: TsMorph.Structure
}[] = directories.flatMap(directory =>
Tsearch.findFunctions(normalize(directory), {
compilerOptions: {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS,
target: TsMorph.ts.ScriptTarget.ES5,
module: TsMorph.ts.ModuleKind.CommonJS,
},
}),
)

if (!out) {
console.log(JSON.stringify(results))
process.stdout.write(JSON.stringify(results) + '\n')
return
}

Expand Down
123 changes: 19 additions & 104 deletions indexer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,35 @@ import path from 'path'

import * as ts from 'ts-morph'

import {FunctionRecord, callSignature} from './types'

export default function findFunctions(
export const findFunctions = (
directory: string,
options: ts.ProjectOptions,
): FunctionRecord[] {
): {name: string; type: ts.Structure}[] => {
const project = new ts.Project({
...options,
addFilesFromTsConfig: false,
skipFileDependencyResolution: true,
})

const filesGlob = path.join(directory, '**', '*.ts')
let packageJson
try {
packageJson = require(path.join(directory, 'package.json'))
} catch (e) {
packageJson = {}
}

const moduleName =
packageJson?.name ?? path.relative(path.join(directory, '..'), directory)

project.addSourceFilesAtPaths(filesGlob)

return project.getSourceFiles().reduce<FunctionRecord[]>((acc, file) => {
const fileName = path.relative(directory, file.getFilePath())

const entries = Array.from(file.getExportedDeclarations().entries())

const fns = entries.reduce<FunctionRecord[]>(
(acc, [_indentifier, exports]) => {
exports.forEach(exp => {
if (ts.TypeGuards.isFunctionDeclaration(exp)) {
acc.push(functionDeclaration(exp, fileName, moduleName))
return
}

if (ts.TypeGuards.isVariableDeclaration(exp)) {
const fn = arrowFunction(exp, fileName, moduleName)
if (fn) {
acc.push(fn)
}
}
})

return acc
},
[],
)

acc.push(...fns)
return acc
}, [])
}

function functionDeclaration(
node: ts.FunctionDeclaration,
file: string,
module: string,
): FunctionRecord {
return {
name: node.getName(),
text: node.getFullText(),
docs: node
.getJsDocs()
.map(doc => doc.getFullText())
.join(''),
signature: callSignature({
typeParameters: node.getTypeParameters(),
parameters: node.getParameters(),
returnType: node.getReturnType(),
}),
module,
location: {
path: file,
lines: {
from: node.getStartLineNumber(),
to: node.getEndLineNumber(),
},
},
}
}

function arrowFunction(
node: ts.VariableDeclaration,
file: string,
module: string,
): FunctionRecord | undefined {
const [arrow] = node.getChildrenOfKind(ts.SyntaxKind.ArrowFunction)

if (!arrow) {
return undefined
}

return {
name: node.getName(),
text: `const${node.getFullText()}`,
docs: arrow
.getJsDocs()
.map(doc => doc.getFullText())
.join(''),
signature: callSignature({
typeParameters: arrow.getTypeParameters(),
parameters: arrow.getParameters(),
returnType: arrow.getReturnType(),
}),
module,
location: {
path: file,
lines: {
from: node.getStartLineNumber(),
to: node.getEndLineNumber(),
},
},
}
const sourceFiles = project.getSourceFiles()
const decls: {name: string; type: ts.Structure}[] = sourceFiles.flatMap(x =>
Array.from(x.getExportedDeclarations().values()).flatMap(decls =>
decls.flatMap(decl => {
// ((decl as any).getTypeNode() as any).getStructure(),
if (!('getTypeNode' in decl)) return []
const typeNode = decl.getTypeNode()
if (typeNode === undefined || !('getStructure' in typeNode)) return []
const node = {
name: decl.getName(),
type: (typeNode as any).getStructure(),
}
return [node]
}),
),
)
return decls
}
Loading