Browse Source

fix(tauri.js) do not require a package.json (#855)

Lucas Fernandes Nogueira 5 years ago
parent
commit
45d3de6d97

+ 5 - 0
.changes/tauri-package-json.md

@@ -0,0 +1,5 @@
+---
+"tauri.js": patch
+---
+
+Do not require a `package.json` file on the app root.

+ 26 - 22
cli/tauri.js/src/api/dependency-manager/npm-packages.ts

@@ -1,7 +1,9 @@
 import { ManagementType, Result } from './types'
 import { getNpmLatestVersion, getNpmPackageVersion, installNpmPackage, updateNpmPackage, semverLt } from './util'
 import logger from '../../helpers/logger'
+import { resolve } from '../../helpers/app-paths'
 import inquirer from 'inquirer'
+import { existsSync } from 'fs'
 
 const log = logger('dependency:npm-packages')
 
@@ -11,31 +13,33 @@ async function manageDependencies(managementType: ManagementType): Promise<Resul
   const installedDeps = []
   const updatedDeps = []
 
-  for (const dependency of dependencies) {
-    const currentVersion = await getNpmPackageVersion(dependency)
-    if (currentVersion === null) {
-      log(`Installing ${dependency}...`)
-      installNpmPackage(dependency)
-      installedDeps.push(dependency)
-    } else if (managementType === ManagementType.Update) {
-      const latestVersion = getNpmLatestVersion(dependency)
-      if (semverLt(currentVersion, latestVersion)) {
-        const inquired = await inquirer.prompt([{
-          type: 'confirm',
-          name: 'answer',
-          message: `[NPM]: "${dependency}" latest version is ${latestVersion}. Do you want to update?`,
-          default: false
-        }])
-        if (inquired.answer) {
-          log(`Updating ${dependency}...`)
-          updateNpmPackage(dependency)
-          updatedDeps.push(dependency)
+  if (existsSync(resolve.app('package.json'))) {
+    for (const dependency of dependencies) {
+      const currentVersion = await getNpmPackageVersion(dependency)
+      if (currentVersion === null) {
+        log(`Installing ${dependency}...`)
+        installNpmPackage(dependency)
+        installedDeps.push(dependency)
+      } else if (managementType === ManagementType.Update) {
+        const latestVersion = getNpmLatestVersion(dependency)
+        if (semverLt(currentVersion, latestVersion)) {
+          const inquired = await inquirer.prompt([{
+            type: 'confirm',
+            name: 'answer',
+            message: `[NPM]: "${dependency}" latest version is ${latestVersion}. Do you want to update?`,
+            default: false
+          }])
+          if (inquired.answer) {
+            log(`Updating ${dependency}...`)
+            updateNpmPackage(dependency)
+            updatedDeps.push(dependency)
+          }
+        } else {
+          log(`"${dependency}" is up to date`)
         }
       } else {
-        log(`"${dependency}" is up to date`)
+        log(`"${dependency}" is already installed`)
       }
-    } else {
-      log(`"${dependency}" is already installed`)
     }
   }
 

+ 2 - 6
cli/tauri.js/src/helpers/tauri-config.ts

@@ -12,10 +12,6 @@ const error = logger('ERROR:', chalk.red)
 const getTauriConfig = (cfg: Partial<TauriConfig>): TauriConfig => {
   const pkgPath = appPaths.resolve.app('package.json')
   const tauriConfPath = appPaths.resolve.tauri('tauri.conf.json')
-  if (!existsSync(pkgPath)) {
-    error("Could not find a package.json in your app's directory.")
-    process.exit(1)
-  }
   if (!existsSync(tauriConfPath)) {
     error(
       "Could not find a tauri config (tauri.conf.json) in your app's directory."
@@ -23,7 +19,7 @@ const getTauriConfig = (cfg: Partial<TauriConfig>): TauriConfig => {
     process.exit(1)
   }
   const tauriConf = JSON.parse(readFileSync(tauriConfPath).toString()) as TauriConfig
-  const pkg = nonWebpackRequire(pkgPath) as { productName: string }
+  const pkg = existsSync(pkgPath) ? nonWebpackRequire(pkgPath) as { productName: string } : null
 
   const config = merge(
     {
@@ -49,7 +45,7 @@ const getTauriConfig = (cfg: Partial<TauriConfig>): TauriConfig => {
           all: false
         },
         window: {
-          title: pkg.productName
+          title: pkg?.productName ?? 'Tauri App'
         },
         security: {
           csp: "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"