Skip to content

AI-native design editor. Open-source Figma alternative.

fix(cli): create missing import output directories

Danila Poyarkovde4c90a0236a
diff --git a/packages/cli/src/commands/import.ts b/packages/cli/src/commands/import.ts
index 77eeb0831..c17e36243 100644
--- a/packages/cli/src/commands/import.ts
+++ b/packages/cli/src/commands/import.ts
@@ -1,5 +1,5 @@
-import { readFile, writeFile } from 'node:fs/promises'
-import { basename, extname, resolve } from 'node:path'
+import { mkdir, readFile, writeFile } from 'node:fs/promises'
+import { basename, dirname, extname, resolve } from 'node:path'
import { defineCommand } from 'citty'
@@ -91,6 +91,8 @@ async function writeOutput(
const format = args.format.toLowerCase()
const output = args.output ? resolve(args.output) : defaultOutput(requireFile(args.file), format)
+ await mkdir(dirname(output), { recursive: true })
+
if (format === 'json') {
await writeFile(output, `${JSON.stringify(document, null, 2)}\n`)
return output
diff --git a/tests/engine/cli/import.test.ts b/tests/engine/cli/import.test.ts
index 080027d0b..7d2174e6f 100644
--- a/tests/engine/cli/import.test.ts
+++ b/tests/engine/cli/import.test.ts
@@ -52,33 +52,36 @@ function findNode(nodes: Iterable<SceneNode>, name: string): SceneNode | undefin
}
}
-test('import CLI writes DesignDOM JSON output', async () => {
- const { htmlPath, cssPath, dir } = await createFixture()
- const output = join(dir, 'card.json')
-
- const { stdout, stderr, exitCode } = await runOpenPencilCLI([
- 'import',
- htmlPath,
- '--css',
- cssPath,
- '--format',
- 'json',
- '--output',
- output,
- '--json'
- ])
-
- expect(stderr).toBe('')
- expect(exitCode).toBe(0)
-
- const summary = JSON.parse(stdout)
- expect(summary).toMatchObject({ format: 'json', output, pages: 1, rootElements: 1 })
-
- const document = JSON.parse(await Bun.file(output).text())
- expect(document.children[0].tagName).toBe('article')
- expect(document.children[0].computedStyle.display).toBe('flex')
- expect(document.children[0].computedStyle.width).toBe('240px')
-})
+test.each(['card.json', 'missing/nested/card.json'])(
+ 'import CLI writes DesignDOM JSON output to %s',
+ async (relativeOutput) => {
+ const { htmlPath, cssPath, dir } = await createFixture()
+ const output = join(dir, relativeOutput)
+
+ const { stdout, stderr, exitCode } = await runOpenPencilCLI([
+ 'import',
+ htmlPath,
+ '--css',
+ cssPath,
+ '--format',
+ 'json',
+ '--output',
+ output,
+ '--json'
+ ])
+
+ expect(stderr).toBe('')
+ expect(exitCode).toBe(0)
+
+ const summary = JSON.parse(stdout)
+ expect(summary).toMatchObject({ format: 'json', output, pages: 1, rootElements: 1 })
+
+ const document = JSON.parse(await Bun.file(output).text())
+ expect(document.children[0].tagName).toBe('article')
+ expect(document.children[0].computedStyle.display).toBe('flex')
+ expect(document.children[0].computedStyle.width).toBe('240px')
+ }
+)
test('import CLI reads embedded HTML styles without a sidecar CSS file', async () => {
const dir = await mkdtemp(join(tmpdir(), 'open-pencil-import-cli-embedded-'))
@@ -118,34 +121,37 @@ test('import CLI reads embedded HTML styles without a sidecar CSS file', async (
expect(document.children[0].computedStyle.gap).toBe('10px')
})
-test('import CLI writes a .fig that core IO can import', async () => {
- const { htmlPath, cssPath, dir } = await createFixture()
- const output = join(dir, 'card.fig')
-
- const { stdout, stderr, exitCode } = await runOpenPencilCLI([
- 'import',
- htmlPath,
- '--css',
- cssPath,
- '--output',
- output,
- '--json'
- ])
-
- expect(stderr).toBe('')
- expect(exitCode).toBe(0)
- expect(JSON.parse(stdout)).toMatchObject({ format: 'fig', output, pages: 1, rootElements: 1 })
-
- const bytes = new Uint8Array(await Bun.file(output).arrayBuffer())
- const graph = await parseFigFile(bytes)
- const nodes = [...graph.nodes.values()]
- const card = findNode(nodes, 'card')
- const title = findNode(nodes, 'DOM/CSS card')
-
- expect(graph.getPages()).toHaveLength(1)
- expect(card?.type).toBe('FRAME')
- expect(title?.type).toBe('TEXT')
-})
+test.each(['card.fig', 'missing/nested/card.fig'])(
+ 'import CLI writes a .fig that core IO can import to %s',
+ async (relativeOutput) => {
+ const { htmlPath, cssPath, dir } = await createFixture()
+ const output = join(dir, relativeOutput)
+
+ const { stdout, stderr, exitCode } = await runOpenPencilCLI([
+ 'import',
+ htmlPath,
+ '--css',
+ cssPath,
+ '--output',
+ output,
+ '--json'
+ ])
+
+ expect(stderr).toBe('')
+ expect(exitCode).toBe(0)
+ expect(JSON.parse(stdout)).toMatchObject({ format: 'fig', output, pages: 1, rootElements: 1 })
+
+ const bytes = new Uint8Array(await Bun.file(output).arrayBuffer())
+ const graph = await parseFigFile(bytes)
+ const nodes = [...graph.nodes.values()]
+ const card = findNode(nodes, 'card')
+ const title = findNode(nodes, 'DOM/CSS card')
+
+ expect(graph.getPages()).toHaveLength(1)
+ expect(card?.type).toBe('FRAME')
+ expect(title?.type).toBe('TEXT')
+ }
+)
test('import CLI compiles Tailwind candidates before import', async () => {
const dir = await mkdtemp(join(tmpdir(), 'open-pencil-import-cli-tailwind-'))