@capgo/cli 4.12.5 → 4.12.7
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/CHANGELOG.md +9 -0
- package/dist/index.js +868 -795
- package/package.json +1 -1
- package/src/init.ts +85 -31
- package/src/utils.ts +62 -30
package/package.json
CHANGED
package/src/init.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
2
3
|
import type { ExecSyncOptions } from 'node:child_process'
|
|
3
4
|
import { execSync, spawnSync } from 'node:child_process'
|
|
4
5
|
import process from 'node:process'
|
|
@@ -192,43 +193,96 @@ async function step4(orgId: string, snag: LogSnag, apikey: string, appId: string
|
|
|
192
193
|
async function step5(orgId: string, snag: LogSnag, apikey: string, appId: string) {
|
|
193
194
|
const doAddCode = await p.confirm({ message: `Automatic Add "${codeInject}" code and import in ${appId}?` })
|
|
194
195
|
await cancelCommand(doAddCode, orgId, snag)
|
|
196
|
+
|
|
195
197
|
if (doAddCode) {
|
|
196
198
|
const s = p.spinner()
|
|
197
199
|
s.start(`Adding @capacitor-updater to your main file`)
|
|
198
|
-
const projectType = await findProjectType()
|
|
199
|
-
let mainFilePath
|
|
200
|
-
if (projectType === 'unknown')
|
|
201
|
-
mainFilePath = await findMainFile()
|
|
202
|
-
else
|
|
203
|
-
mainFilePath = await findMainFileForProjectType(projectType)
|
|
204
|
-
|
|
205
|
-
if (!mainFilePath) {
|
|
206
|
-
s.stop('Error')
|
|
207
|
-
p.log.warn('Cannot find main file, You need to add @capgo/capacitor-updater manually')
|
|
208
|
-
p.outro(`Bye 👋`)
|
|
209
|
-
process.exit()
|
|
210
|
-
}
|
|
211
|
-
// open main file and inject codeInject
|
|
212
|
-
const mainFile = readFileSync(mainFilePath)
|
|
213
|
-
// find the last import line in the file and inject codeInject after it
|
|
214
|
-
const mainFileContent = mainFile.toString()
|
|
215
|
-
const matches = mainFileContent.match(regexImport)
|
|
216
|
-
const last = matches?.pop()
|
|
217
|
-
if (!last) {
|
|
218
|
-
s.stop('Error')
|
|
219
|
-
p.log.warn(`Cannot find import line in main file, use manual installation: https://capgo.app/docs/plugin/installation/`)
|
|
220
|
-
p.outro(`Bye 👋`)
|
|
221
|
-
process.exit()
|
|
222
|
-
}
|
|
223
200
|
|
|
224
|
-
|
|
225
|
-
|
|
201
|
+
const projectType = await findProjectType()
|
|
202
|
+
if (projectType === 'nuxtjs-js' || projectType === 'nuxtjs-ts') {
|
|
203
|
+
// Nuxt.js specific logic
|
|
204
|
+
const nuxtDir = path.join('plugins')
|
|
205
|
+
if (!existsSync(nuxtDir)) {
|
|
206
|
+
mkdirSync(nuxtDir, { recursive: true })
|
|
207
|
+
}
|
|
208
|
+
let nuxtFilePath
|
|
209
|
+
if (projectType === 'nuxtjs-ts') {
|
|
210
|
+
nuxtFilePath = path.join(nuxtDir, 'capacitorUpdater.client.ts')
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
nuxtFilePath = path.join(nuxtDir, 'capacitorUpdater.client.js')
|
|
214
|
+
}
|
|
215
|
+
const nuxtFileContent = `
|
|
216
|
+
import { CapacitorUpdater } from '@capgo/capacitor-updater'
|
|
217
|
+
|
|
218
|
+
export default defineNuxtPlugin(() => {
|
|
219
|
+
CapacitorUpdater.notifyAppReady()
|
|
220
|
+
})
|
|
221
|
+
`
|
|
222
|
+
if (existsSync(nuxtFilePath)) {
|
|
223
|
+
const currentContent = readFileSync(nuxtFilePath, 'utf8')
|
|
224
|
+
if (currentContent.includes('CapacitorUpdater.notifyAppReady()')) {
|
|
225
|
+
s.stop('Code already added to capacitorUpdater.client.ts file inside plugins directory ✅')
|
|
226
|
+
p.log.info('Plugins directory and capacitorUpdater.client.ts file already exist with required code')
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
writeFileSync(nuxtFilePath, nuxtFileContent, 'utf8')
|
|
230
|
+
s.stop('Code added to capacitorUpdater.client.ts file inside plugins directory ✅')
|
|
231
|
+
p.log.info('Updated capacitorUpdater.client.ts file with required code')
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
writeFileSync(nuxtFilePath, nuxtFileContent, 'utf8')
|
|
236
|
+
s.stop('Code added to capacitorUpdater.client.ts file inside plugins directory ✅')
|
|
237
|
+
p.log.info('Created plugins directory and capacitorUpdater.client.ts file')
|
|
238
|
+
}
|
|
226
239
|
}
|
|
227
240
|
else {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
241
|
+
// Handle other project types
|
|
242
|
+
let mainFilePath
|
|
243
|
+
if (projectType === 'unknown') {
|
|
244
|
+
mainFilePath = await findMainFile()
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
const isTypeScript = projectType.endsWith('-ts')
|
|
248
|
+
mainFilePath = await findMainFileForProjectType(projectType, isTypeScript)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!mainFilePath) {
|
|
252
|
+
s.stop('Error')
|
|
253
|
+
if (projectType === 'nextjs-js' || projectType === 'nextjs-ts') {
|
|
254
|
+
p.log.warn(`You might not be using app router configuration or the latest version of Next.js`)
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
p.log.warn(`Cannot find the latest version of ${projectType}, you might need to upgrade to the latest version of ${projectType}`)
|
|
258
|
+
}
|
|
259
|
+
p.outro(`Bye 👋`)
|
|
260
|
+
process.exit()
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Open main file and inject codeInject
|
|
264
|
+
const mainFile = readFileSync(mainFilePath, 'utf8')
|
|
265
|
+
const mainFileContent = mainFile.toString()
|
|
266
|
+
const matches = mainFileContent.match(regexImport)
|
|
267
|
+
const last = matches?.pop()
|
|
268
|
+
|
|
269
|
+
if (!last) {
|
|
270
|
+
s.stop('Error')
|
|
271
|
+
p.log.warn(`Cannot find import line in main file, use manual installation: https://capgo.app/docs/plugin/installation/`)
|
|
272
|
+
p.outro(`Bye 👋`)
|
|
273
|
+
process.exit()
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (mainFileContent.includes(codeInject)) {
|
|
277
|
+
s.stop(`Code already added to ${mainFilePath} ✅`)
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
const newMainFileContent = mainFileContent.replace(last, `${last}\n${importInject};\n\n${codeInject};\n`)
|
|
281
|
+
writeFileSync(mainFilePath, newMainFileContent, 'utf8')
|
|
282
|
+
s.stop(`Code added to ${mainFilePath} ✅`)
|
|
283
|
+
}
|
|
231
284
|
}
|
|
285
|
+
|
|
232
286
|
await markStep(orgId, snag, 5)
|
|
233
287
|
}
|
|
234
288
|
else {
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
|
|
2
2
|
import { homedir, platform as osPlatform } from 'node:os'
|
|
3
|
-
import { join, resolve, sep } from 'node:path'
|
|
3
|
+
import path, { join, resolve, sep } from 'node:path'
|
|
4
4
|
import process from 'node:process'
|
|
5
5
|
import type { Buffer } from 'node:buffer'
|
|
6
6
|
import { loadConfig } from '@capacitor/cli/dist/config'
|
|
@@ -317,13 +317,14 @@ async function* getFiles(dir: string): AsyncGenerator<string> {
|
|
|
317
317
|
const dirents = await readdirSync(dir, { withFileTypes: true })
|
|
318
318
|
for (const dirent of dirents) {
|
|
319
319
|
const res = resolve(dir, dirent.name)
|
|
320
|
-
if (
|
|
320
|
+
if (
|
|
321
|
+
dirent.isDirectory()
|
|
321
322
|
&& !dirent.name.startsWith('.')
|
|
322
323
|
&& !dirent.name.startsWith('node_modules')
|
|
323
|
-
&& !dirent.name.startsWith('dist')
|
|
324
|
+
&& !dirent.name.startsWith('dist')
|
|
325
|
+
) {
|
|
324
326
|
yield * getFiles(res)
|
|
325
327
|
}
|
|
326
|
-
|
|
327
328
|
else {
|
|
328
329
|
yield res
|
|
329
330
|
}
|
|
@@ -334,46 +335,81 @@ export async function findProjectType() {
|
|
|
334
335
|
// for nuxtjs check if nuxt.config.js exists
|
|
335
336
|
// for nextjs check if next.config.js exists
|
|
336
337
|
// for angular check if angular.json exists
|
|
337
|
-
// for sveltekit check if svelte.config.js exists
|
|
338
|
+
// for sveltekit check if svelte.config.js exists or svelte is in package.json dependancies
|
|
339
|
+
// for vue check if vue.config.js exists or vue is in package.json dependancies
|
|
340
|
+
// for react check if package.json exists and react is in dependencies
|
|
338
341
|
const pwd = process.cwd()
|
|
342
|
+
let isTypeScript = false
|
|
343
|
+
|
|
344
|
+
// Check for TypeScript configuration file
|
|
345
|
+
const tsConfigPath = resolve(pwd, 'tsconfig.json')
|
|
346
|
+
if (existsSync(tsConfigPath)) {
|
|
347
|
+
isTypeScript = true
|
|
348
|
+
}
|
|
349
|
+
|
|
339
350
|
for await (const f of getFiles(pwd)) {
|
|
340
351
|
// find number of folder in path after pwd
|
|
341
352
|
if (f.includes('angular.json')) {
|
|
342
353
|
p.log.info('Found angular project')
|
|
343
|
-
return 'angular'
|
|
354
|
+
return isTypeScript ? 'angular-ts' : 'angular-js'
|
|
344
355
|
}
|
|
345
|
-
if (f.includes('nuxt.config.js')) {
|
|
356
|
+
if (f.includes('nuxt.config.js' || f.includes('nuxt.config.ts'))) {
|
|
346
357
|
p.log.info('Found nuxtjs project')
|
|
347
|
-
return 'nuxtjs'
|
|
358
|
+
return isTypeScript ? 'nuxtjs-ts' : 'nuxtjs-js'
|
|
348
359
|
}
|
|
349
|
-
if (f.includes('next.config.js')) {
|
|
360
|
+
if (f.includes('next.config.js') || f.includes('next.config.mjs')) {
|
|
350
361
|
p.log.info('Found nextjs project')
|
|
351
|
-
return 'nextjs'
|
|
362
|
+
return isTypeScript ? 'nextjs-ts' : 'nextjs-js'
|
|
352
363
|
}
|
|
353
364
|
if (f.includes('svelte.config.js')) {
|
|
354
365
|
p.log.info('Found sveltekit project')
|
|
355
|
-
return 'sveltekit'
|
|
366
|
+
return isTypeScript ? 'sveltekit-ts' : 'sveltekit-js'
|
|
367
|
+
}
|
|
368
|
+
if (f.includes('rollup.config.js')) {
|
|
369
|
+
p.log.info('Found svelte project')
|
|
370
|
+
return isTypeScript ? 'svelte-ts' : 'svelte-js'
|
|
371
|
+
}
|
|
372
|
+
if (f.includes('vue.config.js')) {
|
|
373
|
+
p.log.info('Found vue project')
|
|
374
|
+
return isTypeScript ? 'vue-ts' : 'vue-js'
|
|
375
|
+
}
|
|
376
|
+
if (f.includes('package.json')) {
|
|
377
|
+
const packageJsonPath = path.resolve(f)
|
|
378
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'))
|
|
379
|
+
if (packageJson.dependencies) {
|
|
380
|
+
if (packageJson.dependencies.react) {
|
|
381
|
+
p.log.info('Found react project test')
|
|
382
|
+
return isTypeScript ? 'react-ts' : 'react-js'
|
|
383
|
+
}
|
|
384
|
+
if (packageJson.dependencies.vue) {
|
|
385
|
+
p.log.info('Found vue project')
|
|
386
|
+
return isTypeScript ? 'vue-ts' : 'vue-js'
|
|
387
|
+
}
|
|
388
|
+
}
|
|
356
389
|
}
|
|
357
390
|
}
|
|
391
|
+
|
|
358
392
|
return 'unknown'
|
|
359
393
|
}
|
|
360
394
|
|
|
361
|
-
export
|
|
362
|
-
if (projectType === 'angular')
|
|
363
|
-
return 'src/main.ts'
|
|
364
|
-
|
|
365
|
-
if (projectType === '
|
|
366
|
-
return 'src/
|
|
367
|
-
|
|
368
|
-
if (projectType === '
|
|
369
|
-
return '
|
|
370
|
-
|
|
371
|
-
if (projectType === '
|
|
372
|
-
return 'src/main.ts'
|
|
373
|
-
|
|
395
|
+
export function findMainFileForProjectType(projectType: string, isTypeScript: boolean): string | null {
|
|
396
|
+
if (projectType === 'angular') {
|
|
397
|
+
return isTypeScript ? 'src/main.ts' : 'src/main.js'
|
|
398
|
+
}
|
|
399
|
+
if (projectType === 'nextjs-js' || projectType === 'nextjs-ts') {
|
|
400
|
+
return isTypeScript ? 'src/app/layout.tsx' : 'src/app/layout.js'
|
|
401
|
+
}
|
|
402
|
+
if (projectType === 'svelte-js' || projectType === 'svelte-ts') {
|
|
403
|
+
return isTypeScript ? 'src/main.ts' : 'src/main.js'
|
|
404
|
+
}
|
|
405
|
+
if (projectType === 'vue-js' || projectType === 'vue-ts') {
|
|
406
|
+
return isTypeScript ? 'src/main.ts' : 'src/main.js'
|
|
407
|
+
}
|
|
408
|
+
if (projectType === 'react-js' || projectType === 'react-ts') {
|
|
409
|
+
return isTypeScript ? 'src/index.tsx' : 'src/index.js'
|
|
410
|
+
}
|
|
374
411
|
return null
|
|
375
412
|
}
|
|
376
|
-
|
|
377
413
|
// create a function to find the right command to build the project in static mode depending on the project type
|
|
378
414
|
|
|
379
415
|
export async function findBuildCommandForProjectType(projectType: string) {
|
|
@@ -948,10 +984,6 @@ export async function getRemoteChecksums(supabase: SupabaseClient<Database>, app
|
|
|
948
984
|
|| !data.version
|
|
949
985
|
|| !data.version.checksum
|
|
950
986
|
) {
|
|
951
|
-
p.log.error(`Cannot get remote checksum for channel ${channel}`)
|
|
952
|
-
p.log.error(`Error: ${error?.message}`)
|
|
953
|
-
|
|
954
|
-
program.error('')
|
|
955
987
|
return null
|
|
956
988
|
}
|
|
957
989
|
|
|
@@ -1018,7 +1050,7 @@ export async function checkChecksum(supabase: SupabaseClient<Database>, appId: s
|
|
|
1018
1050
|
s.start(`Checking bundle checksum compatibility with channel ${channel}`)
|
|
1019
1051
|
const remoteChecksum = await getRemoteChecksums(supabase, appId, channel)
|
|
1020
1052
|
|
|
1021
|
-
if (remoteChecksum === currentChecksum) {
|
|
1053
|
+
if (remoteChecksum && remoteChecksum === currentChecksum) {
|
|
1022
1054
|
// cannot upload the same bundle
|
|
1023
1055
|
p.log.error(`Cannot upload the same bundle content.\nCurrent bundle checksum matches remote bundle for channel ${channel}\nDid you builded your app before uploading ?`)
|
|
1024
1056
|
program.error('')
|