@flowgram.ai/cli 0.1.8 → 0.4.10
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/index.cjs +324 -95
- package/dist/index.js +325 -92
- package/package.json +1 -1
- package/src/index.ts +3 -2
- package/src/materials/index.ts +23 -9
- package/src/materials/materials.ts +32 -39
- package/src/materials/refresh-project-import.ts +68 -0
- package/src/utils/export.ts +117 -0
- package/src/utils/{traverse-file.ts → file.ts} +1 -1
- package/src/utils/import.ts +3 -3
- package/src/utils/project.ts +3 -0
- package/src/utils/ts-file.ts +134 -0
package/dist/index.cjs
CHANGED
|
@@ -22,23 +22,22 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
mod
|
|
23
23
|
));
|
|
24
24
|
|
|
25
|
-
// ../../common/temp/node_modules/.pnpm/tsup@8.3.5_typescript@5.8.3/node_modules/tsup/assets/cjs_shims.js
|
|
26
|
-
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
27
|
-
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
28
|
-
|
|
29
25
|
// src/index.ts
|
|
30
26
|
var import_commander = require("commander");
|
|
31
27
|
|
|
32
28
|
// src/materials/index.ts
|
|
33
29
|
var import_inquirer = __toESM(require("inquirer"), 1);
|
|
34
|
-
var
|
|
30
|
+
var import_chalk3 = __toESM(require("chalk"), 1);
|
|
35
31
|
|
|
36
32
|
// src/materials/materials.ts
|
|
37
|
-
var
|
|
33
|
+
var import_path3 = __toESM(require("path"), 1);
|
|
34
|
+
var import_fs3 = __toESM(require("fs"), 1);
|
|
35
|
+
|
|
36
|
+
// src/utils/ts-file.ts
|
|
38
37
|
var import_path2 = __toESM(require("path"), 1);
|
|
39
38
|
var import_fs2 = __toESM(require("fs"), 1);
|
|
40
39
|
|
|
41
|
-
// src/utils/
|
|
40
|
+
// src/utils/file.ts
|
|
42
41
|
var import_path = __toESM(require("path"), 1);
|
|
43
42
|
var import_fs = __toESM(require("fs"), 1);
|
|
44
43
|
var File = class {
|
|
@@ -67,16 +66,92 @@ var File = class {
|
|
|
67
66
|
import_fs.default.writeFileSync(this.path, this.content, "utf-8");
|
|
68
67
|
}
|
|
69
68
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
69
|
+
|
|
70
|
+
// src/utils/export.ts
|
|
71
|
+
function extractNamedExports(content) {
|
|
72
|
+
const valueExports = [];
|
|
73
|
+
const typeExports = [];
|
|
74
|
+
const typeDefinitions = /* @__PURE__ */ new Set();
|
|
75
|
+
const typePatterns = [
|
|
76
|
+
/\b(?:type|interface)\s+(\w+)/g,
|
|
77
|
+
/\bexport\s+(?:type|interface)\s+(\w+)/g
|
|
78
|
+
];
|
|
79
|
+
let match;
|
|
80
|
+
for (const pattern of typePatterns) {
|
|
81
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
82
|
+
typeDefinitions.add(match[1]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const exportPatterns = [
|
|
86
|
+
// export const/var/let/function/class/type/interface
|
|
87
|
+
/\bexport\s+(const|var|let|function|class|type|interface)\s+(\w+)/g,
|
|
88
|
+
// export { name1, name2 }
|
|
89
|
+
/\bexport\s*\{([^}]+)\}/g,
|
|
90
|
+
// export { name as alias }
|
|
91
|
+
/\bexport\s*\{[^}]*\b(\w+)\s+as\s+(\w+)[^}]*\}/g,
|
|
92
|
+
// export default function name()
|
|
93
|
+
/\bexport\s+default\s+(?:function|class)\s+(\w+)/g,
|
|
94
|
+
// export type { Type1, Type2 }
|
|
95
|
+
/\bexport\s+type\s*\{([^}]+)\}/g,
|
|
96
|
+
// export type { Original as Alias }
|
|
97
|
+
/\bexport\s+type\s*\{[^}]*\b(\w+)\s+as\s+(\w+)[^}]*\}/g
|
|
98
|
+
];
|
|
99
|
+
exportPatterns[0].lastIndex = 0;
|
|
100
|
+
while ((match = exportPatterns[0].exec(content)) !== null) {
|
|
101
|
+
const [, kind, name] = match;
|
|
102
|
+
if (kind === "type" || kind === "interface" || typeDefinitions.has(name)) {
|
|
103
|
+
typeExports.push(name);
|
|
76
104
|
} else {
|
|
77
|
-
|
|
105
|
+
valueExports.push(name);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exportPatterns[1].lastIndex = 0;
|
|
109
|
+
while ((match = exportPatterns[1].exec(content)) !== null) {
|
|
110
|
+
const exportsList = match[1].split(",").map((item) => item.trim()).filter((item) => item && !item.includes(" as "));
|
|
111
|
+
for (const name of exportsList) {
|
|
112
|
+
if (name.startsWith("type ")) {
|
|
113
|
+
typeExports.push(name.replace("type ", "").trim());
|
|
114
|
+
} else if (typeDefinitions.has(name)) {
|
|
115
|
+
typeExports.push(name);
|
|
116
|
+
} else {
|
|
117
|
+
valueExports.push(name);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exportPatterns[2].lastIndex = 0;
|
|
122
|
+
while ((match = exportPatterns[2].exec(content)) !== null) {
|
|
123
|
+
const [, original, alias] = match;
|
|
124
|
+
if (typeDefinitions.has(original)) {
|
|
125
|
+
typeExports.push(alias);
|
|
126
|
+
} else {
|
|
127
|
+
valueExports.push(alias);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exportPatterns[3].lastIndex = 0;
|
|
131
|
+
while ((match = exportPatterns[3].exec(content)) !== null) {
|
|
132
|
+
const name = match[1];
|
|
133
|
+
if (typeDefinitions.has(name)) {
|
|
134
|
+
typeExports.push(name);
|
|
135
|
+
} else {
|
|
136
|
+
valueExports.push(name);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exportPatterns[4].lastIndex = 0;
|
|
140
|
+
while ((match = exportPatterns[4].exec(content)) !== null) {
|
|
141
|
+
const exportsList = match[1].split(",").map((item) => item.trim()).filter((item) => item && !item.includes(" as "));
|
|
142
|
+
for (const name of exportsList) {
|
|
143
|
+
typeExports.push(name);
|
|
78
144
|
}
|
|
79
145
|
}
|
|
146
|
+
exportPatterns[5].lastIndex = 0;
|
|
147
|
+
while ((match = exportPatterns[5].exec(content)) !== null) {
|
|
148
|
+
const [, original, alias] = match;
|
|
149
|
+
typeExports.push(alias);
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
values: [...new Set(valueExports)].sort(),
|
|
153
|
+
types: [...new Set(typeExports)].sort()
|
|
154
|
+
};
|
|
80
155
|
}
|
|
81
156
|
|
|
82
157
|
// src/utils/import.ts
|
|
@@ -96,14 +171,10 @@ function assembleImport(declaration) {
|
|
|
96
171
|
if (namespaceImport) {
|
|
97
172
|
importClauses.push(`* as ${namespaceImport}`);
|
|
98
173
|
}
|
|
99
|
-
return `import ${importClauses.join(", ")} from '${source}'
|
|
100
|
-
}
|
|
101
|
-
function replaceImport(fileContent, origin, replaceTo) {
|
|
102
|
-
const replaceImportStatements = replaceTo.map(assembleImport);
|
|
103
|
-
return fileContent.replace(origin.statement, replaceImportStatements.join("\n"));
|
|
174
|
+
return `import ${importClauses.join(", ")} from '${source}';`;
|
|
104
175
|
}
|
|
105
176
|
function* traverseFileImports(fileContent) {
|
|
106
|
-
const importRegex = /import\s+([^{}*,]*?)?(?:\s*\*\s*as\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*,?)?(?:\s*\{([^}]*)\}\s*,?)?(?:\s*([a-zA-Z_$][a-zA-Z0-9_$]*)\s*,?)?\s*from\s*['"`]([^'"`]+)['"`]
|
|
177
|
+
const importRegex = /import\s+([^{}*,]*?)?(?:\s*\*\s*as\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*,?)?(?:\s*\{([^}]*)\}\s*,?)?(?:\s*([a-zA-Z_$][a-zA-Z0-9_$]*)\s*,?)?\s*from\s*['"`]([^'"`]+)['"`]\;?/g;
|
|
107
178
|
let match;
|
|
108
179
|
while ((match = importRegex.exec(fileContent)) !== null) {
|
|
109
180
|
const [fullMatch, defaultPart, namespacePart, namedPart, defaultPart2, source] = match;
|
|
@@ -148,9 +219,113 @@ function* traverseFileImports(fileContent) {
|
|
|
148
219
|
}
|
|
149
220
|
}
|
|
150
221
|
|
|
222
|
+
// src/utils/ts-file.ts
|
|
223
|
+
var TsFile = class extends File {
|
|
224
|
+
constructor(filePath) {
|
|
225
|
+
super(filePath);
|
|
226
|
+
this.exports = {
|
|
227
|
+
values: [],
|
|
228
|
+
types: []
|
|
229
|
+
};
|
|
230
|
+
this.imports = [];
|
|
231
|
+
this.exports = extractNamedExports(import_fs2.default.readFileSync(filePath, "utf-8"));
|
|
232
|
+
this.imports = Array.from(
|
|
233
|
+
traverseFileImports(import_fs2.default.readFileSync(filePath, "utf-8"))
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
get allExportNames() {
|
|
237
|
+
return [...this.exports.values, ...this.exports.types];
|
|
238
|
+
}
|
|
239
|
+
addImport(importDeclarations) {
|
|
240
|
+
importDeclarations.forEach((importDeclaration) => {
|
|
241
|
+
importDeclaration.statement = assembleImport(importDeclaration);
|
|
242
|
+
});
|
|
243
|
+
this.replace((content) => {
|
|
244
|
+
const lastImportStatement = this.imports[this.imports.length - 1];
|
|
245
|
+
return content.replace(
|
|
246
|
+
lastImportStatement.statement,
|
|
247
|
+
`${lastImportStatement?.statement}
|
|
248
|
+
${importDeclarations.map(
|
|
249
|
+
(item) => item.statement
|
|
250
|
+
)}
|
|
251
|
+
`
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
this.imports.push(...importDeclarations);
|
|
255
|
+
}
|
|
256
|
+
removeImport(importDeclarations) {
|
|
257
|
+
this.replace(
|
|
258
|
+
(content) => importDeclarations.reduce(
|
|
259
|
+
(prev, cur) => prev.replace(cur.statement, ""),
|
|
260
|
+
content
|
|
261
|
+
)
|
|
262
|
+
);
|
|
263
|
+
this.imports = this.imports.filter(
|
|
264
|
+
(item) => !importDeclarations.includes(item)
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
replaceImport(oldImports, newImports) {
|
|
268
|
+
newImports.forEach((importDeclaration) => {
|
|
269
|
+
importDeclaration.statement = assembleImport(importDeclaration);
|
|
270
|
+
});
|
|
271
|
+
this.replace((content) => {
|
|
272
|
+
oldImports.forEach((oldImport, idx) => {
|
|
273
|
+
const replaceTo = newImports[idx];
|
|
274
|
+
if (replaceTo) {
|
|
275
|
+
content = content.replace(oldImport.statement, replaceTo.statement);
|
|
276
|
+
this.imports.map((_import) => {
|
|
277
|
+
if (_import.statement === oldImport.statement) {
|
|
278
|
+
_import = replaceTo;
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
} else {
|
|
282
|
+
content = content.replace(oldImport.statement, "");
|
|
283
|
+
this.imports = this.imports.filter(
|
|
284
|
+
(_import) => _import.statement !== oldImport.statement
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
const restNewImports = newImports.slice(oldImports.length);
|
|
289
|
+
if (restNewImports.length > 0) {
|
|
290
|
+
const lastImportStatement = newImports[oldImports.length - 1].statement;
|
|
291
|
+
content = content.replace(
|
|
292
|
+
lastImportStatement,
|
|
293
|
+
`${lastImportStatement}
|
|
294
|
+
${restNewImports.map(
|
|
295
|
+
(item) => item.statement
|
|
296
|
+
)}
|
|
297
|
+
`
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
this.imports.push(...restNewImports);
|
|
301
|
+
return content;
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
function* traverseRecursiveTsFiles(folder) {
|
|
306
|
+
const files = import_fs2.default.readdirSync(folder);
|
|
307
|
+
for (const file of files) {
|
|
308
|
+
const filePath = import_path2.default.join(folder, file);
|
|
309
|
+
if (import_fs2.default.statSync(filePath).isDirectory()) {
|
|
310
|
+
yield* traverseRecursiveTsFiles(filePath);
|
|
311
|
+
} else {
|
|
312
|
+
if (file.endsWith(".ts") || file.endsWith(".tsx")) {
|
|
313
|
+
yield new TsFile(filePath);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function getIndexTsFile(folder) {
|
|
319
|
+
const files = import_fs2.default.readdirSync(folder);
|
|
320
|
+
for (const file of files) {
|
|
321
|
+
if (file === "index.ts" || file === "index.tsx") {
|
|
322
|
+
return new TsFile(import_path2.default.join(folder, file));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return void 0;
|
|
326
|
+
}
|
|
327
|
+
|
|
151
328
|
// src/materials/materials.ts
|
|
152
|
-
var __filename2 = (0, import_url.fileURLToPath)(importMetaUrl);
|
|
153
|
-
var __dirname2 = import_path2.default.dirname(__filename2);
|
|
154
329
|
var _types = [
|
|
155
330
|
"components",
|
|
156
331
|
"effects",
|
|
@@ -164,9 +339,9 @@ var _types = [
|
|
|
164
339
|
function listAllMaterials(formMaterialSrc) {
|
|
165
340
|
const _materials = [];
|
|
166
341
|
for (const _type of _types) {
|
|
167
|
-
const materialsPath =
|
|
342
|
+
const materialsPath = import_path3.default.join(formMaterialSrc, _type);
|
|
168
343
|
_materials.push(
|
|
169
|
-
...
|
|
344
|
+
...import_fs3.default.readdirSync(materialsPath).map((_path) => {
|
|
170
345
|
if (_path === "index.ts") {
|
|
171
346
|
return null;
|
|
172
347
|
}
|
|
@@ -174,7 +349,7 @@ function listAllMaterials(formMaterialSrc) {
|
|
|
174
349
|
name: _path,
|
|
175
350
|
// Assuming the folder name is the material name
|
|
176
351
|
type: _type,
|
|
177
|
-
path:
|
|
352
|
+
path: import_path3.default.join(materialsPath, _path)
|
|
178
353
|
};
|
|
179
354
|
}).filter((material) => material !== null)
|
|
180
355
|
);
|
|
@@ -182,50 +357,47 @@ function listAllMaterials(formMaterialSrc) {
|
|
|
182
357
|
return _materials;
|
|
183
358
|
}
|
|
184
359
|
var getFormMaterialDependencies = (formMaterialPath) => {
|
|
185
|
-
const packageJsonPath =
|
|
186
|
-
const packageJson = JSON.parse(
|
|
360
|
+
const packageJsonPath = import_path3.default.join(formMaterialPath, "package.json");
|
|
361
|
+
const packageJson = JSON.parse(import_fs3.default.readFileSync(packageJsonPath, "utf8"));
|
|
187
362
|
return packageJson.dependencies;
|
|
188
363
|
};
|
|
189
364
|
var copyMaterial = (material, project, formMaterialPath) => {
|
|
190
365
|
const formMaterialDependencies = getFormMaterialDependencies(formMaterialPath);
|
|
191
366
|
const sourceDir = material.path;
|
|
192
|
-
const materialRoot =
|
|
367
|
+
const materialRoot = import_path3.default.join(
|
|
193
368
|
project.projectPath,
|
|
194
369
|
"src",
|
|
195
370
|
"form-materials",
|
|
196
371
|
`${material.type}`
|
|
197
372
|
);
|
|
198
|
-
const targetDir =
|
|
373
|
+
const targetDir = import_path3.default.join(materialRoot, material.name);
|
|
199
374
|
const packagesToInstall = /* @__PURE__ */ new Set();
|
|
200
|
-
|
|
201
|
-
for (const file of
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
} else {
|
|
227
|
-
packagesToInstall.add(`${dep}@${version}`);
|
|
228
|
-
}
|
|
375
|
+
import_fs3.default.cpSync(sourceDir, targetDir, { recursive: true });
|
|
376
|
+
for (const file of traverseRecursiveTsFiles(targetDir)) {
|
|
377
|
+
for (const importDeclaration of file.imports) {
|
|
378
|
+
const { source } = importDeclaration;
|
|
379
|
+
if (source.startsWith("@/")) {
|
|
380
|
+
console.log(
|
|
381
|
+
`Replace Import from ${source} to @flowgram.ai/form-materials`
|
|
382
|
+
);
|
|
383
|
+
file.replaceImport(
|
|
384
|
+
[importDeclaration],
|
|
385
|
+
[{ ...importDeclaration, source: "@flowgram.ai/form-materials" }]
|
|
386
|
+
);
|
|
387
|
+
packagesToInstall.add(
|
|
388
|
+
`@flowgram.ai/form-materials@${project.flowgramVersion}`
|
|
389
|
+
);
|
|
390
|
+
} else if (!source.startsWith(".") && !source.startsWith("react")) {
|
|
391
|
+
const [dep, version] = Object.entries(formMaterialDependencies).find(
|
|
392
|
+
([_key]) => source.startsWith(_key)
|
|
393
|
+
) || [];
|
|
394
|
+
if (!dep) {
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
if (dep.startsWith("@flowgram.ai/")) {
|
|
398
|
+
packagesToInstall.add(`${dep}@${project.flowgramVersion}`);
|
|
399
|
+
} else {
|
|
400
|
+
packagesToInstall.add(`${dep}@${version}`);
|
|
229
401
|
}
|
|
230
402
|
}
|
|
231
403
|
}
|
|
@@ -237,19 +409,19 @@ var copyMaterial = (material, project, formMaterialPath) => {
|
|
|
237
409
|
|
|
238
410
|
// src/utils/npm.ts
|
|
239
411
|
var import_child_process = require("child_process");
|
|
240
|
-
var
|
|
241
|
-
var
|
|
412
|
+
var import_fs4 = require("fs");
|
|
413
|
+
var import_path4 = __toESM(require("path"), 1);
|
|
242
414
|
var import_download = __toESM(require("download"), 1);
|
|
243
415
|
async function getLatestVersion(packageName) {
|
|
244
416
|
return (0, import_child_process.execSync)(`npm view ${packageName} version --tag=latest`).toString().trim();
|
|
245
417
|
}
|
|
246
418
|
async function loadNpm(packageName) {
|
|
247
419
|
const packageLatestVersion = await getLatestVersion(packageName);
|
|
248
|
-
const packagePath =
|
|
420
|
+
const packagePath = import_path4.default.join(
|
|
249
421
|
__dirname,
|
|
250
422
|
`./.download/${packageName}-${packageLatestVersion}`
|
|
251
423
|
);
|
|
252
|
-
if ((0,
|
|
424
|
+
if ((0, import_fs4.existsSync)(packagePath)) {
|
|
253
425
|
return packagePath;
|
|
254
426
|
}
|
|
255
427
|
try {
|
|
@@ -265,26 +437,27 @@ async function loadNpm(packageName) {
|
|
|
265
437
|
}
|
|
266
438
|
|
|
267
439
|
// src/materials/index.ts
|
|
268
|
-
var
|
|
440
|
+
var import_path6 = __toESM(require("path"), 1);
|
|
269
441
|
|
|
270
442
|
// src/utils/project.ts
|
|
271
|
-
var
|
|
272
|
-
var
|
|
443
|
+
var import_fs5 = require("fs");
|
|
444
|
+
var import_path5 = __toESM(require("path"), 1);
|
|
273
445
|
var import_chalk = __toESM(require("chalk"), 1);
|
|
274
446
|
var Project = class _Project {
|
|
275
447
|
constructor() {
|
|
276
448
|
}
|
|
277
449
|
async init() {
|
|
278
450
|
let projectPath = process.cwd();
|
|
279
|
-
while (projectPath !== "/" && !(0,
|
|
280
|
-
projectPath =
|
|
451
|
+
while (projectPath !== "/" && !(0, import_fs5.existsSync)(import_path5.default.join(projectPath, "package.json"))) {
|
|
452
|
+
projectPath = import_path5.default.join(projectPath, "..");
|
|
281
453
|
}
|
|
282
454
|
if (projectPath === "/") {
|
|
283
455
|
throw new Error("Please run this command in a valid project");
|
|
284
456
|
}
|
|
285
457
|
this.projectPath = projectPath;
|
|
286
|
-
this.
|
|
287
|
-
this.
|
|
458
|
+
this.srcPath = import_path5.default.join(projectPath, "src");
|
|
459
|
+
this.packageJsonPath = import_path5.default.join(projectPath, "package.json");
|
|
460
|
+
this.packageJson = JSON.parse((0, import_fs5.readFileSync)(this.packageJsonPath, "utf8"));
|
|
288
461
|
this.flowgramVersion = this.packageJson.dependencies["@flowgram.ai/fixed-layout-editor"] || this.packageJson.dependencies["@flowgram.ai/free-layout-editor"] || this.packageJson.dependencies["@flowgram.ai/editor"];
|
|
289
462
|
}
|
|
290
463
|
async addDependency(dependency) {
|
|
@@ -302,7 +475,7 @@ var Project = class _Project {
|
|
|
302
475
|
}
|
|
303
476
|
}
|
|
304
477
|
this.packageJson.dependencies[name] = version;
|
|
305
|
-
(0,
|
|
478
|
+
(0, import_fs5.writeFileSync)(
|
|
306
479
|
this.packageJsonPath,
|
|
307
480
|
JSON.stringify(this.packageJson, null, 2)
|
|
308
481
|
);
|
|
@@ -324,18 +497,70 @@ var Project = class _Project {
|
|
|
324
497
|
}
|
|
325
498
|
};
|
|
326
499
|
|
|
500
|
+
// src/materials/refresh-project-import.ts
|
|
501
|
+
var import_chalk2 = __toESM(require("chalk"), 1);
|
|
502
|
+
function executeRefreshProjectImport(project, material) {
|
|
503
|
+
const materialFile = getIndexTsFile(material.path);
|
|
504
|
+
if (!materialFile) {
|
|
505
|
+
console.warn(`Material ${material.name} not found`);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const targetDir = `@/form-materials/${material.type}/${material.name}`;
|
|
509
|
+
const exportNames = materialFile.allExportNames;
|
|
510
|
+
console.log(`\u{1F440} The exports of ${material.name} is ${exportNames.join(",")}`);
|
|
511
|
+
for (const tsFile of traverseRecursiveTsFiles(project.srcPath)) {
|
|
512
|
+
for (const importDeclaration of tsFile.imports) {
|
|
513
|
+
if (importDeclaration.source === "@flowgram.ai/form-materials") {
|
|
514
|
+
const currentMaterialImports = importDeclaration.namedImports?.filter(
|
|
515
|
+
(item) => exportNames.includes(item.imported)
|
|
516
|
+
);
|
|
517
|
+
if (!currentMaterialImports?.length) {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
const nextImports = [
|
|
521
|
+
{
|
|
522
|
+
...importDeclaration,
|
|
523
|
+
namedImports: currentMaterialImports,
|
|
524
|
+
source: targetDir
|
|
525
|
+
}
|
|
526
|
+
];
|
|
527
|
+
const keepImportNames = importDeclaration.namedImports?.filter(
|
|
528
|
+
(item) => !exportNames.includes(item.imported)
|
|
529
|
+
);
|
|
530
|
+
if (keepImportNames?.length) {
|
|
531
|
+
nextImports.unshift({
|
|
532
|
+
...importDeclaration,
|
|
533
|
+
namedImports: keepImportNames
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
tsFile.replaceImport([importDeclaration], nextImports);
|
|
537
|
+
console.log(import_chalk2.default.green(`\u{1F504} Refresh Imports In: ${tsFile.path}`));
|
|
538
|
+
console.log(
|
|
539
|
+
`From:
|
|
540
|
+
${importDeclaration.statement}
|
|
541
|
+
To:
|
|
542
|
+
${nextImports.map((item) => item.statement).join("\n")}`
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
327
549
|
// src/materials/index.ts
|
|
328
|
-
async function syncMaterial(
|
|
329
|
-
|
|
550
|
+
async function syncMaterial(opts) {
|
|
551
|
+
const { materialName, refreshProjectImports } = opts;
|
|
552
|
+
console.log(import_chalk3.default.bold("\u{1F680} Welcome to @flowgram.ai form-materials!"));
|
|
330
553
|
const project = await Project.getSingleton();
|
|
331
554
|
project.printInfo();
|
|
332
555
|
if (!project.flowgramVersion) {
|
|
333
556
|
throw new Error(
|
|
334
|
-
|
|
557
|
+
import_chalk3.default.red(
|
|
558
|
+
"\u274C Please install @flowgram.ai/fixed-layout-editor or @flowgram.ai/free-layout-editor"
|
|
559
|
+
)
|
|
335
560
|
);
|
|
336
561
|
}
|
|
337
562
|
const formMaterialPath = await loadNpm("@flowgram.ai/form-materials");
|
|
338
|
-
const formMaterialSrc =
|
|
563
|
+
const formMaterialSrc = import_path6.default.join(formMaterialPath, "src");
|
|
339
564
|
const materials = listAllMaterials(formMaterialSrc);
|
|
340
565
|
let material;
|
|
341
566
|
if (materialName) {
|
|
@@ -344,10 +569,10 @@ async function syncMaterial(materialName) {
|
|
|
344
569
|
);
|
|
345
570
|
if (selectedMaterial) {
|
|
346
571
|
material = selectedMaterial;
|
|
347
|
-
console.log(
|
|
572
|
+
console.log(import_chalk3.default.green(`Using material: ${materialName}`));
|
|
348
573
|
} else {
|
|
349
574
|
console.log(
|
|
350
|
-
|
|
575
|
+
import_chalk3.default.yellow(
|
|
351
576
|
`Material "${materialName}" not found. Please select from the list:`
|
|
352
577
|
)
|
|
353
578
|
);
|
|
@@ -370,30 +595,34 @@ async function syncMaterial(materialName) {
|
|
|
370
595
|
material = result.material;
|
|
371
596
|
}
|
|
372
597
|
if (!material) {
|
|
373
|
-
console.error(
|
|
598
|
+
console.error(import_chalk3.default.red("No material selected. Exiting."));
|
|
374
599
|
process.exit(1);
|
|
375
600
|
}
|
|
601
|
+
if (refreshProjectImports) {
|
|
602
|
+
console.log(import_chalk3.default.bold("\u{1F680} Refresh imports in your project"));
|
|
603
|
+
executeRefreshProjectImport(project, material);
|
|
604
|
+
}
|
|
376
605
|
console.log(
|
|
377
|
-
|
|
606
|
+
import_chalk3.default.bold("\u{1F680} The following materials will be added to your project")
|
|
378
607
|
);
|
|
379
608
|
console.log(material);
|
|
380
609
|
let { packagesToInstall } = copyMaterial(material, project, formMaterialPath);
|
|
381
610
|
await project.addDependencies(packagesToInstall);
|
|
382
611
|
console.log(
|
|
383
|
-
|
|
612
|
+
import_chalk3.default.bold("\u2705 These npm dependencies is added to your package.json")
|
|
384
613
|
);
|
|
385
614
|
packagesToInstall.forEach((_package) => {
|
|
386
615
|
console.log(`- ${_package}`);
|
|
387
616
|
});
|
|
388
|
-
console.log(
|
|
617
|
+
console.log(import_chalk3.default.bold("\n\u27A1\uFE0F Please run npm install to install dependencies\n"));
|
|
389
618
|
}
|
|
390
619
|
|
|
391
620
|
// src/create-app/index.ts
|
|
392
|
-
var
|
|
621
|
+
var import_path7 = __toESM(require("path"), 1);
|
|
393
622
|
var import_child_process2 = require("child_process");
|
|
394
623
|
var import_inquirer2 = __toESM(require("inquirer"), 1);
|
|
395
624
|
var import_fs_extra = __toESM(require("fs-extra"), 1);
|
|
396
|
-
var
|
|
625
|
+
var import_chalk4 = __toESM(require("chalk"), 1);
|
|
397
626
|
var import_download2 = __toESM(require("download"), 1);
|
|
398
627
|
var tar = __toESM(require("tar"), 1);
|
|
399
628
|
var args = process.argv.slice(2);
|
|
@@ -405,7 +634,7 @@ var updateFlowGramVersions = (dependencies, latestVersion) => {
|
|
|
405
634
|
}
|
|
406
635
|
};
|
|
407
636
|
var createApp = async (projectName) => {
|
|
408
|
-
console.log(
|
|
637
|
+
console.log(import_chalk4.default.green("Welcome to @flowgram.ai/create-app CLI!"));
|
|
409
638
|
const latest = (0, import_child_process2.execSync)("npm view @flowgram.ai/demo-fixed-layout version --tag=latest latest").toString().trim();
|
|
410
639
|
let folderName = "";
|
|
411
640
|
if (!projectName) {
|
|
@@ -435,18 +664,18 @@ var createApp = async (projectName) => {
|
|
|
435
664
|
}
|
|
436
665
|
}
|
|
437
666
|
try {
|
|
438
|
-
const targetDir =
|
|
667
|
+
const targetDir = import_path7.default.join(process.cwd());
|
|
439
668
|
const downloadPackage = async () => {
|
|
440
669
|
try {
|
|
441
670
|
const tarballBuffer = await (0, import_download2.default)(`https://registry.npmjs.org/@flowgram.ai/${folderName}/-/${folderName}-${latest}.tgz`);
|
|
442
671
|
import_fs_extra.default.ensureDirSync(targetDir);
|
|
443
|
-
const tempTarballPath =
|
|
672
|
+
const tempTarballPath = import_path7.default.join(process.cwd(), `${folderName}.tgz`);
|
|
444
673
|
import_fs_extra.default.writeFileSync(tempTarballPath, tarballBuffer);
|
|
445
674
|
await tar.x({
|
|
446
675
|
file: tempTarballPath,
|
|
447
676
|
C: targetDir
|
|
448
677
|
});
|
|
449
|
-
import_fs_extra.default.renameSync(
|
|
678
|
+
import_fs_extra.default.renameSync(import_path7.default.join(targetDir, "package"), import_path7.default.join(targetDir, folderName));
|
|
450
679
|
import_fs_extra.default.unlinkSync(tempTarballPath);
|
|
451
680
|
return true;
|
|
452
681
|
} catch (error) {
|
|
@@ -455,7 +684,7 @@ var createApp = async (projectName) => {
|
|
|
455
684
|
}
|
|
456
685
|
};
|
|
457
686
|
const res = await downloadPackage();
|
|
458
|
-
const pkgJsonPath =
|
|
687
|
+
const pkgJsonPath = import_path7.default.join(targetDir, folderName, "package.json");
|
|
459
688
|
const data = import_fs_extra.default.readFileSync(pkgJsonPath, "utf-8");
|
|
460
689
|
const packageLatestVersion = (0, import_child_process2.execSync)("npm view @flowgram.ai/core version --tag=latest latest").toString().trim();
|
|
461
690
|
const jsonData = JSON.parse(data);
|
|
@@ -467,13 +696,13 @@ var createApp = async (projectName) => {
|
|
|
467
696
|
}
|
|
468
697
|
import_fs_extra.default.writeFileSync(pkgJsonPath, JSON.stringify(jsonData, null, 2), "utf-8");
|
|
469
698
|
if (res) {
|
|
470
|
-
console.log(
|
|
471
|
-
console.log(
|
|
472
|
-
console.log(
|
|
473
|
-
console.log(
|
|
474
|
-
console.log(
|
|
699
|
+
console.log(import_chalk4.default.green(`${folderName} Demo project created successfully!`));
|
|
700
|
+
console.log(import_chalk4.default.yellow("Run the following commands to start:"));
|
|
701
|
+
console.log(import_chalk4.default.cyan(` cd ${folderName}`));
|
|
702
|
+
console.log(import_chalk4.default.cyan(" npm install"));
|
|
703
|
+
console.log(import_chalk4.default.cyan(" npm start"));
|
|
475
704
|
} else {
|
|
476
|
-
console.log(
|
|
705
|
+
console.log(import_chalk4.default.red("Download failed"));
|
|
477
706
|
}
|
|
478
707
|
} catch (error) {
|
|
479
708
|
console.error("Error downloading repo:", error);
|
|
@@ -487,7 +716,7 @@ program.name("flowgram-cli").version("1.0.0").description("Flowgram CLI");
|
|
|
487
716
|
program.command("create-app").description("Create a new flowgram project").argument("[string]", "Project name").action(async (projectName) => {
|
|
488
717
|
await createApp(projectName);
|
|
489
718
|
});
|
|
490
|
-
program.command("materials").description("Sync materials to the project").argument("[string]", "Material name").action(async (materialName) => {
|
|
491
|
-
await syncMaterial(materialName);
|
|
719
|
+
program.command("materials").description("Sync materials to the project").argument("[string]", "Material name").option("--refresh-project-imports", "Refresh project imports to copied materials", false).action(async (materialName, options) => {
|
|
720
|
+
await syncMaterial({ materialName, refreshProjectImports: options.refreshProjectImports });
|
|
492
721
|
});
|
|
493
722
|
program.parse(process.argv);
|