Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Mar 12, 2024
1 parent 1fd758c commit cf83886
Show file tree
Hide file tree
Showing 13 changed files with 554 additions and 231 deletions.
428 changes: 428 additions & 0 deletions crates/replace-npm-dependencies-sources/Cargo.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[package]
name = "sync-npm-dependencies-with-nix"
name = "replace-npm-dependencies-sources"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.81"
parse-flake-lock = { git = "https://github.com/DeterminateSystems/flake-checker", branch = "main" }
walkdir = "2.5.0"
clap = {version ="4.5.2", features = ["derive"]}
ignore = "0.4"
serde_json = {version ="1", features = ["std"]}
serde = "1"
17 changes: 17 additions & 0 deletions crates/replace-npm-dependencies-sources/fixture/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"author": "",
"dependencies": {
"@holochain-open-dev/profiles": "2221"
},
"description": "",
"keywords": [],
"license": "ISC",
"main": "index.js",
"name": "zome",
"scripts": {
"build": "",
"start": "node index.js"
},
"type": "module",
"version": "1.0.0"
}
70 changes: 70 additions & 0 deletions crates/replace-npm-dependencies-sources/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use anyhow::Result;
use ignore::Walk;
use serde_json::Value;
use std::{collections::HashMap, fs::File, io::BufReader, process::Command};

use clap::Parser;

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// List of dependencies to replace, in the form <NPM_PACKAGE_NAME>=<NEW_SOURCE_FOR_PACKAGE>
dependencies_to_replace: Vec<String>,
}

fn parse_args() -> Result<HashMap<String, String>> {
let cli = Cli::parse();

let mut deps: HashMap<String, String> = HashMap::new();

for dep in cli.dependencies_to_replace {
let split: Vec<&str> = dep.split('=').into_iter().collect();
deps.insert(split[0].to_string(), split[1].to_string());
}

Ok(deps)
}

fn main() -> Result<()> {
let deps_to_replace = parse_args()?;

let mut replaced_some_dep = false;

for entry in Walk::new(".").into_iter().filter_map(|e| e.ok()) {
let f_name = entry.file_name().to_string_lossy();

if f_name == "package.json" {
let file = File::open(entry.path())?;
let reader = BufReader::new(file);
let mut package_json_contents: Value = serde_json::from_reader(reader)?;

if let Some(Value::Object(deps)) = package_json_contents.get_mut("dependencies") {
for (dep_to_replace, new_source) in &deps_to_replace {
if let Some(found_dep) = deps.get_mut(dep_to_replace) {
match &found_dep {
Value::String(old_source) if old_source.eq(new_source) => {}
_ => {
*found_dep = Value::String(new_source.clone());
println!(
"{:?}: setting dependency \"{dep_to_replace}\" to \"{new_source}\"",
entry.path()
);
replaced_some_dep = true;
}
}
}
}
}

let st = serde_json::to_string_pretty(&package_json_contents)?;

std::fs::write(entry.path(), st)?;
}
}

if replaced_some_dep {
Command::new("npm").arg("install").output()?;
}

Ok(())
}
176 changes: 0 additions & 176 deletions crates/sync-npm-dependencies-with-nix/Cargo.lock

This file was deleted.

26 changes: 0 additions & 26 deletions crates/sync-npm-dependencies-with-nix/src/main.rs

This file was deleted.

4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@
];
};

packages.sync-npm-dependencies-with-nix =
packages.replace-npm-dependencies-sources =
let
craneLib = inputs.crane.lib.${system};

cratePath = ./crates/sync-npm-dependencies-with-nix;
cratePath = ./crates/replace-npm-dependencies-sources;

cargoToml = builtins.fromTOML (builtins.readFile "${cratePath}/Cargo.toml");
crate = cargoToml.package.name;
Expand Down
2 changes: 1 addition & 1 deletion nix/fixture/flake.lock

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

6 changes: 3 additions & 3 deletions nix/fixture/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@
# more packages go here
cargo-nextest
] ++ [
inputs'.hcUtils.packages.sync-npm-dependencies-with-nix
inputs'.hcUtils.packages.replace-npm-dependencies-sources
];

shellHook = ''
sync-npm-dependencies-with-nix ${builtins.trace (builtins.toString (builtins.map (p: "${p.meta.packageName}=${p.outPath}/lib") (builtins.attrValues (allNpmPackages {inherit inputs' self';})))) "hey"}
replace-npm-dependencies-sources ${builtins.toString (builtins.map (p: "${p.meta.packageName}=${p.outPath}/lib") (builtins.attrValues (allNpmPackages {inherit inputs' self';})))}
'';
};

# packages.i = inputs'.profiles.packages.profiles_ui;
};
};
}
5 changes: 4 additions & 1 deletion nix/fixture/package-lock.json

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

17 changes: 11 additions & 6 deletions nix/fixture/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"name": "fixture",
"version": "1.0.0",
"author": "",
"dependencies": {
"zome": "/nix/store/3l4n96ahxi1jd59srny1vyn59x7qxx75-zome/lib"
},
"description": "",
"workspaces": ["./zome/"],
"keywords": [],
"author": "",
"license": "ISC"
}
"license": "ISC",
"name": "fixture",
"version": "1.0.0",
"workspaces": [
"./zome/"
]
}
Loading

0 comments on commit cf83886

Please sign in to comment.