@fiodos/cli 0.1.20 → 0.1.21
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/package.json +1 -1
- package/src/index.js +216 -1
- package/src/wireFlutter.js +170 -0
- package/src/wireReactNative.js +217 -0
- package/src/writeEnv.js +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fiodos/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"description": "Fiodos CLI — analyzes your app's source code and generates the in-app voice-agent manifest, then wires the orb. Powers `npx @fiodos/cli analyze`.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"bin": {
|
package/src/index.js
CHANGED
|
@@ -94,6 +94,8 @@ const { resolveAnalysisRoot } = require('./resolveAnalysisRoot');
|
|
|
94
94
|
const { analyzeWithAI, correctActionWiring, DEFAULT_MODEL, resolveModel } = require('./aiAnalyze');
|
|
95
95
|
const { verifyManifest } = require('./verify');
|
|
96
96
|
const { wireWebOrb, reportWireResult, connectOrbRegistries, reportRegistriesResult } = require('./wireWeb');
|
|
97
|
+
const { wireReactNativeOrb, reportReactNativeWire } = require('./wireReactNative');
|
|
98
|
+
const { wireFlutterOrb, writeFlutterEnv, reportFlutterWire } = require('./wireFlutter');
|
|
97
99
|
const { wireHandlers, reportHandlerResult } = require('./wireHandlers');
|
|
98
100
|
const { offerWriteEnv, printEnvManualReminder } = require('./writeEnv');
|
|
99
101
|
const { scoreSurface, computeSurface } = require('./scoreSurface');
|
|
@@ -678,7 +680,173 @@ async function main() {
|
|
|
678
680
|
logDev(`[change-registry] could not write: ${e.message}`);
|
|
679
681
|
}
|
|
680
682
|
}
|
|
683
|
+
} else if (platform === 'mobile' && publishing) {
|
|
684
|
+
// ── Mobile (Expo / React Native): mirror the web pipeline, adapted ─────────
|
|
685
|
+
// Same single command that publishes also mounts the orb (via the SDK's
|
|
686
|
+
// one-line <FiodosExpoAgent/>), records the install proof so the dashboard
|
|
687
|
+
// connection screen completes, writes the EXPO_PUBLIC_* env, and prints the
|
|
688
|
+
// EAS production step — exactly like web, just for an app.
|
|
689
|
+
loadAppEnv(analysisRoot);
|
|
690
|
+
const { apiKey, apiUrl } = resolveApiTarget();
|
|
691
|
+
const noWire = process.argv.includes('--no-wire');
|
|
692
|
+
let orbWireResult = null;
|
|
693
|
+
let envWriteResult = null;
|
|
694
|
+
|
|
695
|
+
await withSpinner('Publishing to your project', async ({ setLabel, pause, resume }) => {
|
|
696
|
+
await publishManifest(manifest, publishMeta, analysisToken, { skipSuccessLog: true });
|
|
697
|
+
if (!noWire && !process.argv.includes('--no-orb-wire')) {
|
|
698
|
+
setLabel(orbMountSpinnerLabel(platform));
|
|
699
|
+
orbWireResult = wireReactNativeOrb(analysisRoot, {
|
|
700
|
+
manifest,
|
|
701
|
+
colors: fyodosTermColors(),
|
|
702
|
+
noWire,
|
|
703
|
+
});
|
|
704
|
+
pause();
|
|
705
|
+
reportReactNativeWire(orbWireResult, fyodosTermColors(), { quiet: !isVerbose() });
|
|
706
|
+
resume();
|
|
707
|
+
// Install proof: only when the orb is genuinely wired into the code. If
|
|
708
|
+
// we could only print a snippet, we DON'T post — the dashboard then
|
|
709
|
+
// waits for the runtime heartbeat (FiodosProvider's reportOrbSeen) so a
|
|
710
|
+
// green tick always reflects a real mount, never a guess.
|
|
711
|
+
if (
|
|
712
|
+
apiKey &&
|
|
713
|
+
(orbWireResult.status === 'added' || orbWireResult.status === 'already')
|
|
714
|
+
) {
|
|
715
|
+
const proof = await postOrbWired({
|
|
716
|
+
apiKey,
|
|
717
|
+
apiUrl,
|
|
718
|
+
file: orbWireResult.file || '',
|
|
719
|
+
platform: 'mobile',
|
|
720
|
+
actionsStatus: 'done',
|
|
721
|
+
});
|
|
722
|
+
if (proof && proof.ok === false && proof.reason !== 'no-api-key') {
|
|
723
|
+
logDev(`[orb-wired] mobile install proof not recorded (${proof.status || proof.reason}); dashboard will rely on the runtime ping`);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
logUser('Published to your project');
|
|
729
|
+
|
|
730
|
+
// API key in the app environment (EXPO_PUBLIC_*) — same consent flow as web.
|
|
731
|
+
if (!process.argv.includes('--no-write-env')) {
|
|
732
|
+
try {
|
|
733
|
+
envWriteResult = await offerWriteEnv({
|
|
734
|
+
appRoot: analysisRoot,
|
|
735
|
+
apiKey,
|
|
736
|
+
apiUrl,
|
|
737
|
+
defaultApiUrl: DEFAULT_API_URL,
|
|
738
|
+
assumeYes: process.argv.includes('--write-env-yes'),
|
|
739
|
+
log: logUser,
|
|
740
|
+
logDev,
|
|
741
|
+
});
|
|
742
|
+
if (envWriteResult) printEnvManualReminder(envWriteResult, { logUser, logDev });
|
|
743
|
+
} catch (e) {
|
|
744
|
+
logDev(`[write-env] skipped: ${e.message}`);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Production (EAS) reminder for the build-time public key. A CLI cannot (and
|
|
749
|
+
// must not) authenticate into the user's Expo account, so we print the exact
|
|
750
|
+
// step instead — mirroring the web deploy-provider reminder.
|
|
751
|
+
if (apiKey) printExpoProductionReminder({ apiKey, appRoot: analysisRoot });
|
|
752
|
+
|
|
753
|
+
if (!noWire) {
|
|
754
|
+
try {
|
|
755
|
+
const record = buildRunRecord(analysisRoot, {
|
|
756
|
+
appName: manifest.appName,
|
|
757
|
+
manifest,
|
|
758
|
+
publishMeta,
|
|
759
|
+
orbResult: orbWireResult,
|
|
760
|
+
envResult: envWriteResult,
|
|
761
|
+
});
|
|
762
|
+
const paths = writeChangeRegistry(analysisRoot, record);
|
|
763
|
+
logUser(`Change log: ${paths.mdRel} (history in .fyodos/changes/)`);
|
|
764
|
+
} catch (e) {
|
|
765
|
+
logDev(`[change-registry] could not write: ${e.message}`);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
} else if (platform === 'flutter' && publishing) {
|
|
769
|
+
// ── Flutter: mirror the web/RN pipeline, adapted to Dart ──────────────────
|
|
770
|
+
// The Flutter SDK's one-line FiodosAgent self-fetches the published manifest
|
|
771
|
+
// by API key, so we don't write a manifest file. We publish, wrap lib/main's
|
|
772
|
+
// home: with FiodosAgent, record the install proof, write the dart-define env
|
|
773
|
+
// file, and print the run + production commands.
|
|
774
|
+
loadAppEnv(analysisRoot);
|
|
775
|
+
const { apiKey, apiUrl } = resolveApiTarget();
|
|
776
|
+
const noWire = process.argv.includes('--no-wire');
|
|
777
|
+
let orbWireResult = null;
|
|
778
|
+
let envWriteResult = null;
|
|
779
|
+
|
|
780
|
+
await withSpinner('Publishing to your project', async ({ setLabel, pause, resume }) => {
|
|
781
|
+
await publishManifest(manifest, publishMeta, analysisToken, { skipSuccessLog: true });
|
|
782
|
+
if (!noWire && !process.argv.includes('--no-orb-wire')) {
|
|
783
|
+
setLabel(orbMountSpinnerLabel(platform));
|
|
784
|
+
orbWireResult = wireFlutterOrb(analysisRoot, { colors: fyodosTermColors(), noWire });
|
|
785
|
+
pause();
|
|
786
|
+
reportFlutterWire(orbWireResult, fyodosTermColors(), { quiet: !isVerbose() });
|
|
787
|
+
resume();
|
|
788
|
+
// Install proof only when the orb is genuinely wired into main.dart. If
|
|
789
|
+
// we could only print a snippet, we DON'T post — the dashboard then waits
|
|
790
|
+
// for the runtime heartbeat (FiodosAgent posts orb-seen on warmUp).
|
|
791
|
+
if (
|
|
792
|
+
apiKey &&
|
|
793
|
+
(orbWireResult.status === 'added' || orbWireResult.status === 'already')
|
|
794
|
+
) {
|
|
795
|
+
const proof = await postOrbWired({
|
|
796
|
+
apiKey,
|
|
797
|
+
apiUrl,
|
|
798
|
+
file: orbWireResult.file || '',
|
|
799
|
+
platform: 'flutter',
|
|
800
|
+
actionsStatus: 'done',
|
|
801
|
+
});
|
|
802
|
+
if (proof && proof.ok === false && proof.reason !== 'no-api-key') {
|
|
803
|
+
logDev(`[orb-wired] flutter install proof not recorded (${proof.status || proof.reason}); dashboard will rely on the runtime ping`);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
});
|
|
808
|
+
logUser('Published to your project');
|
|
809
|
+
|
|
810
|
+
// Dart-define env file (git-ignored) so `flutter run` has the key without
|
|
811
|
+
// hardcoding the secret in committed source.
|
|
812
|
+
if (apiKey && !process.argv.includes('--no-write-env')) {
|
|
813
|
+
try {
|
|
814
|
+
envWriteResult = writeFlutterEnv(analysisRoot, {
|
|
815
|
+
apiKey,
|
|
816
|
+
apiUrl,
|
|
817
|
+
defaultApiUrl: DEFAULT_API_URL,
|
|
818
|
+
});
|
|
819
|
+
if (envWriteResult && envWriteResult.written) {
|
|
820
|
+
logUser(`API key saved to ${envWriteResult.file} (git-ignored).`);
|
|
821
|
+
if (envWriteResult.gitignore && envWriteResult.gitignore.changed) {
|
|
822
|
+
logUser(`Added ${envWriteResult.gitignore.pattern} to .gitignore so your key is never pushed to GitHub.`);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
} catch (e) {
|
|
826
|
+
logDev(`[write-env] flutter skipped: ${e.message}`);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
if (apiKey) printFlutterRunReminder({ apiKey, envFile: envWriteResult && envWriteResult.file });
|
|
831
|
+
|
|
832
|
+
if (!noWire) {
|
|
833
|
+
try {
|
|
834
|
+
const record = buildRunRecord(analysisRoot, {
|
|
835
|
+
appName: manifest.appName,
|
|
836
|
+
manifest,
|
|
837
|
+
publishMeta,
|
|
838
|
+
orbResult: orbWireResult,
|
|
839
|
+
envResult: envWriteResult,
|
|
840
|
+
});
|
|
841
|
+
const paths = writeChangeRegistry(analysisRoot, record);
|
|
842
|
+
logUser(`Change log: ${paths.mdRel} (history in .fyodos/changes/)`);
|
|
843
|
+
} catch (e) {
|
|
844
|
+
logDev(`[change-registry] could not write: ${e.message}`);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
681
847
|
} else if (publishing) {
|
|
848
|
+
// Native (iOS / Android): the orb ships through the native SDK — there is no
|
|
849
|
+
// JS/Dart entrypoint to wire here, so we only publish the manifest.
|
|
682
850
|
loadAppEnv(analysisRoot);
|
|
683
851
|
await withSpinner('Publishing to your project', () =>
|
|
684
852
|
publishManifest(manifest, publishMeta, analysisToken, { skipSuccessLog: true }),
|
|
@@ -687,6 +855,52 @@ async function main() {
|
|
|
687
855
|
}
|
|
688
856
|
}
|
|
689
857
|
|
|
858
|
+
/**
|
|
859
|
+
* How to run a Flutter app with the orb key (dev) and ship it (production).
|
|
860
|
+
* Flutter has no auto-loaded .env: the compile-time key is passed via
|
|
861
|
+
* --dart-define. We never touch the user's build pipeline — we print the exact
|
|
862
|
+
* commands, mirroring the web deploy reminder and the RN EAS reminder.
|
|
863
|
+
*/
|
|
864
|
+
function printFlutterRunReminder({ apiKey, envFile }) {
|
|
865
|
+
const file = envFile || 'fyodos.env.json';
|
|
866
|
+
logUser('Run your app with the orb — pass the key as a compile-time define (2 steps)');
|
|
867
|
+
console.log(' Step 1 — Dev run (reads the git-ignored env file we just wrote):');
|
|
868
|
+
console.log(` flutter run --dart-define-from-file=${file}`);
|
|
869
|
+
console.log('');
|
|
870
|
+
console.log(' Step 2 — Production build — pass the same define to your release build / CI:');
|
|
871
|
+
console.log(` flutter build apk --dart-define-from-file=${file}`);
|
|
872
|
+
console.log(` (or --dart-define=FYODOS_API_KEY=${apiKey})`);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Production (EAS) step for Expo: EXPO_PUBLIC_* vars are inlined by Metro from
|
|
877
|
+
* the build environment. Local `.env` covers `expo start`, but cloud EAS builds
|
|
878
|
+
* (which never receive a git-ignored .env) need the key set in EAS env so the
|
|
879
|
+
* orb is not invisible in TestFlight / Play builds. We never touch the user's
|
|
880
|
+
* Expo account — we print the exact command + dashboard path.
|
|
881
|
+
*/
|
|
882
|
+
function printExpoProductionReminder({ apiKey, appRoot }) {
|
|
883
|
+
const usesEas = (() => {
|
|
884
|
+
try {
|
|
885
|
+
return fs.existsSync(path.join(appRoot, 'eas.json'));
|
|
886
|
+
} catch {
|
|
887
|
+
return false;
|
|
888
|
+
}
|
|
889
|
+
})();
|
|
890
|
+
logUser('Make the orb appear in PRODUCTION builds — add the key to EAS (2 steps)');
|
|
891
|
+
console.log(' Step 1 — Copy your orb key (this exact value):');
|
|
892
|
+
console.log(` ${apiKey}`);
|
|
893
|
+
console.log('');
|
|
894
|
+
console.log(' Step 2 — Set it in your EAS build environment:');
|
|
895
|
+
console.log(` eas env:create --name EXPO_PUBLIC_FYODOS_API_KEY --value ${apiKey} --environment production`);
|
|
896
|
+
console.log(' (or expo.dev → your project → Environment variables, then rebuild)');
|
|
897
|
+
if (!usesEas) {
|
|
898
|
+
console.log('');
|
|
899
|
+
console.log(' No eas.json yet? `expo start` already reads your local .env;');
|
|
900
|
+
console.log(' add the EAS variable when you set up cloud builds.');
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
690
904
|
/**
|
|
691
905
|
* Resolve the target platform from package.json dependencies — the only signal
|
|
692
906
|
* that reliably tells a Next.js App Router web app (which has an app/ dir) apart
|
|
@@ -979,6 +1193,7 @@ async function postOrbWired({
|
|
|
979
1193
|
apiKey,
|
|
980
1194
|
apiUrl,
|
|
981
1195
|
file,
|
|
1196
|
+
platform = 'web',
|
|
982
1197
|
actionsStatus = 'pending',
|
|
983
1198
|
actionsTotal = 0,
|
|
984
1199
|
actionsDone = 0,
|
|
@@ -990,7 +1205,7 @@ async function postOrbWired({
|
|
|
990
1205
|
headers: { 'Content-Type': 'application/json', 'x-api-key': apiKey },
|
|
991
1206
|
body: JSON.stringify({
|
|
992
1207
|
file: file || '',
|
|
993
|
-
platform
|
|
1208
|
+
platform,
|
|
994
1209
|
actionsStatus,
|
|
995
1210
|
actionsTotal,
|
|
996
1211
|
actionsDone,
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wireFlutter — auto-mount the Fiodos orb in a Flutter app, mirroring the web
|
|
3
|
+
* and React Native orb auto-mounts (wireWeb.js / wireReactNative.js) adapted to
|
|
4
|
+
* Dart / Flutter.
|
|
5
|
+
*
|
|
6
|
+
* The Flutter SDK already exposes a true one-line drop-in, FiodosAgent, that
|
|
7
|
+
* self-fetches the published manifest by API key, fetches the dashboard orb
|
|
8
|
+
* theme, posts the orb-seen heartbeat, and renders the themed orb. So there is
|
|
9
|
+
* no manifest file to write (unlike RN): we only
|
|
10
|
+
* 1. add `import 'package:fiodos_flutter/fiodos_flutter.dart';`, and
|
|
11
|
+
* 2. wrap the app's `home:` widget with FiodosAgent (reading the key from a
|
|
12
|
+
* compile-time --dart-define, never hardcoding the secret in source).
|
|
13
|
+
*
|
|
14
|
+
* Edits are idempotent and reversible (FYODOS:ORB markers). The orb must live
|
|
15
|
+
* INSIDE MaterialApp/CupertinoApp (it needs a Directionality/Material ancestor),
|
|
16
|
+
* which is exactly where `home:` is — so we wrap there, not at runApp(). When the
|
|
17
|
+
* `home:` value is not a simple widget expression we DON'T touch the file: we
|
|
18
|
+
* print a short snippet instead (like the web/RN paths), and the caller then
|
|
19
|
+
* skips the install proof so the dashboard waits for the runtime heartbeat.
|
|
20
|
+
*/
|
|
21
|
+
'use strict';
|
|
22
|
+
|
|
23
|
+
const fs = require('fs');
|
|
24
|
+
const path = require('path');
|
|
25
|
+
|
|
26
|
+
const { ensureEnvGitIgnored } = require('./writeEnv');
|
|
27
|
+
|
|
28
|
+
const IMPORT_START = '// FYODOS:ORB:START (auto-generated — safe to remove)';
|
|
29
|
+
const IMPORT_END = '// FYODOS:ORB:END';
|
|
30
|
+
const SDK_IMPORT = "import 'package:fiodos_flutter/fiodos_flutter.dart';";
|
|
31
|
+
|
|
32
|
+
const MAIN_CANDIDATES = ['lib/main.dart'];
|
|
33
|
+
|
|
34
|
+
function findMainDart(appRoot) {
|
|
35
|
+
for (const rel of MAIN_CANDIDATES) {
|
|
36
|
+
const abs = path.join(appRoot, rel);
|
|
37
|
+
if (fs.existsSync(abs)) return { abs, rel };
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function insertImportAfterLastImport(source, block) {
|
|
43
|
+
const lines = source.split('\n');
|
|
44
|
+
let lastImport = -1;
|
|
45
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
46
|
+
if (/^\s*import\s+['"]/.test(lines[i])) lastImport = i;
|
|
47
|
+
}
|
|
48
|
+
if (lastImport === -1) return `${block}\n${source}`;
|
|
49
|
+
lines.splice(lastImport + 1, 0, block);
|
|
50
|
+
return lines.join('\n');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Match the `home:` value when it is a single, simple widget constructor — the
|
|
55
|
+
* common case (e.g. `home: const TodoListScreen()`, `home: MyHomePage(title: 'x')`).
|
|
56
|
+
* We deliberately do NOT try to parse arbitrarily nested expressions: a complex
|
|
57
|
+
* home (Builder, ternaries, nested constructors with parens) falls back to the
|
|
58
|
+
* snippet, never to a risky edit. Group 1 = the whole widget expr (with const).
|
|
59
|
+
*/
|
|
60
|
+
const HOME_RE = /home:\s*((?:const\s+)?[A-Za-z_][\w.]*\([^()]*\))/;
|
|
61
|
+
|
|
62
|
+
/** Build the wired main.dart, or null when home: is not a simple widget. */
|
|
63
|
+
function buildWiredMain(source) {
|
|
64
|
+
if (!HOME_RE.test(source)) return null;
|
|
65
|
+
const wrapped = source.replace(HOME_RE, (full, widget) =>
|
|
66
|
+
'home: FiodosAgent(\n' +
|
|
67
|
+
" apiKey: const String.fromEnvironment('FYODOS_API_KEY'),\n" +
|
|
68
|
+
" baseUrl: const String.fromEnvironment('FYODOS_API_URL', defaultValue: kFiodosDefaultApiUrl),\n" +
|
|
69
|
+
` child: ${widget},\n` +
|
|
70
|
+
' )',
|
|
71
|
+
);
|
|
72
|
+
const importBlock = `${IMPORT_START}\n${SDK_IMPORT}\n${IMPORT_END}`;
|
|
73
|
+
return insertImportAfterLastImport(wrapped, importBlock);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Copy-paste snippet shown when we cannot safely auto-wrap. */
|
|
77
|
+
function mountSnippet() {
|
|
78
|
+
return (
|
|
79
|
+
`${SDK_IMPORT}\n\n` +
|
|
80
|
+
`// Wrap your app's home widget (inside MaterialApp) with FiodosAgent:\n` +
|
|
81
|
+
`home: FiodosAgent(\n` +
|
|
82
|
+
` apiKey: const String.fromEnvironment('FYODOS_API_KEY'),\n` +
|
|
83
|
+
` child: const MyHomePage(),\n` +
|
|
84
|
+
`),`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function printSnippet(colors, rel, reason) {
|
|
89
|
+
const { blue, cyan, dim, reset } = colors;
|
|
90
|
+
console.error(`\n${cyan}◉${reset} ${blue}Fiodos${reset} · ${dim}add the orb to ${rel || 'lib/main.dart'}${reason ? ` (${reason})` : ''}:${reset}`);
|
|
91
|
+
console.error(mountSnippet());
|
|
92
|
+
console.error(`${dim}Then re-run analyze — the mount becomes automatic once home: is a simple widget.${reset}\n`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Auto-mount the orb into a Flutter app.
|
|
97
|
+
* @returns {{status:'added'|'already'|'printed'|'skipped', file?:string, reason?:string}}
|
|
98
|
+
*/
|
|
99
|
+
function wireFlutterOrb(appRoot, { colors = {}, noWire = false } = {}) {
|
|
100
|
+
const main = findMainDart(appRoot);
|
|
101
|
+
if (!main) {
|
|
102
|
+
printSnippet(colors, null, 'no lib/main.dart found');
|
|
103
|
+
return { status: 'printed', reason: 'no-main' };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let source;
|
|
107
|
+
try {
|
|
108
|
+
source = fs.readFileSync(main.abs, 'utf8');
|
|
109
|
+
} catch (e) {
|
|
110
|
+
return { status: 'skipped', reason: `read-failed: ${e.message}` };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (source.includes('FiodosAgent(') || source.includes('FYODOS:ORB')) {
|
|
114
|
+
return { status: 'already', file: main.rel };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (noWire) return { status: 'skipped', reason: 'no-wire' };
|
|
118
|
+
|
|
119
|
+
const wired = buildWiredMain(source);
|
|
120
|
+
if (!wired) {
|
|
121
|
+
printSnippet(colors, main.rel, 'home: is not a simple widget');
|
|
122
|
+
return { status: 'printed', file: main.rel, reason: 'unrecognized-home' };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
fs.writeFileSync(main.abs, wired);
|
|
127
|
+
} catch (e) {
|
|
128
|
+
return { status: 'skipped', reason: `write-failed: ${e.message}` };
|
|
129
|
+
}
|
|
130
|
+
return { status: 'added', file: main.rel };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Flutter env: there is no auto-loaded `.env`. The compile-time keys are passed
|
|
135
|
+
* with --dart-define; the convention for a checked-in run command is a
|
|
136
|
+
* dart-define-from-file JSON. We write a git-ignored `fyodos.env.json` holding
|
|
137
|
+
* the publishable key + URL so `flutter run --dart-define-from-file=fyodos.env.json`
|
|
138
|
+
* works locally, and the secret never lands in committed source.
|
|
139
|
+
* @returns {{file:string, written:boolean, gitignore:object}}
|
|
140
|
+
*/
|
|
141
|
+
function writeFlutterEnv(appRoot, { apiKey, apiUrl, defaultApiUrl }) {
|
|
142
|
+
if (!apiKey) return { file: null, written: false, gitignore: null };
|
|
143
|
+
const rel = 'fyodos.env.json';
|
|
144
|
+
const abs = path.join(appRoot, rel);
|
|
145
|
+
const payload = { FYODOS_API_KEY: apiKey };
|
|
146
|
+
if (apiUrl && apiUrl !== defaultApiUrl) payload.FYODOS_API_URL = apiUrl;
|
|
147
|
+
fs.writeFileSync(abs, `${JSON.stringify(payload, null, 2)}\n`);
|
|
148
|
+
const gitignore = ensureEnvGitIgnored(appRoot, rel);
|
|
149
|
+
return { file: rel, written: true, gitignore };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function reportFlutterWire(result, colors = {}, { quiet = false } = {}) {
|
|
153
|
+
const { blue, cyan, dim, reset } = colors;
|
|
154
|
+
if (!result) return;
|
|
155
|
+
if (result.status === 'added') {
|
|
156
|
+
console.error(`\n${cyan}◉${reset} ${blue}Fiodos${reset} · orb mounted in ${result.file}`);
|
|
157
|
+
} else if (result.status === 'already' && !quiet) {
|
|
158
|
+
console.error(`\n${cyan}◉${reset} ${blue}Fiodos${reset} · ${dim}orb already mounted in ${result.file}${reset}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
module.exports = {
|
|
163
|
+
wireFlutterOrb,
|
|
164
|
+
writeFlutterEnv,
|
|
165
|
+
reportFlutterWire,
|
|
166
|
+
// exported for tests
|
|
167
|
+
buildWiredMain,
|
|
168
|
+
findMainDart,
|
|
169
|
+
HOME_RE,
|
|
170
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wireReactNative — auto-mount the Fiodos orb in an Expo Router app, mirroring
|
|
3
|
+
* the web orb auto-mount (wireWeb.js) but adapted to React Native.
|
|
4
|
+
*
|
|
5
|
+
* Web drops a one-line <FiodosAgent/> into the app entry. The RN counterpart is
|
|
6
|
+
* the SDK's <FiodosExpoAgent/> (from '@fiodos/react-native/expo'), which wires
|
|
7
|
+
* the provider + floating layer + Expo adapters + published orb design behind a
|
|
8
|
+
* single component. So here we:
|
|
9
|
+
* 1. write the published manifest to `fyodos/manifest.json`, and
|
|
10
|
+
* 2. wrap the Expo Router root layout's returned tree with <FiodosExpoAgent>.
|
|
11
|
+
*
|
|
12
|
+
* Edits are idempotent and reversible (FYODOS:ORB markers). When the layout is
|
|
13
|
+
* not in the safe, recognized shape we DON'T touch it — we print a short snippet
|
|
14
|
+
* instead (exactly like the web path), and the caller then skips the install
|
|
15
|
+
* proof so the dashboard waits for the runtime heartbeat rather than lying.
|
|
16
|
+
*/
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
const fs = require('fs');
|
|
20
|
+
const path = require('path');
|
|
21
|
+
|
|
22
|
+
const ORB_START = '{/* FYODOS:ORB:START (auto-generated — safe to remove) */}';
|
|
23
|
+
const ORB_END = '{/* FYODOS:ORB:END */}';
|
|
24
|
+
const IMPORT_START = '// FYODOS:ORB:START (auto-generated — safe to remove)';
|
|
25
|
+
const IMPORT_END = '// FYODOS:ORB:END';
|
|
26
|
+
|
|
27
|
+
const LAYOUT_CANDIDATES = [
|
|
28
|
+
'app/_layout.tsx',
|
|
29
|
+
'app/_layout.jsx',
|
|
30
|
+
'src/app/_layout.tsx',
|
|
31
|
+
'src/app/_layout.jsx',
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
function findRootLayout(appRoot) {
|
|
35
|
+
for (const rel of LAYOUT_CANDIDATES) {
|
|
36
|
+
const abs = path.join(appRoot, rel);
|
|
37
|
+
if (fs.existsSync(abs)) return { abs, rel };
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Write the published manifest the SDK mounts from. Returns the rel path. */
|
|
43
|
+
function writeManifestFile(appRoot, manifest) {
|
|
44
|
+
const dir = path.join(appRoot, 'fyodos');
|
|
45
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
46
|
+
const file = path.join(dir, 'manifest.json');
|
|
47
|
+
fs.writeFileSync(file, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
48
|
+
return path.relative(appRoot, file);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Import path (POSIX) from the layout file to fyodos/manifest.json. */
|
|
52
|
+
function manifestImportSpecifier(layoutAbs, appRoot) {
|
|
53
|
+
const manifestAbs = path.join(appRoot, 'fyodos', 'manifest.json');
|
|
54
|
+
let rel = path.relative(path.dirname(layoutAbs), manifestAbs);
|
|
55
|
+
rel = rel.split(path.sep).join('/');
|
|
56
|
+
if (!rel.startsWith('.')) rel = `./${rel}`;
|
|
57
|
+
return rel;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function insertImportsAfterLastImport(source, block) {
|
|
61
|
+
const lines = source.split('\n');
|
|
62
|
+
let lastImport = -1;
|
|
63
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
64
|
+
if (/^\s*import\b/.test(lines[i])) lastImport = i;
|
|
65
|
+
}
|
|
66
|
+
if (lastImport === -1) return `${block}\n${source}`;
|
|
67
|
+
lines.splice(lastImport + 1, 0, block);
|
|
68
|
+
return lines.join('\n');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Find the parenthesized expression of the FIRST `return (` and return its inner
|
|
73
|
+
* span [start,end) (exclusive of the wrapping parens). null when the return is
|
|
74
|
+
* not the safe `return ( … )` shape we auto-wrap.
|
|
75
|
+
*/
|
|
76
|
+
function findReturnParenSpan(source) {
|
|
77
|
+
const m = /return\s*\(/.exec(source);
|
|
78
|
+
if (!m) return null;
|
|
79
|
+
const open = m.index + m[0].length - 1; // index of '('
|
|
80
|
+
let depth = 0;
|
|
81
|
+
for (let i = open; i < source.length; i += 1) {
|
|
82
|
+
const ch = source[i];
|
|
83
|
+
if (ch === '(') depth += 1;
|
|
84
|
+
else if (ch === ')') {
|
|
85
|
+
depth -= 1;
|
|
86
|
+
if (depth === 0) return { innerStart: open + 1, innerEnd: i, openIdx: open, closeIdx: i };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Detect indentation (spaces) of the line containing index. */
|
|
93
|
+
function indentAt(source, index) {
|
|
94
|
+
const lineStart = source.lastIndexOf('\n', index - 1) + 1;
|
|
95
|
+
const m = /^[ \t]*/.exec(source.slice(lineStart));
|
|
96
|
+
return m ? m[0] : '';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Produce the wired layout source, or null when the layout is not auto-wrappable.
|
|
101
|
+
*/
|
|
102
|
+
function buildWiredLayout(source, manifestSpecifier) {
|
|
103
|
+
const span = findReturnParenSpan(source);
|
|
104
|
+
if (!span) return null;
|
|
105
|
+
const inner = source.slice(span.innerStart, span.innerEnd);
|
|
106
|
+
const baseIndent = indentAt(source, span.openIdx);
|
|
107
|
+
const innerIndent = `${baseIndent} `;
|
|
108
|
+
|
|
109
|
+
// Re-indent the existing tree one level deeper so it nests cleanly.
|
|
110
|
+
const nested = inner
|
|
111
|
+
.replace(/^\n/, '')
|
|
112
|
+
.split('\n')
|
|
113
|
+
.map((l) => (l.trim() ? ` ${l}` : l))
|
|
114
|
+
.join('\n');
|
|
115
|
+
|
|
116
|
+
const wrapped =
|
|
117
|
+
`\n${innerIndent}${ORB_START}\n` +
|
|
118
|
+
`${innerIndent}<FiodosExpoAgent manifest={fyodosManifest as never}>\n` +
|
|
119
|
+
`${nested}\n` +
|
|
120
|
+
`${innerIndent}</FiodosExpoAgent>\n` +
|
|
121
|
+
`${innerIndent}${ORB_END}\n${baseIndent}`;
|
|
122
|
+
|
|
123
|
+
const withWrap = source.slice(0, span.innerStart) + wrapped + source.slice(span.innerEnd);
|
|
124
|
+
|
|
125
|
+
const importBlock =
|
|
126
|
+
`${IMPORT_START}\n` +
|
|
127
|
+
`import { FiodosExpoAgent } from '@fiodos/react-native/expo';\n` +
|
|
128
|
+
`import fyodosManifest from '${manifestSpecifier}';\n` +
|
|
129
|
+
`${IMPORT_END}`;
|
|
130
|
+
|
|
131
|
+
return insertImportsAfterLastImport(withWrap, importBlock);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The copy-paste snippet shown when we can't safely auto-wrap. */
|
|
135
|
+
function mountSnippet(manifestSpecifier) {
|
|
136
|
+
return (
|
|
137
|
+
`import { FiodosExpoAgent } from '@fiodos/react-native/expo';\n` +
|
|
138
|
+
`import fyodosManifest from '${manifestSpecifier}';\n\n` +
|
|
139
|
+
`// Wrap your root layout's returned tree:\n` +
|
|
140
|
+
`<FiodosExpoAgent manifest={fyodosManifest as never}>\n` +
|
|
141
|
+
` {/* your existing <Stack/> / <Slot/> tree */}\n` +
|
|
142
|
+
`</FiodosExpoAgent>`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function printSnippet(colors, layoutRel, manifestSpecifier, reason) {
|
|
147
|
+
const { blue, cyan, dim, reset } = colors;
|
|
148
|
+
console.error(`\n${cyan}◉${reset} ${blue}Fiodos${reset} · ${dim}add the orb to ${layoutRel || 'app/_layout.tsx'}${reason ? ` (${reason})` : ''}:${reset}`);
|
|
149
|
+
console.error(mountSnippet(manifestSpecifier));
|
|
150
|
+
console.error(`${dim}Then re-run analyze — the mount becomes automatic once the layout matches.${reset}\n`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Auto-mount the orb into an Expo Router app.
|
|
155
|
+
* @returns {{status:'added'|'already'|'printed'|'skipped', file?:string, reason?:string}}
|
|
156
|
+
*/
|
|
157
|
+
function wireReactNativeOrb(appRoot, { manifest, colors = {}, noWire = false } = {}) {
|
|
158
|
+
const layout = findRootLayout(appRoot);
|
|
159
|
+
const manifestSpecifier = layout
|
|
160
|
+
? manifestImportSpecifier(layout.abs, appRoot)
|
|
161
|
+
: './fyodos/manifest.json';
|
|
162
|
+
|
|
163
|
+
if (!layout) {
|
|
164
|
+
if (!noWire && manifest) writeManifestFile(appRoot, manifest);
|
|
165
|
+
printSnippet(colors, null, manifestSpecifier, 'no Expo Router app/_layout found');
|
|
166
|
+
return { status: 'printed', reason: 'no-layout' };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let source;
|
|
170
|
+
try {
|
|
171
|
+
source = fs.readFileSync(layout.abs, 'utf8');
|
|
172
|
+
} catch (e) {
|
|
173
|
+
return { status: 'skipped', reason: `read-failed: ${e.message}` };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (source.includes('FiodosExpoAgent') || source.includes('FYODOS:ORB')) {
|
|
177
|
+
if (!noWire && manifest) writeManifestFile(appRoot, manifest);
|
|
178
|
+
return { status: 'already', file: layout.rel };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (noWire) return { status: 'skipped', reason: 'no-wire' };
|
|
182
|
+
|
|
183
|
+
if (manifest) writeManifestFile(appRoot, manifest);
|
|
184
|
+
const wired = buildWiredLayout(source, manifestSpecifier);
|
|
185
|
+
if (!wired) {
|
|
186
|
+
printSnippet(colors, layout.rel, manifestSpecifier, "layout has no plain `return ( … )`");
|
|
187
|
+
return { status: 'printed', file: layout.rel, reason: 'unrecognized-layout' };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
fs.writeFileSync(layout.abs, wired);
|
|
192
|
+
} catch (e) {
|
|
193
|
+
return { status: 'skipped', reason: `write-failed: ${e.message}` };
|
|
194
|
+
}
|
|
195
|
+
return { status: 'added', file: layout.rel };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function reportReactNativeWire(result, colors = {}, { quiet = false } = {}) {
|
|
199
|
+
const { blue, cyan, dim, reset } = colors;
|
|
200
|
+
if (!result) return;
|
|
201
|
+
if (result.status === 'added') {
|
|
202
|
+
console.error(`\n${cyan}◉${reset} ${blue}Fiodos${reset} · orb mounted in ${result.file}`);
|
|
203
|
+
} else if (result.status === 'already' && !quiet) {
|
|
204
|
+
console.error(`\n${cyan}◉${reset} ${blue}Fiodos${reset} · ${dim}orb already mounted in ${result.file}${reset}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
module.exports = {
|
|
209
|
+
wireReactNativeOrb,
|
|
210
|
+
reportReactNativeWire,
|
|
211
|
+
// exported for tests
|
|
212
|
+
buildWiredLayout,
|
|
213
|
+
findReturnParenSpan,
|
|
214
|
+
writeManifestFile,
|
|
215
|
+
manifestImportSpecifier,
|
|
216
|
+
findRootLayout,
|
|
217
|
+
};
|
package/src/writeEnv.js
CHANGED
|
@@ -60,6 +60,18 @@ function resolveEnvPlan(appRoot) {
|
|
|
60
60
|
if (deps['@angular/core'] || has('angular.json')) {
|
|
61
61
|
return { framework: 'angular', file: null, candidates: [], keyVar: null, urlVar: null };
|
|
62
62
|
}
|
|
63
|
+
// Expo / React Native: the orb reads EXPO_PUBLIC_* (Expo inlines these at
|
|
64
|
+
// build time and exposes them via process.env). A plain `.env` at the app root
|
|
65
|
+
// is the convention; we still ensure it is git-ignored like any secret file.
|
|
66
|
+
if (deps.expo || deps['expo-router'] || deps['react-native']) {
|
|
67
|
+
return {
|
|
68
|
+
framework: 'expo',
|
|
69
|
+
file: '.env',
|
|
70
|
+
candidates: ['.env.local', '.env.development.local', '.env.development', '.env'],
|
|
71
|
+
keyVar: 'EXPO_PUBLIC_FYODOS_API_KEY',
|
|
72
|
+
urlVar: 'EXPO_PUBLIC_FYODOS_API_URL',
|
|
73
|
+
};
|
|
74
|
+
}
|
|
63
75
|
if (
|
|
64
76
|
deps['@sveltejs/kit'] ||
|
|
65
77
|
deps.svelte ||
|