@saltcorn/plugins-loader 0.9.5-beta.9 → 0.9.6-beta.0

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 +3 -2
  2. package/plugin_installer.js +44 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/plugins-loader",
3
- "version": "0.9.5-beta.9",
3
+ "version": "0.9.6-beta.0",
4
4
  "description": "Saltcorn plugin loader",
5
5
  "homepage": "https://saltcorn.com",
6
6
  "scripts": {
@@ -10,7 +10,8 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "env-paths": "^2.2.0",
13
- "@saltcorn/data": "0.9.5-beta.9"
13
+ "npm-registry-fetch": "16.0.0",
14
+ "@saltcorn/data": "0.9.6-beta.0"
14
15
  },
15
16
  "author": "Christian Hugo",
16
17
  "license": "MIT",
@@ -25,6 +25,17 @@ const readPackageJson = async (filePath) => {
25
25
  else return null;
26
26
  };
27
27
 
28
+ const npmInstallNeeded = (oldPckJSON, newPckJSON) => {
29
+ const oldDeps = oldPckJSON.dependencies || {};
30
+ const oldDevDeps = oldPckJSON.devDependencies || {};
31
+ const newDeps = newPckJSON.dependencies || {};
32
+ const newDevDeps = newPckJSON.devDependencies || {};
33
+ return (
34
+ JSON.stringify(oldDeps) !== JSON.stringify(newDeps) ||
35
+ JSON.stringify(oldDevDeps) !== JSON.stringify(newDevDeps)
36
+ );
37
+ };
38
+
28
39
  class PluginInstaller {
29
40
  constructor(plugin, opts = {}) {
30
41
  this.plugin = plugin;
@@ -55,9 +66,14 @@ class PluginInstaller {
55
66
  const installer = async () => {
56
67
  if (await this.prepPluginsFolder(force, pckJSON)) {
57
68
  const tmpPckJSON = await this.removeDependencies(
58
- await readPackageJson(this.tempPckJsonPath)
69
+ await readPackageJson(this.tempPckJsonPath),
70
+ true
59
71
  );
60
- await this.npmInstall(tmpPckJSON);
72
+ if (
73
+ !pckJSON ||
74
+ npmInstallNeeded(await this.removeDependencies(pckJSON), tmpPckJSON)
75
+ )
76
+ await this.npmInstall(tmpPckJSON);
61
77
  await this.movePlugin();
62
78
  if (await tarballExists(this.plugin)) await removeTarball(this.plugin);
63
79
  }
@@ -88,7 +104,7 @@ class PluginInstaller {
88
104
  version: pckJSON.version,
89
105
  plugin_module: module,
90
106
  location: this.pluginDir,
91
- name: this.name,
107
+ name: this.plugin.name,
92
108
  loadedWithReload,
93
109
  };
94
110
  }
@@ -100,29 +116,30 @@ class PluginInstaller {
100
116
 
101
117
  async prepPluginsFolder(force, pckJSON) {
102
118
  let wasLoaded = false;
119
+ const folderExists = await pathExists(this.pluginDir);
103
120
  switch (this.plugin.source) {
104
121
  case "npm":
105
122
  if (
106
123
  (force && !(await this.versionIsInstalled(pckJSON))) ||
107
- !(await pathExists(this.pluginDir))
124
+ !folderExists
108
125
  ) {
109
126
  wasLoaded = await downloadFromNpm(this.plugin, this.tempDir, pckJSON);
110
127
  }
111
128
  break;
112
129
  case "github":
113
- if (force || !(await pathExists(this.pluginDir))) {
130
+ if (force || !folderExists) {
114
131
  await downloadFromGithub(this.plugin, this.tempDir);
115
132
  wasLoaded = true;
116
133
  }
117
134
  break;
118
135
  case "local":
119
- if (force || !(await pathExists(this.pluginDir))) {
136
+ if (force || !folderExists) {
120
137
  await copy(this.plugin.location, this.tempDir);
121
138
  wasLoaded = true;
122
139
  }
123
140
  break;
124
141
  case "git":
125
- if (force || !(await pathExists(this.pluginDir))) {
142
+ if (force || !folderExists) {
126
143
  await gitPullOrClone(this.plugin, this.tempDir);
127
144
  this.pckJsonPath = join(this.pluginDir, "package.json");
128
145
  wasLoaded = true;
@@ -176,7 +193,7 @@ class PluginInstaller {
176
193
  }
177
194
  }
178
195
 
179
- async removeDependencies(tmpPckJSON) {
196
+ async removeDependencies(tmpPckJSON, writeToDisk) {
180
197
  const pckJSON = { ...tmpPckJSON };
181
198
  const oldDepsLength = Object.keys(pckJSON.dependencies || {}).length;
182
199
  const oldDevDepsLength = Object.keys(pckJSON.devDependencies || {}).length;
@@ -188,8 +205,9 @@ class PluginInstaller {
188
205
  if (pckJSON.dependencies) staticsRemover(pckJSON.dependencies);
189
206
  if (pckJSON.devDependencies) staticsRemover(pckJSON.devDependencies);
190
207
  if (
191
- Object.keys(pckJSON.dependencies || {}).length !== oldDepsLength ||
192
- Object.keys(pckJSON.devDependencies || {}).length !== oldDevDepsLength
208
+ writeToDisk &&
209
+ (Object.keys(pckJSON.dependencies || {}).length !== oldDepsLength ||
210
+ Object.keys(pckJSON.devDependencies || {}).length !== oldDevDepsLength)
193
211
  )
194
212
  await writeFile(
195
213
  join(this.tempDir, "package.json"),
@@ -199,6 +217,7 @@ class PluginInstaller {
199
217
  }
200
218
 
201
219
  async npmInstall(pckJSON) {
220
+ getState().log(5, `NPM install plugin: ${pckJSON.name}`);
202
221
  const isWindows = process.platform === "win32";
203
222
  if (
204
223
  Object.keys(pckJSON.dependencies || {}).length > 0 ||
@@ -222,12 +241,24 @@ class PluginInstaller {
222
241
 
223
242
  async movePlugin() {
224
243
  const isWindows = process.platform === "win32";
244
+ const copyMove = async () => {
245
+ await cp(this.tempDir, this.pluginDir, { recursive: true, force: true });
246
+ try {
247
+ await rm(this.tempDir, { recursive: true });
248
+ } catch (error) {
249
+ getState().log(2, `Error removing temp folder ${this.tempDir}`);
250
+ }
251
+ };
225
252
  if (await pathExists(this.pluginDir))
226
253
  await rm(this.pluginDir, { recursive: true });
227
254
  await mkdir(this.pluginDir, { recursive: true });
228
- if (!isWindows) await rename(this.tempDir, this.pluginDir);
229
- else
230
- await cp(this.tempDir, this.pluginDir, { recursive: true, force: true });
255
+ if (!isWindows) {
256
+ try {
257
+ await rename(this.tempDir, this.pluginDir);
258
+ } catch (error) {
259
+ await copyMove();
260
+ }
261
+ } else await copyMove();
231
262
  }
232
263
  }
233
264