ui-thing 0.0.18 → 0.0.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ui-thing",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "CLI used to add Nuxt 3 components to a project",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
@@ -61,12 +61,12 @@
61
61
  "@types/figlet": "^1.5.8",
62
62
  "@types/fs-extra": "^11.0.4",
63
63
  "@types/lodash": "^4.14.202",
64
- "@types/node": "^20.10.3",
64
+ "@types/node": "^20.10.4",
65
65
  "@types/prompts": "^2.4.9",
66
- "@vitest/coverage-v8": "^0.34.6",
66
+ "@vitest/coverage-v8": "^1.0.4",
67
67
  "magicast": "^0.3.2",
68
68
  "tsup": "^8.0.1",
69
- "typescript": "^5.3.2",
70
- "vitest": "^0.34.6"
69
+ "typescript": "^5.3.3",
70
+ "vitest": "^1.0.4"
71
71
  }
72
72
  }
@@ -1,286 +1,286 @@
1
- import path from "node:path";
2
- import { Command } from "commander";
3
- import { consola } from "consola";
4
- import kleur from "kleur";
5
- import _ from "lodash";
6
- import prompts from "prompts";
7
-
8
- import allComponents from "../comps";
9
- import { Component } from "../types";
10
- import { compareUIConfig } from "../utils/compareUIConfig";
11
- import { addModuleToConfig, getNuxtConfig, getUIConfig, updateConfig } from "../utils/config";
12
- import { fileExists } from "../utils/fileExists";
13
- import { installPackages } from "../utils/installPackages";
14
- import { printFancyBoxMessage } from "../utils/printFancyBoxMessage";
15
- import { promptUserForComponents } from "../utils/promptForComponents";
16
- import { writeFile } from "../utils/writeFile";
17
-
18
- const currentDirectory = process.cwd();
19
-
20
- const findComponent = (name: string) => {
21
- return allComponents.find((c) => c.value.toLowerCase() === name.toLowerCase());
22
- };
23
-
24
- /**
25
- * Adds a component to your project
26
- */
27
- export const add = new Command()
28
- .name("add")
29
- .command("add")
30
- .description("Add a list of components to your project.")
31
- .argument("[componentNames...]", "Components that you want to add.")
32
- .action(async (components: Array<string>) => {
33
- // Get nuxt config
34
- const cfg = await getNuxtConfig();
35
- // Get ui config
36
- let uiConfig = await getUIConfig();
37
- let uiConfigIsCorrect = await compareUIConfig();
38
- if (!uiConfigIsCorrect) {
39
- uiConfig = await getUIConfig({ force: true });
40
- }
41
- if (_.isEmpty(uiConfig)) {
42
- consola.info("Config file not set. Exiting...");
43
- process.exit(0);
44
- }
45
-
46
- let componentNames = components;
47
- // if no components are passed, prompt the user to select components
48
- if (componentNames.length === 0) {
49
- const response = await promptUserForComponents();
50
- if ((response && response.length === 0) || !response) {
51
- consola.info("No components selected. Exiting...");
52
- process.exit(0);
53
- }
54
- componentNames = response;
55
- }
56
-
57
- // store the components that are not found
58
- let notFound: string[] = [];
59
- componentNames.forEach((c) => {
60
- if (!findComponent(c)) {
61
- notFound.push(c);
62
- }
63
- });
64
- if (notFound.length > 0) {
65
- consola.error(`The following components were not found: ${kleur.bgRed(notFound.join(", "))}`);
66
- }
67
-
68
- // store the components that are found
69
- let found: Component[] = [];
70
- componentNames.forEach((c) => {
71
- if (findComponent(c)) {
72
- found.push(findComponent(c)!);
73
- }
74
- });
75
- // check if the found components depends on other components and add them to the list
76
- for (let i = 0; i < found.length; i++) {
77
- const component = found[i];
78
- if (component.components) {
79
- for (let j = 0; j < component.components.length; j++) {
80
- const comp = component.components[j];
81
- if (!found.find((c) => c.value === comp)) {
82
- found.push(findComponent(comp)!);
83
- }
84
- }
85
- }
86
- }
87
-
88
- // add the components & files associated with them
89
- for (let i = 0; i < found.length; i++) {
90
- const component = found[i];
91
- loop2: for (let k = 0; k < component.files.length; k++) {
92
- const file = component.files[k];
93
- let fileName = file.fileName;
94
- let dirPath = uiConfig.componentsLocation;
95
- let filePath = path.join(currentDirectory, dirPath, fileName);
96
- if (!uiConfig.useDefaultFilename) {
97
- const res = await prompts({
98
- type: "text",
99
- name: "value",
100
- message: `Where should we add the file`,
101
- initial: dirPath,
102
- onRender(kleur) {
103
- //@ts-ignore
104
- this.msg =
105
- kleur.bgCyan(" Location ") +
106
- ` Where should we add the file ${kleur.cyan(`${fileName}`)} `;
107
- },
108
- });
109
- if (res.value) {
110
- dirPath = res.value;
111
- filePath = path.join(currentDirectory, res.value, fileName);
112
- }
113
- }
114
- // Check if the file exists
115
- const exists = await fileExists(filePath);
116
- // if it exists & the force option was not passed, ask the user to confirm overwriting the file
117
- if (exists && !uiConfig.force) {
118
- const res = await prompts({
119
- type: "confirm",
120
- name: "value",
121
- message: `The file that we are trying to add ${kleur.bold(
122
- fileName
123
- )} to is already taken. Overwrite?`,
124
- initial: false,
125
- });
126
- if (!res.value) {
127
- consola.info(`We will not overwrite the file for ${kleur.cyan(fileName)}`);
128
- continue loop2;
129
- }
130
- }
131
- await writeFile(filePath, file.fileContent);
132
-
133
- // @not-scalable
134
- if (component.value === "vue-sonner") {
135
- // Update the nuxt config
136
- cfg.defaultExport.imports ||= {};
137
- cfg.defaultExport.build ||= {};
138
- cfg.defaultExport.imports.imports ||= [];
139
- cfg.defaultExport.build.transpile ||= [];
140
- const sonnerExists = cfg.defaultExport.imports.imports.find(
141
- (i: any) => i.from === "vue-sonner" && i.name === "toast"
142
- );
143
- if (!sonnerExists) {
144
- cfg.defaultExport.imports.imports.push({
145
- from: "vue-sonner",
146
- name: "toast",
147
- as: "useSonner",
148
- });
149
- }
150
- const transpileExists = cfg.defaultExport.build.transpile.find((i: any) => "vue-sonner");
151
- if (!transpileExists) {
152
- cfg.defaultExport.build.transpile.push("vue-sonner");
153
- }
154
- }
155
- // @not-scalable
156
- if (component.value === "datatable") {
157
- cfg.defaultExport.app ||= {};
158
- cfg.defaultExport.app.head ||= {};
159
- cfg.defaultExport.app.head.script ||= [];
160
- const scriptOneExists = cfg.defaultExport.app.head.script.find(
161
- (i: any) =>
162
- i.src === "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"
163
- );
164
- if (!scriptOneExists) {
165
- cfg.defaultExport.app.head.script.push({
166
- src: "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js",
167
- defer: true,
168
- });
169
- }
170
- const scriptTwoExists = cfg.defaultExport.app.head.script.find(
171
- (i: any) =>
172
- i.src === "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.min.js"
173
- );
174
- if (!scriptTwoExists) {
175
- cfg.defaultExport.app.head.script.push({
176
- src: "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.min.js",
177
- defer: true,
178
- });
179
- }
180
- }
181
-
182
- // add utils attached to the component
183
- loop3: for (let j = 0; j < component.utils.length; j++) {
184
- const util = component.utils[j];
185
- const filePath = path.join(currentDirectory, uiConfig.utilsLocation, util.fileName);
186
- // Check if the file exists
187
- const exists = await fileExists(filePath);
188
- if (exists && !uiConfig.force) {
189
- const res = await prompts({
190
- type: "confirm",
191
- name: "value",
192
- message: `The utils file that we are trying to add ${kleur.bold(
193
- util.fileName
194
- )} already exists. Overwrite?`,
195
- initial: true,
196
- });
197
- if (!res.value) {
198
- consola.info(`We will not overwrite the file for ${kleur.cyan(util.fileName)}`);
199
- continue loop3;
200
- }
201
- }
202
- await writeFile(filePath, util.fileContent);
203
- }
204
- // add composables attached to the component
205
- loop4: for (let j = 0; j < component.composables.length; j++) {
206
- const composable = component.composables[j];
207
- const filePath = path.join(
208
- currentDirectory,
209
- uiConfig.composablesLocation,
210
- composable.fileName
211
- );
212
- // Check if the file exists
213
- const exists = await fileExists(filePath);
214
- if (exists && !uiConfig.force) {
215
- const res = await prompts({
216
- type: "confirm",
217
- name: "value",
218
- message: `The composables file that we are trying to add ${kleur.bold(
219
- composable.fileName
220
- )} already exists. Overwrite?`,
221
- initial: true,
222
- });
223
- if (!res.value) {
224
- consola.info(`We will not overwrite the file for ${kleur.cyan(composable.fileName)}`);
225
- continue loop4;
226
- }
227
- }
228
- await writeFile(filePath, composable.fileContent);
229
- }
230
- // add plugins attached to the component
231
- loop5: for (let j = 0; j < component.plugins.length; j++) {
232
- const plugin = component.plugins[j];
233
- const filePath = path.join(currentDirectory, plugin.dirPath, plugin.fileName);
234
- // Check if the file exists
235
- const exists = await fileExists(filePath);
236
- if (exists && !uiConfig.force) {
237
- const res = await prompts({
238
- type: "confirm",
239
- name: "value",
240
- message: `The plugins file that we are trying to add ${kleur.bold(
241
- plugin.fileName
242
- )} already exists. Overwrite?`,
243
- initial: true,
244
- });
245
- if (!res.value) {
246
- consola.info(`We will not overwrite the file for ${kleur.cyan(plugin.fileName)}`);
247
- continue loop5;
248
- }
249
- }
250
- await writeFile(filePath, plugin.fileContent);
251
- }
252
- }
253
- }
254
- // Add modules to nuxt config
255
- addModuleToConfig(cfg.nuxtConfig, _.uniq(found.map((c) => c.nuxtModules).flat()));
256
- // Write the changes to the nuxt config
257
- await updateConfig(cfg.nuxtConfig, "nuxt.config.ts");
258
- const foundDeps = _.uniq(found.map((c) => c.deps).flat());
259
- const foundDevDeps = _.uniq(found.map((c) => c.devDeps).flat());
260
- const { confirmInstall } = await prompts({
261
- type: "confirm",
262
- name: "confirmInstall",
263
- message: `Do you want to install the following packages: ${kleur.cyan(
264
- foundDeps.join(", ")
265
- )} ${kleur.cyan(foundDevDeps.join(", "))}`,
266
- initial: true,
267
- });
268
- if (confirmInstall) {
269
- await installPackages(uiConfig.packageManager, foundDeps, foundDevDeps);
270
- }
271
-
272
- printFancyBoxMessage(
273
- "All Done!",
274
- { title: "Components Added" },
275
- `Run the ${kleur.cyan("ui-thing@latest --help")} command to learn more.\n`
276
- );
277
- const combinedInstructions = found.map((c) => c.instructions).flat();
278
- // remove undefined from the array
279
- _.remove(combinedInstructions, (i) => !i);
280
- if (combinedInstructions.length > 0) {
281
- console.log(kleur.bgCyan(" Instructions "));
282
- combinedInstructions.forEach((i) => {
283
- console.log(`${kleur.cyan("-")} ${i}`);
284
- });
285
- }
286
- });
1
+ import path from "node:path";
2
+ import { Command } from "commander";
3
+ import { consola } from "consola";
4
+ import kleur from "kleur";
5
+ import _ from "lodash";
6
+ import prompts from "prompts";
7
+
8
+ import allComponents from "../comps";
9
+ import { Component } from "../types";
10
+ import { compareUIConfig } from "../utils/compareUIConfig";
11
+ import { addModuleToConfig, getNuxtConfig, getUIConfig, updateConfig } from "../utils/config";
12
+ import { fileExists } from "../utils/fileExists";
13
+ import { installPackages } from "../utils/installPackages";
14
+ import { printFancyBoxMessage } from "../utils/printFancyBoxMessage";
15
+ import { promptUserForComponents } from "../utils/promptForComponents";
16
+ import { writeFile } from "../utils/writeFile";
17
+
18
+ const currentDirectory = process.cwd();
19
+
20
+ const findComponent = (name: string) => {
21
+ return allComponents.find((c) => c.value.toLowerCase() === name.toLowerCase());
22
+ };
23
+
24
+ /**
25
+ * Adds a component to your project
26
+ */
27
+ export const add = new Command()
28
+ .name("add")
29
+ .command("add")
30
+ .description("Add a list of components to your project.")
31
+ .argument("[componentNames...]", "Components that you want to add.")
32
+ .action(async (components: Array<string>) => {
33
+ // Get nuxt config
34
+ const cfg = await getNuxtConfig();
35
+ // Get ui config
36
+ let uiConfig = await getUIConfig();
37
+ let uiConfigIsCorrect = await compareUIConfig();
38
+ if (!uiConfigIsCorrect) {
39
+ uiConfig = await getUIConfig({ force: true });
40
+ }
41
+ if (_.isEmpty(uiConfig)) {
42
+ consola.info("Config file not set. Exiting...");
43
+ process.exit(0);
44
+ }
45
+
46
+ let componentNames = components;
47
+ // if no components are passed, prompt the user to select components
48
+ if (componentNames.length === 0) {
49
+ const response = await promptUserForComponents();
50
+ if ((response && response.length === 0) || !response) {
51
+ consola.info("No components selected. Exiting...");
52
+ process.exit(0);
53
+ }
54
+ componentNames = response;
55
+ }
56
+
57
+ // store the components that are not found
58
+ let notFound: string[] = [];
59
+ componentNames.forEach((c) => {
60
+ if (!findComponent(c)) {
61
+ notFound.push(c);
62
+ }
63
+ });
64
+ if (notFound.length > 0) {
65
+ consola.error(`The following components were not found: ${kleur.bgRed(notFound.join(", "))}`);
66
+ }
67
+
68
+ // store the components that are found
69
+ let found: Component[] = [];
70
+ componentNames.forEach((c) => {
71
+ if (findComponent(c)) {
72
+ found.push(findComponent(c)!);
73
+ }
74
+ });
75
+ // check if the found components depends on other components and add them to the list
76
+ for (let i = 0; i < found.length; i++) {
77
+ const component = found[i];
78
+ if (component.components) {
79
+ for (let j = 0; j < component.components.length; j++) {
80
+ const comp = component.components[j];
81
+ if (!found.find((c) => c.value === comp)) {
82
+ found.push(findComponent(comp)!);
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ // add the components & files associated with them
89
+ for (let i = 0; i < found.length; i++) {
90
+ const component = found[i];
91
+ loop2: for (let k = 0; k < component.files.length; k++) {
92
+ const file = component.files[k];
93
+ let fileName = file.fileName;
94
+ let dirPath = uiConfig.componentsLocation;
95
+ let filePath = path.join(currentDirectory, dirPath, fileName);
96
+ if (!uiConfig.useDefaultFilename) {
97
+ const res = await prompts({
98
+ type: "text",
99
+ name: "value",
100
+ message: `Where should we add the file`,
101
+ initial: dirPath,
102
+ onRender(kleur) {
103
+ //@ts-ignore
104
+ this.msg =
105
+ kleur.bgCyan(" Location ") +
106
+ ` Where should we add the file ${kleur.cyan(`${fileName}`)} `;
107
+ },
108
+ });
109
+ if (res.value) {
110
+ dirPath = res.value;
111
+ filePath = path.join(currentDirectory, res.value, fileName);
112
+ }
113
+ }
114
+ // Check if the file exists
115
+ const exists = await fileExists(filePath);
116
+ // if it exists & the force option was not passed, ask the user to confirm overwriting the file
117
+ if (exists && !uiConfig.force) {
118
+ const res = await prompts({
119
+ type: "confirm",
120
+ name: "value",
121
+ message: `The file that we are trying to add ${kleur.bold(
122
+ fileName
123
+ )} to is already taken. Overwrite?`,
124
+ initial: false,
125
+ });
126
+ if (!res.value) {
127
+ consola.info(`We will not overwrite the file for ${kleur.cyan(fileName)}`);
128
+ continue loop2;
129
+ }
130
+ }
131
+ await writeFile(filePath, file.fileContent);
132
+
133
+ // @not-scalable
134
+ if (component.value === "vue-sonner") {
135
+ // Update the nuxt config
136
+ cfg.defaultExport.imports ||= {};
137
+ cfg.defaultExport.build ||= {};
138
+ cfg.defaultExport.imports.imports ||= [];
139
+ cfg.defaultExport.build.transpile ||= [];
140
+ const sonnerExists = cfg.defaultExport.imports.imports.find(
141
+ (i: any) => i.from === "vue-sonner" && i.name === "toast"
142
+ );
143
+ if (!sonnerExists) {
144
+ cfg.defaultExport.imports.imports.push({
145
+ from: "vue-sonner",
146
+ name: "toast",
147
+ as: "useSonner",
148
+ });
149
+ }
150
+ const transpileExists = cfg.defaultExport.build.transpile.find((i: any) => "vue-sonner");
151
+ if (!transpileExists) {
152
+ cfg.defaultExport.build.transpile.push("vue-sonner");
153
+ }
154
+ }
155
+ // @not-scalable
156
+ if (component.value === "datatable") {
157
+ cfg.defaultExport.app ||= {};
158
+ cfg.defaultExport.app.head ||= {};
159
+ cfg.defaultExport.app.head.script ||= [];
160
+ const scriptOneExists = cfg.defaultExport.app.head.script.find(
161
+ (i: any) =>
162
+ i.src === "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"
163
+ );
164
+ if (!scriptOneExists) {
165
+ cfg.defaultExport.app.head.script.push({
166
+ src: "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js",
167
+ defer: true,
168
+ });
169
+ }
170
+ const scriptTwoExists = cfg.defaultExport.app.head.script.find(
171
+ (i: any) =>
172
+ i.src === "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.min.js"
173
+ );
174
+ if (!scriptTwoExists) {
175
+ cfg.defaultExport.app.head.script.push({
176
+ src: "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.min.js",
177
+ defer: true,
178
+ });
179
+ }
180
+ }
181
+
182
+ // add utils attached to the component
183
+ loop3: for (let j = 0; j < component.utils.length; j++) {
184
+ const util = component.utils[j];
185
+ const filePath = path.join(currentDirectory, uiConfig.utilsLocation, util.fileName);
186
+ // Check if the file exists
187
+ const exists = await fileExists(filePath);
188
+ if (exists && !uiConfig.force) {
189
+ const res = await prompts({
190
+ type: "confirm",
191
+ name: "value",
192
+ message: `The utils file that we are trying to add ${kleur.bold(
193
+ util.fileName
194
+ )} already exists. Overwrite?`,
195
+ initial: true,
196
+ });
197
+ if (!res.value) {
198
+ consola.info(`We will not overwrite the file for ${kleur.cyan(util.fileName)}`);
199
+ continue loop3;
200
+ }
201
+ }
202
+ await writeFile(filePath, util.fileContent);
203
+ }
204
+ // add composables attached to the component
205
+ loop4: for (let j = 0; j < component.composables.length; j++) {
206
+ const composable = component.composables[j];
207
+ const filePath = path.join(
208
+ currentDirectory,
209
+ uiConfig.composablesLocation,
210
+ composable.fileName
211
+ );
212
+ // Check if the file exists
213
+ const exists = await fileExists(filePath);
214
+ if (exists && !uiConfig.force) {
215
+ const res = await prompts({
216
+ type: "confirm",
217
+ name: "value",
218
+ message: `The composables file that we are trying to add ${kleur.bold(
219
+ composable.fileName
220
+ )} already exists. Overwrite?`,
221
+ initial: true,
222
+ });
223
+ if (!res.value) {
224
+ consola.info(`We will not overwrite the file for ${kleur.cyan(composable.fileName)}`);
225
+ continue loop4;
226
+ }
227
+ }
228
+ await writeFile(filePath, composable.fileContent);
229
+ }
230
+ // add plugins attached to the component
231
+ loop5: for (let j = 0; j < component.plugins.length; j++) {
232
+ const plugin = component.plugins[j];
233
+ const filePath = path.join(currentDirectory, plugin.dirPath, plugin.fileName);
234
+ // Check if the file exists
235
+ const exists = await fileExists(filePath);
236
+ if (exists && !uiConfig.force) {
237
+ const res = await prompts({
238
+ type: "confirm",
239
+ name: "value",
240
+ message: `The plugins file that we are trying to add ${kleur.bold(
241
+ plugin.fileName
242
+ )} already exists. Overwrite?`,
243
+ initial: true,
244
+ });
245
+ if (!res.value) {
246
+ consola.info(`We will not overwrite the file for ${kleur.cyan(plugin.fileName)}`);
247
+ continue loop5;
248
+ }
249
+ }
250
+ await writeFile(filePath, plugin.fileContent);
251
+ }
252
+ }
253
+ }
254
+ // Add modules to nuxt config
255
+ addModuleToConfig(cfg.nuxtConfig, _.uniq(found.map((c) => c.nuxtModules).flat()));
256
+ // Write the changes to the nuxt config
257
+ await updateConfig(cfg.nuxtConfig, "nuxt.config.ts");
258
+ const foundDeps = _.uniq(found.map((c) => c.deps).flat());
259
+ const foundDevDeps = _.uniq(found.map((c) => c.devDeps).flat());
260
+ const { confirmInstall } = await prompts({
261
+ type: "confirm",
262
+ name: "confirmInstall",
263
+ message: `Do you want to install the following packages: ${kleur.cyan(
264
+ foundDeps.join(", ")
265
+ )} ${kleur.cyan(foundDevDeps.join(", "))}`,
266
+ initial: true,
267
+ });
268
+ if (confirmInstall) {
269
+ await installPackages(uiConfig.packageManager, foundDeps, foundDevDeps);
270
+ }
271
+
272
+ printFancyBoxMessage(
273
+ "All Done!",
274
+ { title: "Components Added" },
275
+ `Run the ${kleur.cyan("ui-thing@latest --help")} command to learn more.\n`
276
+ );
277
+ const combinedInstructions = found.map((c) => c.instructions).flat();
278
+ // remove undefined from the array
279
+ _.remove(combinedInstructions, (i) => !i);
280
+ if (combinedInstructions.length > 0) {
281
+ console.log(kleur.bgCyan(" Instructions "));
282
+ combinedInstructions.forEach((i) => {
283
+ console.log(`${kleur.cyan("-")} ${i}`);
284
+ });
285
+ }
286
+ });