@amaster.ai/cli 1.1.28 → 1.1.30
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.
- package/README.md +67 -29
- package/dist/cli.cjs +505 -11
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +505 -11
- package/dist/cli.js.map +1 -1
- package/package.json +2 -1
package/dist/cli.cjs
CHANGED
|
@@ -12,7 +12,8 @@ var os = require('os');
|
|
|
12
12
|
var tar = require('tar');
|
|
13
13
|
var http = require('http');
|
|
14
14
|
var child_process = require('child_process');
|
|
15
|
-
var
|
|
15
|
+
var util = require('util');
|
|
16
|
+
var YAML2 = require('yaml');
|
|
16
17
|
var ts = require('typescript');
|
|
17
18
|
|
|
18
19
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
@@ -37,7 +38,7 @@ function _interopNamespace(e) {
|
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
var tar__namespace = /*#__PURE__*/_interopNamespace(tar);
|
|
40
|
-
var
|
|
41
|
+
var YAML2__default = /*#__PURE__*/_interopDefault(YAML2);
|
|
41
42
|
var ts__default = /*#__PURE__*/_interopDefault(ts);
|
|
42
43
|
|
|
43
44
|
var EXCLUDED_NAMES = /* @__PURE__ */ new Set([".git", "node_modules", ".DS_Store"]);
|
|
@@ -581,6 +582,71 @@ function validateAddonManifest(manifest) {
|
|
|
581
582
|
if (!Array.isArray(manifest.members)) {
|
|
582
583
|
throw new Error("addon.json missing members");
|
|
583
584
|
}
|
|
585
|
+
validateInstallRequirements(manifest);
|
|
586
|
+
validateInstallNpm(manifest);
|
|
587
|
+
}
|
|
588
|
+
function validateInstallRequirements(manifest) {
|
|
589
|
+
const requirements = manifest.install?.requirements;
|
|
590
|
+
if (!requirements) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
if (requirements.envs !== void 0 && !Array.isArray(requirements.envs)) {
|
|
594
|
+
throw new Error("addon.json install.requirements.envs must be an array");
|
|
595
|
+
}
|
|
596
|
+
if (requirements.secrets !== void 0 && !Array.isArray(requirements.secrets)) {
|
|
597
|
+
throw new Error("addon.json install.requirements.secrets must be an array");
|
|
598
|
+
}
|
|
599
|
+
for (const item of requirements.envs ?? []) {
|
|
600
|
+
validateRequirementItem(item, "env");
|
|
601
|
+
}
|
|
602
|
+
for (const item of requirements.secrets ?? []) {
|
|
603
|
+
validateRequirementItem(item, "secret");
|
|
604
|
+
if (item.defaultValue !== void 0) {
|
|
605
|
+
throw new Error("addon.json install.requirements.secrets cannot declare defaultValue");
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
function validateRequirementItem(item, kind) {
|
|
610
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
611
|
+
throw new Error(`addon.json install.requirements.${kind}s item is invalid`);
|
|
612
|
+
}
|
|
613
|
+
const requirement = item;
|
|
614
|
+
if (typeof requirement.key !== "string" || !requirement.key.trim()) {
|
|
615
|
+
throw new Error(`addon.json install.requirements.${kind}s item missing key`);
|
|
616
|
+
}
|
|
617
|
+
if (requirement.required !== void 0 && typeof requirement.required !== "boolean") {
|
|
618
|
+
throw new Error(`addon.json install.requirements.${kind}s item required must be boolean`);
|
|
619
|
+
}
|
|
620
|
+
if (requirement.description !== void 0 && typeof requirement.description !== "string") {
|
|
621
|
+
throw new Error(`addon.json install.requirements.${kind}s item description must be string`);
|
|
622
|
+
}
|
|
623
|
+
if (requirement.defaultValue !== void 0 && typeof requirement.defaultValue !== "string") {
|
|
624
|
+
throw new Error(`addon.json install.requirements.${kind}s item defaultValue must be string`);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
function validateInstallNpm(manifest) {
|
|
628
|
+
const npm = manifest.install?.npm;
|
|
629
|
+
if (npm === void 0) {
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
if (!npm || typeof npm !== "object" || Array.isArray(npm)) {
|
|
633
|
+
throw new Error("addon.json install.npm must be an object");
|
|
634
|
+
}
|
|
635
|
+
const dependencies = npm.dependencies;
|
|
636
|
+
if (dependencies === void 0) {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
if (!dependencies || typeof dependencies !== "object" || Array.isArray(dependencies)) {
|
|
640
|
+
throw new Error("addon.json install.npm.dependencies must be an object");
|
|
641
|
+
}
|
|
642
|
+
for (const [name, version] of Object.entries(dependencies)) {
|
|
643
|
+
if (!name.trim()) {
|
|
644
|
+
throw new Error("addon.json install.npm.dependencies contains an empty package name");
|
|
645
|
+
}
|
|
646
|
+
if (typeof version !== "string" || !version.trim()) {
|
|
647
|
+
throw new Error(`addon.json install.npm.dependencies.${name} must be a string`);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
584
650
|
}
|
|
585
651
|
function resolveMemberSourcePath(member) {
|
|
586
652
|
const sourcePath = member.source?.path;
|
|
@@ -631,7 +697,7 @@ async function readAppSettingsApplicationType(packageRoot) {
|
|
|
631
697
|
if (!fs.existsSync(appSettingsPath)) {
|
|
632
698
|
return null;
|
|
633
699
|
}
|
|
634
|
-
const parsed =
|
|
700
|
+
const parsed = YAML2__default.default.parse(await promises$1.readFile(appSettingsPath, "utf8"));
|
|
635
701
|
if (parsed?.type === "web" || parsed?.type === "mini") {
|
|
636
702
|
return parsed.type;
|
|
637
703
|
}
|
|
@@ -666,6 +732,7 @@ async function tryDetectNearestApplicationType(startDir = process.cwd()) {
|
|
|
666
732
|
}
|
|
667
733
|
|
|
668
734
|
// src/marketplace/install.ts
|
|
735
|
+
var execFileAsync = util.promisify(child_process.execFile);
|
|
669
736
|
var AddonInstallConflictError = class extends Error {
|
|
670
737
|
constructor(input) {
|
|
671
738
|
super(`Addon ${input.operation} has ${input.conflicts.length} conflict(s)`);
|
|
@@ -694,6 +761,18 @@ function isAddonInstallConflictError(error) {
|
|
|
694
761
|
function sha256Buffer(buffer) {
|
|
695
762
|
return `sha256:${crypto.createHash("sha256").update(buffer).digest("hex")}`;
|
|
696
763
|
}
|
|
764
|
+
function stableJsonStringify(value) {
|
|
765
|
+
if (Array.isArray(value)) {
|
|
766
|
+
return `[${value.map((item) => stableJsonStringify(item)).join(",")}]`;
|
|
767
|
+
}
|
|
768
|
+
if (isPlainObject(value)) {
|
|
769
|
+
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableJsonStringify(value[key])}`).join(",")}}`;
|
|
770
|
+
}
|
|
771
|
+
return JSON.stringify(value);
|
|
772
|
+
}
|
|
773
|
+
function sha256Value(value) {
|
|
774
|
+
return sha256Buffer(Buffer.from(stableJsonStringify(value) ?? "undefined"));
|
|
775
|
+
}
|
|
697
776
|
function ensurePathInsideTarget(targetDir, relativePath) {
|
|
698
777
|
const absolute = path.resolve(targetDir, relativePath);
|
|
699
778
|
const normalizedTarget = path.resolve(targetDir);
|
|
@@ -775,6 +854,31 @@ function resolveInstallParameters(manifest, rawParameters) {
|
|
|
775
854
|
}
|
|
776
855
|
return resolved;
|
|
777
856
|
}
|
|
857
|
+
function resolveInstallRequirements(manifest) {
|
|
858
|
+
const requirements = manifest.install?.requirements ?? {};
|
|
859
|
+
return {
|
|
860
|
+
envs: requirements.envs?.map((item) => ({ ...item })),
|
|
861
|
+
secrets: requirements.secrets?.map((item) => ({ ...item }))
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
function resolveInstallNpm(manifest) {
|
|
865
|
+
const npm = manifest.install?.npm ?? {};
|
|
866
|
+
return {
|
|
867
|
+
dependencies: npm.dependencies ? { ...npm.dependencies } : void 0
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
function listNpmDependencies(npm) {
|
|
871
|
+
return Object.entries(npm.dependencies ?? {}).map(([packageName, version]) => ({
|
|
872
|
+
packageName,
|
|
873
|
+
version
|
|
874
|
+
}));
|
|
875
|
+
}
|
|
876
|
+
function buildMissingRequirements(requirements) {
|
|
877
|
+
return {
|
|
878
|
+
envs: requirements.envs?.filter((item) => item.required !== false).map((item) => ({ ...item })),
|
|
879
|
+
secrets: requirements.secrets?.filter((item) => item.required !== false).map((item) => ({ ...item }))
|
|
880
|
+
};
|
|
881
|
+
}
|
|
778
882
|
function getAddonMetaPath(targetDir, addonName) {
|
|
779
883
|
return path.join(targetDir, "src", "addons", addonName, "meta.json");
|
|
780
884
|
}
|
|
@@ -819,6 +923,36 @@ function buildOwnershipMap(metas, excludeAddonName) {
|
|
|
819
923
|
}
|
|
820
924
|
return ownership;
|
|
821
925
|
}
|
|
926
|
+
async function readPackageJson(targetDir) {
|
|
927
|
+
const packageJsonPath = ensurePathInsideTarget(targetDir, "package.json");
|
|
928
|
+
const parsed = JSON.parse(await promises$1.readFile(packageJsonPath, "utf8"));
|
|
929
|
+
if (!isPlainObject(parsed)) {
|
|
930
|
+
throw new Error("package.json must contain a JSON object");
|
|
931
|
+
}
|
|
932
|
+
return parsed;
|
|
933
|
+
}
|
|
934
|
+
function readDependencies(packageJson) {
|
|
935
|
+
const value = packageJson.dependencies;
|
|
936
|
+
if (value === void 0) {
|
|
937
|
+
return {};
|
|
938
|
+
}
|
|
939
|
+
if (!isPlainObject(value)) {
|
|
940
|
+
throw new Error("package.json dependencies must be an object");
|
|
941
|
+
}
|
|
942
|
+
const dependencies = {};
|
|
943
|
+
for (const [name, version] of Object.entries(value)) {
|
|
944
|
+
if (typeof version === "string") {
|
|
945
|
+
dependencies[name] = version;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
return dependencies;
|
|
949
|
+
}
|
|
950
|
+
function findPackageDependency(packageJson, packageName) {
|
|
951
|
+
return readDependencies(packageJson)[packageName] ?? null;
|
|
952
|
+
}
|
|
953
|
+
function buildNpmDependencyMap(npm) {
|
|
954
|
+
return new Map(listNpmDependencies(npm).map((item) => [item.packageName, item]));
|
|
955
|
+
}
|
|
822
956
|
async function collectDirectoryFiles(sourceDir) {
|
|
823
957
|
const entries = await promises$1.readdir(sourceDir, { withFileTypes: true });
|
|
824
958
|
const files = [];
|
|
@@ -877,19 +1011,37 @@ async function buildInstallPlan(options) {
|
|
|
877
1011
|
}
|
|
878
1012
|
const installParameters = resolveInstallParameters(manifest, options.installParameters);
|
|
879
1013
|
const files = [];
|
|
1014
|
+
const settings = [];
|
|
880
1015
|
const members = [];
|
|
881
1016
|
for (const member of manifest.members) {
|
|
882
1017
|
if (member.type === "skill") {
|
|
883
1018
|
throw new Error(`Member ${member.name} is a runtime skill. Design-time CLI install does not support skill members yet.`);
|
|
884
1019
|
}
|
|
885
|
-
if (member.type === "settings") {
|
|
886
|
-
throw new Error(`Member ${member.name} is a settings fragment. settings install is not implemented yet.`);
|
|
887
|
-
}
|
|
888
1020
|
const sourceRelativePath = resolveMemberSourcePath(member);
|
|
889
1021
|
if (!sourceRelativePath) {
|
|
890
1022
|
throw new Error(`Member ${member.name} is missing source.path`);
|
|
891
1023
|
}
|
|
892
1024
|
await assertPathExists(options.extractedDir, sourceRelativePath);
|
|
1025
|
+
if (member.type === "settings") {
|
|
1026
|
+
const sourcePath = path.join(options.extractedDir, sourceRelativePath);
|
|
1027
|
+
const patch = await readYamlObject(sourcePath);
|
|
1028
|
+
settings.push({
|
|
1029
|
+
sourcePath,
|
|
1030
|
+
sourceRelativePath,
|
|
1031
|
+
targetPath: "app.settings.yml",
|
|
1032
|
+
memberName: member.name,
|
|
1033
|
+
memberType: "settings",
|
|
1034
|
+
patch,
|
|
1035
|
+
paths: flattenSettingsPatch(patch)
|
|
1036
|
+
});
|
|
1037
|
+
members.push({
|
|
1038
|
+
name: member.name,
|
|
1039
|
+
type: member.type,
|
|
1040
|
+
sourcePath: sourceRelativePath,
|
|
1041
|
+
targetPaths: ["app.settings.yml"]
|
|
1042
|
+
});
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
893
1045
|
let targetBaseRelativeDir;
|
|
894
1046
|
switch (member.type) {
|
|
895
1047
|
case "component":
|
|
@@ -955,6 +1107,7 @@ async function buildInstallPlan(options) {
|
|
|
955
1107
|
}
|
|
956
1108
|
return {
|
|
957
1109
|
files,
|
|
1110
|
+
settings,
|
|
958
1111
|
members,
|
|
959
1112
|
metaPath: getAddonMetaPath(targetDir, manifest.name),
|
|
960
1113
|
applicationType,
|
|
@@ -994,6 +1147,8 @@ function buildInstalledAddonMeta(options) {
|
|
|
994
1147
|
memberTypes: [...new Set(options.manifest.members.map((member) => member.type))]
|
|
995
1148
|
},
|
|
996
1149
|
installParameters: options.installParameters,
|
|
1150
|
+
requirements: resolveInstallRequirements(options.manifest),
|
|
1151
|
+
npm: resolveInstallNpm(options.manifest),
|
|
997
1152
|
members: options.members,
|
|
998
1153
|
files: options.files
|
|
999
1154
|
};
|
|
@@ -1008,6 +1163,161 @@ async function writePlannedFiles(targetDir, planFiles) {
|
|
|
1008
1163
|
await promises$1.copyFile(file.sourcePath, absoluteTargetPath);
|
|
1009
1164
|
}
|
|
1010
1165
|
}
|
|
1166
|
+
function isPlainObject(value) {
|
|
1167
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1168
|
+
}
|
|
1169
|
+
async function readYamlObject(filePath) {
|
|
1170
|
+
const parsed = YAML2__default.default.parse(await promises$1.readFile(filePath, "utf8"));
|
|
1171
|
+
if (parsed == null) {
|
|
1172
|
+
return {};
|
|
1173
|
+
}
|
|
1174
|
+
if (!isPlainObject(parsed)) {
|
|
1175
|
+
throw new Error(`${filePath} must contain a YAML object`);
|
|
1176
|
+
}
|
|
1177
|
+
return parsed;
|
|
1178
|
+
}
|
|
1179
|
+
function flattenSettingsPatch(value, prefix = []) {
|
|
1180
|
+
const paths = [];
|
|
1181
|
+
for (const [key, child] of Object.entries(value)) {
|
|
1182
|
+
const segments = prefix.concat(key);
|
|
1183
|
+
if (isPlainObject(child)) {
|
|
1184
|
+
paths.push(...flattenSettingsPatch(child, segments));
|
|
1185
|
+
continue;
|
|
1186
|
+
}
|
|
1187
|
+
paths.push({
|
|
1188
|
+
path: segments.join("."),
|
|
1189
|
+
segments,
|
|
1190
|
+
value: child
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
return paths;
|
|
1194
|
+
}
|
|
1195
|
+
async function readYamlDocument(targetPath) {
|
|
1196
|
+
const text = fs.existsSync(targetPath) ? await promises$1.readFile(targetPath, "utf8") : "";
|
|
1197
|
+
return YAML2__default.default.parseDocument(text);
|
|
1198
|
+
}
|
|
1199
|
+
function buildSettingsOwnershipMap(metas, excludeAddonName) {
|
|
1200
|
+
const ownership = /* @__PURE__ */ new Map();
|
|
1201
|
+
for (const meta of metas) {
|
|
1202
|
+
if (excludeAddonName && meta.name === excludeAddonName) {
|
|
1203
|
+
continue;
|
|
1204
|
+
}
|
|
1205
|
+
for (const member of meta.members) {
|
|
1206
|
+
for (const item of member.settings?.paths ?? []) {
|
|
1207
|
+
ownership.set(`${item.targetPath}#${item.path}`, meta.name);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
return ownership;
|
|
1212
|
+
}
|
|
1213
|
+
function listPlannedSettingPaths(settings) {
|
|
1214
|
+
return settings.flatMap(
|
|
1215
|
+
(item) => item.paths.map((settingPath) => ({
|
|
1216
|
+
memberName: item.memberName,
|
|
1217
|
+
sourceRelativePath: item.sourceRelativePath,
|
|
1218
|
+
targetPath: item.targetPath,
|
|
1219
|
+
path: settingPath.path,
|
|
1220
|
+
segments: settingPath.segments,
|
|
1221
|
+
value: settingPath.value
|
|
1222
|
+
}))
|
|
1223
|
+
);
|
|
1224
|
+
}
|
|
1225
|
+
function getPlainYamlValue(document, segments) {
|
|
1226
|
+
const value = document.getIn(segments);
|
|
1227
|
+
if (value && typeof value === "object" && "toJSON" in value && typeof value.toJSON === "function") {
|
|
1228
|
+
return value.toJSON();
|
|
1229
|
+
}
|
|
1230
|
+
return value;
|
|
1231
|
+
}
|
|
1232
|
+
function attachAppliedSettings(members, appliedSettings) {
|
|
1233
|
+
return members.map((member) => {
|
|
1234
|
+
const applied = appliedSettings.find((item) => item.memberName === member.name);
|
|
1235
|
+
if (!applied) {
|
|
1236
|
+
return member;
|
|
1237
|
+
}
|
|
1238
|
+
return {
|
|
1239
|
+
...member,
|
|
1240
|
+
settings: {
|
|
1241
|
+
targetPath: applied.targetPath,
|
|
1242
|
+
paths: applied.paths
|
|
1243
|
+
}
|
|
1244
|
+
};
|
|
1245
|
+
});
|
|
1246
|
+
}
|
|
1247
|
+
async function applyPlannedSettings(targetDir, settings) {
|
|
1248
|
+
const written = /* @__PURE__ */ new Set();
|
|
1249
|
+
const appliedSettings = [];
|
|
1250
|
+
for (const item of settings) {
|
|
1251
|
+
const targetPath = ensurePathInsideTarget(targetDir, item.targetPath);
|
|
1252
|
+
const document = await readYamlDocument(targetPath);
|
|
1253
|
+
const pathMetas = [];
|
|
1254
|
+
for (const settingPath of item.paths) {
|
|
1255
|
+
const previousValueExists = document.hasIn(settingPath.segments);
|
|
1256
|
+
const previousValue = previousValueExists ? getPlainYamlValue(document, settingPath.segments) : void 0;
|
|
1257
|
+
document.setIn(settingPath.segments, settingPath.value);
|
|
1258
|
+
pathMetas.push({
|
|
1259
|
+
targetPath: item.targetPath,
|
|
1260
|
+
path: settingPath.path,
|
|
1261
|
+
sha256: sha256Value(settingPath.value),
|
|
1262
|
+
previousValueExists,
|
|
1263
|
+
previousValue
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
await ensureParentDirectory(targetPath);
|
|
1267
|
+
await promises$1.writeFile(targetPath, document.toString());
|
|
1268
|
+
written.add(item.targetPath);
|
|
1269
|
+
appliedSettings.push({
|
|
1270
|
+
memberName: item.memberName,
|
|
1271
|
+
targetPath: item.targetPath,
|
|
1272
|
+
paths: pathMetas
|
|
1273
|
+
});
|
|
1274
|
+
}
|
|
1275
|
+
return {
|
|
1276
|
+
writtenFiles: [...written],
|
|
1277
|
+
appliedSettings
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
async function restoreObsoleteSettings(targetDir, previousMeta, nextSettings) {
|
|
1281
|
+
if (!previousMeta) {
|
|
1282
|
+
return [];
|
|
1283
|
+
}
|
|
1284
|
+
const nextSettingKeys = new Set(
|
|
1285
|
+
listPlannedSettingPaths(nextSettings).map((item) => `${item.targetPath}#${item.path}`)
|
|
1286
|
+
);
|
|
1287
|
+
const documents = /* @__PURE__ */ new Map();
|
|
1288
|
+
const written = /* @__PURE__ */ new Set();
|
|
1289
|
+
const getDocument = async (targetPath) => {
|
|
1290
|
+
const cached = documents.get(targetPath);
|
|
1291
|
+
if (cached) {
|
|
1292
|
+
return cached;
|
|
1293
|
+
}
|
|
1294
|
+
const document = await readYamlDocument(ensurePathInsideTarget(targetDir, targetPath));
|
|
1295
|
+
documents.set(targetPath, document);
|
|
1296
|
+
return document;
|
|
1297
|
+
};
|
|
1298
|
+
for (const member of previousMeta.members) {
|
|
1299
|
+
for (const settingPath of member.settings?.paths ?? []) {
|
|
1300
|
+
if (nextSettingKeys.has(`${settingPath.targetPath}#${settingPath.path}`)) {
|
|
1301
|
+
continue;
|
|
1302
|
+
}
|
|
1303
|
+
const document = await getDocument(settingPath.targetPath);
|
|
1304
|
+
const segments = settingPath.path.split(".");
|
|
1305
|
+
if (settingPath.previousValueExists) {
|
|
1306
|
+
document.setIn(segments, settingPath.previousValue);
|
|
1307
|
+
} else {
|
|
1308
|
+
document.deleteIn(segments);
|
|
1309
|
+
}
|
|
1310
|
+
written.add(settingPath.targetPath);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
for (const targetPath of written) {
|
|
1314
|
+
const document = documents.get(targetPath);
|
|
1315
|
+
if (document) {
|
|
1316
|
+
await promises$1.writeFile(ensurePathInsideTarget(targetDir, targetPath), document.toString());
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
return [...written];
|
|
1320
|
+
}
|
|
1011
1321
|
async function removeObsoleteFiles(targetDir, previousMeta, nextPaths) {
|
|
1012
1322
|
const removed = [];
|
|
1013
1323
|
for (const file of previousMeta.files) {
|
|
@@ -1022,9 +1332,77 @@ async function removeObsoleteFiles(targetDir, previousMeta, nextPaths) {
|
|
|
1022
1332
|
}
|
|
1023
1333
|
return removed;
|
|
1024
1334
|
}
|
|
1335
|
+
async function applyNpmDependencies(targetDir, npm) {
|
|
1336
|
+
const dependencies = listNpmDependencies(npm);
|
|
1337
|
+
if (!dependencies.length) {
|
|
1338
|
+
return [];
|
|
1339
|
+
}
|
|
1340
|
+
const packageJsonPath = ensurePathInsideTarget(targetDir, "package.json");
|
|
1341
|
+
const packageJson = await readPackageJson(targetDir);
|
|
1342
|
+
let changed = false;
|
|
1343
|
+
for (const dependency of dependencies) {
|
|
1344
|
+
const section = packageJson.dependencies;
|
|
1345
|
+
const nextSection = isPlainObject(section) ? section : {};
|
|
1346
|
+
if (!isPlainObject(section)) {
|
|
1347
|
+
packageJson.dependencies = nextSection;
|
|
1348
|
+
changed = true;
|
|
1349
|
+
}
|
|
1350
|
+
if (nextSection[dependency.packageName] !== dependency.version) {
|
|
1351
|
+
nextSection[dependency.packageName] = dependency.version;
|
|
1352
|
+
changed = true;
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
if (!changed) {
|
|
1356
|
+
return [];
|
|
1357
|
+
}
|
|
1358
|
+
await promises$1.writeFile(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}
|
|
1359
|
+
`);
|
|
1360
|
+
return ["package.json"];
|
|
1361
|
+
}
|
|
1362
|
+
function resolvePackageManagerCommand(targetDir, packageJson) {
|
|
1363
|
+
const packageManager = typeof packageJson.packageManager === "string" ? packageJson.packageManager : "";
|
|
1364
|
+
if (fs.existsSync(path.join(targetDir, "bun.lockb")) || fs.existsSync(path.join(targetDir, "bun.lock")) || packageManager.startsWith("bun@")) {
|
|
1365
|
+
return { command: "bun", args: ["install"] };
|
|
1366
|
+
}
|
|
1367
|
+
if (fs.existsSync(path.join(targetDir, "pnpm-lock.yaml")) || packageManager.startsWith("pnpm@")) {
|
|
1368
|
+
return { command: "pnpm", args: ["install"] };
|
|
1369
|
+
}
|
|
1370
|
+
if (fs.existsSync(path.join(targetDir, "yarn.lock")) || packageManager.startsWith("yarn@")) {
|
|
1371
|
+
return { command: "yarn", args: ["install"] };
|
|
1372
|
+
}
|
|
1373
|
+
return { command: "npm", args: ["install"] };
|
|
1374
|
+
}
|
|
1375
|
+
async function installPackageDependencies(targetDir) {
|
|
1376
|
+
const packageJson = await readPackageJson(targetDir);
|
|
1377
|
+
const { command, args } = resolvePackageManagerCommand(targetDir, packageJson);
|
|
1378
|
+
try {
|
|
1379
|
+
await execFileAsync(command, args, {
|
|
1380
|
+
cwd: targetDir,
|
|
1381
|
+
maxBuffer: 1024 * 1024 * 10
|
|
1382
|
+
});
|
|
1383
|
+
} catch (error) {
|
|
1384
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1385
|
+
throw new Error(`Failed to install addon npm dependencies with ${command} ${args.join(" ")}: ${message}`);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1025
1388
|
async function validateInstallConflicts(targetDir, plan, existingMetas) {
|
|
1026
1389
|
const ownership = buildOwnershipMap(existingMetas);
|
|
1390
|
+
const settingsOwnership = buildSettingsOwnershipMap(existingMetas);
|
|
1027
1391
|
const conflicts = [];
|
|
1392
|
+
const packageJson = await readPackageJson(targetDir);
|
|
1393
|
+
for (const dependency of listNpmDependencies(resolveInstallNpm(plan.manifest))) {
|
|
1394
|
+
const existing = findPackageDependency(packageJson, dependency.packageName);
|
|
1395
|
+
if (existing && existing !== dependency.version) {
|
|
1396
|
+
conflicts.push({
|
|
1397
|
+
kind: "package_dependency_conflict",
|
|
1398
|
+
path: `package.json#dependencies.${dependency.packageName}`,
|
|
1399
|
+
packageName: dependency.packageName,
|
|
1400
|
+
requestedVersion: dependency.version,
|
|
1401
|
+
existingVersion: existing,
|
|
1402
|
+
message: `${dependency.packageName} is already declared as ${existing} in package.json`
|
|
1403
|
+
});
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1028
1406
|
for (const file of plan.files) {
|
|
1029
1407
|
const owner = ownership.get(file.targetPath);
|
|
1030
1408
|
if (owner && owner !== plan.manifest.name) {
|
|
@@ -1051,6 +1429,22 @@ async function validateInstallConflicts(targetDir, plan, existingMetas) {
|
|
|
1051
1429
|
});
|
|
1052
1430
|
}
|
|
1053
1431
|
}
|
|
1432
|
+
for (const item of listPlannedSettingPaths(plan.settings)) {
|
|
1433
|
+
const owner = settingsOwnership.get(`${item.targetPath}#${item.path}`);
|
|
1434
|
+
if (owner && owner !== plan.manifest.name) {
|
|
1435
|
+
conflicts.push({
|
|
1436
|
+
kind: "owned_by_other_addon",
|
|
1437
|
+
path: `${item.targetPath}#${item.path}`,
|
|
1438
|
+
sourcePath: item.sourceRelativePath,
|
|
1439
|
+
memberName: item.memberName,
|
|
1440
|
+
memberType: "settings",
|
|
1441
|
+
ownerAddon: owner,
|
|
1442
|
+
settingPath: item.path,
|
|
1443
|
+
nextValue: item.value,
|
|
1444
|
+
message: `${item.targetPath}#${item.path} is already managed by addon ${owner}`
|
|
1445
|
+
});
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1054
1448
|
if (conflicts.length) {
|
|
1055
1449
|
throw new AddonInstallConflictError({
|
|
1056
1450
|
operation: "install",
|
|
@@ -1066,9 +1460,42 @@ async function validateInstallConflicts(targetDir, plan, existingMetas) {
|
|
|
1066
1460
|
}
|
|
1067
1461
|
async function validateUpgradeConflicts(targetDir, plan, existingMetas, previousMeta) {
|
|
1068
1462
|
const ownership = buildOwnershipMap(existingMetas, previousMeta.name);
|
|
1463
|
+
const settingsOwnership = buildSettingsOwnershipMap(existingMetas, previousMeta.name);
|
|
1069
1464
|
const previousFilesByPath = new Map(previousMeta.files.map((file) => [file.path, file]));
|
|
1070
1465
|
const nextPaths = new Set(plan.files.map((file) => file.targetPath));
|
|
1466
|
+
const plannedSettings = listPlannedSettingPaths(plan.settings);
|
|
1467
|
+
const nextSettingKeys = new Set(plannedSettings.map((item) => `${item.targetPath}#${item.path}`));
|
|
1468
|
+
const plannedSettingsByKey = new Map(plannedSettings.map((item) => [`${item.targetPath}#${item.path}`, item]));
|
|
1071
1469
|
const conflicts = [];
|
|
1470
|
+
const packageJson = await readPackageJson(targetDir);
|
|
1471
|
+
const previousNpmByName = buildNpmDependencyMap(previousMeta.npm ?? {});
|
|
1472
|
+
for (const dependency of listNpmDependencies(resolveInstallNpm(plan.manifest))) {
|
|
1473
|
+
const existing = findPackageDependency(packageJson, dependency.packageName);
|
|
1474
|
+
const previous = previousNpmByName.get(dependency.packageName);
|
|
1475
|
+
if (!existing) {
|
|
1476
|
+
if (previous) {
|
|
1477
|
+
conflicts.push({
|
|
1478
|
+
kind: "package_dependency_conflict",
|
|
1479
|
+
path: `package.json#dependencies.${dependency.packageName}`,
|
|
1480
|
+
packageName: dependency.packageName,
|
|
1481
|
+
requestedVersion: dependency.version,
|
|
1482
|
+
message: `managed npm package missing from package.json: ${dependency.packageName}`
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
continue;
|
|
1486
|
+
}
|
|
1487
|
+
const canUpdateManagedVersion = previous && existing === previous.version;
|
|
1488
|
+
if (existing !== dependency.version && !canUpdateManagedVersion) {
|
|
1489
|
+
conflicts.push({
|
|
1490
|
+
kind: "package_dependency_conflict",
|
|
1491
|
+
path: `package.json#dependencies.${dependency.packageName}`,
|
|
1492
|
+
packageName: dependency.packageName,
|
|
1493
|
+
requestedVersion: dependency.version,
|
|
1494
|
+
existingVersion: existing,
|
|
1495
|
+
message: `${dependency.packageName} is already declared as ${existing} in package.json`
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1072
1499
|
for (const managedFile of previousMeta.files) {
|
|
1073
1500
|
const absolutePath = ensurePathInsideTarget(targetDir, managedFile.path);
|
|
1074
1501
|
if (!fs.existsSync(absolutePath)) {
|
|
@@ -1122,6 +1549,60 @@ async function validateUpgradeConflicts(targetDir, plan, existingMetas, previous
|
|
|
1122
1549
|
});
|
|
1123
1550
|
}
|
|
1124
1551
|
}
|
|
1552
|
+
const settingsDocuments = /* @__PURE__ */ new Map();
|
|
1553
|
+
const getSettingsDocument = async (targetPath) => {
|
|
1554
|
+
const cached = settingsDocuments.get(targetPath);
|
|
1555
|
+
if (cached) {
|
|
1556
|
+
return cached;
|
|
1557
|
+
}
|
|
1558
|
+
const document = await readYamlDocument(ensurePathInsideTarget(targetDir, targetPath));
|
|
1559
|
+
settingsDocuments.set(targetPath, document);
|
|
1560
|
+
return document;
|
|
1561
|
+
};
|
|
1562
|
+
for (const member of previousMeta.members) {
|
|
1563
|
+
for (const settingPath of member.settings?.paths ?? []) {
|
|
1564
|
+
const document = await getSettingsDocument(settingPath.targetPath);
|
|
1565
|
+
const segments = settingPath.path.split(".");
|
|
1566
|
+
const currentValue = document.hasIn(segments) ? getPlainYamlValue(document, segments) : void 0;
|
|
1567
|
+
const actualSha256 = sha256Value(currentValue);
|
|
1568
|
+
if (actualSha256 === settingPath.sha256) {
|
|
1569
|
+
continue;
|
|
1570
|
+
}
|
|
1571
|
+
const settingKey = `${settingPath.targetPath}#${settingPath.path}`;
|
|
1572
|
+
const isStillManaged = nextSettingKeys.has(settingKey);
|
|
1573
|
+
const nextSetting = plannedSettingsByKey.get(settingKey);
|
|
1574
|
+
conflicts.push({
|
|
1575
|
+
kind: isStillManaged ? "managed_file_modified" : "obsolete_file_modified",
|
|
1576
|
+
path: settingKey,
|
|
1577
|
+
sourcePath: member.sourcePath,
|
|
1578
|
+
memberName: member.name,
|
|
1579
|
+
memberType: "settings",
|
|
1580
|
+
expectedSha256: settingPath.sha256,
|
|
1581
|
+
actualSha256,
|
|
1582
|
+
settingPath: settingPath.path,
|
|
1583
|
+
currentValue,
|
|
1584
|
+
nextValue: nextSetting?.value,
|
|
1585
|
+
previousValue: settingPath.previousValue,
|
|
1586
|
+
message: isStillManaged ? `managed setting modified locally: ${settingKey}` : `setting changed locally and would be restored: ${settingKey}`
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
for (const item of plannedSettings) {
|
|
1591
|
+
const owner = settingsOwnership.get(`${item.targetPath}#${item.path}`);
|
|
1592
|
+
if (owner) {
|
|
1593
|
+
conflicts.push({
|
|
1594
|
+
kind: "owned_by_other_addon",
|
|
1595
|
+
path: `${item.targetPath}#${item.path}`,
|
|
1596
|
+
sourcePath: item.sourceRelativePath,
|
|
1597
|
+
memberName: item.memberName,
|
|
1598
|
+
memberType: "settings",
|
|
1599
|
+
ownerAddon: owner,
|
|
1600
|
+
settingPath: item.path,
|
|
1601
|
+
nextValue: item.value,
|
|
1602
|
+
message: `${item.targetPath}#${item.path} is already managed by addon ${owner}`
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1125
1606
|
for (const oldFile of previousMeta.files) {
|
|
1126
1607
|
if (!nextPaths.has(oldFile.path)) {
|
|
1127
1608
|
const absolutePath = ensurePathInsideTarget(targetDir, oldFile.path);
|
|
@@ -1289,7 +1770,11 @@ async function materializeAddon(options) {
|
|
|
1289
1770
|
await validateUpgradeConflicts(targetDir, plan, existingMetas, previousMeta);
|
|
1290
1771
|
}
|
|
1291
1772
|
const removedFiles = previousMeta ? await removeObsoleteFiles(targetDir, previousMeta, new Set(plan.files.map((file) => file.targetPath))) : [];
|
|
1773
|
+
const restoredSettingsFiles = await restoreObsoleteSettings(targetDir, previousMeta, plan.settings);
|
|
1774
|
+
const npm = resolveInstallNpm(plan.manifest);
|
|
1775
|
+
const packageJsonFiles = await applyNpmDependencies(targetDir, npm);
|
|
1292
1776
|
await writePlannedFiles(targetDir, plan.files);
|
|
1777
|
+
const settingsResult = await applyPlannedSettings(targetDir, plan.settings);
|
|
1293
1778
|
const installedFiles = [];
|
|
1294
1779
|
for (const plannedFile of plan.files) {
|
|
1295
1780
|
installedFiles.push(
|
|
@@ -1302,7 +1787,7 @@ async function materializeAddon(options) {
|
|
|
1302
1787
|
applicationType: plan.applicationType,
|
|
1303
1788
|
organizationCode: options.organizationCode,
|
|
1304
1789
|
installParameters: plan.installParameters,
|
|
1305
|
-
members: plan.members,
|
|
1790
|
+
members: attachAppliedSettings(plan.members, settingsResult.appliedSettings),
|
|
1306
1791
|
files: installedFiles,
|
|
1307
1792
|
installedAt: previousMeta?.installedAt
|
|
1308
1793
|
});
|
|
@@ -1310,10 +1795,17 @@ async function materializeAddon(options) {
|
|
|
1310
1795
|
await promises$1.writeFile(plan.metaPath, JSON.stringify(meta, null, 2));
|
|
1311
1796
|
const nextMetas = existingMetas.filter((item) => item.name !== meta.name).concat(meta);
|
|
1312
1797
|
await rebuildRegistryFiles(targetDir, plan.applicationType, nextMetas.filter((item) => item.applicationType === plan.applicationType));
|
|
1798
|
+
if (options.installPackageDependencies && packageJsonFiles.length) {
|
|
1799
|
+
await installPackageDependencies(targetDir);
|
|
1800
|
+
}
|
|
1801
|
+
const requirements = resolveInstallRequirements(plan.manifest);
|
|
1313
1802
|
return {
|
|
1314
1803
|
meta,
|
|
1315
|
-
writtenFiles: installedFiles.map((file) => file.path),
|
|
1316
|
-
removedFiles
|
|
1804
|
+
writtenFiles: installedFiles.map((file) => file.path).concat(packageJsonFiles, restoredSettingsFiles, settingsResult.writtenFiles),
|
|
1805
|
+
removedFiles,
|
|
1806
|
+
requirements,
|
|
1807
|
+
missingRequirements: buildMissingRequirements(requirements),
|
|
1808
|
+
npm
|
|
1317
1809
|
};
|
|
1318
1810
|
}
|
|
1319
1811
|
async function installAddonFromExtractedDirectory(options) {
|
|
@@ -1569,7 +2061,8 @@ async function installAddonFromRegistry(options) {
|
|
|
1569
2061
|
extractedDir,
|
|
1570
2062
|
applicationType: options.applicationType,
|
|
1571
2063
|
organizationCode: options.organizationCode,
|
|
1572
|
-
installParameters: options.installParameters
|
|
2064
|
+
installParameters: options.installParameters,
|
|
2065
|
+
installPackageDependencies: true
|
|
1573
2066
|
});
|
|
1574
2067
|
} finally {
|
|
1575
2068
|
await removeTempDirectory(extractedDir).catch(() => void 0);
|
|
@@ -1587,7 +2080,8 @@ async function upgradeAddonFromRegistry(options) {
|
|
|
1587
2080
|
extractedDir,
|
|
1588
2081
|
applicationType: options.applicationType,
|
|
1589
2082
|
organizationCode: options.organizationCode,
|
|
1590
|
-
installParameters: options.installParameters
|
|
2083
|
+
installParameters: options.installParameters,
|
|
2084
|
+
installPackageDependencies: true
|
|
1591
2085
|
});
|
|
1592
2086
|
} finally {
|
|
1593
2087
|
await removeTempDirectory(extractedDir).catch(() => void 0);
|