@aiot-nexus/plugin-sdk 1.0.2 → 1.1.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.
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.beginManifestAndRendererDeployment = exports.beginRendererDeployment = exports.beginPluginPackageDeployment = exports.resolveNexusExecutable = void 0;
27
+ const crypto = __importStar(require("crypto"));
28
+ const fs = __importStar(require("fs"));
29
+ const path = __importStar(require("path"));
30
+ const dev_1 = require("./dev");
31
+ function resolveNexusExecutable(hostPath) {
32
+ const resolved = path.resolve(hostPath);
33
+ if (!fs.existsSync(resolved)) {
34
+ throw new dev_1.PluginDevError("Nexus host path does not exist: " + resolved);
35
+ }
36
+ const stat = fs.statSync(resolved);
37
+ const executable = stat.isDirectory() ? path.join(resolved, "nexus.exe") : resolved;
38
+ if (!fs.existsSync(executable) || !fs.statSync(executable).isFile()) {
39
+ throw new dev_1.PluginDevError("Nexus executable is missing: " + executable);
40
+ }
41
+ if (process.platform === "win32" && path.extname(executable).toLowerCase() !== ".exe") {
42
+ throw new dev_1.PluginDevError("Nexus host must be a Windows executable: " + executable);
43
+ }
44
+ return executable;
45
+ }
46
+ exports.resolveNexusExecutable = resolveNexusExecutable;
47
+ function beginPluginPackageDeployment(packageDirectory, nexusExecutable, pluginId) {
48
+ const pluginsDirectory = path.join(path.dirname(nexusExecutable), "Plugins");
49
+ fs.mkdirSync(pluginsDirectory, { recursive: true });
50
+ const targetDirectory = path.join(pluginsDirectory, pluginId);
51
+ assertDirectChild(pluginsDirectory, targetDirectory);
52
+ return activateReplacements([prepareReplacement(packageDirectory, targetDirectory)]);
53
+ }
54
+ exports.beginPluginPackageDeployment = beginPluginPackageDeployment;
55
+ function beginRendererDeployment(rendererDirectory, deployedPluginDirectory) {
56
+ const targetDirectory = path.join(deployedPluginDirectory, "renderer");
57
+ assertDirectChild(deployedPluginDirectory, targetDirectory);
58
+ return activateReplacements([prepareReplacement(rendererDirectory, targetDirectory)]);
59
+ }
60
+ exports.beginRendererDeployment = beginRendererDeployment;
61
+ function beginManifestAndRendererDeployment(manifestPath, rendererDirectory, deployedPluginDirectory) {
62
+ const targetManifestPath = path.join(deployedPluginDirectory, "manifest.json");
63
+ const targetRendererDirectory = path.join(deployedPluginDirectory, "renderer");
64
+ assertDirectChild(deployedPluginDirectory, targetManifestPath);
65
+ assertDirectChild(deployedPluginDirectory, targetRendererDirectory);
66
+ const prepared = prepareReplacements([
67
+ { sourcePath: rendererDirectory, targetPath: targetRendererDirectory },
68
+ { sourcePath: manifestPath, targetPath: targetManifestPath }
69
+ ]);
70
+ return activateReplacements(prepared);
71
+ }
72
+ exports.beginManifestAndRendererDeployment = beginManifestAndRendererDeployment;
73
+ function prepareReplacements(items) {
74
+ const prepared = [];
75
+ try {
76
+ for (const item of items) {
77
+ prepared.push(prepareReplacement(item.sourcePath, item.targetPath));
78
+ }
79
+ return prepared;
80
+ }
81
+ catch (error) {
82
+ rollbackPrepared(prepared);
83
+ throw error;
84
+ }
85
+ }
86
+ function prepareReplacement(sourcePath, targetPath) {
87
+ if (!fs.existsSync(sourcePath)) {
88
+ throw new dev_1.PluginDevError("Plugin deployment source is missing: " + sourcePath);
89
+ }
90
+ const parentDirectory = path.dirname(targetPath);
91
+ fs.mkdirSync(parentDirectory, { recursive: true });
92
+ const suffix = String(process.pid) + "-" + crypto.randomBytes(8).toString("hex");
93
+ const safeName = path.basename(targetPath).replace(/[^A-Za-z0-9._-]/g, "_");
94
+ const stagedPath = path.join(parentDirectory, ".nexus-plugin-dev-" + safeName + "-stage-" + suffix);
95
+ const backupPath = path.join(parentDirectory, ".nexus-plugin-dev-" + safeName + "-backup-" + suffix);
96
+ try {
97
+ copyPath(sourcePath, stagedPath);
98
+ }
99
+ catch (error) {
100
+ removePath(stagedPath);
101
+ throw new dev_1.PluginDevError("Could not stage plugin deployment: " + errorMessage(error));
102
+ }
103
+ return {
104
+ targetPath,
105
+ stagedPath,
106
+ backupPath,
107
+ targetExisted: fs.existsSync(targetPath),
108
+ activated: false
109
+ };
110
+ }
111
+ function activateReplacements(prepared) {
112
+ try {
113
+ for (const item of prepared) {
114
+ if (item.targetExisted) {
115
+ fs.renameSync(item.targetPath, item.backupPath);
116
+ }
117
+ try {
118
+ fs.renameSync(item.stagedPath, item.targetPath);
119
+ item.activated = true;
120
+ }
121
+ catch (error) {
122
+ if (item.targetExisted && fs.existsSync(item.backupPath)) {
123
+ fs.renameSync(item.backupPath, item.targetPath);
124
+ }
125
+ throw error;
126
+ }
127
+ }
128
+ }
129
+ catch (error) {
130
+ rollbackPrepared(prepared);
131
+ throw new dev_1.PluginDevError("Could not activate staged plugin deployment: " + errorMessage(error));
132
+ }
133
+ let finished = false;
134
+ return {
135
+ targetPath: prepared[0].targetPath,
136
+ commit() {
137
+ if (finished) {
138
+ return;
139
+ }
140
+ for (const item of prepared) {
141
+ removePath(item.backupPath);
142
+ removePath(item.stagedPath);
143
+ }
144
+ finished = true;
145
+ },
146
+ rollback() {
147
+ if (finished) {
148
+ return;
149
+ }
150
+ rollbackPrepared(prepared);
151
+ finished = true;
152
+ }
153
+ };
154
+ }
155
+ function rollbackPrepared(prepared) {
156
+ for (const item of prepared.slice().reverse()) {
157
+ if (item.activated) {
158
+ removePath(item.targetPath);
159
+ item.activated = false;
160
+ }
161
+ if (item.targetExisted && fs.existsSync(item.backupPath)) {
162
+ fs.renameSync(item.backupPath, item.targetPath);
163
+ }
164
+ removePath(item.stagedPath);
165
+ removePath(item.backupPath);
166
+ }
167
+ }
168
+ function copyPath(sourcePath, targetPath) {
169
+ const stat = fs.lstatSync(sourcePath);
170
+ if (stat.isSymbolicLink()) {
171
+ throw new Error("Symbolic links are not supported: " + sourcePath);
172
+ }
173
+ if (stat.isFile()) {
174
+ fs.copyFileSync(sourcePath, targetPath);
175
+ return;
176
+ }
177
+ if (!stat.isDirectory()) {
178
+ throw new Error("Unsupported deployment entry: " + sourcePath);
179
+ }
180
+ fs.mkdirSync(targetPath, { recursive: true });
181
+ for (const entry of fs.readdirSync(sourcePath, { withFileTypes: true })) {
182
+ copyPath(path.join(sourcePath, entry.name), path.join(targetPath, entry.name));
183
+ }
184
+ }
185
+ function removePath(value) {
186
+ fs.rmSync(value, { recursive: true, force: true });
187
+ }
188
+ function assertDirectChild(parentDirectory, targetPath) {
189
+ if (path.dirname(path.resolve(targetPath)).toLowerCase() !== path.resolve(parentDirectory).toLowerCase()) {
190
+ throw new dev_1.PluginDevError("Plugin deployment target escaped its expected directory: " + targetPath);
191
+ }
192
+ }
193
+ function errorMessage(error) {
194
+ return error instanceof Error ? error.message : String(error);
195
+ }
@@ -0,0 +1,21 @@
1
+ import { type PluginDebugSelection, type PluginDebugTarget, type PluginMainInspectorMode } from "./dev";
2
+ import { NexusDevControlServer } from "./nexus-dev-control";
3
+ import type { PluginManifest } from "./types";
4
+ export interface PluginNexusDevOptions extends PluginDebugSelection {
5
+ hostPath: string;
6
+ openDevTools: boolean;
7
+ inspectorMode?: PluginMainInspectorMode;
8
+ inspectorPort?: number;
9
+ }
10
+ export interface PluginNexusDevIo {
11
+ stdout(message: string): void;
12
+ stderr(message: string): void;
13
+ }
14
+ export type PluginSourceChangeKind = "renderer" | "manifest" | "restart";
15
+ export declare function runPluginNexusDev(options: PluginNexusDevOptions, io: PluginNexusDevIo): Promise<number>;
16
+ export declare function classifyPluginSourceChange(projectDirectory: string, changedPath: string): PluginSourceChangeKind | undefined;
17
+ export declare function manifestChangeRequiresRestart(previous: PluginManifest, next: PluginManifest): boolean;
18
+ export declare function mergePluginSourceChange(current: PluginSourceChangeKind | undefined, incoming: PluginSourceChangeKind): PluginSourceChangeKind;
19
+ export declare function createNexusHostArguments(control: Pick<NexusDevControlServer, "pipePath" | "token">, target: PluginDebugTarget, options: Pick<PluginNexusDevOptions, "openDevTools" | "inspectorMode" | "inspectorPort">): string[];
20
+ export declare function isRetryablePluginDeploymentError(error: unknown): boolean;
21
+ export declare function shouldSuppressNativeBuildEvent(changedPath: string, buildRunning: boolean, ignoreUntil: number, now?: number): boolean;