Skip to content

Commit

Permalink
feat: support the prisma cli
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Nov 13, 2024
1 parent 44e181a commit 521cd03
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 31 deletions.
62 changes: 33 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Format markdown code snippets using your favorite code formatters.
- [npm/npx](#npmnpx)
- [Homebrew](#homebrew)
- [Usage](#usage)
- [Verify code is formatted](#verify-code-is-formatted)
- [Formatting code](#formatting-code)
- [Verifying code](#verifying-code)
- [Configuration](#configuration)
- [Tools](#tools)
- [Commands](#commands)
Expand All @@ -35,30 +36,6 @@ Format markdown code snippets using your favorite code formatters.

<!-- END_SECTION:toc -->

<!-- START_SECTION:base-command-help -->

```
mdsf 0.3.1-dev
Format markdown code snippets using your favorite code formatters
Mads Hougesen <[email protected]>
Usage: mdsf <COMMAND>
Commands:
format Run formatters on input files
verify Verify files are formatted
init Create a new mdsf config
completions Generate shell completion
cache-prune Remove old caches
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
```

<!-- END_SECTION:base-command-help -->

## Installation

The latest version of `mdsf` can be downloaded directly from [github.com/hougesen/mdsf/releases](https://github.com/hougesen/mdsf/releases).
Expand Down Expand Up @@ -117,6 +94,32 @@ brew install hougesen/tap/mdsf

## Usage

<!-- START_SECTION:base-command-help -->

```
mdsf 0.3.1-dev
Format markdown code snippets using your favorite code formatters
Mads Hougesen <[email protected]>
Usage: mdsf <COMMAND>
Commands:
format Run formatters on input files
verify Verify files are formatted
init Create a new mdsf config
completions Generate shell completion
cache-prune Remove old caches
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
```

<!-- END_SECTION:base-command-help -->

### Formatting code

```shell
mdsf format file.md
```
Expand All @@ -143,7 +146,7 @@ Options:

<!-- END_SECTION:format-command-help -->

### Verify code is formatted
### Verifying code

```shell
mdsf verify docs/
Expand Down Expand Up @@ -210,13 +213,13 @@ mdsf init
<!-- START_SECTION:supported-tools -->

`mdsf` currently supports 208 tools. Feel free to open an issue/pull-request if your favorite tool/command is missing! 😃
`mdsf` currently supports 209 tools. Feel free to open an issue/pull-request if your favorite tool/command is missing! 😃

| Name | Description | Categories | Languages |
| --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | --------------------- | ------------------------------------------------------------------------- |
| [alejandra](https://github.com/kamadorueda/alejandra) | The Uncompromising Nix Code Formatter | `formatter` | `nix` |
| [ameba](https://github.com/crystal-ameba/ameba) | A static code analysis tool for Crystal | `linter` | `crystal` |
| [asmfmt](https://github.com/klauspost/asmfmt) | Go Assembler Formatter | `formatter` | |
| [asmfmt](https://github.com/klauspost/asmfmt) | Go Assembler Formatter | `formatter` | `go` |
| [astyle](https://gitlab.com/saalen/astyle) | A Free, Fast, and Small Automatic Formatter for C, C++, C++/CLI, Objective-C, C#, and Java Source Code | `formatter` | `c#`, `c++`, `c`, `java`, `objective-c` |
| [auto-optional](https://github.com/Luttik/auto-optional) | Adds the Optional type-hint to arguments where the default value is None | `formatter` | `python` |
| [autocorrect](https://github.com/huacnlee/autocorrect) | A linter and formatter to help you to improve copywriting, correct spaces, words, and punctuations between CJK (Chinese, Japanese, Korean) | `autocorrection` | |
Expand Down Expand Up @@ -319,7 +322,7 @@ mdsf init
| [luaformatter](https://github.com/Koihik/LuaFormatter) | Code formatter for Lua | `formatter` | `lua` |
| [markdownfmt](https://github.com/shurcooL/markdownfmt) | Like gofmt, but for Markdown | `formatter` | `markdown` |
| [markdownlint-cli2](https://github.com/davidanson/markdownlint-cli2) | A fast, flexible, configuration-based command-line interface for linting Markdown/CommonMark files with the markdownlint library | `linter` | `markdown` |
| [markdownlint](https://github.com/davidanson/markdownlint) | A Node.js style checker and lint tool for Markdown/CommonMark files | `linter` | |
| [markdownlint](https://github.com/davidanson/markdownlint) | A Node.js style checker and lint tool for Markdown/CommonMark files | `linter` | `markdown` |
| [markuplint](https://markuplint.dev/) | An HTML linter for all markup developers | `linter` | `html` |
| [mdformat](https://github.com/executablebooks/mdformat) | CommonMark compliant Markdown formatter | `formatter` | `markdwon` |
| [mdslw](https://github.com/razziel89/mdslw) | Prepare your markdown for easy diff'ing! | `formatter` | `markdown` |
Expand Down Expand Up @@ -348,6 +351,7 @@ mdsf init
| [prettier](https://github.com/prettier/prettier) | Prettier is an opinionated code formatter | `formatter` | `css`, `html`, `javascript`, `json`, `typescript` |
| [pretty-php](https://github.com/lkrms/pretty-php) | The opinionated PHP code formatter | `formatter` | `php` |
| [prettypst](https://github.com/antonWetzel/prettypst) | Formatter for Typst | `formatter` | `typst` |
| [prisma](https://www.prisma.io) | Commands for interacting with the prisma ORM | `formatter` | `prisma` |
| [protolint](https://github.com/yoheimuta/protolint) | A pluggable linter and fixer to enforce Protocol Buffer style and conventions | `linter` | `protobuf` |
| [ptop](https://www.freepascal.org/tools/ptop.html) | Free Pascal source formatter | `formatter` | `pascal` |
| [puppet-lint](https://github.com/puppetlabs/puppet-lint) | Check that your Puppet manifests conform to the style guide | `linter` | `puppet` |
Expand Down
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ changelog:
codegen:
just changelog
cargo run --package mdsf-codegen
cargo run --package mdsf-codegen
dist init --yes
just format

Expand Down
7 changes: 7 additions & 0 deletions mdsf/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub mod pint;
pub mod prettier;
pub mod pretty_php;
pub mod prettypst;
pub mod prisma_format;
pub mod protolint;
pub mod ptop;
pub mod puppet_lint;
Expand Down Expand Up @@ -779,6 +780,10 @@ pub enum Tooling {
/// `prettypst $PATH`
Prettypst,

#[serde(rename = "prisma:format")]
/// `prisma format --schema={$PATH_STRING}`
PrismaFormat,

#[serde(rename = "protolint")]
/// `protolint lint -fix $PATH`
Protolint,
Expand Down Expand Up @@ -1232,6 +1237,7 @@ impl Tooling {
Self::Prettier => prettier::run(snippet_path),
Self::PrettyPhp => pretty_php::run(snippet_path),
Self::Prettypst => prettypst::run(snippet_path),
Self::PrismaFormat => prisma_format::run(snippet_path),
Self::Protolint => protolint::run(snippet_path),
Self::Ptop => ptop::run(snippet_path),
Self::PuppetLint => puppet_lint::run(snippet_path),
Expand Down Expand Up @@ -1457,6 +1463,7 @@ impl AsRef<str> for Tooling {
Self::Prettier => "prettier",
Self::PrettyPhp => "pretty_php",
Self::Prettypst => "prettypst",
Self::PrismaFormat => "prisma_format",
Self::Protolint => "protolint",
Self::Ptop => "ptop",
Self::PuppetLint => "puppet_lint",
Expand Down
66 changes: 66 additions & 0 deletions mdsf/src/tools/prisma_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::process::Command;

use crate::{error::MdsfError, execution::execute_command, runners::CommandType};

#[inline]
fn set_prisma_format_args(mut cmd: Command, file_path: &std::path::Path) -> Command {
cmd.arg("format");
let fps = file_path.to_string_lossy();
cmd.arg(format!("--schema={fps}"));
cmd
}

#[inline]
pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfError> {
let commands = [
CommandType::NodeModules("prisma"),
CommandType::Direct("prisma"),
CommandType::Npm("prisma"),
];

for (index, cmd) in commands.iter().enumerate() {
let cmd = set_prisma_format_args(cmd.build(), file_path);
let execution_result = execute_command(cmd, file_path);

if index == commands.len() - 1 {
return execution_result;
}

if let Ok(r) = execution_result {
if !r.0 {
return Ok(r);
}
}
}

Ok((true, None))
}

#[cfg(test)]
mod test_prisma_format {
#[test_with::executable(npx)]
fn test_prisma_format_schema_f14c422420ff523a() {
let input = r#"datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL")
}
"#;
let output = r#"datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL")
}
"#;
let file_ext = crate::fttype::get_file_extension("schema");
let snippet =
crate::execution::setup_snippet(input, &file_ext).expect("it to create a snippet file");
let result = crate::tools::prisma_format::run(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");
assert_eq!(result, output);
}
}
5 changes: 5 additions & 0 deletions schemas/v0.3.1-dev/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,11 @@
"type": "string",
"enum": ["prettypst"]
},
{
"description": "`prisma format --schema={$PATH_STRING}`",
"type": "string",
"enum": ["prisma:format"]
},
{
"description": "`protolint lint -fix $PATH`",
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion tools/asmfmt/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"description": "Go Assembler Formatter",
"homepage": "https://github.com/klauspost/asmfmt",
"languages": [],
"languages": ["go"],
"name": null,
"npm": null,
"php": null,
Expand Down
2 changes: 1 addition & 1 deletion tools/markdownlint/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"description": "A Node.js style checker and lint tool for Markdown/CommonMark files",
"homepage": "https://github.com/davidanson/markdownlint",
"languages": [],
"languages": ["markdown"],
"name": null,
"npm": "markdownlint",
"php": null,
Expand Down
22 changes: 22 additions & 0 deletions tools/prisma/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "../tool.schema.json",
"binary": "prisma",
"categories": ["formatter"],
"commands": {
"format": ["format", "--schema={$PATH_STRING}"]
},
"description": "Commands for interacting with the prisma ORM",
"homepage": "https://www.prisma.io",
"languages": ["prisma"],
"name": null,
"npm": "prisma",
"php": null,
"tests": [
{
"command": "format",
"language": "schema",
"test_input": "datasource db {\n provider = \"postgresql\"\n url = env(\"DATABASE_URL\")\n directUrl = env(\"DIRECT_DATABASE_URL\")\n}\n\n\n",
"test_output": "datasource db {\n provider = \"postgresql\"\n url = env(\"DATABASE_URL\")\n directUrl = env(\"DIRECT_DATABASE_URL\")\n}\n"
}
]
}

0 comments on commit 521cd03

Please sign in to comment.