|
| 1 | +// Copyright 2019-2021 Tauri Programme within The Commons Conservancy |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +use std::fmt; |
| 6 | + |
| 7 | +#[derive(Debug, Clone)] |
| 8 | +pub enum Framework { |
| 9 | + Svelte, |
| 10 | + Angular, |
| 11 | + React, |
| 12 | + Nextjs, |
| 13 | + |
| 14 | + Gatsby, |
| 15 | + Nuxt, |
| 16 | + Quasar, |
| 17 | + VueCli, |
| 18 | + Vue, |
| 19 | +} |
| 20 | + |
| 21 | +impl Framework { |
| 22 | + pub fn dev_path(&self) -> String { |
| 23 | + match self { |
| 24 | + Self::Svelte => "http://localhost:5000", |
| 25 | + Self::Angular => "http://localhost:4200", |
| 26 | + Self::React => "http://localhost:3000", |
| 27 | + Self::Nextjs => "http://localhost:3000", |
| 28 | + Self::Gatsby => "http://localhost:8000", |
| 29 | + Self::Nuxt => "http://localhost:3000", |
| 30 | + Self::Quasar => "http://localhost:8080", |
| 31 | + Self::VueCli => "http://localhost:8080", |
| 32 | + Self::Vue => "http://localhost:8080", |
| 33 | + } |
| 34 | + .into() |
| 35 | + } |
| 36 | + |
| 37 | + pub fn dist_dir(&self) -> String { |
| 38 | + match self { |
| 39 | + Self::Svelte => "../public", |
| 40 | + Self::Angular => "../dist", |
| 41 | + Self::React => "../build", |
| 42 | + Self::Nextjs => "../out", |
| 43 | + Self::Gatsby => "../public", |
| 44 | + Self::Nuxt => "../dist", |
| 45 | + Self::Quasar => "../dist/spa", |
| 46 | + Self::VueCli => "../dist", |
| 47 | + Self::Vue => "../dist", |
| 48 | + } |
| 49 | + .into() |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl fmt::Display for Framework { |
| 54 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 55 | + match self { |
| 56 | + Self::Svelte => write!(f, "Svelte"), |
| 57 | + Self::Angular => write!(f, "Angular"), |
| 58 | + Self::React => write!(f, "React"), |
| 59 | + Self::Nextjs => write!(f, "React (Next.js)"), |
| 60 | + Self::Gatsby => write!(f, "React (Gatsby)"), |
| 61 | + Self::Nuxt => write!(f, "Vue.js (Nuxt)"), |
| 62 | + Self::Quasar => write!(f, "Vue.js (Quasar)"), |
| 63 | + Self::VueCli => write!(f, "Vue.js (Vue CLI)"), |
| 64 | + Self::Vue => write!(f, "Vue.js"), |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[derive(Debug, Clone)] |
| 70 | +pub enum Bundler { |
| 71 | + Webpack, |
| 72 | + Rollup, |
| 73 | +} |
| 74 | + |
| 75 | +impl fmt::Display for Bundler { |
| 76 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 77 | + match self { |
| 78 | + Self::Webpack => write!(f, "Webpack"), |
| 79 | + Self::Rollup => write!(f, "Rollup"), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +pub fn infer_from_package_json(package_json: &str) -> (Option<Framework>, Option<Bundler>) { |
| 85 | + let framework_map = [ |
| 86 | + ("svelte", Framework::Svelte, None), |
| 87 | + ("@angular", Framework::Angular, Some(Bundler::Webpack)), |
| 88 | + (r#""next""#, Framework::Nextjs, Some(Bundler::Webpack)), |
| 89 | + ("gatsby", Framework::Gatsby, Some(Bundler::Webpack)), |
| 90 | + ("react", Framework::React, None), |
| 91 | + ("nuxt", Framework::Nuxt, Some(Bundler::Webpack)), |
| 92 | + ("quasar", Framework::Quasar, Some(Bundler::Webpack)), |
| 93 | + ("@vue/cli", Framework::VueCli, Some(Bundler::Webpack)), |
| 94 | + ("vue", Framework::Vue, None), |
| 95 | + ]; |
| 96 | + let bundler_map = [("webpack", Bundler::Webpack), ("rollup", Bundler::Rollup)]; |
| 97 | + |
| 98 | + let (framework, framework_bundler) = framework_map |
| 99 | + .iter() |
| 100 | + .find(|(keyword, _, _)| package_json.contains(keyword)) |
| 101 | + .map(|(_, framework, bundler)| (Some(framework.clone()), bundler.clone())) |
| 102 | + .unwrap_or((None, None)); |
| 103 | + |
| 104 | + let bundler = bundler_map |
| 105 | + .iter() |
| 106 | + .find(|(keyword, _)| package_json.contains(keyword)) |
| 107 | + .map(|(_, bundler)| bundler.clone()) |
| 108 | + .or(framework_bundler); |
| 109 | + |
| 110 | + (framework, bundler) |
| 111 | +} |
0 commit comments