Skip to content

Commit

Permalink
feature(npm): export ts types, json schema (#42)
Browse files Browse the repository at this point in the history
* create schema and types for the npm package

* use rari-npm gitignore

---------

Co-authored-by: Florian Dieminger <[email protected]>
  • Loading branch information
argl and fiji-flo authored Nov 21, 2024
1 parent b2d36b5 commit 242b078
Show file tree
Hide file tree
Showing 21 changed files with 323 additions and 56 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ jobs:
registry-url: "https://registry.npmjs.org/"
node-version-file: "./rari-npm/package.json"

- name: Generate Schema
working-directory: ./rari-npm
run: npm install && npm run export-schema && npm run generate-types
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish
working-directory: ./rari-npm
run: npm publish --access public
Expand Down
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ console = "0.15"
sha2 = "0.10"
dashmap = "6"
serde_yaml_ng = "0.10"
schemars = { version = "0.8", features = ["chrono"] }
pretty_yaml = "0.5"
yaml_parser = "0.2"
const_format = "0.2"
Expand All @@ -112,6 +113,7 @@ tracing.workspace = true
tracing-subscriber.workspace = true
anyhow.workspace = true
dashmap.workspace = true
schemars.workspace = true

self_update = { version = "0.41", default-features = false, features = [
"rustls",
Expand Down
18 changes: 18 additions & 0 deletions crates/rari-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rari_doc::build::{
};
use rari_doc::cached_readers::{read_and_cache_doc_pages, CACHED_DOC_PAGE_FILES};
use rari_doc::issues::{issues_by, InMemoryLayer};
use rari_doc::pages::json::BuiltPage;
use rari_doc::pages::types::doc::Doc;
use rari_doc::reader::read_docs_parallel;
use rari_doc::search_index::build_search_index;
Expand All @@ -30,6 +31,7 @@ use rari_tools::sync_translated_content::sync_translated_content;
use rari_types::globals::{build_out_root, content_root, content_translated_root, SETTINGS};
use rari_types::locale::Locale;
use rari_types::settings::Settings;
use schemars::schema_for;
use self_update::cargo_crate_version;
use tabwriter::TabWriter;
use tracing::Level;
Expand Down Expand Up @@ -62,10 +64,16 @@ enum Commands {
GitHistory,
Popularities,
Update(UpdateArgs),
ExportSchema(ExportSchemaArgs),
#[command(subcommand)]
Content(ContentSubcommand),
}

#[derive(Args)]
struct ExportSchemaArgs {
output_file: Option<PathBuf>,
}

#[derive(Subcommand)]
enum ContentSubcommand {
/// Moves content from one slug to another
Expand Down Expand Up @@ -381,10 +389,20 @@ fn main() -> Result<(), Error> {
}
},
Commands::Update(args) => update(args.version)?,
Commands::ExportSchema(args) => export_schema(args)?,
}
Ok(())
}

fn export_schema(args: ExportSchemaArgs) -> Result<(), Error> {
let out_path = args
.output_file
.unwrap_or_else(|| PathBuf::from("schema.json"));
let schema = schema_for!(BuiltPage);
fs::write(out_path, serde_json::to_string_pretty(&schema)?)?;
Ok(())
}

fn update(version: Option<String>) -> Result<(), Error> {
let mut rel_builder = self_update::backends::github::ReleaseList::configure();
rel_builder.repo_owner("mdn");
Expand Down
1 change: 1 addition & 0 deletions crates/rari-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ serde_json.workspace = true
url.workspace = true
chrono.workspace = true
indexmap.workspace = true
schemars.workspace = true
7 changes: 4 additions & 3 deletions crates/rari-data/src/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::marker::PhantomData;
use std::path::Path;

use rari_utils::io::read_to_string;
use schemars::JsonSchema;
use serde::de::{self, value, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use url::Url;
Expand Down Expand Up @@ -98,7 +99,7 @@ pub enum BrowserIdentifier {
SafariIos,
}

#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum BaselineHighLow {
High,
Expand All @@ -107,7 +108,7 @@ pub enum BaselineHighLow {
False(bool),
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(Deserialize, Serialize, Clone, Debug, JsonSchema)]
pub struct SupportStatus {
/// Whether the feature is Baseline (low substatus), Baseline (high substatus), or not (false)
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -122,7 +123,7 @@ pub struct SupportStatus {
pub support: BTreeMap<BrowserIdentifier, String>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(Deserialize, Serialize, Clone, Debug, JsonSchema)]
pub struct SupportStatusWithByKey {
/// Whether the feature is Baseline (low substatus), Baseline (high substatus), or not (false)
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ dashmap.workspace = true
pretty_yaml.workspace = true
serde_yaml_ng.workspace = true
yaml_parser.workspace = true
schemars.workspace = true

yaml-rust = "0.4"
percent-encoding = "2"
pest = "2"
pest_derive = "2"
validator = { version = "0.19", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }

scraper = { version = "0.21", features = ["deterministic"] }
lol_html = "2"
html-escape = "0.2"
Expand Down
5 changes: 3 additions & 2 deletions crates/rari-doc/src/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::sync::atomic::AtomicI64;
use std::sync::{Arc, Mutex};

use schemars::JsonSchema;
use serde::Serialize;
use tracing::field::{Field, Visit};
use tracing::span::{Attributes, Id};
Expand Down Expand Up @@ -232,7 +233,7 @@ where
}
}

#[derive(Serialize, Debug, Default, Clone)]
#[derive(Serialize, Debug, Default, Clone, JsonSchema)]
#[serde(untagged)]
pub enum Additional {
BrokenLink {
Expand All @@ -245,7 +246,7 @@ pub enum Additional {
None,
}

#[derive(Serialize, Debug, Default, Clone)]
#[derive(Serialize, Debug, Default, Clone, JsonSchema)]
pub struct DisplayIssue {
pub id: i64,
pub explanation: Option<String>,
Expand Down
Loading

0 comments on commit 242b078

Please sign in to comment.