Expand description
Detection of what a project is from what it contains: its package manager, its language runtime, its web framework, and whether a build is served by a server or as static files.
Each question has a detector fed with inputs (file names, language names or manifest contents) and options to choose from:
PackagerDetectorwithPackagers (Npm,Pnpm,Yarn);RuntimeDetectorwithRuntimes (Node,Php,Pythonand eight more), matching by aStrategy;FrameworkDetectorwithFrameworks (NextJs,Nuxt,SvelteKit,Astro,Flutterand ten more), scored on files anddependencies;RenderingDetectorwithRenderings (Ssr,Static).
Options are traits, so a project can add its own. Runtime and framework
detections come back as Detected, which carries the detector’s
packager into the install and build commands.
use utopia_detector::{FrameworkDetector, NextJs, Nuxt, SvelteKit};
let mut detector = FrameworkDetector::new("pnpm");
detector.add_option(NextJs).add_option(Nuxt).add_option(SvelteKit);
for file in ["src", "package.json", "svelte.config.js"] {
detector.add_file(file);
}
let framework = detector.detect().expect("a framework");
assert_eq!(framework.name(), "sveltekit");
assert_eq!(framework.build_command(), "pnpm run build");use Utopia\Detector\Detection\Framework\NextJs;
use Utopia\Detector\Detection\Framework\Nuxt;
use Utopia\Detector\Detection\Framework\SvelteKit;
use Utopia\Detector\Detector\Framework;
$detector = new Framework('pnpm');
$detector->addOption(new NextJs())->addOption(new Nuxt())->addOption(new SvelteKit());
foreach (['src', 'package.json', 'svelte.config.js'] as $file) {
$detector->addInput($file, Framework::INPUT_FILE);
}
$framework = $detector->detect();
echo $framework->getName(), "\n"; // sveltekit
echo $framework->getBuildCommand(), "\n"; // pnpm run buildPHP: ports utopia-php/detector.
| PHP | Rust |
|---|---|
Utopia\Detector\Detector (abstract), Detection (abstract) | one detector type per question; no common base |
Detector::addOption(), addInput(), detect() | add_option, add_input (add_file / add_manifest on FrameworkDetector), detect on each detector |
Detector\Packager, Detection\Packager | PackagerDetector, Packager |
Detection\Packager\NPM, PNPM, Yarn | Npm, Pnpm, Yarn |
Detector\Runtime, Detection\Runtime | RuntimeDetector, Runtime |
Detector\Strategy (FILEMATCH, EXTENSION, LANGUAGES) | Strategy |
Detection\Runtime\Bun, CPP, Dart, Deno, Dotnet, Java, Node, PHP, Python, Ruby, Swift | Bun, Cpp, Dart, Deno, Dotnet, Java, Node, Php, Python, Ruby, Swift |
Detector\Framework, Detection\Framework | FrameworkDetector, Framework |
Framework::INPUT_FILE, INPUT_PACKAGES | Input |
Framework::dependencies() | dependencies |
Detection\Framework\JS (abstract) | the JavaScript frameworks’ shared install and build commands |
Detection\Framework\Analog, Angular, Astro, Flutter, Jaspr, Lynx, NextJs, Nuxt, React, ReactNative, Remix, Svelte, SvelteKit, TanStackStart, Vue | Analog, Angular, Astro, Flutter, Jaspr, Lynx, NextJs, Nuxt, React, ReactNative, Remix, Svelte, SvelteKit, TanStackStart, Vue |
getAdapter() ('ssr', 'static', '') | Framework::adapter (Adapter, None) |
setPackager() on a runtime or framework | the packager argument of Runtime::commands, Framework::install_command, Framework::build_command; Detected passes the detector’s |
Detector\Rendering, Detection\Rendering | RenderingDetector, Rendering (Rendered from detect) |
Detection\Rendering\SSR, XStatic | Ssr, Static |
\InvalidArgumentException, \Exception | Error::InvalidArgument, Error::Exception |
PHP passes file names as byte strings; here they are Strings, so a name
that is not UTF-8 has to be converted before it is added.
Structs§
- Analog
- Analog, the Angular meta-framework.
- Angular
- Angular.
- Astro
- Astro. It renders components of other frameworks, so their files and packages count for it too; a tie between Astro and another framework goes to the other one.
- Bun
- Bun.
- Cpp
- C++.
- Dart
- Dart.
- Deno
- Deno.
- Detected
- A runtime or framework a detector chose, with the detector’s packager:
its commands are the ones for that packager. Everything else is the
option itself, through
Deref. - Dotnet
- .NET.
- Flutter
- Flutter (web).
- Framework
Detector - Finds a project’s framework by scoring every option: one point per framework file among the file inputs, and one per framework package declared in each manifest input. The highest score wins; a tie goes to the framework with the fewest parents, except that Astro yields to the runner-up.
- Jaspr
- Jaspr, the Dart web framework. It is detected from the same pubspec
files as Flutter and wins once a
jasprpackage appears, whatever order the options were added in. - Java
- Java.
- Lynx
- Lynx.
- NextJs
- Next.js.
- Node
- Node.js: the only runtime whose install command follows the packager.
- Npm
- npm:
package.jsonorpackage-lock.json. - Nuxt
- Nuxt.
- Packager
Detector - Finds the packager of a project from its file names: the first option, in the order they were added, with one of its files among the inputs.
- Php
- PHP.
- Pnpm
- pnpm:
pnpm-lock.yaml. - Python
- Python.
- React
- React.
- React
Native - React Native.
- Remix
- Remix: always server-side.
- Rendering
Detector - Tells how a framework’s build is served from the build’s file names: the first option, in the order they were added, with one of its files for the framework among the inputs; otherwise static output, falling back to the only HTML file when there is exactly one.
- Ruby
- Ruby.
- Runtime
Detector - Finds the runtime of a project: the first option, in the order they were
added, that matches an input the way the
Strategysays. - Score
- One option as a framework detector scores it.
- Ssr
- Server-side rendering: the build has a server entry for the framework.
- Static
- Static output: plain files, with an optional fallback page.
- Svelte
- Svelte.
- Svelte
Kit - SvelteKit: static when the configuration (or
package.json) uses@sveltejs/adapter-static, server-side otherwise. - Swift
- Swift.
- TanStack
Start - TanStack Start: server-side with a Nitro plugin or without prerendering, static when it prerenders.
- Vue
- Vue.
- Yarn
- Yarn:
yarn.lock.
Enums§
- Adapter
- How a framework’s build is served, as its configuration says.
- Error
- Why a detector refused an argument.
- Input
- What a framework detector input is.
- Rendered
- What a
RenderingDetectorfound: one of its options, or static output it made up because none matched. - Strategy
- What a
RuntimeDetector’s inputs are, and so how they are matched.
Traits§
- Framework
- A web framework, recognised by its configuration files and the packages a project depends on.
- Packager
- A package manager, recognised by the files it leaves in a project.
- Rendering
- How a framework’s build output is served, recognised by the files the build leaves for a given framework.
- Runtime
- A language runtime, recognised by its languages, file extensions or files.
Functions§
- best
- The name of the framework a detector picks from
scores, one per option in the order the options were added: options sharing a name add up; the most matches win; a tie goes to the fewest parents, except that Astro yields to the runner-up.Nonewhen nothing matches. - dependencies
- The dependency names declared in a
package.json(JSON) orpubspec.yaml(YAML), section by section; none when it parses as neither. - html_
fallback - The only file among
inputswith the extensionhtml, the static output’s fallback;Nonewith none or several. - matches
- How many of an option’s
filesare among the fileinputs, plus how many of itspackageseach manifest’sdependenciesdeclare. A file or package the option lists twice counts twice.