@enerlence/suntropy-cli 0.10.0 → 0.11.1
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/dist/bin/suntropy.js +194 -8
- package/dist/bin/suntropy.js.map +1 -1
- package/package.json +1 -1
package/dist/bin/suntropy.js
CHANGED
|
@@ -2149,6 +2149,32 @@ function getGlobalOpts7(cmd) {
|
|
|
2149
2149
|
function resolveFile(opts) {
|
|
2150
2150
|
return opts.file || process.env.SUNTROPY_STUDY || "./study.json";
|
|
2151
2151
|
}
|
|
2152
|
+
function resolveSurfaceCoordinates(surface, study) {
|
|
2153
|
+
const isValid = (c) => {
|
|
2154
|
+
const p = c;
|
|
2155
|
+
return !!p && Number.isFinite(p.lat) && Number.isFinite(p.lng);
|
|
2156
|
+
};
|
|
2157
|
+
const center = surface.center;
|
|
2158
|
+
if (isValid(center)) return { lat: center.lat, lng: center.lng };
|
|
2159
|
+
const studyLocation = study.location;
|
|
2160
|
+
if (isValid(studyLocation)) return { lat: studyLocation.lat, lng: studyLocation.lng };
|
|
2161
|
+
const path = surface.polygonPath;
|
|
2162
|
+
if (Array.isArray(path) && path.length > 0) {
|
|
2163
|
+
const points = path.filter(
|
|
2164
|
+
(p) => Number.isFinite(p?.lat) && Number.isFinite(p?.lng)
|
|
2165
|
+
);
|
|
2166
|
+
if (points.length > 0) {
|
|
2167
|
+
const sum = points.reduce(
|
|
2168
|
+
(acc, p) => ({ lat: acc.lat + p.lat, lng: acc.lng + p.lng }),
|
|
2169
|
+
{ lat: 0, lng: 0 }
|
|
2170
|
+
);
|
|
2171
|
+
const centroid = { lat: sum.lat / points.length, lng: sum.lng / points.length };
|
|
2172
|
+
if (isValid(centroid)) return centroid;
|
|
2173
|
+
return { lat: points[0].lat, lng: points[0].lng };
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
return null;
|
|
2177
|
+
}
|
|
2152
2178
|
function registerStudyBuilderCommands(studies) {
|
|
2153
2179
|
studies.command("init").description(
|
|
2154
2180
|
'Create a new study workspace (local JSON file with defaults).\nExample: suntropy studies init --file /tmp/study.json --name "Residencial 5kW"'
|
|
@@ -2694,14 +2720,12 @@ Example: suntropy studies set data --file study.json --data '{"referenceId":"REF
|
|
|
2694
2720
|
for (const idx of indicesToCalc) {
|
|
2695
2721
|
const surface = surfaces[idx];
|
|
2696
2722
|
if (!surface) continue;
|
|
2697
|
-
const
|
|
2698
|
-
|
|
2699
|
-
const lat = center?.lat || studyLocation?.lat;
|
|
2700
|
-
const lng = center?.lng || studyLocation?.lng;
|
|
2701
|
-
if (!lat || !lng) {
|
|
2723
|
+
const coords = resolveSurfaceCoordinates(surface, study);
|
|
2724
|
+
if (!coords) {
|
|
2702
2725
|
outputError(new Error(`Surface ${idx} has no coordinates. Use: studies add surface --lat N --lon N`));
|
|
2703
2726
|
continue;
|
|
2704
2727
|
}
|
|
2728
|
+
const { lat, lng } = coords;
|
|
2705
2729
|
const body = {
|
|
2706
2730
|
lat,
|
|
2707
2731
|
long: lng,
|
|
@@ -2959,7 +2983,7 @@ Example: suntropy studies set data --file study.json --data '{"referenceId":"REF
|
|
|
2959
2983
|
surface.panelInclination || 0,
|
|
2960
2984
|
surface.inclination || 0,
|
|
2961
2985
|
surface.panelPosition || "vertical",
|
|
2962
|
-
surface
|
|
2986
|
+
resolveSurfaceCoordinates(surface, study)?.lat ?? 37
|
|
2963
2987
|
);
|
|
2964
2988
|
} else {
|
|
2965
2989
|
maxPanels = remainingPanels;
|
|
@@ -3071,7 +3095,7 @@ Example: suntropy studies set data --file study.json --data '{"referenceId":"REF
|
|
|
3071
3095
|
surface.panelInclination || 0,
|
|
3072
3096
|
surface.inclination || 0,
|
|
3073
3097
|
surface.panelPosition || "vertical",
|
|
3074
|
-
surface
|
|
3098
|
+
resolveSurfaceCoordinates(surface, study)?.lat ?? 37
|
|
3075
3099
|
);
|
|
3076
3100
|
surfacesMaxPeakPower[sid] = maxPanels * panelPeakPower / 1e3;
|
|
3077
3101
|
} else {
|
|
@@ -4392,8 +4416,168 @@ function registerGeocodeCommands(program2) {
|
|
|
4392
4416
|
});
|
|
4393
4417
|
}
|
|
4394
4418
|
|
|
4419
|
+
// src/access.ts
|
|
4420
|
+
var COMMAND_TIERS = ["read", "write", "delete"];
|
|
4421
|
+
var TIER_VALUE = { read: 0, write: 1, delete: 2 };
|
|
4422
|
+
var DEFAULT_TIER = "delete";
|
|
4423
|
+
var ENV_VAR = "SUNTROPY_COMMAND_PROFILE";
|
|
4424
|
+
function tierValue(t) {
|
|
4425
|
+
return TIER_VALUE[t];
|
|
4426
|
+
}
|
|
4427
|
+
function isValidTier(value) {
|
|
4428
|
+
return typeof value === "string" && COMMAND_TIERS.includes(value);
|
|
4429
|
+
}
|
|
4430
|
+
function getActiveCommandProfile() {
|
|
4431
|
+
const envRaw = process.env[ENV_VAR]?.trim();
|
|
4432
|
+
if (envRaw) {
|
|
4433
|
+
const envNorm = envRaw.toLowerCase();
|
|
4434
|
+
if (isValidTier(envNorm)) return { tier: envNorm, source: "env" };
|
|
4435
|
+
const fallback = resolveFromConfig();
|
|
4436
|
+
return { ...fallback, invalidEnv: envRaw };
|
|
4437
|
+
}
|
|
4438
|
+
return resolveFromConfig();
|
|
4439
|
+
}
|
|
4440
|
+
function resolveFromConfig() {
|
|
4441
|
+
try {
|
|
4442
|
+
const cfg = loadConfig();
|
|
4443
|
+
if (isValidTier(cfg.commandProfile)) return { tier: cfg.commandProfile, source: "config" };
|
|
4444
|
+
} catch {
|
|
4445
|
+
}
|
|
4446
|
+
return { tier: DEFAULT_TIER, source: "default" };
|
|
4447
|
+
}
|
|
4448
|
+
function setStoredCommandProfile(tier) {
|
|
4449
|
+
const cfg = loadConfig();
|
|
4450
|
+
if (tier === null) delete cfg.commandProfile;
|
|
4451
|
+
else cfg.commandProfile = tier;
|
|
4452
|
+
saveConfig(cfg);
|
|
4453
|
+
}
|
|
4454
|
+
var DELETE_VERBS = /* @__PURE__ */ new Set(["delete", "delete-batch", "archive"]);
|
|
4455
|
+
var WRITE_VERBS = /* @__PURE__ */ new Set([
|
|
4456
|
+
"create",
|
|
4457
|
+
"update",
|
|
4458
|
+
"edit",
|
|
4459
|
+
"set",
|
|
4460
|
+
"add",
|
|
4461
|
+
"remove",
|
|
4462
|
+
"assemble",
|
|
4463
|
+
"featured",
|
|
4464
|
+
"save",
|
|
4465
|
+
"init",
|
|
4466
|
+
"init-default",
|
|
4467
|
+
"add-comment",
|
|
4468
|
+
"comment",
|
|
4469
|
+
"calculate-results",
|
|
4470
|
+
"optimize-peakpower"
|
|
4471
|
+
]);
|
|
4472
|
+
var PATH_OVERRIDES = {
|
|
4473
|
+
"studies calculate production": "write"
|
|
4474
|
+
};
|
|
4475
|
+
function classifyPath(segs) {
|
|
4476
|
+
const key = segs.join(" ");
|
|
4477
|
+
if (PATH_OVERRIDES[key]) return PATH_OVERRIDES[key];
|
|
4478
|
+
let tier = "read";
|
|
4479
|
+
for (const seg of segs) {
|
|
4480
|
+
if (DELETE_VERBS.has(seg)) return "delete";
|
|
4481
|
+
if (WRITE_VERBS.has(seg)) tier = "write";
|
|
4482
|
+
}
|
|
4483
|
+
return tier;
|
|
4484
|
+
}
|
|
4485
|
+
var EXEMPT_CONFIG_LEAVES = /* @__PURE__ */ new Set(["set", "get", "list", "create-profile", "use"]);
|
|
4486
|
+
function isExemptPath(segs) {
|
|
4487
|
+
if (segs[0] === "auth") return true;
|
|
4488
|
+
if (segs[0] === "command-profile") return true;
|
|
4489
|
+
if (segs[0] === "config" && segs.length === 2 && EXEMPT_CONFIG_LEAVES.has(segs[1])) return true;
|
|
4490
|
+
return false;
|
|
4491
|
+
}
|
|
4492
|
+
function applyCommandProfile(program2) {
|
|
4493
|
+
const resolved = getActiveCommandProfile();
|
|
4494
|
+
pruneChildren(program2, [], tierValue(resolved.tier));
|
|
4495
|
+
return resolved;
|
|
4496
|
+
}
|
|
4497
|
+
function pruneChildren(parent, parentSegs, max) {
|
|
4498
|
+
const kept = parent.commands.filter((child) => {
|
|
4499
|
+
const segs = [...parentSegs, child.name()];
|
|
4500
|
+
return keepCommand(child, segs, max);
|
|
4501
|
+
});
|
|
4502
|
+
const mutable = parent.commands;
|
|
4503
|
+
mutable.length = 0;
|
|
4504
|
+
mutable.push(...kept);
|
|
4505
|
+
}
|
|
4506
|
+
function keepCommand(cmd, segs, max) {
|
|
4507
|
+
if (cmd.commands.length > 0) {
|
|
4508
|
+
pruneChildren(cmd, segs, max);
|
|
4509
|
+
if (cmd.commands.length > 0) return true;
|
|
4510
|
+
return isExemptPath(segs);
|
|
4511
|
+
}
|
|
4512
|
+
if (isExemptPath(segs)) return true;
|
|
4513
|
+
return tierValue(classifyPath(segs)) <= max;
|
|
4514
|
+
}
|
|
4515
|
+
|
|
4516
|
+
// src/commands/command-profile.ts
|
|
4517
|
+
function getGlobalOpts16(cmd) {
|
|
4518
|
+
let root = cmd;
|
|
4519
|
+
while (root.parent) root = root.parent;
|
|
4520
|
+
return root.opts();
|
|
4521
|
+
}
|
|
4522
|
+
function registerCommandProfileCommand(program2) {
|
|
4523
|
+
program2.command("command-profile [tier]", { hidden: true }).description("Admin: get/set the command access profile (read | write | delete | reset)").action((tier) => {
|
|
4524
|
+
try {
|
|
4525
|
+
const global = getGlobalOpts16(program2);
|
|
4526
|
+
if (!tier) {
|
|
4527
|
+
const resolved = getActiveCommandProfile();
|
|
4528
|
+
output(
|
|
4529
|
+
{
|
|
4530
|
+
commandProfile: resolved.tier,
|
|
4531
|
+
source: resolved.source,
|
|
4532
|
+
...resolved.invalidEnv ? { warning: `Ignoring invalid ${ENV_VAR}="${resolved.invalidEnv}" (expected: ${COMMAND_TIERS.join(" | ")})` } : {},
|
|
4533
|
+
available: COMMAND_TIERS,
|
|
4534
|
+
envVar: ENV_VAR
|
|
4535
|
+
},
|
|
4536
|
+
global
|
|
4537
|
+
);
|
|
4538
|
+
return;
|
|
4539
|
+
}
|
|
4540
|
+
const value = tier.trim().toLowerCase();
|
|
4541
|
+
if (value === "reset") {
|
|
4542
|
+
setStoredCommandProfile(null);
|
|
4543
|
+
const resolved = getActiveCommandProfile();
|
|
4544
|
+
output(
|
|
4545
|
+
{
|
|
4546
|
+
success: true,
|
|
4547
|
+
message: `Stored command profile cleared. Effective tier: ${resolved.tier} (${resolved.source}).`,
|
|
4548
|
+
commandProfile: resolved.tier,
|
|
4549
|
+
source: resolved.source
|
|
4550
|
+
},
|
|
4551
|
+
global
|
|
4552
|
+
);
|
|
4553
|
+
return;
|
|
4554
|
+
}
|
|
4555
|
+
if (!isValidTier(value)) {
|
|
4556
|
+
outputError(new Error(`Invalid tier "${tier}". Expected one of: ${COMMAND_TIERS.join(", ")}, or "reset".`));
|
|
4557
|
+
return;
|
|
4558
|
+
}
|
|
4559
|
+
setStoredCommandProfile(value);
|
|
4560
|
+
const envOverride = getActiveCommandProfile();
|
|
4561
|
+
const message = envOverride.source === "env" ? `Stored command profile set to "${value}", but ${ENV_VAR}="${process.env[ENV_VAR]}" overrides it (effective: ${envOverride.tier}). Unset the env var for the stored value to apply.` : `Command profile set to "${value}". Takes effect on the next command.`;
|
|
4562
|
+
output(
|
|
4563
|
+
{
|
|
4564
|
+
success: true,
|
|
4565
|
+
commandProfile: value,
|
|
4566
|
+
effectiveTier: envOverride.tier,
|
|
4567
|
+
effectiveSource: envOverride.source,
|
|
4568
|
+
default: DEFAULT_TIER,
|
|
4569
|
+
message
|
|
4570
|
+
},
|
|
4571
|
+
global
|
|
4572
|
+
);
|
|
4573
|
+
} catch (err) {
|
|
4574
|
+
outputError(err);
|
|
4575
|
+
}
|
|
4576
|
+
});
|
|
4577
|
+
}
|
|
4578
|
+
|
|
4395
4579
|
// src/index.ts
|
|
4396
|
-
var CLI_VERSION = true ? "0.
|
|
4580
|
+
var CLI_VERSION = true ? "0.11.1" : "0.0.0-dev";
|
|
4397
4581
|
function createProgram() {
|
|
4398
4582
|
const program2 = new Command3();
|
|
4399
4583
|
program2.name("suntropy").description("Agent-first CLI for Suntropy solar platform. Optimized for programmatic data manipulation and progressive exploration.").version(CLI_VERSION).option("--format <format>", "Output format: json (default), human, csv", "json").option("--fields <fields>", "Comma-separated fields to include in output").option("--server <url>", "Override API server URL").option("--token <jwt>", "Override authentication token").option("--profile <name>", "Use a specific config profile").option("--verbose", "Show HTTP request/response details on stderr").option("--quiet", "Suppress non-data output").option("--save <file>", "Save output to file (also writes to stdout)");
|
|
@@ -4408,6 +4592,8 @@ function createProgram() {
|
|
|
4408
4592
|
registerShareableCommands(program2);
|
|
4409
4593
|
registerTemplatesCommands(program2);
|
|
4410
4594
|
registerGeocodeCommands(program2);
|
|
4595
|
+
registerCommandProfileCommand(program2);
|
|
4596
|
+
applyCommandProfile(program2);
|
|
4411
4597
|
return program2;
|
|
4412
4598
|
}
|
|
4413
4599
|
|