-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Support ZModel format command in CLI (#869)
- Loading branch information
1 parent
b9f2e10
commit bf85ceb
Showing
6 changed files
with
108 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getVersion } from '@zenstackhq/runtime'; | ||
import { formatDocument } from '../cli-util'; | ||
import colors from 'colors'; | ||
import ora from 'ora'; | ||
import { writeFile } from 'fs/promises'; | ||
|
||
export async function format(projectPath: string, options: { schema: string }) { | ||
const version = getVersion(); | ||
console.log(colors.bold(`⌛️ ZenStack CLI v${version}`)); | ||
|
||
const schemaFile = options.schema; | ||
const spinner = ora(`Formatting ${schemaFile}`).start(); | ||
try { | ||
const formattedDoc = await formatDocument(schemaFile); | ||
await writeFile(schemaFile, formattedDoc); | ||
spinner.succeed(); | ||
} catch (e) { | ||
spinner.fail(); | ||
throw e; | ||
} | ||
} |
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
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
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,43 @@ | ||
import * as fs from 'fs'; | ||
import * as tmp from 'tmp'; | ||
import { createProgram } from '../../../../packages/schema/src/cli'; | ||
describe('CLI format test', () => { | ||
let origDir: string; | ||
|
||
beforeEach(() => { | ||
origDir = process.cwd(); | ||
const r = tmp.dirSync({ unsafeCleanup: true }); | ||
console.log(`Project dir: ${r.name}`); | ||
process.chdir(r.name); | ||
}); | ||
|
||
afterEach(() => { | ||
process.chdir(origDir); | ||
}); | ||
|
||
it('basic format', async () => { | ||
const model = ` | ||
datasource db {provider="sqlite" url="file:./dev.db"} | ||
generator client {provider = "prisma-client-js"} | ||
model Post {id Int @id() @default(autoincrement())users User[]}`; | ||
|
||
const formattedModel = ` | ||
datasource db { | ||
provider="sqlite" | ||
url="file:./dev.db" | ||
} | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
model Post { | ||
id Int @id() @default(autoincrement()) | ||
users User[] | ||
}`; | ||
// set up schema | ||
fs.writeFileSync('schema.zmodel', model, 'utf-8'); | ||
const program = createProgram(); | ||
await program.parseAsync(['format'], { from: 'user' }); | ||
|
||
expect(fs.readFileSync('schema.zmodel', 'utf-8')).toEqual(formattedModel); | ||
}); | ||
}); |