-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from liam-hq/setup-erd-generator-cli
new cli
- Loading branch information
Showing
10 changed files
with
258 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,8 @@ dist-ssr | |
*.sw? | ||
|
||
# typed-css-modules | ||
*.css.d.ts | ||
*.css.d.ts | ||
|
||
|
||
dist-cli | ||
public/schema.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,62 @@ | ||
# @liam/cli | ||
|
||
Command-line tool designed to generate a web application that displays ER diagrams. | ||
|
||
```bash | ||
$ liam erd build --input {your .sql} | ||
# Outputs the web application to the ./public and ./dist directories | ||
|
||
$ liam erd preview | ||
# Launches the web application for preview | ||
``` | ||
|
||
## Building and Installing the Standalone CLI for Development | ||
|
||
To build the CLI for development purposes, run: | ||
|
||
```bash | ||
pnpm run build:cli | ||
# The executable will be output to dist-cli/bin/cli.js. | ||
``` | ||
|
||
After building, you can invoke it locally with: | ||
|
||
```bash | ||
node ./dist-cli/bin/cli.js erd build --input ./fixtures/input.sql | ||
``` | ||
|
||
To make it globally accessible as `liam`, use: | ||
|
||
```bash | ||
pnpm link --global | ||
``` | ||
|
||
## Explanation of npm Scripts for Development | ||
|
||
### Commands | ||
|
||
1. **Build** | ||
```bash | ||
pnpm run build | ||
``` | ||
- Internally, `./fixtures/input.sql` is passed as the `build --input` argument. | ||
- Runs Vite's build process. | ||
|
||
2. **Dev** | ||
```bash | ||
pnpm run dev | ||
``` | ||
- Internally, `./fixtures/input.sql` is passed as the `dev --input` argument. | ||
- Starts the Vite development server. | ||
|
||
3. **Preview** | ||
```bash | ||
pnpm run preview | ||
``` | ||
- Previews the production build using Vite's built-in preview functionality. | ||
|
||
## File Structure | ||
|
||
- **bin/cli.ts**: The main CLI script. | ||
- **fixtures/input.sql**: A sample `.sql` file for testing purposes. | ||
- **src/**: The web application that displays ER diagrams. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { Command } from 'commander' | ||
import { build, createServer, preview } from 'vite' | ||
|
||
const __filename = fileURLToPath(import.meta.url) | ||
const __dirname = path.dirname(__filename) | ||
const root = path.resolve(__dirname, '../..') | ||
const publicDir = path.join(process.cwd(), 'public') | ||
const outDir = path.join(process.cwd(), 'dist') | ||
|
||
function runPreprocess(inputPath: string | null) { | ||
if (inputPath && !fs.existsSync(inputPath)) { | ||
throw new Error('Invalid input path. Please provide a valid .sql file.') | ||
} | ||
|
||
const sqlContent = inputPath ? fs.readFileSync(inputPath, 'utf8') : '{}' | ||
const filePath = path.join(publicDir, 'schema.json') | ||
|
||
if (!fs.existsSync(publicDir)) { | ||
fs.mkdirSync(publicDir, { recursive: true }) | ||
} | ||
|
||
try { | ||
const jsonContent = JSON.stringify({ sql: sqlContent }, null, 2) | ||
fs.writeFileSync(filePath, jsonContent, 'utf8') | ||
return filePath | ||
} catch (error) { | ||
console.error( | ||
`Error during preprocessing: ${error instanceof Error ? error.message : 'Unknown error'}`, | ||
) | ||
return null | ||
} | ||
} | ||
|
||
const program = new Command() | ||
|
||
program.name('liam').description('CLI tool for Liam').version('0.0.0') | ||
|
||
const erdCommand = new Command('erd').description('ERD commands') | ||
|
||
program.addCommand(erdCommand) | ||
|
||
erdCommand | ||
.command('build') | ||
.description('Run Vite build') | ||
.option('--input <path>', 'Path to the .sql file') | ||
.action(async (options) => { | ||
try { | ||
const inputPath = options.input | ||
runPreprocess(inputPath) | ||
await build({ | ||
publicDir, | ||
root, | ||
build: { | ||
outDir, | ||
emptyOutDir: false, | ||
}, | ||
}) | ||
} catch (error) { | ||
console.error('Build failed:', error) | ||
process.exit(1) | ||
} | ||
}) | ||
|
||
erdCommand | ||
.command('dev') | ||
.description('Run Vite dev server') | ||
.option('--input <path>', 'Path to the .sql file') | ||
.action(async (options) => { | ||
try { | ||
const inputPath = options.input | ||
runPreprocess(inputPath) | ||
const server = await createServer({ publicDir, root }) | ||
const address = server.httpServer?.address() | ||
const port = typeof address === 'object' && address ? address.port : 5173 | ||
console.info(`Dev server is running at http://localhost:${port}`) | ||
await server.listen() | ||
} catch (error) { | ||
console.error('Failed to start dev server:', error) | ||
process.exit(1) | ||
} | ||
}) | ||
|
||
erdCommand | ||
.command('preview') | ||
.description('Preview the production build') | ||
.action(async () => { | ||
try { | ||
const previewServer = await preview({ | ||
publicDir, | ||
root, | ||
build: { | ||
outDir, | ||
emptyOutDir: false, | ||
}, | ||
}) | ||
const address = previewServer.httpServer?.address() | ||
const port = typeof address === 'object' && address ? address.port : 4173 | ||
console.info(`Preview server is running at http://localhost:${port}`) | ||
} catch (error) { | ||
console.error('Failed to start preview server:', error) | ||
process.exit(1) | ||
} | ||
}) | ||
program.parse(process.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE products ( | ||
brand VARCHAR(255), | ||
model VARCHAR(255), | ||
year INT | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,37 @@ | ||
import { ERDRenderer } from '@liam/erd-core' | ||
import { useEffect, useState } from 'react' | ||
|
||
function App() { | ||
return <ERDRenderer /> | ||
const [fileContent, setFileContent] = useState<string | null>(null) | ||
|
||
useEffect(() => { | ||
async function loadSchemaContent() { | ||
const response = await fetch('/schema.json') | ||
const data = (await response.json()) || 'No file content available' | ||
setFileContent(JSON.stringify(data, null, 2)) | ||
} | ||
|
||
loadSchemaContent() | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<ERDRenderer /> | ||
|
||
{/* TODO: Remove this */} | ||
{/* Display the file content in a pre tag for demo purposes */} | ||
<pre | ||
style={{ | ||
backgroundColor: '#f5f5f5', | ||
padding: '10px', | ||
borderRadius: '4px', | ||
whiteSpace: 'pre-wrap', | ||
}} | ||
> | ||
{fileContent || 'Loading...'} | ||
</pre> | ||
</> | ||
) | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
{ | ||
"compilerOptions": { | ||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", | ||
"incremental": true, | ||
"target": "ES2022", | ||
"lib": ["ES2023"], | ||
"module": "ESNext", | ||
"skipLibCheck": true, | ||
"moduleResolution": "Bundler", | ||
"allowImportingTsExtensions": true, | ||
"isolatedModules": true, | ||
"moduleDetection": "force", | ||
"noEmit": true, | ||
"noEmit": false, | ||
"strict": true, | ||
"outDir": "dist-cli", | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noUncheckedSideEffectImports": true | ||
}, | ||
"include": ["vite.config.ts"] | ||
"include": ["vite.config.ts", "bin"] | ||
} |
Oops, something went wrong.