slicejs-cli 3.6.3 → 3.6.4

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.
@@ -1,172 +1,172 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import Table from 'cli-table3';
4
- import chalk from 'chalk';
5
- import Print from '../Print.js';
6
- import { getSrcPath, getComponentsJsPath, getConfigPath } from '../utils/PathHelper.js';
7
-
8
- /**
9
- * Loads configuration from sliceConfig.json
10
- * @returns {object} - Configuration object
11
- */
12
- const loadConfig = () => {
13
- try {
14
- const configPath = getConfigPath(import.meta.url);
15
- if (!fs.existsSync(configPath)) {
16
- Print.error('sliceConfig.json not found');
17
- Print.info('Run "slice init" to initialize your project');
18
- return null;
19
- }
20
- const rawData = fs.readFileSync(configPath, 'utf-8');
21
- return JSON.parse(rawData);
22
- } catch (error) {
23
- Print.error(`Failed to load configuration: ${error.message}`);
24
- Print.info('Check that sliceConfig.json is valid JSON');
25
- return null;
26
- }
27
- };
28
-
29
- /**
30
- * Lists files in a given folder, filtering only .js files
31
- * @param {string} folderPath - Path of the folder to read
32
- * @returns {string[]} - List of found files
33
- */
34
- const listComponents = (folderPath) => {
35
- try {
36
- if (!fs.existsSync(folderPath)) {
37
- return [];
38
- }
39
- const result = fs.readdirSync(folderPath);
40
- return result;
41
- } catch (error) {
42
- Print.error(`Failed to read directory ${folderPath}: ${error.message}`);
43
- return [];
44
- }
45
- };
46
-
47
- /**
48
- * Counts files in a component directory
49
- */
50
- const countComponentFiles = (componentPath) => {
51
- try {
52
- if (!fs.existsSync(componentPath)) return 0;
53
- const files = fs.readdirSync(componentPath);
54
- return files.filter(f => fs.statSync(path.join(componentPath, f)).isFile()).length;
55
- } catch {
56
- return 0;
57
- }
58
- };
59
-
60
- /**
61
- * Gets components dynamically from sliceConfig.json
62
- * @returns {object} - Component mapping with their category
63
- */
64
- const getComponents = () => {
65
- const config = loadConfig();
66
- if (!config) return {};
67
-
68
- const folderSuffix = 'src'; // Always use 'src' for development
69
- const componentPaths = config.paths?.components || {};
70
- let allComponents = new Map();
71
-
72
- Object.entries(componentPaths).forEach(([category, { path: folderPath }]) => {
73
- const cleanFolderPath = folderPath ? folderPath.replace(/^[/\\]+/, '') : '';
74
- const fullPath = getSrcPath(import.meta.url, cleanFolderPath);
75
- const files = listComponents(fullPath);
76
-
77
- files.forEach(file => {
78
- const componentPath = path.join(fullPath, file);
79
- if (fs.statSync(componentPath).isDirectory()) {
80
- const fileCount = countComponentFiles(componentPath);
81
- allComponents.set(file, { category, files: fileCount });
82
- }
83
- });
84
- });
85
-
86
- return Object.fromEntries(allComponents);
87
- };
88
-
89
- function listComponentsReal() {
90
- try {
91
- // Get components dynamically
92
- const components = getComponents();
93
-
94
- if (Object.keys(components).length === 0) {
95
- Print.warning('No components found in your project');
96
- Print.info('Create your first component with "slice component create"');
97
- return;
98
- }
99
-
100
- // Create table with cli-table3
101
- const table = new Table({
102
- head: [
103
- chalk.cyan.bold('Component'),
104
- chalk.cyan.bold('Category'),
105
- chalk.cyan.bold('Files')
106
- ],
107
- colWidths: [30, 20, 10],
108
- style: {
109
- head: [],
110
- border: ['gray']
111
- }
112
- });
113
-
114
- // Group by category for better visualization
115
- const byCategory = {};
116
- Object.entries(components).forEach(([name, data]) => {
117
- if (!byCategory[data.category]) {
118
- byCategory[data.category] = [];
119
- }
120
- byCategory[data.category].push({ name, files: data.files });
121
- });
122
-
123
- // Add rows to the table
124
- Object.entries(byCategory).forEach(([category, comps]) => {
125
- comps.forEach((comp, index) => {
126
- if (index === 0) {
127
- // First row of the category
128
- table.push([
129
- chalk.bold(comp.name),
130
- chalk.yellow(category),
131
- comp.files.toString()
132
- ]);
133
- } else {
134
- // Rest of components in the category
135
- table.push([
136
- chalk.bold(comp.name),
137
- chalk.gray('″'), // Ditto mark
138
- comp.files.toString()
139
- ]);
140
- }
141
- });
142
- });
143
-
144
- Print.newLine();
145
- Print.title('📦 Local Components');
146
- Print.newLine();
147
- console.log(table.toString());
148
- Print.newLine();
149
- Print.info(`Total: ${Object.keys(components).length} component${Object.keys(components).length !== 1 ? 's' : ''} found`);
150
-
151
- // Path where components.js will be generated
152
- const outputPath = getComponentsJsPath(import.meta.url);
153
-
154
- // Ensure the directory exists
155
- const outputDir = path.dirname(outputPath);
156
- if (!fs.existsSync(outputDir)) {
157
- fs.mkdirSync(outputDir, { recursive: true });
158
- }
159
-
160
- // Generate components.js file with detected components
161
- const componentsForExport = Object.fromEntries(
162
- Object.entries(components).map(([name, data]) => [name, data.category])
163
- );
164
- fs.writeFileSync(outputPath, `const components = ${JSON.stringify(componentsForExport, null, 2)};\n\nexport default components;\n`);
165
-
166
- } catch (error) {
167
- Print.error(`Failed to list components: ${error.message}`);
168
- Print.info('Make sure your project structure is correct');
169
- }
170
- }
171
-
172
- export default listComponentsReal;
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import Table from 'cli-table3';
4
+ import chalk from 'chalk';
5
+ import Print from '../Print.js';
6
+ import { getSrcPath, getComponentsJsPath, getConfigPath } from '../utils/PathHelper.js';
7
+
8
+ /**
9
+ * Loads configuration from sliceConfig.json
10
+ * @returns {object} - Configuration object
11
+ */
12
+ const loadConfig = () => {
13
+ try {
14
+ const configPath = getConfigPath(import.meta.url);
15
+ if (!fs.existsSync(configPath)) {
16
+ Print.error('sliceConfig.json not found');
17
+ Print.info('Run "slice init" to initialize your project');
18
+ return null;
19
+ }
20
+ const rawData = fs.readFileSync(configPath, 'utf-8');
21
+ return JSON.parse(rawData);
22
+ } catch (error) {
23
+ Print.error(`Failed to load configuration: ${error.message}`);
24
+ Print.info('Check that sliceConfig.json is valid JSON');
25
+ return null;
26
+ }
27
+ };
28
+
29
+ /**
30
+ * Lists files in a given folder, filtering only .js files
31
+ * @param {string} folderPath - Path of the folder to read
32
+ * @returns {string[]} - List of found files
33
+ */
34
+ const listComponents = (folderPath) => {
35
+ try {
36
+ if (!fs.existsSync(folderPath)) {
37
+ return [];
38
+ }
39
+ const result = fs.readdirSync(folderPath);
40
+ return result;
41
+ } catch (error) {
42
+ Print.error(`Failed to read directory ${folderPath}: ${error.message}`);
43
+ return [];
44
+ }
45
+ };
46
+
47
+ /**
48
+ * Counts files in a component directory
49
+ */
50
+ const countComponentFiles = (componentPath) => {
51
+ try {
52
+ if (!fs.existsSync(componentPath)) return 0;
53
+ const files = fs.readdirSync(componentPath);
54
+ return files.filter(f => fs.statSync(path.join(componentPath, f)).isFile()).length;
55
+ } catch {
56
+ return 0;
57
+ }
58
+ };
59
+
60
+ /**
61
+ * Gets components dynamically from sliceConfig.json
62
+ * @returns {object} - Component mapping with their category
63
+ */
64
+ const getComponents = () => {
65
+ const config = loadConfig();
66
+ if (!config) return {};
67
+
68
+ const folderSuffix = 'src'; // Always use 'src' for development
69
+ const componentPaths = config.paths?.components || {};
70
+ let allComponents = new Map();
71
+
72
+ Object.entries(componentPaths).forEach(([category, { path: folderPath }]) => {
73
+ const cleanFolderPath = folderPath ? folderPath.replace(/^[/\\]+/, '') : '';
74
+ const fullPath = getSrcPath(import.meta.url, cleanFolderPath);
75
+ const files = listComponents(fullPath);
76
+
77
+ files.forEach(file => {
78
+ const componentPath = path.join(fullPath, file);
79
+ if (fs.statSync(componentPath).isDirectory()) {
80
+ const fileCount = countComponentFiles(componentPath);
81
+ allComponents.set(file, { category, files: fileCount });
82
+ }
83
+ });
84
+ });
85
+
86
+ return Object.fromEntries(allComponents);
87
+ };
88
+
89
+ function listComponentsReal() {
90
+ try {
91
+ // Get components dynamically
92
+ const components = getComponents();
93
+
94
+ if (Object.keys(components).length === 0) {
95
+ Print.warning('No components found in your project');
96
+ Print.info('Create your first component with "slice component create"');
97
+ return;
98
+ }
99
+
100
+ // Create table with cli-table3
101
+ const table = new Table({
102
+ head: [
103
+ chalk.cyan.bold('Component'),
104
+ chalk.cyan.bold('Category'),
105
+ chalk.cyan.bold('Files')
106
+ ],
107
+ colWidths: [30, 20, 10],
108
+ style: {
109
+ head: [],
110
+ border: ['gray']
111
+ }
112
+ });
113
+
114
+ // Group by category for better visualization
115
+ const byCategory = {};
116
+ Object.entries(components).forEach(([name, data]) => {
117
+ if (!byCategory[data.category]) {
118
+ byCategory[data.category] = [];
119
+ }
120
+ byCategory[data.category].push({ name, files: data.files });
121
+ });
122
+
123
+ // Add rows to the table
124
+ Object.entries(byCategory).forEach(([category, comps]) => {
125
+ comps.forEach((comp, index) => {
126
+ if (index === 0) {
127
+ // First row of the category
128
+ table.push([
129
+ chalk.bold(comp.name),
130
+ chalk.yellow(category),
131
+ comp.files.toString()
132
+ ]);
133
+ } else {
134
+ // Rest of components in the category
135
+ table.push([
136
+ chalk.bold(comp.name),
137
+ chalk.gray('″'), // Ditto mark
138
+ comp.files.toString()
139
+ ]);
140
+ }
141
+ });
142
+ });
143
+
144
+ Print.newLine();
145
+ Print.title('📦 Local Components');
146
+ Print.newLine();
147
+ console.log(table.toString());
148
+ Print.newLine();
149
+ Print.info(`Total: ${Object.keys(components).length} component${Object.keys(components).length !== 1 ? 's' : ''} found`);
150
+
151
+ // Path where components.js will be generated
152
+ const outputPath = getComponentsJsPath(import.meta.url);
153
+
154
+ // Ensure the directory exists
155
+ const outputDir = path.dirname(outputPath);
156
+ if (!fs.existsSync(outputDir)) {
157
+ fs.mkdirSync(outputDir, { recursive: true });
158
+ }
159
+
160
+ // Generate components.js file with detected components
161
+ const componentsForExport = Object.fromEntries(
162
+ Object.entries(components).map(([name, data]) => [name, data.category])
163
+ );
164
+ fs.writeFileSync(outputPath, `const components = ${JSON.stringify(componentsForExport, null, 2)};\n\nexport default components;\n`);
165
+
166
+ } catch (error) {
167
+ Print.error(`Failed to list components: ${error.message}`);
168
+ Print.info('Make sure your project structure is correct');
169
+ }
170
+ }
171
+
172
+ export default listComponentsReal;