nuxt-capacitor 0.1.3 → 0.1.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.
package/README.md CHANGED
@@ -121,19 +121,37 @@ export default defineNuxtConfig({
121
121
 
122
122
  ## Config file
123
123
 
124
- On first run, if no `capacitor.config.ts` exists in your project root, the module creates one for you:
124
+ The module uses `capacitor.config.json` by default (preferred, since it requires no transpiling or build step).
125
+
126
+ On first run, if no config file exists in your project root, the module creates one for you:
127
+ ```json
128
+ {
129
+ "appId": "com.example.app",
130
+ "appName": "Nuxt Capacitor",
131
+ "webDir": "<rootDir>/.output/public"
132
+ }
133
+ ```
125
134
 
135
+ This file is automatically kept in sync with your `nuxt.config.ts` capacitor settings on each build/run.
136
+
137
+ If you prefer TypeScript, set `output: 'ts'` in your module config:
126
138
  ```ts
127
- import {defineCapacitorConfig} from './.nuxt/capacitor.mjs';
139
+ export default defineNuxtConfig({
140
+ capacitor: {
141
+ output: 'ts',
142
+ },
143
+ })
144
+ ```
145
+
146
+ This will generate a `capacitor.config.ts` instead:
147
+ ```ts
148
+ import { defineCapacitorConfig } from './.nuxt/capacitor.mjs';
128
149
 
129
150
  export default defineCapacitorConfig({
130
- // Add your overrides here, or configure via nuxt.config.ts > capacitor: {}
151
+ // Add your overrides here, or configure via nuxt.config.ts > capacitor: {}
131
152
  });
132
153
  ```
133
-
134
- This file is type-safe and merges your overrides over the defaults set in `nuxt.config.ts`.
135
-
136
- ---
154
+ >NOTE: `output: 'ts'` might not work on other targets than `ios` and `android`.
137
155
 
138
156
  ## Setting Up Mobile Platforms
139
157
 
package/modules/config.ts CHANGED
@@ -1,16 +1,23 @@
1
- import { createResolver, defineNuxtModule, addTemplate, addTypeTemplate } from '@nuxt/kit'
1
+ import { createResolver, defineNuxtModule } from '@nuxt/kit'
2
2
  import { readPackageJSON, type PackageJson } from 'pkg-types'
3
3
  import { packageIsInstalled } from '../utils/meta'
4
4
  import type { CapacitorConfig } from '@capacitor/cli'
5
5
  import defu from 'defu'
6
6
  import { withLeadingSlash, withoutTrailingSlash } from 'ufo'
7
- import { existsSync, writeFileSync } from 'node:fs'
7
+ import { existsSync } from 'node:fs'
8
8
  import { join } from 'node:path'
9
9
  import { consola } from 'consola'
10
10
  import { execSync } from 'node:child_process'
11
+ import {
12
+ addCodeTemplates,
13
+ createCapacitorTsConfig,
14
+ hasAnyCapacitorConfigFile,
15
+ upsertCapacitorJsonConfig,
16
+ } from '../utils/capacitor'
11
17
 
12
18
  interface NuxtCapacitorOptions {
13
19
  autoSync?: boolean
20
+ output?: 'json' | 'ts'
14
21
  config?: CapacitorConfig
15
22
  }
16
23
 
@@ -29,6 +36,7 @@ export default defineNuxtModule<NuxtCapacitorOptions>({
29
36
  },
30
37
  defaults: {
31
38
  autoSync: true,
39
+ output: 'json',
32
40
  config: {
33
41
  appId: 'com.example.app',
34
42
  appName: 'Nuxt Capacitor',
@@ -82,58 +90,20 @@ export default defineNuxtModule<NuxtCapacitorOptions>({
82
90
  webDir: withoutTrailingSlash(nuxt.options.rootDir) + withLeadingSlash(distPath),
83
91
  })
84
92
 
85
- const userConfigPath = join(rootDir, 'capacitor.config.ts')
86
- const userConfigExists = existsSync(userConfigPath)
87
-
88
- if (!userConfigExists && thisModuleDir !== rootDir) {
89
- const defaultUserConfig = [
90
- `import { defineCapacitorConfig } from './.nuxt/capacitor.mjs';`,
91
- ``,
92
- `export default defineCapacitorConfig({`,
93
- ` // Add your overrides here, or configure via nuxt.config.ts > capacitor: {}`,
94
- `});`,
95
- ].join('\n')
96
-
97
- writeFileSync(userConfigPath, defaultUserConfig, 'utf-8')
98
- logger.info(`Created default capacitor.config.ts in project root.`)
93
+ const capacitorConfigFile = hasAnyCapacitorConfigFile(nuxt)
94
+ if (thisModuleDir !== rootDir) {
95
+ if (nuxt.options.capacitor.output === 'json') {
96
+ await upsertCapacitorJsonConfig(nuxt)
97
+ }
98
+ else if (!existsSync(join(rootDir, 'capacitor.config.ts')) && nuxt.options.capacitor.output === 'ts' && capacitorConfigFile === 'capacitor.config.ts') {
99
+ await createCapacitorTsConfig(nuxt)
100
+ }
101
+ else {
102
+ logger.warn(`Unhandled capacitor config file "${capacitorConfigFile}"`)
103
+ }
99
104
  }
100
105
 
101
- addTemplate({
102
- filename: 'capacitor.mjs',
103
- write: true,
104
- options: {
105
- config: nuxt.options?.capacitor?.config ?? {},
106
- },
107
- getContents: ({ options }) => {
108
- const finalConfig = JSON.stringify(options.config, null, 2)
109
- return [
110
- `/* Auto generated by ${name}. Do not edit. */`,
111
- `const _defaultConfig = ${finalConfig};`,
112
- ``,
113
- `/** @param {import('@capacitor/cli').CapacitorConfig} config */`,
114
- `export const defineCapacitorConfig = (config) => Object.assign({}, _defaultConfig, config);`,
115
- ].join('\n')
116
- },
117
- })
118
-
119
- addTypeTemplate({
120
- filename: 'capacitor.d.ts',
121
- write: true,
122
- getContents: () => {
123
- return [
124
- `import type { CapacitorConfig } from '@capacitor/cli';`,
125
- ``,
126
- `/**`,
127
- ` * Define your Capacitor config with full type safety.`,
128
- ` * Values are deeply merged over the defaults provided by ${name}.`,
129
- ` *`,
130
- ` * @param config - Partial CapacitorConfig overrides`,
131
- ` * @returns Fully merged CapacitorConfig`,
132
- ` */`,
133
- `export declare function defineCapacitorConfig(config: CapacitorConfig): CapacitorConfig;`,
134
- ].join('\n')
135
- },
136
- })
106
+ addCodeTemplates(nuxt)
137
107
 
138
108
  // Auto sync
139
109
  if (nuxt.options.capacitor.autoSync) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-capacitor",
3
3
  "description": "A layer for building nuxt apps with capacitor",
4
- "version": "0.1.3",
4
+ "version": "0.1.4",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
7
7
  "repository": {
@@ -0,0 +1,88 @@
1
+ import { addTemplate, addTypeTemplate } from '@nuxt/kit'
2
+ import type { Nuxt } from '@nuxt/schema'
3
+ import type { CapacitorConfig } from '@capacitor/cli'
4
+ import { join } from 'node:path'
5
+ import { existsSync, writeFileSync, readFileSync } from 'node:fs'
6
+ import { defu } from 'defu'
7
+
8
+ const name = 'nuxt-capacitor'
9
+
10
+ const capacitorConfigFiles = ['capacitor.config.json', 'capacitor.config.ts', 'capacitor.config.mjs'] as const
11
+
12
+ export const hasAnyCapacitorConfigFile = (nuxt: Nuxt): typeof capacitorConfigFiles[number] | false => {
13
+ for (const file of capacitorConfigFiles) {
14
+ if (existsSync(join(nuxt.options.rootDir, file))) {
15
+ return file
16
+ }
17
+ }
18
+ return false
19
+ }
20
+ export const upsertCapacitorJsonConfig = async (nuxt: Nuxt) => {
21
+ // @ts-expect-error Not typed...
22
+ const config = (nuxt.options?.capacitor?.config || {}) as CapacitorConfig
23
+ const configFile = join(nuxt.options.rootDir, 'capacitor.config.json')
24
+ if (!existsSync(configFile)) {
25
+ writeFileSync(configFile, JSON.stringify(config, null, 2), 'utf-8')
26
+ return
27
+ }
28
+
29
+ const { server, ...oldContent } = JSON.parse(readFileSync(configFile, 'utf-8'))
30
+
31
+ const merged = defu(oldContent, config)
32
+
33
+ writeFileSync(configFile, JSON.stringify(merged, null, 2), 'utf-8')
34
+ }
35
+
36
+ export const createCapacitorTsConfig = async (nuxt: Nuxt) => {
37
+ const userConfigPath = join(nuxt.options.rootDir, 'capacitor.config.ts')
38
+
39
+ const defaultUserConfig = [
40
+ `import { defineCapacitorConfig } from './.nuxt/capacitor.mjs';`,
41
+ ``,
42
+ `export default defineCapacitorConfig({`,
43
+ ` // Add your overrides here, or configure via nuxt.config.ts > capacitor: {}`,
44
+ `});`,
45
+ ].join('\n')
46
+
47
+ writeFileSync(userConfigPath, defaultUserConfig, 'utf-8')
48
+ }
49
+
50
+ export const addCodeTemplates = (nuxt: Nuxt) => {
51
+ addTemplate({
52
+ filename: 'capacitor.mjs',
53
+ write: true,
54
+ options: {
55
+ // @ts-expect-error Not typed...
56
+ config: nuxt.options?.capacitor?.config ?? {},
57
+ },
58
+ getContents: ({ options }) => {
59
+ const finalConfig = JSON.stringify(options.config, null, 2)
60
+ return [
61
+ `/* Auto generated by ${name}. Do not edit. */`,
62
+ `const _defaultConfig = ${finalConfig};`,
63
+ ``,
64
+ `/** @param {import('@capacitor/cli').CapacitorConfig} config */`,
65
+ `export const defineCapacitorConfig = (config) => Object.assign({}, _defaultConfig, config);`,
66
+ ].join('\n')
67
+ },
68
+ })
69
+
70
+ addTypeTemplate({
71
+ filename: 'capacitor.d.ts',
72
+ write: true,
73
+ getContents: () => {
74
+ return [
75
+ `import type { CapacitorConfig } from '@capacitor/cli';`,
76
+ ``,
77
+ `/**`,
78
+ ` * Define your Capacitor config with full type safety.`,
79
+ ` * Values are deeply merged over the defaults provided by ${name}.`,
80
+ ` *`,
81
+ ` * @param config - Partial CapacitorConfig overrides`,
82
+ ` * @returns Fully merged CapacitorConfig`,
83
+ ` */`,
84
+ `export declare function defineCapacitorConfig(config: CapacitorConfig): CapacitorConfig;`,
85
+ ].join('\n')
86
+ },
87
+ })
88
+ }