@flowgram.ai/cli 0.4.11 → 0.4.13

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.
@@ -3,15 +3,12 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
- import path from "path";
7
- import fs from "fs";
8
- import { File, traverseRecursiveFilePaths } from "./file";
9
- import { extractNamedExports } from "./export";
10
- import {
11
- assembleImport,
12
- ImportDeclaration,
13
- traverseFileImports,
14
- } from "./import";
6
+ import path, { join } from 'path';
7
+ import fs from 'fs';
8
+
9
+ import { assembleImport, ImportDeclaration, traverseFileImports } from './import';
10
+ import { File, traverseRecursiveFilePaths } from './file';
11
+ import { extractNamedExports } from './export';
15
12
 
16
13
  class TsFile extends File {
17
14
  exports: {
@@ -28,13 +25,11 @@ class TsFile extends File {
28
25
  return [...this.exports.values, ...this.exports.types];
29
26
  }
30
27
 
31
- constructor(filePath: string) {
32
- super(filePath);
28
+ constructor(filePath: string, root?: string) {
29
+ super(filePath, root);
33
30
 
34
- this.exports = extractNamedExports(fs.readFileSync(filePath, "utf-8"));
35
- this.imports = Array.from(
36
- traverseFileImports(fs.readFileSync(filePath, "utf-8")),
37
- );
31
+ this.exports = extractNamedExports(fs.readFileSync(filePath, 'utf-8'));
32
+ this.imports = Array.from(traverseFileImports(fs.readFileSync(filePath, 'utf-8')));
38
33
  }
39
34
 
40
35
  addImport(importDeclarations: ImportDeclaration[]) {
@@ -46,9 +41,7 @@ class TsFile extends File {
46
41
  const lastImportStatement = this.imports[this.imports.length - 1];
47
42
  return content.replace(
48
43
  lastImportStatement.statement,
49
- `${lastImportStatement?.statement}\n${importDeclarations.map(
50
- (item) => item.statement,
51
- )}\n`,
44
+ `${lastImportStatement?.statement}\n${importDeclarations.map((item) => item.statement)}\n`
52
45
  );
53
46
  });
54
47
  this.imports.push(...importDeclarations);
@@ -56,20 +49,12 @@ class TsFile extends File {
56
49
 
57
50
  removeImport(importDeclarations: ImportDeclaration[]) {
58
51
  this.replace((content) =>
59
- importDeclarations.reduce(
60
- (prev, cur) => prev.replace(cur.statement, ""),
61
- content,
62
- ),
63
- );
64
- this.imports = this.imports.filter(
65
- (item) => !importDeclarations.includes(item),
52
+ importDeclarations.reduce((prev, cur) => prev.replace(cur.statement, ''), content)
66
53
  );
54
+ this.imports = this.imports.filter((item) => !importDeclarations.includes(item));
67
55
  }
68
56
 
69
- replaceImport(
70
- oldImports: ImportDeclaration[],
71
- newImports: ImportDeclaration[],
72
- ) {
57
+ replaceImport(oldImports: ImportDeclaration[], newImports: ImportDeclaration[]) {
73
58
  newImports.forEach((importDeclaration) => {
74
59
  importDeclaration.statement = assembleImport(importDeclaration);
75
60
  });
@@ -84,9 +69,9 @@ class TsFile extends File {
84
69
  }
85
70
  });
86
71
  } else {
87
- content = content.replace(oldImport.statement, "");
72
+ content = content.replace(oldImport.statement, '');
88
73
  this.imports = this.imports.filter(
89
- (_import) => _import.statement !== oldImport.statement,
74
+ (_import) => _import.statement !== oldImport.statement
90
75
  );
91
76
  }
92
77
  });
@@ -96,9 +81,9 @@ class TsFile extends File {
96
81
  const lastImportStatement = newImports[oldImports.length - 1].statement;
97
82
  content = content.replace(
98
83
  lastImportStatement,
99
- `${lastImportStatement}\n${restNewImports.map(
100
- (item) => item.statement,
101
- )}\n`,
84
+ `${lastImportStatement}
85
+ ${restNewImports.map((item) => item.statement).join('\n')}
86
+ `
102
87
  );
103
88
  }
104
89
  this.imports.push(...restNewImports);
@@ -110,8 +95,8 @@ class TsFile extends File {
110
95
 
111
96
  export function* traverseRecursiveTsFiles(folder: string): Generator<TsFile> {
112
97
  for (const filePath of traverseRecursiveFilePaths(folder)) {
113
- if (filePath.endsWith(".ts") || filePath.endsWith(".tsx")) {
114
- yield new TsFile(filePath);
98
+ if (filePath.endsWith('.ts') || filePath.endsWith('.tsx')) {
99
+ yield new TsFile(filePath, folder);
115
100
  }
116
101
  }
117
102
  }
@@ -120,8 +105,8 @@ export function getIndexTsFile(folder: string): TsFile | undefined {
120
105
  // ts or tsx
121
106
  const files = fs.readdirSync(folder);
122
107
  for (const file of files) {
123
- if (file === "index.ts" || file === "index.tsx") {
124
- return new TsFile(path.join(folder, file));
108
+ if (file === 'index.ts' || file === 'index.tsx') {
109
+ return new TsFile(path.join(folder, file), folder);
125
110
  }
126
111
  }
127
112
  return undefined;
@@ -1,127 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
- * SPDX-License-Identifier: MIT
4
- */
5
-
6
- import path from "path";
7
- import fs from "fs";
8
-
9
- import { Project } from "../utils/project"; // Import ProjectInfo
10
- import { traverseRecursiveTsFiles } from "../utils/ts-file";
11
-
12
- // Added type definitions
13
- export interface Material {
14
- name: string;
15
- type: string;
16
- path: string;
17
- [key: string]: any; // For other properties from config.json
18
- }
19
-
20
- const _types: string[] = [
21
- "components",
22
- "effects",
23
- "plugins",
24
- "shared",
25
- "typings",
26
- "validate",
27
- "form-plugins",
28
- "hooks",
29
- ];
30
-
31
- export function listAllMaterials(formMaterialSrc: string): Material[] {
32
- const _materials: Material[] = [];
33
-
34
- for (const _type of _types) {
35
- // 在 Node.js 中,import.meta.dirname 不可用,可使用 import.meta.url 结合 url 模块来获取目录路径
36
-
37
- const materialsPath: string = path.join(formMaterialSrc, _type);
38
- _materials.push(
39
- ...fs
40
- .readdirSync(materialsPath)
41
- .map((_path: string) => {
42
- if (_path === "index.ts") {
43
- return null;
44
- }
45
-
46
- return {
47
- name: _path, // Assuming the folder name is the material name
48
- type: _type,
49
- path: path.join(materialsPath, _path),
50
- } as Material;
51
- })
52
- .filter((material): material is Material => material !== null),
53
- );
54
- }
55
-
56
- return _materials;
57
- }
58
-
59
- export const getFormMaterialDependencies = (
60
- formMaterialPath: string,
61
- ): Record<string, string> => {
62
- const packageJsonPath: string = path.join(formMaterialPath, "package.json");
63
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
64
-
65
- return packageJson.dependencies;
66
- };
67
-
68
- export const copyMaterial = (
69
- material: Material,
70
- project: Project,
71
- formMaterialPath: string,
72
- ): {
73
- packagesToInstall: string[];
74
- } => {
75
- const formMaterialDependencies =
76
- getFormMaterialDependencies(formMaterialPath);
77
-
78
- const sourceDir: string = material.path;
79
- const materialRoot: string = path.join(
80
- project.projectPath,
81
- "src",
82
- "form-materials",
83
- `${material.type}`,
84
- );
85
- const targetDir = path.join(materialRoot, material.name);
86
- const packagesToInstall: Set<string> = new Set();
87
-
88
- fs.cpSync(sourceDir, targetDir, { recursive: true });
89
-
90
- for (const file of traverseRecursiveTsFiles(targetDir)) {
91
- for (const importDeclaration of file.imports) {
92
- const { source } = importDeclaration;
93
-
94
- if (source.startsWith("@/")) {
95
- // is inner import
96
- console.log(
97
- `Replace Import from ${source} to @flowgram.ai/form-materials`,
98
- );
99
- file.replaceImport(
100
- [importDeclaration],
101
- [{ ...importDeclaration, source: "@flowgram.ai/form-materials" }],
102
- );
103
- packagesToInstall.add(
104
- `@flowgram.ai/form-materials@${project.flowgramVersion}`,
105
- );
106
- } else if (!source.startsWith(".") && !source.startsWith("react")) {
107
- // check if is in form material dependencies
108
- const [dep, version] =
109
- Object.entries(formMaterialDependencies).find(([_key]) =>
110
- source.startsWith(_key),
111
- ) || [];
112
- if (!dep) {
113
- continue;
114
- }
115
- if (dep.startsWith("@flowgram.ai/")) {
116
- packagesToInstall.add(`${dep}@${project.flowgramVersion}`);
117
- } else {
118
- packagesToInstall.add(`${dep}@${version}`);
119
- }
120
- }
121
- }
122
- }
123
-
124
- return {
125
- packagesToInstall: [...packagesToInstall],
126
- };
127
- };