|
3 | 3 | // SPDX-License-Identifier: Apache-2.0 |
4 | 4 | // SPDX-License-Identifier: MIT |
5 | 5 |
|
6 | | -const parseArgs = require('minimist') |
7 | | -const inquirer = require('inquirer') |
8 | | -const { resolve, join } = require('path') |
9 | | -const { |
10 | | - recipeShortNames, |
11 | | - recipeDescriptiveNames, |
12 | | - recipeByDescriptiveName, |
13 | | - recipeByShortName, |
14 | | - install, |
15 | | - checkPackageManager, |
16 | | - shell, |
17 | | - addTauriScript |
18 | | -} = require('../dist/') |
19 | | - |
20 | | -/** |
21 | | - * @type {object} |
22 | | - * @property {boolean} h |
23 | | - * @property {boolean} help |
24 | | - * @property {boolean} v |
25 | | - * @property {boolean} version |
26 | | - * @property {string|boolean} f |
27 | | - * @property {string|boolean} force |
28 | | - * @property {boolean} l |
29 | | - * @property {boolean} log |
30 | | - * @property {boolean} d |
31 | | - * @property {boolean} directory |
32 | | - * @property {boolean} dev |
33 | | - * @property {string} r |
34 | | - * @property {string} recipe |
35 | | - */ |
36 | | -const createTauriApp = async (cliArgs) => { |
37 | | - const argv = parseArgs(cliArgs, { |
38 | | - alias: { |
39 | | - h: 'help', |
40 | | - v: 'version', |
41 | | - f: 'force', |
42 | | - l: 'log', |
43 | | - m: 'manager', |
44 | | - d: 'directory', |
45 | | - dev: 'dev', |
46 | | - t: 'tauri-path', |
47 | | - A: 'app-name', |
48 | | - W: 'window-title', |
49 | | - D: 'dist-dir', |
50 | | - P: 'dev-path', |
51 | | - r: 'recipe' |
52 | | - }, |
53 | | - boolean: ['h', 'l', 'ci', 'dev'] |
54 | | - }) |
55 | | - |
56 | | - if (argv.help) { |
57 | | - printUsage() |
58 | | - return 0 |
59 | | - } |
60 | | - |
61 | | - if (argv.v) { |
62 | | - console.log(require('../package.json').version) |
63 | | - return false // do this for node consumers and tests |
64 | | - } |
65 | | - |
66 | | - return getOptionsInteractive(argv, !argv.ci).then((responses) => |
67 | | - runInit(argv, responses) |
68 | | - ) |
69 | | -} |
70 | | - |
71 | | -function printUsage() { |
72 | | - console.log(` |
73 | | - Description |
74 | | - Starts a new tauri app from a "recipe" or pre-built template. |
75 | | - Usage |
76 | | - $ yarn create tauri-app <app-name> # npm create-tauri-app <app-name> |
77 | | - Options |
78 | | - --help, -h Displays this message |
79 | | - -v, --version Displays the Tauri CLI version |
80 | | - --ci Skip prompts |
81 | | - --force, -f Force init to overwrite [conf|template|all] |
82 | | - --log, -l Logging [boolean] |
83 | | - --manager, -d Set package manager to use [npm|yarn] |
84 | | - --directory, -d Set target directory for init |
85 | | - --binary, -b Optional path to a tauri binary from which to run init |
86 | | - --app-name, -A Name of your Tauri application |
87 | | - --window-title, -W Window title of your Tauri application |
88 | | - --dist-dir, -D Web assets location, relative to <project-dir>/src-tauri |
89 | | - --dev-path, -P Url of your dev server |
90 | | - --recipe, -r Add UI framework recipe. None by default. |
91 | | - Supported recipes: [${recipeShortNames.join('|')}] |
92 | | - `) |
93 | | -} |
94 | | - |
95 | | -const getOptionsInteractive = (argv, ask) => { |
96 | | - const defaults = { |
97 | | - appName: argv.A || 'tauri-app', |
98 | | - tauri: { window: { title: 'Tauri App' } }, |
99 | | - recipeName: argv.r || 'vanillajs' |
100 | | - } |
101 | | - |
102 | | - return inquirer |
103 | | - .prompt([ |
104 | | - { |
105 | | - type: 'input', |
106 | | - name: 'appName', |
107 | | - message: 'What is your app name?', |
108 | | - default: defaults.appName, |
109 | | - when: ask && !argv.A |
110 | | - }, |
111 | | - { |
112 | | - type: 'input', |
113 | | - name: 'tauri.window.title', |
114 | | - message: 'What should the window title be?', |
115 | | - default: defaults.tauri.window.title, |
116 | | - when: ask && !argv.W |
117 | | - }, |
118 | | - { |
119 | | - type: 'list', |
120 | | - name: 'recipeName', |
121 | | - message: 'Would you like to add a UI recipe?', |
122 | | - choices: recipeDescriptiveNames, |
123 | | - default: defaults.recipeName, |
124 | | - when: ask && !argv.r |
125 | | - } |
126 | | - ]) |
127 | | - .then((answers) => ({ |
128 | | - ...defaults, |
129 | | - ...answers |
130 | | - })) |
131 | | - .catch((error) => { |
132 | | - if (error.isTtyError) { |
133 | | - // Prompt couldn't be rendered in the current environment |
134 | | - console.warn( |
135 | | - 'It appears your terminal does not support interactive prompts. Using default values.' |
136 | | - ) |
137 | | - runInit() |
138 | | - } else { |
139 | | - // Something else went wrong |
140 | | - console.error('An unknown error occurred:', error) |
141 | | - } |
142 | | - }) |
143 | | -} |
144 | | - |
145 | | -async function runInit(argv, config = {}) { |
146 | | - const { |
147 | | - appName, |
148 | | - recipeName, |
149 | | - tauri: { |
150 | | - window: { title } |
151 | | - } |
152 | | - } = config |
153 | | - // this little fun snippet pulled from vite determines the package manager the script was run from |
154 | | - const packageManager = /yarn/.test(process.env.npm_execpath) ? 'yarn' : 'npm' |
155 | | - |
156 | | - let recipe |
157 | | - |
158 | | - if (argv.r) { |
159 | | - recipe = recipeByShortName(argv.r) |
160 | | - } else if (recipeName !== undefined) { |
161 | | - recipe = recipeByDescriptiveName(recipeName) |
162 | | - } |
163 | | - |
164 | | - let buildConfig = { |
165 | | - distDir: argv.D, |
166 | | - devPath: argv.P |
167 | | - } |
168 | | - |
169 | | - if (recipe !== undefined) { |
170 | | - buildConfig = recipe.configUpdate({ buildConfig, packageManager }) |
171 | | - } |
172 | | - |
173 | | - const directory = argv.d || process.cwd() |
174 | | - const cfg = { |
175 | | - ...buildConfig, |
176 | | - appName: appName || argv.A, |
177 | | - windowTitle: title || argv.w |
178 | | - } |
179 | | - |
180 | | - // note that our app directory is reliant on the appName and |
181 | | - // generally there are issues if the path has spaces (see Windows) |
182 | | - // future TODO prevent app names with spaces or escape here? |
183 | | - const appDirectory = join(directory, cfg.appName) |
184 | | - |
185 | | - // this throws an error if we can't run the package manager they requested |
186 | | - await checkPackageManager({ cwd: directory, packageManager }) |
187 | | - |
188 | | - if (recipe.preInit) { |
189 | | - console.log('===== running initial command(s) =====') |
190 | | - await recipe.preInit({ cwd: directory, cfg, packageManager }) |
191 | | - } |
192 | | - |
193 | | - const initArgs = [ |
194 | | - ['--app-name', cfg.appName], |
195 | | - ['--window-title', cfg.windowTitle], |
196 | | - ['--dist-dir', cfg.distDir], |
197 | | - ['--dev-path', cfg.devPath] |
198 | | - ].reduce((final, argSet) => { |
199 | | - if (argSet[1]) { |
200 | | - return final.concat(argSet) |
201 | | - } else { |
202 | | - return final |
203 | | - } |
204 | | - }, []) |
205 | | - |
206 | | - // Vue CLI plugin automatically runs these |
207 | | - if (recipe.shortName !== 'vuecli') { |
208 | | - console.log('===== installing any additional needed deps =====') |
209 | | - if (argv.dev) { |
210 | | - await shell('yarn', ['link', '@tauri-apps/cli'], { |
211 | | - cwd: appDirectory |
212 | | - }) |
213 | | - await shell('yarn', ['link', '@tauri-apps/api'], { |
214 | | - cwd: appDirectory |
215 | | - }) |
216 | | - } |
217 | | - |
218 | | - await install({ |
219 | | - appDir: appDirectory, |
220 | | - dependencies: recipe.extraNpmDependencies, |
221 | | - devDependencies: argv.dev |
222 | | - ? [...recipe.extraNpmDevDependencies] |
223 | | - : ['@tauri-apps/cli'].concat(recipe.extraNpmDevDependencies), |
224 | | - packageManager |
225 | | - }) |
226 | | - |
227 | | - console.log('===== running tauri init =====') |
228 | | - addTauriScript(appDirectory) |
229 | | - |
230 | | - const binary = !argv.b ? packageManager : resolve(appDirectory, argv.b) |
231 | | - const runTauriArgs = |
232 | | - packageManager === 'npm' && !argv.b |
233 | | - ? ['run', 'tauri', '--', 'init'] |
234 | | - : ['tauri', 'init'] |
235 | | - await shell(binary, [...runTauriArgs, ...initArgs, '--ci'], { |
236 | | - cwd: appDirectory |
237 | | - }) |
238 | | - } |
239 | | - |
240 | | - if (recipe.postInit) { |
241 | | - console.log('===== running final command(s) =====') |
242 | | - await recipe.postInit({ |
243 | | - cwd: appDirectory, |
244 | | - cfg, |
245 | | - packageManager |
246 | | - }) |
247 | | - } |
248 | | -} |
| 6 | +const { createTauriApp } = require('../dist/') |
249 | 7 |
|
250 | 8 | createTauriApp(process.argv.slice(2)).catch((err) => { |
251 | 9 | console.error(err) |
|
0 commit comments