Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ UIGraph currently links a sibling `uigraph-lens` package (`link:../uigraph-lens`

```sh
cd uigraph-lens && pnpm install && pnpm build
cd ../uigraph && pnpm install && pnpm build
cd ../uigraph && pnpm install && NODE_OPTIONS=--max-old-space-size=8192 pnpm build
node dist/bin.mjs
```

Expand Down
8 changes: 2 additions & 6 deletions src/artifacts/databases.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import path from 'node:path'
import { exists, loadValid } from './docs.js'
import { DATABASE_DIALECTS, parseDatabaseSchema, slugify } from './load.js'
import {
deleteArtifactFile,
isArtifactPath,
writeArtifactFile,
} from './write.js'
import { deleteArtifactFile, writeArtifactFile } from './write.js'
import {
appendItem,
applyEdits,
Expand Down Expand Up @@ -103,7 +99,7 @@ export async function deleteDatabase(
const stillUsed = db.databases.some(
(d) => d !== database && d.entry.schemaPath === database.entry.schemaPath
)
if (isArtifactPath(database.entry.schemaPath) && !stillUsed) {
if (!stillUsed) {
await deleteArtifactFile(root, database.entry.schemaPath)
}
}
31 changes: 31 additions & 0 deletions src/artifacts/diagram-images.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'vitest'
import { createRepo } from '../test-repo.js'
import { listDiagramImages, writeDiagramImage } from './diagram-images.js'

describe('listDiagramImages', () => {
it.each(['./diagram/a.mmd', 'diagram/a.mmd'])(
'returns a clean path for %s',
async (diagramPath) => {
const root = await createRepo({ 'diagram/images/shot.png': 'png' })
const images = await listDiagramImages(root, diagramPath)
expect(images.map((image) => image.imagePath)).toEqual([
'diagram/images/shot.png',
])
}
)
})

describe('writeDiagramImage', () => {
it('saves an upload next to a diagram outside .uigraph', async () => {
const root = await createRepo({ 'diagram/a.mmd': 'flowchart LR\n' })
const imagePath = await writeDiagramImage(
root,
'./diagram/a.mmd',
'My Shot.png',
{ data: Buffer.from('png'), contentType: 'image/png' }
)
expect(imagePath).toBe('diagram/images/my-shot.png')
const images = await listDiagramImages(root, './diagram/a.mmd')
expect(images.map((image) => image.imagePath)).toEqual([imagePath])
})
})
2 changes: 1 addition & 1 deletion src/artifacts/diagram-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const DIAGRAM_IMAGE_EXTENSIONS: Record<string, string> = {
}

function imageDirectory(diagramPath: string) {
return `${path.posix.dirname(diagramPath)}/images`
return path.posix.join(path.posix.dirname(diagramPath), 'images')
}

export async function listDiagramImages(root: string, diagramPath: string) {
Expand Down
8 changes: 2 additions & 6 deletions src/artifacts/docs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { loadArtifacts, slugify } from './load.js'
import {
deleteArtifactFile,
isArtifactPath,
writeArtifactFile,
} from './write.js'
import { deleteArtifactFile, writeArtifactFile } from './write.js'
import {
appendItem,
applyEdits,
Expand Down Expand Up @@ -107,7 +103,7 @@ export async function deleteDoc(
const stillUsed = allDocs.some(
(d) => d !== doc && d.entry.path === doc.entry.path
)
if (isArtifactPath(doc.entry.path) && !stillUsed) {
if (!stillUsed) {
await deleteArtifactFile(root, doc.entry.path)
}
}
8 changes: 2 additions & 6 deletions src/artifacts/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import {
type CanvasFile,
type MapEntry,
} from './load.js'
import {
deleteArtifactFile,
isArtifactPath,
writeArtifactFile,
} from './write.js'
import { deleteArtifactFile, writeArtifactFile } from './write.js'
import {
appendItem,
applyEdits,
Expand Down Expand Up @@ -168,7 +164,7 @@ export async function deleteUnusedImages(
)
)
for (const imagePath of imagePaths) {
if (isArtifactPath(imagePath) && !used.has(imagePath)) {
if (!used.has(imagePath)) {
await deleteArtifactFile(root, imagePath)
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/artifacts/queries.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { loadValid } from './docs.js'
import { slugify } from './load.js'
import {
deleteArtifactFile,
isArtifactPath,
writeArtifactFile,
} from './write.js'
import { deleteArtifactFile, writeArtifactFile } from './write.js'
import {
appendItem,
applyEdits,
Expand Down Expand Up @@ -100,11 +96,6 @@ export async function updateQuery(
)
const { description, tags, queryText, path, ...rest } = query.entry
if (path !== undefined) {
if (!isArtifactPath(path)) {
throw new Error(
`The query file ${path} is outside .uigraph and cannot be edited`
)
}
await writeArtifactFile(root, path, input.queryText)
}
const { source, document } = await readYaml(root, query.file)
Expand Down Expand Up @@ -141,7 +132,7 @@ export async function deleteQuery(
)
const path = query.entry.path
const stillUsed = db.queries.some((q) => q !== query && q.entry.path === path)
if (path !== undefined && isArtifactPath(path) && !stillUsed) {
if (path !== undefined && !stillUsed) {
await deleteArtifactFile(root, path)
}
}
46 changes: 40 additions & 6 deletions src/artifacts/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ describe('writeArtifactFile', () => {
it.each([
['../outside.txt', 'Path must be relative to the repository'],
['/etc/passwd', 'Path must be relative to the repository'],
[
'.uigraph/../src/index.ts',
'Only .uigraph.yaml and files under .uigraph/',
],
['src/index.ts', 'Only .uigraph.yaml and files under .uigraph/'],
['.uigraph/', 'Only .uigraph.yaml and files under .uigraph/'],
['.uigraph/', 'Not a writable file path'],
['.git/config', 'Not a writable file path'],
])('refuses %s', async (requested, message) => {
const root = await createRepo({})
await expect(writeArtifactFile(root, requested, 'x')).rejects.toThrow(
message
)
})

it('writes files outside .uigraph', async () => {
const root = await createRepo({})
expect(await writeArtifactFile(root, './diagram/a.mmd', 'hi')).toBe(
'diagram/a.mmd'
)
expect(await readRepoFile(root, 'diagram/a.mmd')).toBe('hi')
})

it('refuses to write through a symbolic link', async () => {
const root = await createRepo({})
const outside = await createRepo({})
Expand All @@ -63,6 +67,15 @@ describe('deleteArtifactFile', () => {
})
})

describe('deleteArtifactFile outside .uigraph', () => {
it('removes the file and its empty folders', async () => {
const root = await createRepo({ 'diagram/a/b.mmd': 'x', 'keep.txt': 'y' })
await deleteArtifactFile(root, 'diagram/a/b.mmd')
await expect(fs.readdir(root)).resolves.not.toContain('diagram')
expect(await readRepoFile(root, 'keep.txt')).toBe('y')
})
})

describe('diagrams', () => {
it('creates a diagram with its own folder', async () => {
const root = await createRepo({
Expand Down Expand Up @@ -160,6 +173,27 @@ describe('diagrams', () => {
).rejects.toThrow('ENOENT')
})

it("keeps a shared folder's images when deleting one diagram", async () => {
const root = await createRepo({
'.uigraph.yaml': `version: 1
architectureDiagrams:
- name: A
path: diagram/a.mmd
- name: B
path: diagram/b.mmd
`,
'diagram/a.mmd': 'flowchart LR\n X --> Y\n',
'diagram/b.mmd': 'flowchart LR\n X --> Y\n',
'diagram/images/shot.png': 'png',
})
await deleteDiagram(root, '.uigraph.yaml', 'diagram-a-mmd')
await expect(
fs.readFile(path.join(root, 'diagram/a.mmd'))
).rejects.toThrow()
expect(await readRepoFile(root, 'diagram/images/shot.png')).toBe('png')
expect(await readRepoFile(root, 'diagram/b.mmd')).toContain('X')
})

it('renames references to a diagram in maps', async () => {
const root = await createRepo({
'.uigraph.yaml': `version: 1
Expand Down
38 changes: 22 additions & 16 deletions src/artifacts/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import fs from 'node:fs/promises'
import path from 'node:path'
import { isMap, isScalar, stringify, visit, type Node as YamlNode } from 'yaml'
import { listDiagramImages } from './diagram-images.js'
import { contentHash, loadArtifacts, ROOT_CONFIG, slugify } from './load.js'
import { contentHash, loadArtifacts, slugify } from './load.js'
import {
appendItem,
applyEdits,
Expand All @@ -24,11 +24,7 @@ import {
const MAX_WRITE_BYTES = 2_000_000
export const MAX_UPLOAD_BYTES = 50_000_000

export function isArtifactPath(relativePath: string) {
return relativePath === ROOT_CONFIG || relativePath.startsWith('.uigraph/')
}

function toArtifactPath(requested: string) {
function toRepoPath(requested: string) {
const relativePath = path.posix
.normalize(requested.replaceAll('\\', '/'))
.replace(/^\.\//, '')
Expand All @@ -38,16 +34,19 @@ function toArtifactPath(requested: string) {
) {
throw new Error(`Path must be relative to the repository: ${requested}`)
}
if (!isArtifactPath(relativePath) || relativePath.endsWith('/')) {
throw new Error(
`Only ${ROOT_CONFIG} and files under .uigraph/ can be written: ${requested}`
)
if (
relativePath === '' ||
relativePath === '.' ||
relativePath.endsWith('/') ||
relativePath.split('/')[0] === '.git'
) {
throw new Error(`Not a writable file path: ${requested}`)
}
return relativePath
}

export async function deleteArtifactFile(root: string, requested: string) {
const relativePath = toArtifactPath(requested)
const relativePath = toRepoPath(requested)
let current = root
for (const segment of relativePath.split('/')) {
current = path.join(current, segment)
Expand All @@ -59,10 +58,9 @@ export async function deleteArtifactFile(root: string, requested: string) {
}
}
await fs.rm(current)
const artifactRoot = path.join(root, '.uigraph')
for (
let directory = path.dirname(current);
directory.startsWith(`${artifactRoot}${path.sep}`);
directory !== root && directory !== path.join(root, '.uigraph');
directory = path.dirname(directory)
) {
if ((await fs.readdir(directory)).length > 0) {
Expand All @@ -77,7 +75,7 @@ export async function writeArtifactFile(
requested: string,
content: string | Buffer
) {
const relativePath = toArtifactPath(requested)
const relativePath = toRepoPath(requested)
const limit = typeof content === 'string' ? MAX_WRITE_BYTES : MAX_UPLOAD_BYTES
if (Buffer.byteLength(content) > limit) {
throw new Error(`Content is larger than ${limit} bytes`)
Expand Down Expand Up @@ -311,7 +309,15 @@ export async function deleteDiagram(
if (diagram === undefined) {
throw new Error(`Diagram ${diagramId} not found`)
}
const images = await listDiagramImages(root, diagram.entry.path)
const sharesFolder = db.diagrams.some(
(d) =>
d !== diagram &&
path.posix.dirname(d.entry.path) ===
path.posix.dirname(diagram.entry.path)
)
const images = sharesFolder
? []
: await listDiagramImages(root, diagram.entry.path)
const { source, document } = await readYaml(root, diagram.file)
await writeArtifactFile(
root,
Expand All @@ -325,7 +331,7 @@ export async function deleteDiagram(
diagram.entry.contextPath,
...images.map((image) => image.imagePath),
]) {
if (file !== undefined && isArtifactPath(file)) {
if (file !== undefined) {
await deleteArtifactFile(root, file)
}
}
Expand Down
39 changes: 0 additions & 39 deletions viewer/src/components/virtual-cursor.tsx

This file was deleted.

Loading
Loading