소스 검색

feat(tauri.js/init): prompt for default values (fix #422/#162) (#472)

* feat(tauri.js/init): prompt for default values

* fix(tauri.js/init): update help wording

* feat(tauri.js) prompt for appName on init

* feat(tauri.js) add --ci option

* chore(changes) add changefile

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Noah Klayman 5 년 전
부모
커밋
ee8724b90a
4개의 변경된 파일257개의 추가작업 그리고 414개의 파일을 삭제
  1. 5 0
      .changes/tauri-init-prompt.md
  2. 121 14
      cli/tauri.js/bin/tauri-init.js
  3. 1 0
      cli/tauri.js/package.json
  4. 130 400
      cli/tauri.js/yarn.lock

+ 5 - 0
.changes/tauri-init-prompt.md

@@ -0,0 +1,5 @@
+---
+"tauri.js": minor
+---
+
+`tauri init` now prompt for default values such as window title, app name, dist dir and dev path. You can use `--ci` to skip the prompts.

+ 121 - 14
cli/tauri.js/bin/tauri-init.js

@@ -1,4 +1,16 @@
 const parseArgs = require('minimist')
+const inquirer = require('inquirer')
+const {
+  resolve
+} = require('path')
+const {
+  readFileSync,
+  writeFileSync
+} = require('fs')
+const {
+  merge
+} = require('lodash')
+const toml = require('@tauri-apps/toml')
 
 /**
  * @type {object}
@@ -17,9 +29,13 @@ const argv = parseArgs(process.argv.slice(2), {
     f: 'force',
     l: 'log',
     d: 'directory',
-    t: 'tauri-path'
+    t: 'tauri-path',
+    A: 'app-name',
+    W: 'window-title',
+    D: 'dist-dir',
+    P: 'dev-path'
   },
-  boolean: ['h', 'l']
+  boolean: ['h', 'l', 'ci']
 })
 
 if (argv.help) {
@@ -30,20 +46,111 @@ if (argv.help) {
   Usage
     $ tauri init
   Options
-    --help, -h        Displays this message
-    --force, -f       Force init to overwrite [conf|template|all]
-    --log, -l         Logging [boolean]
-    --directory, -d   Set target directory for init
-    --tauri-path, -t   Path of the Tauri project to use (relative to the cwd)
+    --help, -h           Displays this message
+    --ci                 Skip prompts
+    --force, -f          Force init to overwrite [conf|template|all]
+    --log, -l            Logging [boolean]
+    --directory, -d      Set target directory for init
+    --tauri-path, -t     Path of the Tauri project to use (relative to the cwd)
+    --app-name, -A       Name of your Tauri application
+    --window-title, -W   Window title of your Tauri application
+    --dist-dir, -D       Web assets location, relative to <project-dir>/src-tauri
+    --dev-path, -P       Url of your dev server
     `)
   process.exit(0)
 }
 
-const init = require('../dist/api/init')
+let appName = argv.A
+if (!appName) {
+  try {
+    const packageJson = JSON.parse(readFileSync(resolve(process.cwd(), 'package.json')).toString())
+    appName = packageJson.displayName || packageJson.name
+  } catch {}
+}
 
-init({
-  directory: argv.d || process.cwd(),
-  force: argv.f || null,
-  logging: argv.l || null,
-  tauriPath: argv.t || null
-})
+if (argv.ci) {
+  runInit()
+} else {
+  inquirer
+    .prompt([{
+        type: 'input',
+        name: 'appName',
+        message: 'What is your app name?',
+        default: appName
+      }, {
+        type: 'input',
+        name: 'tauri.window.title',
+        message: 'What should the window title be?',
+        default: 'Tauri App',
+        when: () => !argv.W
+      },
+      {
+        type: 'input',
+        name: 'build.distDir',
+        message: 'Where are your web assets (HTML/CSS/JS) located, relative to the "<current dir>/src-tauri" folder that will be created?',
+        default: '../dist',
+        when: () => !argv.D
+      },
+      {
+        type: 'input',
+        name: 'build.devPath',
+        message: 'What is the url of your dev server?',
+        default: 'http://localhost:4000',
+        when: () => !argv.P
+      }
+    ])
+    .then(answers => {
+      runInit(answers)
+    })
+    .catch(error => {
+      if (error.isTtyError) {
+        // Prompt couldn't be rendered in the current environment
+        console.log(
+          'It appears your terminal does not support interactive prompts. Using default values.'
+        )
+        runInit()
+      } else {
+        // Something else when wrong
+        console.error('An unknown error occurred:', error)
+      }
+    })
+}
+
+function runInit(config = {}) {
+  const {
+    appName,
+    ...configOptions
+  } = config
+  const init = require('../dist/api/init')
+
+  const directory = argv.d || process.cwd()
+  init({
+    directory,
+    force: argv.f || null,
+    logging: argv.l || null,
+    tauriPath: argv.t || null,
+    customConfig: merge(configOptions, {
+      build: {
+        distDir: argv.D,
+        devPath: argv.p
+      },
+      tauri: {
+        window: {
+          title: argv.w
+        }
+      }
+    })
+  })
+
+  if (appName || argv.A) {
+    const manifestPath = resolve(directory, 'src-tauri/Cargo.toml')
+    const cargoManifest = toml.parse(readFileSync(manifestPath).toString())
+    let binName = (appName || argv.A).replace(/ /g, '-')
+    cargoManifest.package.name = binName
+    cargoManifest.package['default-run'] = binName
+    if (cargoManifest.bin && cargoManifest.bin.length) {
+      cargoManifest.bin[0].name = binName
+    }
+    writeFileSync(manifestPath, toml.stringify(cargoManifest))
+  }
+}

+ 1 - 0
cli/tauri.js/package.json

@@ -60,6 +60,7 @@
     "imagemin-optipng": "8.0.0",
     "imagemin-pngquant": "9.0.0",
     "imagemin-zopfli": "7.0.0",
+    "inquirer": "^7.3.0",
     "is-png": "2.0.0",
     "is-reachable": "^4.0.0",
     "isbinaryfile": "4.0.6",

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 130 - 400
cli/tauri.js/yarn.lock


이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.