kapi-ui 0.1.3 → 0.2.0

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/dist/setup.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import prompts from 'prompts';
3
- import { installKapi, injectVitePlugin, KAPI_PACKAGE_NAME } from './utils.js';
3
+ import { installKapi, injectVitePlugin, injectNuxtVitePlugin, KAPI_PACKAGE_NAME } from './utils.js';
4
4
  async function setup() {
5
5
  console.log(`
6
6
  ██ ██ █████ ██████ ██
@@ -15,6 +15,7 @@ async function setup() {
15
15
  message: 'Setup',
16
16
  choices: [
17
17
  { title: 'Vite', value: 'vite' },
18
+ { title: 'Nuxt', value: 'nuxt' },
18
19
  ],
19
20
  });
20
21
  if (!response.setupChoice) {
@@ -38,6 +39,28 @@ Add this manually to your vite.config:
38
39
  export default defineConfig({
39
40
  plugins: [kapi()],
40
41
  })
42
+ `);
43
+ process.exit(1);
44
+ }
45
+ }
46
+ if (response.setupChoice === 'nuxt') {
47
+ try {
48
+ installKapi(process.cwd());
49
+ await injectNuxtVitePlugin(process.cwd());
50
+ console.log('done!');
51
+ }
52
+ catch (err) {
53
+ console.error('Failed to update nuxt.config automatically:', err);
54
+ console.log(`
55
+ Add this manually to your nuxt.config:
56
+
57
+ import kapi from '${KAPI_PACKAGE_NAME}/vite-plugin'
58
+
59
+ export default defineNuxtConfig({
60
+ vite: {
61
+ plugins: [kapi()],
62
+ },
63
+ })
41
64
  `);
42
65
  process.exit(1);
43
66
  }
package/dist/utils.js CHANGED
@@ -1,5 +1,5 @@
1
- import { loadFile, writeFile } from 'magicast';
2
- import { addVitePlugin } from 'magicast/helpers';
1
+ import { loadFile, writeFile, builders } from 'magicast';
2
+ import { addVitePlugin, getDefaultExportOptions } from 'magicast/helpers';
3
3
  import { existsSync, readFileSync } from 'fs';
4
4
  import path from 'path';
5
5
  import { fileURLToPath } from 'url';
@@ -34,3 +34,34 @@ export async function injectVitePlugin(cwd) {
34
34
  await writeFile(mod, configPath);
35
35
  console.log(`✔ Added kapi plugin to ${configFile}`);
36
36
  }
37
+ export async function injectNuxtVitePlugin(cwd) {
38
+ var _a;
39
+ const candidates = ['nuxt.config.ts', 'nuxt.config.js', 'nuxt.config.mjs'];
40
+ const configFile = candidates.find((f) => existsSync(path.join(cwd, f)));
41
+ if (!configFile) {
42
+ throw new Error('No nuxt.config found.');
43
+ }
44
+ const configPath = path.join(cwd, configFile);
45
+ const importSpecifier = `${KAPI_PACKAGE_NAME}/vite-plugin`;
46
+ const existingSource = readFileSync(configPath, 'utf-8');
47
+ if (existingSource.includes(importSpecifier)) {
48
+ console.log(`✔ kapi plugin already configured in ${configFile}`);
49
+ return;
50
+ }
51
+ const mod = await loadFile(configPath);
52
+ const constructor = 'kapi';
53
+ // Nuxt reads Vite plugins from `vite.plugins` in nuxt.config, not from a
54
+ // top-level `plugins` array, so magicast's addVitePlugin helper (which
55
+ // targets the latter) can't be reused here directly.
56
+ const config = getDefaultExportOptions(mod);
57
+ config.vite || (config.vite = {});
58
+ (_a = config.vite).plugins || (_a.plugins = []);
59
+ config.vite.plugins.push(builders.functionCall(constructor));
60
+ mod.imports.$prepend({
61
+ from: importSpecifier,
62
+ imported: 'default',
63
+ local: constructor,
64
+ });
65
+ await writeFile(mod, configPath);
66
+ console.log(`✔ Added kapi plugin to ${configFile}`);
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kapi-ui",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Visually prompt your UI",
5
5
  "scripts": {
6
6
  "build": "tsc",