@saltcorn/plugins-loader 0.9.5-beta.6 → 0.9.5-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +5 -1
  2. package/plugin_installer.js +38 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/plugins-loader",
3
- "version": "0.9.5-beta.6",
3
+ "version": "0.9.5-beta.8",
4
4
  "description": "Saltcorn plugin loader",
5
5
  "homepage": "https://saltcorn.com",
6
6
  "scripts": {
@@ -8,6 +8,10 @@
8
8
  "tsc": "echo 'No TypeScript build'",
9
9
  "clean": "echo 'No TypeScript build'"
10
10
  },
11
+ "dependencies": {
12
+ "env-paths": "^2.2.0",
13
+ "@saltcorn/data": "0.9.5-beta.8"
14
+ },
11
15
  "author": "Christian Hugo",
12
16
  "license": "MIT",
13
17
  "main": "index.js",
@@ -8,7 +8,9 @@ const {
8
8
  tarballExists,
9
9
  removeTarball,
10
10
  } = require("./download_utils");
11
+ const { getState } = require("@saltcorn/data/db/state");
11
12
  const { rm, rename, cp, readFile } = require("fs").promises;
13
+ const envPaths = require("env-paths");
12
14
 
13
15
  const staticDeps = ["@saltcorn/markup", "@saltcorn/data", "jest"];
14
16
  const fixedPlugins = ["@saltcorn/base-plugin", "@saltcorn/sbadmin2"];
@@ -27,7 +29,8 @@ class PluginInstaller {
27
29
  constructor(plugin, opts = {}) {
28
30
  this.plugin = plugin;
29
31
  this.rootFolder = opts.rootFolder || process.cwd();
30
- this.tempRootFolder = opts.tempRootFolder || process.cwd();
32
+ this.tempRootFolder =
33
+ opts.tempRootFolder || envPaths("saltcorn", { suffix: "tmp" }).temp;
31
34
  const tokens =
32
35
  plugin.source === "npm"
33
36
  ? plugin.location.split("/")
@@ -49,20 +52,44 @@ class PluginInstaller {
49
52
  return { plugin_module: require(this.plugin.location) };
50
53
 
51
54
  let pckJSON = await readPackageJson(this.pckJsonPath);
52
- if (await this.prepPluginsFolder(force, pckJSON)) {
53
- const tmpPckJSON = await this.removeDependencies(
54
- await readPackageJson(this.tempPckJsonPath)
55
+ const installer = async () => {
56
+ if (await this.prepPluginsFolder(force, pckJSON)) {
57
+ const tmpPckJSON = await this.removeDependencies(
58
+ await readPackageJson(this.tempPckJsonPath)
59
+ );
60
+ await this.npmInstall(tmpPckJSON);
61
+ await this.movePlugin();
62
+ if (await tarballExists(this.plugin)) await removeTarball(this.plugin);
63
+ }
64
+ pckJSON = await readPackageJson(this.pckJsonPath);
65
+ };
66
+ await installer();
67
+ let module = null;
68
+ let loadedWithReload = false;
69
+ try {
70
+ // try importing it and if it fails, remove and try again
71
+ // could happen when there is a directory with a valid package.json
72
+ // but without a valid node modules folder
73
+ module = await this.loadMainFile(pckJSON);
74
+ } catch (e) {
75
+ getState().log(
76
+ 2,
77
+ `Error loading plugin ${this.plugin.name}. Removing and trying again.`
55
78
  );
56
- await this.npmInstall(tmpPckJSON);
57
- await this.movePlugin();
58
- if (await tarballExists(this.plugin)) await removeTarball(this.plugin);
79
+ if (force) {
80
+ await this.remove();
81
+ pckJSON = null;
82
+ await installer();
83
+ }
84
+ module = await this.loadMainFile(pckJSON, true);
85
+ loadedWithReload = true;
59
86
  }
60
- pckJSON = await readPackageJson(this.pckJsonPath);
61
87
  return {
62
88
  version: pckJSON.version,
63
- plugin_module: await this.loadMainFile(pckJSON),
89
+ plugin_module: module,
64
90
  location: this.pluginDir,
65
91
  name: this.name,
92
+ loadedWithReload,
66
93
  };
67
94
  }
68
95
 
@@ -134,7 +161,7 @@ class PluginInstaller {
134
161
  }
135
162
  }
136
163
 
137
- async loadMainFile(pckJSON) {
164
+ async loadMainFile(pckJSON, reload) {
138
165
  const isWindows = process.platform === "win32";
139
166
  if (process.env.NODE_ENV === "test") {
140
167
  // in jest, downgrad to require
@@ -142,7 +169,7 @@ class PluginInstaller {
142
169
  } else {
143
170
  const res = await import(
144
171
  `${isWindows ? `file://` : ""}${normalize(
145
- join(this.pluginDir, pckJSON.main)
172
+ join(this.pluginDir, pckJSON.main + (reload ? "?reload=true" : ""))
146
173
  )}`
147
174
  );
148
175
  return res.default;