Skip to content

Commit 816ecad

Browse files
author
hersveit
committed
find the engine based on the files in the engines dir
1 parent 4435b71 commit 816ecad

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

api/buildPrepare.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const { execSync } = require('child_process');
43

54
const root = path.resolve(__dirname);
65
const dist = path.resolve(__dirname, 'dist');
6+
const enginesDir = path.resolve(__dirname, '../libs/repository/node_modules/@prisma/engines');
77

88
if (!fs.existsSync(dist)) {
99
fs.mkdirSync(dist);
@@ -14,19 +14,13 @@ fs.copyFileSync(
1414
path.resolve(dist, 'schema.prisma'),
1515
);
1616

17-
const enginesList = {
18-
win32: 'node_modules/@prisma/engines/query_engine-windows.dll.node',
19-
ubuntu: 'node_modules/@prisma/engines/libquery_engine-debian-openssl-3.0.x.so.node',
20-
linux: 'node_modules/@prisma/engines/libquery_engine-linux-musl-openssl-3.0.x.so.node',
21-
};
22-
2317
let engine = getEngine();
2418
if (!engine) {
2519
throw `Cannot find engine for '${process.platform}'`;
2620
}
2721

28-
const engineFile = path.resolve(root, `../libs/repository/${engine}`);
29-
fs.copyFileSync(engineFile, path.resolve(dist, path.basename(engineFile)));
22+
const engineFile = path.resolve(enginesDir, engine);
23+
fs.copyFileSync(engineFile, path.resolve(dist, engine));
3024

3125
function getEngine() {
3226
const libConfigFile = path.resolve(__dirname, '.prisma-lib');
@@ -35,16 +29,25 @@ function getEngine() {
3529
return JSON.parse(fs.readFileSync(libConfigFile)).lib;
3630
}
3731

38-
const platform = process.platform;
39-
let engine = enginesList[platform];
40-
if (platform === 'linux') {
41-
const lsb = execSync('lsb_release -a | grep Description').toString();
42-
if (new RegExp(/.*ubuntu.*/gi).test(lsb)) {
43-
engine = enginesList.ubuntu;
44-
}
32+
const files = fs.readdirSync(enginesDir);
33+
const findEngine = (regexp) =>
34+
files.find(
35+
(f) => !fs.statSync(path.resolve(enginesDir, f)).isDirectory() && regexp.test(f),
36+
);
37+
38+
let engine = null;
39+
switch (process.platform) {
40+
case 'linux':
41+
engine = findEngine(new RegExp(/.*\.so\.node$/gi));
42+
break;
43+
case 'win32':
44+
engine = findEngine(new RegExp(/.*\.dll\.node$/gi));
45+
break;
4546
}
4647

47-
fs.writeFileSync(libConfigFile, JSON.stringify({ lib: engine }));
48+
if (engine !== null) {
49+
fs.writeFileSync(libConfigFile, JSON.stringify({ lib: engine }));
50+
}
4851

4952
return engine;
5053
}

0 commit comments

Comments
 (0)