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