Skip to content
Open
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
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@spec.dev/tables": "^0.0.18",
"@spec.types/spec": "^0.0.27",
"bn.js": "^5.2.1",
"comment-parser": "^1.3.1",
"strip-comments": "^2.0.1"
},
"devDependencies": {
Expand Down
95 changes: 79 additions & 16 deletions src/lib/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { StringKeyMap, Manifest, PropertyMetadata } from '../types'
import stripComments from 'strip-comments'
import { StringKeyMap, Manifest } from '../types'
import { promises as fs } from 'fs'
import { parse } from 'comment-parser'

interface LineDictionary {
[key: string]: string
}

interface Property {
name: string
type: string
desc: string
}

export async function readTextFile(path: string): Promise<string> {
const decoder = new TextDecoder('utf-8')
// @ts-ignore
const data = await Deno.readFile(path)
const data = await fs.readFile(path)
return decoder.decode(data)
}

Expand All @@ -19,39 +29,90 @@ export async function readManifest(liveObjectSpecPath: string): Promise<Manifest
return (await readJsonFile(`${callerDirPath}/manifest.json`)) as Manifest
}

export async function buildPropertyMetadata(liveObjectSpecPath: string): Promise<{
[key: string]: PropertyMetadata
}> {
export async function buildPropertyMetadata(liveObjectSpecPath: string): Promise<Property[]> {
// Read Live Object spec.ts file contents.
const specFileContents = await readTextFile(liveObjectSpecPath)
if (!specFileContents) return {}
if (!specFileContents) return []

// Get property lines of Live Object.
const propertyLines = findPropertyLines(specFileContents)
if (!propertyLines.length) return {}
const { propertyLines, comments, decoratorLines } =
findPropertyLinesAndComments(specFileContents)
if (!propertyLines.length) return []

// Get multiline comments
const parsed = parse(specFileContents)

for (let i = 0; i < parsed.length; i++) {
const currentComment = parsed[i]
const sourceLength = currentComment.source.length
const lineNumber = currentComment.source[sourceLength - 1].number + 1
const description = currentComment.description
comments[lineNumber] = description
}

// Group and attribute comments to decorators
const descriptions = {}
for (const [decoratorLine, decoratorTitle] of Object.entries(decoratorLines)) {
let description = ''
let commentsToAdd: String[] = []
for (const [lineNumber, comment] of Object.entries(comments)) {
const currentLineNumber = parseInt(lineNumber)
if (currentLineNumber < parseInt(decoratorLine)) {
commentsToAdd.push(comment)
delete comments[lineNumber]
}
}
description = commentsToAdd.join(' ')
descriptions[decoratorTitle] = description
}

// Parse property name:type info from property lines.
const metadata = {}
let properties: Property[] = []
for (const line of propertyLines) {
// Get accomanying comment from property name:type
const desc = descriptions[line]
const { name, type } = parsePropertyNameAndTypeFromLine(line)
if (name && type) {
metadata[name] = { type }
let property = { name: name, type: type, desc: desc }
properties.push(property)
}
}
return metadata
return properties
}

function getLines(contents: string): string[] {
return (contents || '').split('\n').map((line) => line.trim())
}

function findPropertyLines(contents: string): string[] {
const lines = getLines(stripComments(contents))
function findPropertyLinesAndComments(contents: string): {
propertyLines: string[]
comments: LineDictionary
decoratorLines: LineDictionary
} {
const lines = getLines(contents)
const propertyLines: string[] = []
const comments: LineDictionary = {}
const decoratorLines: LineDictionary = {}
let takeNextLine = false

for (let i = 0; i < lines.length; i++) {
const line = lines[i]

// Avoid 0-indexed line number
const currentLineNumber = i + 1

// Designate lines to exclude for later
if (line.startsWith('class') && line.includes('extends')) {
decoratorLines[currentLineNumber] = 'Do not include.'
}

// Found a comment line.
if (line.startsWith('//')) {
let cleanedLine = line.replace('//', '').trim()
comments[currentLineNumber] = cleanedLine
continue
}

// Found property decorator.
if (line.startsWith('@Property(')) {
takeNextLine = true
Expand All @@ -64,11 +125,13 @@ function findPropertyLines(contents: string): string[] {

// Take property line.
if (takeNextLine) {
decoratorLines[currentLineNumber] = line
propertyLines.push(line)
takeNextLine = false
}
}
return propertyLines

return { propertyLines, comments, decoratorLines }
}

function parsePropertyNameAndTypeFromLine(line: string): StringKeyMap {
Expand Down