-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fd758c
commit cf83886
Showing
13 changed files
with
554 additions
and
231 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
8 changes: 5 additions & 3 deletions
8
...sync-npm-dependencies-with-nix/Cargo.toml → ...place-npm-dependencies-sources/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
crates/replace-npm-dependencies-sources/fixture/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
] | ||
} |
Oops, something went wrong.