Skip to content

Commit

Permalink
fix(generator): fixed Windows system cannot find the file specified
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Mar 6, 2023
1 parent 9062441 commit 6446739
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
58 changes: 50 additions & 8 deletions bin/generator/prisma_info.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';
import 'dart:convert' as convert;

import 'package:path/path.dart';

class PrismaInfo {
/// Prisma version
final String version;
Expand All @@ -12,20 +14,60 @@ class PrismaInfo {

/// Lookup the Prisma version and platform.
factory PrismaInfo.lookup(String packageManager) {
final pm = NodePackageManager(packageManager.trim());

final result = Process.runSync(
packageManager,
[
'exec',
if (packageManager == 'npm') '--',
'prisma',
'version',
'--json'
],
pm.toExecutable(),
['exec', if (pm.isNpm) '--', 'prisma', 'version', '--json'],
stdoutEncoding: convert.utf8,
);

// Find JSON block.
final json = result.stdout.toString().split('{').last.split('}').first;
final map = convert.json.decode('{$json}');

return PrismaInfo._(map['prisma'], map['current-platform']);
}
}

class NodePackageManager {
static final separator = Platform.isWindows ? ';' : ':';

final String packageManager;

const NodePackageManager(this.packageManager);

Iterable<String> get executables sync* {
yield packageManager;
yield '$packageManager.cmd';
yield '$packageManager.ps1';
yield '$packageManager.bat';
yield '$packageManager.exe';
}

Iterable<String> get paths sync* {
yield Directory.current.path;
yield* Platform.environment['PATH']?.split(separator) ?? [];
}

/// Find the executable for the package manager full path.
String toExecutable() {
// If the package manager is already an executable, return it.
if (File(packageManager).existsSync()) {
return packageManager;
}

for (final path in paths) {
for (final executable in executables) {
final full = join(path.trim(), executable);
if (File(full).existsSync()) {
return full;
}
}
}

throw StateError('Unable to find $packageManager executable.');
}

bool get isNpm => basename(toExecutable()).toLowerCase().startsWith('npm');
}
13 changes: 11 additions & 2 deletions bin/orm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'generator/prisma_info.dart';
void main(Iterable<String> args) async {
final parase = ArgParser();

// Register the `ecosystem` option.
// Register the `package-manager` option.
parase.addOption(
'package-manager',
abbr: 'p',
Expand All @@ -21,6 +21,13 @@ void main(Iterable<String> args) async {
defaultsTo: 'npm',
);

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

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

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

final completer = Completer<Generator>();
Library((LibraryBuilder updates) {
Expand Down
26 changes: 26 additions & 0 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ generator client {
}
```

`--package-manager` is optional, the default value is `npm`, It also has an alias `-p`:

```prisma
generator client {
provider = "dart run orm -p <package-manager>"
}
```

### 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

2 comments on commit 6446739

@vercel
Copy link

@vercel vercel bot commented on 6446739 Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@medz
Copy link
Owner Author

@medz medz commented on 6446739 Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.