samre-cli 1.0.4 → 1.0.5
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/utils/flutter.js +54 -32
package/package.json
CHANGED
package/src/utils/flutter.js
CHANGED
|
@@ -54,6 +54,24 @@ export const FlutterUtil = {
|
|
|
54
54
|
return sdkPath;
|
|
55
55
|
},
|
|
56
56
|
|
|
57
|
+
cleanMainDartContent(rawContent) {
|
|
58
|
+
let content = rawContent;
|
|
59
|
+
|
|
60
|
+
// 1. Supprimer l'import du SDK Samré
|
|
61
|
+
content = content.replace(/import\s+['"]samre_sdk\.dart['"];\s*\n?/g, '');
|
|
62
|
+
content = content.replace(/\/\/\s*SDK Samré\s*\n?/gi, '');
|
|
63
|
+
content = content.replace(/\/\/\s*👉\s*SamreOverlay[^\n]*\n?/gi, '');
|
|
64
|
+
|
|
65
|
+
// 2. Supprimer la propriété builder SamreOverlay dans MaterialApp
|
|
66
|
+
content = content.replace(/builder\s*:\s*\([^)]*\)\s*=>\s*SamreOverlay\s*\(\s*child\s*:\s*child\s*\?\?\s*const\s*SizedBox\(\)\s*\),?\s*\n?/g, '');
|
|
67
|
+
content = content.replace(/builder\s*:\s*\([^)]*\)\s*=>\s*SamreOverlay\s*\(\s*child\s*:\s*([^)]+)\),?\s*\n?/g, 'builder: (context, child) => $1,\n');
|
|
68
|
+
|
|
69
|
+
// 3. Déballer SamreOverlay s'il entoure un widget (ex: home: SamreOverlay(child: SplashPage()))
|
|
70
|
+
content = content.replace(/SamreOverlay\s*\(\s*child\s*:\s*([^)]+)\)/g, '$1');
|
|
71
|
+
|
|
72
|
+
return content;
|
|
73
|
+
},
|
|
74
|
+
|
|
57
75
|
injectIntoMain(cwd = process.cwd()) {
|
|
58
76
|
const mainPath = path.join(cwd, 'lib', 'main.dart');
|
|
59
77
|
const backupPath = path.join(cwd, 'lib', 'main.dart.samre_bak');
|
|
@@ -62,49 +80,45 @@ export const FlutterUtil = {
|
|
|
62
80
|
return { injected: false, reason: 'main.dart introuvable' };
|
|
63
81
|
}
|
|
64
82
|
|
|
65
|
-
const
|
|
83
|
+
const originalContent = fs.readFileSync(mainPath, 'utf-8');
|
|
66
84
|
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
fs.writeFileSync(backupPath, content, 'utf-8');
|
|
70
|
-
}
|
|
85
|
+
// Nettoyer d'abord tout résidu ou ancienne injection
|
|
86
|
+
let cleanContent = this.cleanMainDartContent(originalContent);
|
|
71
87
|
|
|
72
|
-
//
|
|
73
|
-
if (
|
|
74
|
-
|
|
88
|
+
// Sauvegarde de secours du fichier propre
|
|
89
|
+
if (!fs.existsSync(backupPath)) {
|
|
90
|
+
fs.writeFileSync(backupPath, cleanContent, 'utf-8');
|
|
75
91
|
}
|
|
76
92
|
|
|
77
|
-
let modified =
|
|
93
|
+
let modified = cleanContent;
|
|
78
94
|
|
|
79
|
-
// 1. Ajouter l'import de samre_sdk en haut (sans dupliquer
|
|
95
|
+
// 1. Ajouter l'import de samre_sdk en haut (sans dupliquer)
|
|
80
96
|
if (!modified.includes('samre_sdk.dart')) {
|
|
81
97
|
modified = "import 'samre_sdk.dart';\n" + modified;
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
// 2.
|
|
85
|
-
if (modified.includes('MaterialApp(')
|
|
86
|
-
// Si MaterialApp a déjà une propriété builder
|
|
100
|
+
// 2. Injecter proprement dans builder de MaterialApp
|
|
101
|
+
if (modified.includes('MaterialApp(')) {
|
|
102
|
+
// Si MaterialApp a déjà une propriété builder existante
|
|
87
103
|
const builderRegex = /builder\s*:\s*\(([^)]*)\)\s*=>\s*([^,\n;]+)/;
|
|
88
104
|
if (builderRegex.test(modified)) {
|
|
89
105
|
modified = modified.replace(builderRegex, (match, args, body) => {
|
|
90
106
|
return `builder: (${args}) => SamreOverlay(child: ${body.trim()})`;
|
|
91
107
|
});
|
|
92
|
-
|
|
93
|
-
|
|
108
|
+
} else {
|
|
109
|
+
// Sinon insérer builder: (context, child) => SamreOverlay(...)
|
|
110
|
+
modified = modified.replace(
|
|
111
|
+
/MaterialApp\s*\(/,
|
|
112
|
+
'MaterialApp(\n builder: (context, child) => SamreOverlay(child: child ?? const SizedBox()),'
|
|
113
|
+
);
|
|
94
114
|
}
|
|
95
|
-
|
|
96
|
-
// Sinon insérer la propriété builder juste après MaterialApp(
|
|
97
|
-
modified = modified.replace(
|
|
98
|
-
/MaterialApp\s*\(/,
|
|
99
|
-
'MaterialApp(\n builder: (context, child) => SamreOverlay(child: child ?? const SizedBox()),'
|
|
100
|
-
);
|
|
101
115
|
fs.writeFileSync(mainPath, modified, 'utf-8');
|
|
102
116
|
return { injected: true, modifiedBuilder: true, backupPath };
|
|
103
117
|
}
|
|
104
118
|
|
|
105
119
|
// Fallback : injection sur home:
|
|
106
120
|
const homeRegex = /home\s*:\s*([^,\n\);]+)/;
|
|
107
|
-
if (homeRegex.test(modified)
|
|
121
|
+
if (homeRegex.test(modified)) {
|
|
108
122
|
modified = modified.replace(homeRegex, (match, p1) => {
|
|
109
123
|
return `home: SamreOverlay(child: ${p1.trim()})`;
|
|
110
124
|
});
|
|
@@ -125,22 +139,30 @@ export const FlutterUtil = {
|
|
|
125
139
|
let sdkDeleted = false;
|
|
126
140
|
let mainRestored = false;
|
|
127
141
|
|
|
142
|
+
// 1. Supprimer le fichier SDK
|
|
128
143
|
if (fs.existsSync(sdkPath)) {
|
|
129
144
|
fs.unlinkSync(sdkPath);
|
|
130
145
|
sdkDeleted = true;
|
|
131
146
|
}
|
|
132
147
|
|
|
148
|
+
// 2. Si un backup existe, essayer de le restaurer
|
|
133
149
|
if (fs.existsSync(backupPath)) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
150
|
+
try {
|
|
151
|
+
const backupContent = fs.readFileSync(backupPath, 'utf-8');
|
|
152
|
+
const cleanBackup = this.cleanMainDartContent(backupContent);
|
|
153
|
+
fs.writeFileSync(mainPath, cleanBackup, 'utf-8');
|
|
154
|
+
fs.unlinkSync(backupPath);
|
|
155
|
+
mainRestored = true;
|
|
156
|
+
} catch {
|
|
157
|
+
// Fallback nettoyage direct
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 3. Dans tous les cas, s'assurer que main.dart est 100% purgé de toute trace de Samré
|
|
162
|
+
if (fs.existsSync(mainPath)) {
|
|
163
|
+
const currentContent = fs.readFileSync(mainPath, 'utf-8');
|
|
164
|
+
const cleanContent = this.cleanMainDartContent(currentContent);
|
|
165
|
+
fs.writeFileSync(mainPath, cleanContent, 'utf-8');
|
|
144
166
|
mainRestored = true;
|
|
145
167
|
}
|
|
146
168
|
|