mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 05:52:22 -04:00

* Update to latest Node LTS * Add sveltekit * Split tslib into separate @generated and @tslib components SvelteKit's path aliases don't support multiple locations, so our old approach of using @tslib to refer to both ts/lib and out/ts/lib will no longer work. Instead, all generated sources and their includes are placed in a separate out/ts/generated folder, and imported via @generated instead. This also allows us to generate .ts files, instead of needing to output separate .d.ts and .js files. * Switch package.json to module type * Avoid usage of baseUrl Incompatible with SvelteKit * Move sass into ts; use relative links SvelteKit's default sass support doesn't allow overriding loadPaths * jest->vitest, graphs example working with yarn dev * most pages working in dev mode * Some fixes after rebasing * Fix/silence some svelte-check errors * Get image-occlusion working with Fabric types * Post-rebase lock changes * Editor is now checked * SvelteKit build integrated into ninja * Use the new SvelteKit entrypoint for pages like congrats/deck options/etc * Run eslint once for ts/**; fix some tests * Fix a bunch of issues introduced when rebasing over latest main * Run eslint fix * Fix remaining eslint+pylint issues; tests now all pass * Fix some issues with a clean build * Latest bufbuild no longer requires @__PURE__ hack * Add a few missed dependencies * Add yarn.bat to fix Windows build * Fix pages failing to show when ANKI_API_PORT not defined * Fix svelte-check and vitest on Windows * Set node path in ./yarn * Move svelte-kit output to ts/.svelte-kit Sadly, I couldn't figure out a way to store it in out/ if out/ is a symlink, as it breaks module resolution when SvelteKit is run. * Allow HMR inside Anki * Skip SvelteKit build when HMR is defined * Fix some post-rebase issues I should have done a normal merge instead.
128 lines
3.4 KiB
Rust
128 lines
3.4 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use std::env;
|
|
use std::fmt::Write;
|
|
use std::path::PathBuf;
|
|
|
|
use anki_io::create_dir_all;
|
|
use anki_io::write_file_if_changed;
|
|
use anyhow::Result;
|
|
use inflections::Inflect;
|
|
use itertools::Itertools;
|
|
|
|
use crate::extract::Module;
|
|
use crate::extract::Variable;
|
|
use crate::extract::VariableKind;
|
|
|
|
pub fn write_ts_interface(modules: &[Module]) -> Result<()> {
|
|
let mut ts_out = header();
|
|
write_imports(&mut ts_out);
|
|
|
|
render_module_map(modules, &mut ts_out);
|
|
render_methods(modules, &mut ts_out);
|
|
|
|
if let Ok(path) = env::var("STRINGS_TS") {
|
|
let path = PathBuf::from(path);
|
|
create_dir_all(path.parent().unwrap())?;
|
|
write_file_if_changed(path, ts_out)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn render_module_map(modules: &[Module], ts_out: &mut String) {
|
|
ts_out.push_str("export enum ModuleName {\n");
|
|
for module in modules {
|
|
let name = &module.name;
|
|
let upper = name.to_upper_case();
|
|
writeln!(ts_out, r#" {upper} = "{name}","#).unwrap();
|
|
}
|
|
ts_out.push('}');
|
|
}
|
|
|
|
fn render_methods(modules: &[Module], ts_out: &mut String) {
|
|
for module in modules {
|
|
for translation in &module.translations {
|
|
let text = &translation.text;
|
|
let key = &translation.key;
|
|
let func_name = key.replace('-', "_").to_camel_case();
|
|
let arg_types = get_arg_types(&translation.variables);
|
|
let args = get_args(&translation.variables);
|
|
let maybe_args = if translation.variables.is_empty() {
|
|
"".to_string()
|
|
} else {
|
|
arg_types
|
|
};
|
|
writeln!(
|
|
ts_out,
|
|
r#"
|
|
/** {text} */
|
|
export function {func_name}({maybe_args}) {{
|
|
return translate("{key}", {args})
|
|
}}"#,
|
|
)
|
|
.unwrap();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn get_args(variables: &[Variable]) -> String {
|
|
if variables.is_empty() {
|
|
"".into()
|
|
} else if variables
|
|
.iter()
|
|
.all(|v| v.name == typescript_arg_name(&v.name))
|
|
{
|
|
// can use as-is
|
|
"args".into()
|
|
} else {
|
|
let out = variables
|
|
.iter()
|
|
.map(|v| format!("\"{}\": args.{}", v.name, typescript_arg_name(&v.name)))
|
|
.join(", ");
|
|
format!("{{{}}}", out)
|
|
}
|
|
}
|
|
|
|
fn typescript_arg_name(name: &str) -> String {
|
|
name.replace('-', "_").to_camel_case()
|
|
}
|
|
|
|
fn write_imports(buf: &mut String) {
|
|
buf.push_str(
|
|
"
|
|
import { translate } from './ftl-helpers';
|
|
export { firstLanguage, setBundles } from './ftl-helpers';
|
|
export { FluentBundle, FluentResource } from '@fluent/bundle';
|
|
",
|
|
);
|
|
}
|
|
|
|
fn header() -> String {
|
|
"// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
"
|
|
.to_string()
|
|
}
|
|
|
|
fn get_arg_types(args: &[Variable]) -> String {
|
|
let args = args
|
|
.iter()
|
|
.map(|arg| format!("{}: {}", arg.name.to_camel_case(), arg_kind(&arg.kind)))
|
|
.join(", ");
|
|
if args.is_empty() {
|
|
"".into()
|
|
} else {
|
|
format!("args: {{{args}}}",)
|
|
}
|
|
}
|
|
|
|
fn arg_kind(kind: &VariableKind) -> &str {
|
|
match kind {
|
|
VariableKind::Int | VariableKind::Float => "number",
|
|
VariableKind::String => "string",
|
|
VariableKind::Any => "number | string",
|
|
}
|
|
}
|