Skip to content
This repository was archived by the owner on Nov 23, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/ditto-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ pub struct CodegenJsConfig {
/// package is built as a dependency.
#[serde(rename = "package-json")]
pub package_json_additions: Option<serde_json::Map<String, serde_json::Value>>,
/// Package manager being used. This affects the format of generated `package.json` files.
#[serde(rename = "package-manager", default)]
pub package_manager: PackageManager,
}

/// Flavours of JavaScript package management.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum PackageManager {
/// The standard package manager shipped with NodeJS.
#[serde(rename = "npm")]
Npm,
/// https://pnpm.io/
#[serde(rename = "pnpm")]
Pnpm,
}

impl Default for PackageManager {
fn default() -> Self {
Self::Npm
}
}

impl Default for CodegenJsConfig {
Expand All @@ -167,6 +187,7 @@ impl Default for CodegenJsConfig {
dist_dir: default_js_dist_dir(),
packages_dir: default_js_packages_dir(),
package_json_additions: None,
package_manager: PackageManager::Npm,
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions crates/ditto-config/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,39 @@ mod successes {
}
);
}

#[test]
fn it_parses_js_package_manager() {
use crate::PackageManager::*;
assert_parses!(
r#"
name = "test"
targets = []
[codegen-js]
package-manager = "pnpm"
"#,
Config {
codegen_js_config: CodegenJsConfig {
package_manager: Pnpm,
..
},
..
}
);
assert_parses!(
r#"
name = "test"
targets = []
"#,
Config {
codegen_js_config: CodegenJsConfig {
package_manager: Npm, // default
..
},
..
}
);
}
}

mod errors {
Expand Down
9 changes: 5 additions & 4 deletions crates/ditto-make/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ Options:
<!-- prettier-ignore-start -->
```console
$ ditto compile package-json --help
Usage: ditto compile package-json -i <input> -o <output>
Usage: ditto compile package-json -i <input> -o <output> --package-manager <PKG_MANAGER>

Options:
-i <input>
-o <output>
-h, --help Print help information
-i <input>
-o <output>
--package-manager <PKG_MANAGER> [possible values: npm, pnpm]
-h, --help Print help information

```
<!-- prettier-ignore-end -->
13 changes: 8 additions & 5 deletions crates/ditto-make/src/build_ninja.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl Rule {
let ditto = ditto_bin.to_string_lossy();
Self {
name: RULE_NAME_PACKAGE_JSON.to_string(),
command: format!("{ditto} {compile} {package_json} -{i} ${{in}} -{o} ${{out}}"),
command: format!("{ditto} {compile} {package_json} -{i} ${{in}} -{o} ${{out}} --package-manager ${{package-manager}}"),
}
}

Expand Down Expand Up @@ -590,10 +590,13 @@ impl Build {
outputs,
rule_name: String::from(RULE_NAME_PACKAGE_JSON),
inputs,
variables: HashMap::from_iter(vec![(
String::from("description"),
format!("Generating package.json for {}", package_name.as_str()),
)]),
variables: HashMap::from_iter(vec![
(String::from("package-manager"), "pnpm".to_string()),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

(
String::from("description"),
format!("Generating package.json for {}", package_name.as_str()),
),
]),
}
}

Expand Down
38 changes: 29 additions & 9 deletions crates/ditto-make/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{arg, Arg, ArgMatches, Command};
use ditto_ast as ast;
use ditto_checker as checker;
use ditto_codegen_js as js;
use ditto_config::read_config;
use ditto_config::{read_config, PackageManager};
use ditto_cst as cst;
use miette::{miette, IntoDiagnostic, NamedSource, Report, Result};
use std::{
Expand Down Expand Up @@ -40,7 +40,12 @@ pub fn command(name: impl Into<clap::builder::Str>) -> Command {
.subcommand(
Command::new(SUBCOMMAND_PACKAGE_JSON)
.arg(arg_input())
.arg(arg_output()),
.arg(arg_output())
.arg(
arg!(--"package-manager" <PKG_MANAGER>)
.value_parser(["npm", "pnpm"])
.required(true),
),
);

fn arg_input() -> Arg {
Expand Down Expand Up @@ -81,9 +86,15 @@ pub fn run(matches: &ArgMatches) -> Result<()> {
let outputs = matches.get_many("outputs").unwrap().cloned().collect();
run_js(inputs, outputs)
} else if let Some(matches) = matches.subcommand_matches(SUBCOMMAND_PACKAGE_JSON) {
let package_manager_string = matches.get_one::<String>("package-manager").unwrap();
let package_manager = match package_manager_string.as_str() {
"npm" => PackageManager::Npm,
"pnpm" => PackageManager::Pnpm,
_ => unreachable!(),
};
let input = matches.get_one::<String>("input").unwrap();
let output = matches.get_one::<String>("output").unwrap();
run_package_json(input, output)
run_package_json(package_manager, input, output)
} else {
unreachable!()
}
Expand Down Expand Up @@ -343,20 +354,29 @@ fn run_js(inputs: Vec<String>, outputs: Vec<String>) -> Result<()> {
}

/// Generates a `package.json` from a `ditto.toml` input.
fn run_package_json(input: &str, output: &str) -> Result<()> {
fn run_package_json(package_manager: PackageManager, input: &str, output: &str) -> Result<()> {
use serde_json::{json, Map, Value};

let config = read_config(input)?;
let dependencies = config
.dependencies
.into_iter()
.map(|name| {
(
name.into_string(),
match package_manager {
PackageManager::Npm => String::from("*"),
PackageManager::Pnpm => String::from("workspace:*"),
},
)
})
.collect::<HashMap<_, _>>();

// https://stackoverflow.com/a/68558580/17263155
let value = json!({
"name": config.name.into_string(),
"type": "module",
"dependencies": config
.dependencies
.into_iter()
.map(|name| (name.into_string(), String::from("*")))
.collect::<HashMap<_, _>>(),
"dependencies": dependencies
});

let mut object = if let Value::Object(object) = value {
Expand Down
3 changes: 2 additions & 1 deletion crates/ditto-make/tests/cmd/all-good/test.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rule js
command = ditto compile js -i ${in} -o ${out}

rule package_json
command = ditto compile package-json -i ${in} -o ${out}
command = ditto compile package-json -i ${in} -o ${out} --package-manager ${package-manager}

build builddir/A.ast builddir/A.ast-exports builddir/A.checker-warnings: ast ./ditto-src/A.ditto
description = Checking A
Expand Down Expand Up @@ -53,5 +53,6 @@ build packages/dep/Util.js: js builddir/dep/Util.ast

build packages/dep/package.json: package_json dep/ditto.toml
description = Generating package.json for dep
package-manager = pnpm