|
|
@@ -3,105 +3,76 @@
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
*/
|
|
|
|
|
|
-import inquirer from "inquirer";
|
|
|
-import chalk from "chalk";
|
|
|
+import path from 'path';
|
|
|
|
|
|
-import { copyMaterial, listAllMaterials, Material } from "./materials";
|
|
|
-import { loadNpm } from "../utils/npm";
|
|
|
-import path from "path";
|
|
|
-import { Project } from "../utils/project";
|
|
|
-import { executeRefreshProjectImport } from "./refresh-project-import";
|
|
|
+import chalk from 'chalk';
|
|
|
|
|
|
-export async function syncMaterial(opts: {
|
|
|
- materialName?: string;
|
|
|
- refreshProjectImports?: boolean;
|
|
|
-}) {
|
|
|
- const { materialName, refreshProjectImports } = opts;
|
|
|
+import { Project } from '../utils/project';
|
|
|
+import { loadNpm } from '../utils/npm';
|
|
|
+import { MaterialCliOptions, SyncMaterialContext } from './types';
|
|
|
+import { getSelectedMaterials } from './select';
|
|
|
+import { executeRefreshProjectImport } from './refresh-project-import';
|
|
|
+import { Material } from './material';
|
|
|
+import { copyMaterials } from './copy';
|
|
|
+
|
|
|
+export async function syncMaterial(cliOpts: MaterialCliOptions) {
|
|
|
+ const { refreshProjectImports, targetMaterialRootDir } = cliOpts;
|
|
|
|
|
|
// materialName can be undefined
|
|
|
- console.log(chalk.bold("🚀 Welcome to @flowgram.ai form-materials!"));
|
|
|
+ console.log(chalk.bold('🚀 Welcome to @flowgram.ai form-materials CLI!'));
|
|
|
|
|
|
const project = await Project.getSingleton();
|
|
|
project.printInfo();
|
|
|
|
|
|
+ // where to place all material in target project
|
|
|
+ const targetFormMaterialRoot =
|
|
|
+ targetMaterialRootDir || path.join(project.projectPath, 'src', 'form-materials');
|
|
|
+ console.log(chalk.black(` - Target material root: ${targetFormMaterialRoot}`));
|
|
|
+
|
|
|
if (!project.flowgramVersion) {
|
|
|
throw new Error(
|
|
|
chalk.red(
|
|
|
- "❌ Please install @flowgram.ai/fixed-layout-editor or @flowgram.ai/free-layout-editor",
|
|
|
- ),
|
|
|
+ '❌ Please install @flowgram.ai/fixed-layout-editor or @flowgram.ai/free-layout-editor'
|
|
|
+ )
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- const formMaterialPath = await loadNpm("@flowgram.ai/form-materials");
|
|
|
- const formMaterialSrc = path.join(formMaterialPath, "src");
|
|
|
-
|
|
|
- const materials: Material[] = listAllMaterials(formMaterialSrc);
|
|
|
+ const formMaterialPkg = await loadNpm('@flowgram.ai/form-materials');
|
|
|
|
|
|
- let material: Material | undefined; // material can be undefined
|
|
|
-
|
|
|
- // 1. Check if materialName is provided and exists in materials
|
|
|
- if (materialName) {
|
|
|
- const selectedMaterial = materials.find(
|
|
|
- (m) => `${m.type}/${m.name}` === materialName,
|
|
|
- );
|
|
|
- if (selectedMaterial) {
|
|
|
- material = selectedMaterial;
|
|
|
- console.log(chalk.green(`Using material: ${materialName}`));
|
|
|
- } else {
|
|
|
- console.log(
|
|
|
- chalk.yellow(
|
|
|
- `Material "${materialName}" not found. Please select from the list:`,
|
|
|
- ),
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
+ let selectedMaterials: Material[] = await getSelectedMaterials(cliOpts, formMaterialPkg);
|
|
|
|
|
|
- // 2. If material not found or materialName not provided, prompt user to select
|
|
|
- if (!material) {
|
|
|
- // User select one component
|
|
|
- const result = await inquirer.prompt<{
|
|
|
- material: Material; // Specify type for prompt result
|
|
|
- }>([
|
|
|
- {
|
|
|
- type: "list",
|
|
|
- name: "material",
|
|
|
- message: "Select one material to add:",
|
|
|
- choices: [
|
|
|
- ...materials.map((_material) => ({
|
|
|
- name: `${_material.type}/${_material.name}`,
|
|
|
- value: _material,
|
|
|
- })),
|
|
|
- ],
|
|
|
- },
|
|
|
- ]);
|
|
|
- material = result.material;
|
|
|
- }
|
|
|
// Ensure material is defined before proceeding
|
|
|
- if (!material) {
|
|
|
- console.error(chalk.red("No material selected. Exiting."));
|
|
|
+ if (!selectedMaterials.length) {
|
|
|
+ console.error(chalk.red('No material selected. Exiting.'));
|
|
|
process.exit(1);
|
|
|
}
|
|
|
|
|
|
- // 3. Refresh project imports
|
|
|
+ const context: SyncMaterialContext = {
|
|
|
+ selectedMaterials: selectedMaterials,
|
|
|
+ project,
|
|
|
+ formMaterialPkg,
|
|
|
+ cliOpts,
|
|
|
+ targetFormMaterialRoot,
|
|
|
+ };
|
|
|
+
|
|
|
+ // Copy the materials to the project
|
|
|
+ console.log(chalk.bold('🚀 The following materials will be added to your project'));
|
|
|
+ console.log(selectedMaterials.map((material) => `📦 ${material.fullName}`).join('\n'));
|
|
|
+ console.log('\n');
|
|
|
+
|
|
|
+ let { packagesToInstall } = copyMaterials(context);
|
|
|
+
|
|
|
+ // Refresh project imports
|
|
|
if (refreshProjectImports) {
|
|
|
- console.log(chalk.bold("🚀 Refresh imports in your project"));
|
|
|
- executeRefreshProjectImport(project, material);
|
|
|
+ console.log(chalk.bold('🚀 Refresh imports in your project'));
|
|
|
+ executeRefreshProjectImport(context);
|
|
|
}
|
|
|
|
|
|
- // 4. Copy the materials to the project
|
|
|
- console.log(
|
|
|
- chalk.bold("🚀 The following materials will be added to your project"),
|
|
|
- );
|
|
|
- console.log(material);
|
|
|
- let { packagesToInstall } = copyMaterial(material, project, formMaterialPath);
|
|
|
-
|
|
|
- // 4. Install the dependencies
|
|
|
+ // Install the dependencies
|
|
|
await project.addDependencies(packagesToInstall);
|
|
|
- console.log(
|
|
|
- chalk.bold("✅ These npm dependencies is added to your package.json"),
|
|
|
- );
|
|
|
+ console.log(chalk.bold('\n✅ These npm dependencies is added to your package.json'));
|
|
|
packagesToInstall.forEach((_package) => {
|
|
|
console.log(`- ${_package}`);
|
|
|
});
|
|
|
- console.log(chalk.bold("\n➡️ Please run npm install to install dependencies\n"));
|
|
|
+ console.log(chalk.bold(chalk.bold('\n➡️ Please run npm install to install dependencies\n')));
|
|
|
}
|