← Files Flows by plugin.spaceARCHIVED FILE

lib/brand-compat.mjs

2 KB · Nov 30, 2026 · 00:17 UTC

↓ Download file

import { createHash } from 'node:crypto';
import { existsSync, lstatSync, readdirSync, renameSync } from 'node:fs';
import { join } from 'node:path';

// A fingerprint identifies the retired namespace only while adopting existing
// data/configuration. New files, packages and keys always use the Flows name.
const previousNamespace = 'ed9bb6757aa1f88cd1c8957c719d390111df9adb93e64150d875a6187a8c74ad';
export const isPreviousNamespace = value => typeof value === 'string' && createHash('sha256').update(value.toLowerCase()).digest('hex') === previousNamespace;
export function flowsEnv(name, env = process.env) {
  if (Object.hasOwn(env, `FLOWS_${name}`)) return env[`FLOWS_${name}`];
  const suffix = `_${name}`;
  const previous = Object.keys(env).find(key => key.endsWith(suffix) && isPreviousNamespace(key.slice(0, -suffix.length)));
  return previous ? env[previous] : undefined;
}
export function isFlowFile(name) {
  if (name.endsWith('.flows.json')) return true;
  const match = name.match(/\.([a-z]+)\.json$/);
  return Boolean(match && isPreviousNamespace(match[1]));
}
// Move the complete library in one filesystem operation: flow IDs, comments,
// account receipts and native-panel preferences retain their original paths.
// Existing user documents are read in place, never renamed behind their backs.
export function adoptLibrary(parent) {
  const target = join(parent, 'flows');
  let names;
  try { names = readdirSync(parent); } catch (error) { if (error.code === 'ENOENT') return target; throw error; }
  const previous = names.find(isPreviousNamespace);
  if (!previous) return target;
  const source = join(parent, previous);
  if (!lstatSync(source).isDirectory() || lstatSync(source).isSymbolicLink()) throw new Error('The previous library must be a real directory.');
  if (existsSync(target)) throw new Error('Two local libraries exist. Reconcile them before opening Flows.');
  try { renameSync(source, target); }
  catch (error) { if (error.code !== 'ENOENT' || !existsSync(target)) throw error; }
  return target;
}

SHA-256: 87ff229d813a685b1f16cb470dd966bb02494b21728c3c605f0d09484cc9cac3