95 lines
2.6 KiB
JavaScript
95 lines
2.6 KiB
JavaScript
import { useDocument } from '@terrace/core'
|
|
import { createStringReader } from '@terrace/core/readers/js-string'
|
|
|
|
const lines = [
|
|
`name @terrace/core`,
|
|
`version 0.0.1`,
|
|
`randomthing test`,
|
|
`license MIT`,
|
|
`license GPL`,
|
|
`exports`,
|
|
` .`,
|
|
` import ./dist/index.js`,
|
|
` require ./dist/index.cjs`,
|
|
` ./parser`,
|
|
` import ./dist/parser.js`,
|
|
` require ./dist/parser.cjs`,
|
|
``,
|
|
` ./document`,
|
|
` import ./dist/document.js`,
|
|
` require ./dist/document.cjs`,
|
|
``,
|
|
` ./readers/node-readline`,
|
|
` import ./dist/readers/node-readline.js`,
|
|
` require ./dist/readers/node-readline.cjs`,
|
|
``,
|
|
` ./readers/js-string`,
|
|
` import ./dist/readers/js-string.js`,
|
|
` require ./dist/readers/js-string.cjs`,
|
|
`scripts`,
|
|
` test vitest ./src`,
|
|
` build vite build`,
|
|
`devDependencies`,
|
|
` vite ^3.2.3`,
|
|
` vitest ^0.24.5`,
|
|
``,
|
|
`author`,
|
|
` name Joshua Bemenderfer`,
|
|
` email josh@thederf.com`,
|
|
` `,
|
|
` Further comments below. As I will now demonstrate, there is no simple`,
|
|
` even if embedded`,
|
|
` way of dealing with this problem.`,
|
|
]
|
|
|
|
async function main() {
|
|
const { toArrays } = useDocument(createStringReader(lines))
|
|
const resultArrays = await toArrays()
|
|
console.dir(resultArrays, { depth: null })
|
|
|
|
const { tail, each, match, buildObject } = useDocument(createStringReader(lines))
|
|
|
|
const structure = {
|
|
name: null,
|
|
version: null,
|
|
license: null,
|
|
exports: null,
|
|
scripts: null,
|
|
devDependencies: null,
|
|
author: null
|
|
}
|
|
|
|
await each(async () => {
|
|
if (match('name')) structure.name = tail().trim()
|
|
if (match('version')) structure.version = tail().trim()
|
|
if (match('license')) structure.license = tail().trim()
|
|
// FIXME: Order of operations causes other parts to break if this doesn't run first?!
|
|
if (match('exports')) structure.exports = await buildObject([], async () => {
|
|
const section = { import: null, require: null }
|
|
|
|
await each(() => {
|
|
if (match('import')) section.import = tail().trim()
|
|
if (match('require')) section.require = tail().trim()
|
|
if (section.import && section.require) return true
|
|
})
|
|
|
|
return section
|
|
})
|
|
if (match('scripts')) structure.scripts = await buildObject()
|
|
if (match('devDependencies')) structure.devDependencies = await buildObject()
|
|
if (match('author')) structure.author = await buildObject(['name', 'email', '#text'])
|
|
|
|
return structure.name &&
|
|
structure.version &&
|
|
structure.license &&
|
|
structure.exports &&
|
|
structure.scripts &&
|
|
structure.devDependencies &&
|
|
structure.author
|
|
})
|
|
|
|
console.dir(structure, { depth: null })
|
|
}
|
|
|
|
main()
|