@supernovaio/cli
Version:
Supernova.io Command Line Interface
192 lines (190 loc) • 7.2 kB
JavaScript
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="27a21ef3-2e5f-5c00-97ad-3275493a3ef9")}catch(e){}}();
import fs from "node:fs";
import path from "node:path";
import { globSync } from "glob";
export function splitCsv(input) {
return input
.split(",")
.map(value => value.trim())
.filter(Boolean);
}
export function validateDirectoryPath(directoryPath) {
if (!directoryPath)
return "Path is required.";
if (!fs.existsSync(directoryPath))
return `Path does not exist: ${directoryPath}`;
const stat = fs.statSync(directoryPath);
if (!stat.isDirectory())
return `Path is not a directory: ${directoryPath}`;
return null;
}
export function discoverPackagesUnderPath(scanSourcePath) {
const packageJsonPaths = globSync("**/package.json", {
absolute: true,
cwd: scanSourcePath,
dot: false,
ignore: [
"**/.git/**",
"**/.next/**",
"**/.supernova/**",
"**/build/**",
"**/coverage/**",
"**/dist/**",
"**/node_modules/**",
"**/out/**",
],
nodir: true,
});
return packageJsonPaths
.map(packageJsonPath => {
const packageDir = path.dirname(packageJsonPath);
const name = readPackageNameFromManifest(packageJsonPath) ?? path.basename(packageDir);
return {
name,
packageDir,
relativePath: path.relative(scanSourcePath, packageDir) || ".",
};
})
.sort((a, b) => a.name.localeCompare(b.name) || a.relativePath.localeCompare(b.relativePath));
}
export function listSnapshotRootsUnderPath(scanSourcePath) {
return globSync("**/.supernova/snapshots/*", {
absolute: true,
cwd: scanSourcePath,
dot: true,
ignore: ["**/node_modules/**"],
})
.filter(snapshotRoot => {
try {
return fs.statSync(snapshotRoot).isDirectory();
}
catch {
return false;
}
})
.sort((a, b) => a.localeCompare(b));
}
export function readPackageNameFromManifest(packageJsonPath) {
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
return packageJson.name?.trim() || undefined;
}
catch {
return undefined;
}
}
export function deriveRepoPathFromSnapshotRoot(snapshotRoot) {
return path.resolve(snapshotRoot, "..", "..", "..");
}
export function detectSnapshotScannerType(snapshotRoot) {
const usagePath = path.join(snapshotRoot, "raw", "component-usage.json");
const componentsPath = path.join(snapshotRoot, "raw", "static-components.json");
if (fs.existsSync(componentsPath))
return "Components";
if (fs.existsSync(usagePath))
return "Usage";
return "Components";
}
export function hasUploadableSnapshotContent(snapshotRoot) {
const usagePath = path.join(snapshotRoot, "raw", "component-usage.json");
const componentsPath = path.join(snapshotRoot, "raw", "static-components.json");
return fs.existsSync(componentsPath) || fs.existsSync(usagePath);
}
export function uniqueStrings(values) {
return [...new Set(values.map(value => value.trim()).filter(Boolean))];
}
export function createPrivateDependencyEntries(packageNames) {
const inferredScope = inferSinglePrivateScope(packageNames);
if (inferredScope) {
return [`${inferredScope}/*`];
}
return uniqueStrings(packageNames);
}
export function resolveExactPrivatePackages(context) {
const declaredPackages = (context.privateDependencyEntries ?? []).filter(entry => !isPrivateDependencyScopePattern(entry));
const selectedPackages = context.dependencyPackageNames ?? [];
if (selectedPackages.length > 0) {
return uniqueStrings([...selectedPackages, ...declaredPackages]);
}
const scannedPackages = context.componentPackages?.filter(packageName => matchesAnyPrivateDependencyScopePattern(packageName, context.privateDependencyEntries ?? [])) ?? [];
return uniqueStrings([...declaredPackages, ...scannedPackages]);
}
function matchesAnyPrivateDependencyScopePattern(packageName, privateDependencyEntries) {
return privateDependencyEntries.some(entry => {
if (!isPrivateDependencyScopePattern(entry))
return false;
const scope = entry.slice(0, -2);
return packageName === scope || packageName.startsWith(`${scope}/`);
});
}
function inferSinglePrivateScope(values) {
if (!values?.length)
return undefined;
const scopes = uniqueStrings(values
.map(value => {
if (!value.startsWith("@"))
return undefined;
if (value.endsWith("/*"))
return value.slice(0, -2);
const slashIndex = value.indexOf("/");
return slashIndex === -1 ? undefined : value.slice(0, slashIndex);
})
.filter(Boolean));
return scopes.length === 1 ? scopes[0] : undefined;
}
export function isPrivateDependencyScopePattern(entry) {
return entry.endsWith("/*");
}
export function deriveProjectName(promptText, contextName) {
const fromPrompt = promptText.replaceAll(/\s+/g, " ").trim().slice(0, 60);
if (fromPrompt.length > 0)
return fromPrompt;
return `${contextName ?? "Shared Context"} Prototype`;
}
export function portalBaseUrlForEnvironment(env) {
switch (env) {
case "demo":
return "https://portal.demo.supernova.io";
case "development":
return "https://portal.dev.supernova.io";
case "local":
return "https://portal.dev.supernova.io";
case "staging":
return "https://portal.staging.supernova.io";
default:
return "https://portal.supernova.io";
}
}
export function containerRepositoryUrlForFramework(framework) {
switch (framework) {
case "React":
return "https://github.com/Supernova-Studio/component-container-template";
case "Angular":
return "https://github.com/Supernova-Studio/component-container-template-angular";
case "Vue":
return "https://github.com/Supernova-Studio/component-container-template-vue";
}
}
function bumpPatchVersion(version) {
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version.trim());
if (!match)
return null;
const major = Number(match[1]);
const minor = Number(match[2]);
const patch = Number(match[3]) + 1;
return `${major}.${minor}.${patch}`;
}
export function findNextAvailablePatchVersion(currentVersion, existingVersions) {
let candidate = currentVersion;
for (let i = 0; i < 1000; i += 1) {
if (!existingVersions.has(candidate))
return candidate;
const next = bumpPatchVersion(candidate);
if (!next)
return null;
candidate = next;
}
return null;
}
//# sourceMappingURL=helpers.js.map
//# debugId=27a21ef3-2e5f-5c00-97ad-3275493a3ef9