Skip to content

Commit

Permalink
feat(generator): Only support npm
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Mar 7, 2023
1 parent d717bb0 commit 41adccb
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 190 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

11 changes: 1 addition & 10 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@ on:
- pull_request
jobs:
build:
strategy:
fail-fast: false
matrix:
os:
# - ubuntu-latest
- windows-latest
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: latest
cache: "npm"
- uses: dart-lang/setup-dart@v1
- uses: pnpm/action-setup@v2
with:
version: latest
- run: dart pub get
- run: npm ci
- run: dart test
17 changes: 17 additions & 0 deletions .github/workflows/test_windows.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: test on Windows
on:
- push
- pull_request
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: latest
cache: "npm"
- uses: dart-lang/setup-dart@v1
- run: dart pub get
- run: npm ci
- run: dart test test_windows
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ docs/.vitepress/*
pubspec.lock

# Development files or directories.
/lib/prisma_client.*
lib/prisma_client.*
prisma/prisma.sqlite
20 changes: 8 additions & 12 deletions bin/generator/prisma_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ const Iterable<String> _extensions = <String>[
'sh', // Shell Script
];

String findNodePackageManagerExecutable(String packageManager) {
if (File(packageManager).existsSync()) {
return packageManager;
String findNpmExecutable(String executable) {
if (File(executable).existsSync()) {
return executable;
}

return _globalPaths.expand((path) sync* {
// Generate all possible executable paths.
yield* _extensions
.map((extension) => join(path, '$packageManager.$extension'));
yield* _extensions.map((extension) => join(path, '$executable.$extension'));

// Generate without extension executable paths.
yield join(path, packageManager);
yield join(path, executable);
}).firstWhere((executable) => File(executable).existsSync(), orElse: () {
// If no executable was found, throw an error.
throw StateError('Unable to find $packageManager executable.');
throw StateError('Unable to find NPM executable.');
});
}

Expand All @@ -47,23 +46,20 @@ class PrismaInfo {
const PrismaInfo._(this.version, this.platform);

/// Lookup the Prisma version and platform.
factory PrismaInfo.lookup(String packageManager) {
factory PrismaInfo.lookup(String excutable) {
final String result = Process.runSync(
findNodePackageManagerExecutable(packageManager),
findNpmExecutable(excutable),
['exec', 'prisma', 'version'],
stdoutEncoding: convert.utf8,
).stdout;

print(result);

final entries = result
.split('\n')
.where((element) => element.trim().isNotEmpty)
.map((e) => e.split(':'))
.where((element) => element.length == 2)
.map((e) => MapEntry(e[0], e[1]))
.map((e) => MapEntry(e.key.trim().toLowerCase(), e.value.trim()));
print(entries);
final json = Map.fromEntries(entries);

return PrismaInfo._(json['prisma']!, json['current platform']!);
Expand Down
19 changes: 3 additions & 16 deletions bin/orm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@ import 'generator/prisma_info.dart';
void main(Iterable<String> args) async {
final parase = ArgParser();

// Register the `package-manager` option.
parase.addOption(
'package-manager',
abbr: 'p',
help: 'The NodeJS package manager to use.',
allowed: ['npm', 'yarn', 'pnpm'],
'npm',
defaultsTo: 'npm',
);

// Register the `pm-path` option.
parase.addOption(
'package-manager-executable',
abbr: 'e',
help: 'The path to the NodeJS package manager executable.',
help: 'The npm executable.',
);

// Register the `help` option.
Expand All @@ -42,10 +32,7 @@ void main(Iterable<String> args) async {
// Print the help information if requested.
if (results['help'] as bool) return stdout.writeln(parase.usage);

final executable = results['package-manager-executable'] as String?;
final pm = executable ?? results['package-manager'] as String;
final info = PrismaInfo.lookup(pm);

final info = PrismaInfo.lookup(results['npm']);
final completer = Completer<Generator>();
Library((LibraryBuilder updates) {
updates.name = 'prisma.client';
Expand Down
66 changes: 14 additions & 52 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,42 @@

Prisma client Dart requires no other configuration, just configure the data model in the `schema.prisma` file, but there are always some options!

## Choose Node's package management tool
## NPM executable

Prisma client Dart depends on Node's package management tool, because it needs to interact with Prisma CLI, so you need to choose a Node package management tool, currently supported package management tools are:
By default, you are not required to specify the path to the npm executable. We will search in the global `$PATH`.

- npm
- yarn
- pnpm
If your NPM is not installed globally (after installing NodeJS globally, NPM is available globally), then you need to tell the generator the path of NPM:

You can configure it in the `schema.prisma` file:

::: code-group

```prisma [npm]
generator client {
provider = "dart run orm --package-manager npm"
}
```

```prisma [yarn]
```prisma
generator client {
provider = "dart run orm --package-manager yarn"
provider = "dart run orm --npm=<path>"
}
```

```prisma [pnpm]
generator client {
provider = "dart run orm --package-manager pnpm"
}
```
## Prisma client output

:::
By default, the generated Prisma client Dart code will be placed in the `lib/prisma_client.dart` file.

`--package-manager` is optional, the default value is `npm`, It also has an alias `-p`:
You can change the output path by configuring the `output` option:

::: code-group

```prisma [npm]
generator client {
provider = "dart run orm -p npm"
}
```

```prisma [yarn]
```prisma [Generage into directory]
generator client {
provider = "dart run orm -p yarn"
provider = "dart run orm"
output = "../lib/generated" // Output path is `lib/generated/prisma_client.dart`
}
```

```prisma [pnpm]
```prisma [Generage into file]
generator client {
provider = "dart run orm -p pnpm"
provider = "dart run orm"
output = "../lib/custom_generated.dart" // Output path is `lib/custom_generated.dart
}
```

:::

### Node package manager executable

If you have multiple versions of the Node package manager installed, or if you don't have the Node package manager installed globally. Please use the `-e` parameter to tell the generator:

```prisma
generator client {
provider = "dart run orm -e <executable>"
}
```

Example:

```prisma
generator client {
provider = "dart run orm -e node_modules/.bin/pnpm"
}
```

## About database connection

After the Prisma CLI initializes the project, there will be a `.env` file, which contains the configuration of the database connection. You can configure the database connection in this file:
Expand Down
59 changes: 4 additions & 55 deletions docs/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,10 @@ dart create hello

Prisma CLI is a command-line tool officially provided by Prisma for managing Prisma projects. It helps you generate Prisma clients, generate the models required by Prisma clients, and perform database migrations.

::: code-group

```bash [npm]
```bash
npm i prisma
```

```bash [yarn]
yarn add prisma
```

```bash [pnpm]
pnpm add prisma
```

:::

> Since Prisma CLI is a Node.js application, you need to have Node.js installed. If you are not familiar with Node.js, you can refer to [Node.js official documentation](https://nodejs.org/).
>
> Also, you need to add the `node_modules` directory to your `.gitignore` file to avoid committing the `node_modules` directory to the Git repository.
Expand All @@ -45,22 +33,10 @@ pnpm add prisma

A Prisma project is a project that contains the Prisma client and the models required by the Prisma client. You can initialize a Prisma project using the Prisma CLI.

::: code-group

```bash [npm]
```bash
npx prisma init
```

```bash [yarn]
yarn prisma init
```

```bash [pnpm]
pnpx prisma init
```

:::

You will get a `.env` file that contains configuration information for the Prisma project.

```bash
Expand Down Expand Up @@ -112,22 +88,10 @@ model User {

## 7. Push the model to the database

::: code-group

```bash [npm]
```bash
npx prisma db push
```

```bash [yarn]
yarn prisma db push
```

```bash [pnpm]
pnpx prisma db push
```

:::

## 8. Install `build_runner` and `json_serializable`

```bash
Expand All @@ -138,23 +102,8 @@ dart pub add json_annotation # optional, but recommended

## 9. Generate Prisma client

::: code-group

```bash [npm]
npx prisma generate
```

```bash [yarn]
yarn prisma generate
```

```bash [pnpm]
pnpx prisma generate
```

:::

```bash
npx prisma generate
dart run build_runner build
```

Expand Down
10 changes: 2 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
provider = "sqlite"
url = "file:./prisma.sqlite"
}

model User {
id Int @id @default(autoincrement())
name String @unique
createdAt DateTime @default(now())
posts Post[]
role UserType @default(user)
}

enum UserType {
user @map("user1")
admin @map("user2")
}

model Post {
Expand Down

0 comments on commit 41adccb

Please sign in to comment.