Skip to content

Commit

Permalink
feat(jsonc): added jsonc support (#1234)
Browse files Browse the repository at this point in the history
* feat(jsonc): added jsonc support

* chore: removed unnecessary println

* chore: clippy

* add snapshot test for jsonc

---------

Co-authored-by: Jake Runzer <[email protected]>
  • Loading branch information
shubhexists and coffee-cup authored Jan 6, 2025
1 parent 38c42fa commit 75bca15
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
15 changes: 15 additions & 0 deletions examples/deno-jsonc/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// THIS FILE HAS ALL KINDS OF COMMENTS POSSIBLE TO TEST JSONC
{
//some random comment
"tasks": {
// random afterline comment
/* :) */ "dev": "deno run --watch main.ts",
"start": "deno start main.ts"
} /*
This
is some multiline comment
*/,
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
}
18 changes: 18 additions & 0 deletions examples/deno-jsonc/deno.lock

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

7 changes: 7 additions & 0 deletions examples/deno-jsonc/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function add(a: number, b: number): number {
return a + b;
}
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
console.log("Add 2 + 3 =", add(2, 3));
}
6 changes: 6 additions & 0 deletions examples/deno-jsonc/main_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { assertEquals } from "@std/assert";
import { add } from "./main.ts";

Deno.test(function addTest() {
assertEquals(add(2, 3), 5);
});
49 changes: 49 additions & 0 deletions src/nixpacks/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,47 @@ impl App {
Ok(toml_file)
}

/// Parse jsonc files as json by ignoring all kinds of comments
pub fn read_jsonc<T>(&self, name: &str) -> Result<T>
where
T: DeserializeOwned,
{
let mut cleaned_jsonc = String::new();
let contents = self.read_file(name)?;
let mut chars = contents.chars().peekable();
while let Some(current_char) = chars.next() {
match current_char {
'/' if chars.peek() == Some(&'/') => {
while let Some(&next_char) = chars.peek() {
chars.next();
if next_char == '\n' {
break;
}
}
}
'/' if chars.peek() == Some(&'*') => {
chars.next();
loop {
match chars.next() {
Some('*') if chars.peek() == Some(&'/') => {
chars.next();
break;
}
None => break,
_ => continue,
}
}
}
_ => cleaned_jsonc.push(current_char),
}
}
let value: T = serde_json::from_str(cleaned_jsonc.as_str()).with_context(|| {
let relative_path = self.strip_source_path(Path::new(name)).unwrap();
format!("Error reading {} as JSONC", relative_path.to_str().unwrap())
})?;
Ok(value)
}

/// Try to yaml-parse a file.
pub fn read_yaml<T>(&self, name: &str) -> Result<T>
where
Expand Down Expand Up @@ -281,6 +322,14 @@ mod tests {
Ok(())
}

#[test]
fn test_read_jsonc_file() -> Result<()> {
let app = App::new("./examples/deno-jsonc")?;
let value: Map<String, Value> = app.read_jsonc("deno.jsonc")?;
assert!(value.get("tasks").is_some());
Ok(())
}

#[test]
fn test_read_toml_file() -> Result<()> {
let app = App::new("./examples/rust-rocket")?;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl DenoProvider {
if app.includes_file("deno.json") || app.includes_file("deno.jsonc") {
let deno_json: DenoJson = app
.read_json("deno.json")
.or_else(|_| app.read_json("deno.jsonc"))?;
.or_else(|_| app.read_jsonc("deno.jsonc"))?;

if let Some(tasks) = deno_json.tasks {
if let Some(start) = tasks.start {
Expand Down
24 changes: 24 additions & 0 deletions tests/snapshots/generate_plan_tests__deno_jsonc.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: tests/generate_plan_tests.rs
expression: plan
---
{
"providers": [],
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "deno"
},
"phases": {
"setup": {
"name": "setup",
"nixPkgs": [
"deno"
],
"nixOverlays": [],
"nixpkgsArchive": "[archive]"
}
},
"start": {
"cmd": "deno start main.ts"
}
}

0 comments on commit 75bca15

Please sign in to comment.