@flowgram.ai/form-materials 0.1.8

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/bin/index.js ADDED
@@ -0,0 +1,63 @@
1
+ import chalk from 'chalk';
2
+ import { Command } from 'commander';
3
+ import inquirer from 'inquirer';
4
+
5
+ import { bfsMaterials, copyMaterial, listAllMaterials } from './materials.js';
6
+ import { getProjectInfo, installDependencies } from './project.js';
7
+
8
+ const program = new Command();
9
+
10
+ program
11
+ .version('1.0.0')
12
+ .description('Add official materials to your project')
13
+ .action(async () => {
14
+ console.log(chalk.bgGreenBright('Welcome to @flowgram.ai/form-materials CLI!'));
15
+
16
+ const projectInfo = getProjectInfo();
17
+
18
+ console.log(chalk.bold('Project Info:'));
19
+ console.log(chalk.black(` - Flowgram Version: ${projectInfo.flowgramVersion}`));
20
+ console.log(chalk.black(` - Project Path: ${projectInfo.projectPath}`));
21
+
22
+ const materials = listAllMaterials();
23
+
24
+ // 2. User select one component
25
+ const { material } = await inquirer.prompt([
26
+ {
27
+ type: 'list',
28
+ name: 'material',
29
+ message: 'Select one material to add:',
30
+ choices: [
31
+ ...materials.map((_material) => ({
32
+ name: `${_material.type}/${_material.name}`,
33
+ value: _material,
34
+ })),
35
+ ],
36
+ },
37
+ ]);
38
+
39
+ console.log(material);
40
+
41
+ // 3. Get the component dependencies by BFS (include depMaterials and depPackages)
42
+ const { allMaterials, allPackages } = bfsMaterials(material, materials);
43
+
44
+ // 4. Install the dependencies
45
+ let flowgramPackage = `@flowgram.ai/editor`;
46
+ if (projectInfo.flowgramVersion !== 'workspace:*') {
47
+ flowgramPackage = `@flowgram.ai/editor@${projectInfo.flowgramVersion}`;
48
+ }
49
+ const packagesToInstall = [flowgramPackage, ...allPackages];
50
+
51
+ console.log(chalk.bold('These npm dependencies will be added to your project'));
52
+ console.log(packagesToInstall);
53
+ installDependencies(packagesToInstall, projectInfo);
54
+
55
+ // 5. Copy the materials to the project
56
+ console.log(chalk.bold('These Materials will be added to your project'));
57
+ console.log(allMaterials);
58
+ allMaterials.forEach((material) => {
59
+ copyMaterial(material, projectInfo);
60
+ });
61
+ });
62
+
63
+ program.parse(process.argv);
@@ -0,0 +1,72 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ const _types = ['components'];
5
+
6
+ export function listAllMaterials() {
7
+ const _materials = [];
8
+
9
+ for (const _type of _types) {
10
+ const materialsPath = path.join(import.meta.dirname, '..', 'src', _type);
11
+ _materials.push(
12
+ ...fs
13
+ .readdirSync(materialsPath)
14
+ .map((_path) => {
15
+ if (_path === 'index.ts') {
16
+ return null;
17
+ }
18
+
19
+ const config = fs.readFileSync(path.join(materialsPath, _path, 'config.json'), 'utf8');
20
+ return {
21
+ ...JSON.parse(config),
22
+ type: _type,
23
+ path: path.join(materialsPath, _path),
24
+ };
25
+ })
26
+ .filter(Boolean)
27
+ );
28
+ }
29
+
30
+ return _materials;
31
+ }
32
+
33
+ export function bfsMaterials(material, _materials = []) {
34
+ function findConfigByName(name) {
35
+ return _materials.find((_config) => _config.name === name);
36
+ }
37
+
38
+ const queue = [material];
39
+ const allMaterials = new Set();
40
+ const allPackages = new Set();
41
+
42
+ while (queue.length > 0) {
43
+ const _material = queue.shift();
44
+ if (allMaterials.has(_material)) {
45
+ continue;
46
+ }
47
+ allMaterials.add(_material);
48
+
49
+ if (_material.depPackages) {
50
+ for (const _package of _material.depPackages) {
51
+ allPackages.add(_package);
52
+ }
53
+ }
54
+
55
+ if (_material.depMaterials) {
56
+ for (const _materialName of _material.depMaterials) {
57
+ queue.push(findConfigByName(_materialName));
58
+ }
59
+ }
60
+ }
61
+
62
+ return {
63
+ allMaterials: Array.from(allMaterials),
64
+ allPackages: Array.from(allPackages),
65
+ };
66
+ }
67
+
68
+ export const copyMaterial = (material, projectInfo) => {
69
+ const sourceDir = material.path;
70
+ const targetDir = path.join(projectInfo.projectPath, `form-${material.type}`, material.name);
71
+ fs.cpSync(sourceDir, targetDir, { recursive: true });
72
+ };
package/bin/project.js ADDED
@@ -0,0 +1,72 @@
1
+ import { execSync } from 'child_process';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+
5
+ export function getProjectInfo() {
6
+ // get nearest package.json
7
+ let projectPath = process.cwd();
8
+
9
+ while (projectPath !== '/' && !fs.existsSync(path.join(projectPath, 'package.json'))) {
10
+ projectPath = path.join(projectPath, '..');
11
+ }
12
+
13
+ if (projectPath === '/') {
14
+ throw new Error('Please run this command in a valid project');
15
+ }
16
+
17
+ const packageJsonPath = path.join(projectPath, 'package.json');
18
+
19
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
20
+
21
+ // fixed layout or free layout
22
+ const flowgramVersion =
23
+ packageJson.dependencies['@flowgram.ai/fixed-layout-editor'] ||
24
+ packageJson.dependencies['@flowgram.ai/free-layout-editor'] ||
25
+ packageJson.dependencies['@flowgram.ai/editor'];
26
+
27
+ if (!flowgramVersion) {
28
+ throw new Error(
29
+ 'Please install @flowgram.ai/fixed-layout-editor or @flowgram.ai/free-layout-editor'
30
+ );
31
+ }
32
+
33
+ return {
34
+ projectPath,
35
+ packageJsonPath,
36
+ packageJson,
37
+ flowgramVersion,
38
+ };
39
+ }
40
+
41
+ export function findRushJson(startPath) {
42
+ let currentPath = startPath;
43
+ while (currentPath !== '/' && !fs.existsSync(path.join(currentPath, 'rush.json'))) {
44
+ currentPath = path.join(currentPath, '..');
45
+ }
46
+ if (fs.existsSync(path.join(currentPath, 'rush.json'))) {
47
+ return path.join(currentPath, 'rush.json');
48
+ }
49
+ return null;
50
+ }
51
+
52
+ export function installDependencies(packages, projectInfo) {
53
+ if (fs.existsSync(path.join(projectInfo.projectPath, 'yarn.lock'))) {
54
+ execSync(`yarn add ${packages.join(' ')}`, { stdio: 'inherit' });
55
+ return;
56
+ }
57
+
58
+ if (fs.existsSync(path.join(projectInfo.projectPath, 'pnpm-lock.yaml'))) {
59
+ execSync(`pnpm add ${packages.join(' ')}`, { stdio: 'inherit' });
60
+ return;
61
+ }
62
+
63
+ // rush monorepo
64
+ if (findRushJson(projectInfo.projectPath)) {
65
+ execSync(`rush add ${packages.map((pkg) => `--package ${pkg}`).join(' ')}`, {
66
+ stdio: 'inherit',
67
+ });
68
+ return;
69
+ }
70
+
71
+ execSync(`npm install ${packages.join(' ')}`, { stdio: 'inherit' });
72
+ }