-
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.
Merge pull request #28 from nestdotland/dev
0.8.2
- Loading branch information
Showing
8 changed files
with
106 additions
and
21 deletions.
There are no files selected for viewing
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
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,17 +1,16 @@ | ||
export { writeJson } from "https://x.nest.land/std@0.65.0/fs/write_json.ts"; | ||
|
||
export { readJson } from "https://x.nest.land/std@0.65.0/fs/read_json.ts"; | ||
|
||
export * as colors from "https://x.nest.land/std@0.65.0/fmt/colors.ts"; | ||
|
||
export * as semver from "https://deno.land/x/semver@v1.0.0/mod.ts"; | ||
|
||
export { Table } from "https://x.nest.land/cliffy@0.11.1/table.ts"; | ||
/**************** std ****************/ | ||
export * as colors from "https://x.nest.land/std@0.69.0/fmt/colors.ts"; | ||
|
||
export { | ||
assertEquals, | ||
assertMatch, | ||
assert, | ||
} from "https://x.nest.land/std@0.65.0/testing/asserts.ts"; | ||
} from "https://x.nest.land/std@0.69.0/testing/asserts.ts"; | ||
|
||
export * as path from "https://x.nest.land/std@0.69.0/path/mod.ts"; | ||
|
||
/**************** semver ****************/ | ||
export * as semver from "https://deno.land/x/semver@v1.0.0/mod.ts"; | ||
|
||
export * as path from "https://x.nest.land/std@0.65.0/path/mod.ts"; | ||
/**************** cliffy ****************/ | ||
export { Table, Cell } from "https://x.nest.land/cliffy@0.14.1/table/mod.ts"; |
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
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 @@ | ||
console.log("FOO"); |
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
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
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,84 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type Replacer = (key: string, value: any) => any; | ||
|
||
export interface WriteJsonOptions extends Deno.WriteFileOptions { | ||
replacer?: Array<number | string> | Replacer; | ||
spaces?: number | string; | ||
} | ||
|
||
function serialize( | ||
filePath: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
object: any, | ||
options: WriteJsonOptions, | ||
): string { | ||
try { | ||
const jsonString = JSON.stringify( | ||
object, | ||
options.replacer as string[], | ||
options.spaces, | ||
); | ||
return `${jsonString}\n`; | ||
} catch (err) { | ||
err.message = `${filePath}: ${err.message}`; | ||
throw err; | ||
} | ||
} | ||
|
||
/* Writes an object to a JSON file. */ | ||
export async function writeJson( | ||
filePath: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
object: any, | ||
options: WriteJsonOptions = {}, | ||
): Promise<void> { | ||
const jsonString = serialize(filePath, object, options); | ||
await Deno.writeTextFile(filePath, jsonString, { | ||
append: options.append, | ||
create: options.create, | ||
mode: options.mode, | ||
}); | ||
} | ||
|
||
/* Writes an object to a JSON file. */ | ||
export function writeJsonSync( | ||
filePath: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
object: any, | ||
options: WriteJsonOptions = {}, | ||
): void { | ||
const jsonString = serialize(filePath, object, options); | ||
Deno.writeTextFileSync(filePath, jsonString, { | ||
append: options.append, | ||
create: options.create, | ||
mode: options.mode, | ||
}); | ||
} | ||
|
||
/** Reads a JSON file and then parses it into an object */ | ||
export async function readJson(filePath: string): Promise<unknown> { | ||
const decoder = new TextDecoder("utf-8"); | ||
|
||
const content = decoder.decode(await Deno.readFile(filePath)); | ||
|
||
try { | ||
return JSON.parse(content); | ||
} catch (err) { | ||
err.message = `${filePath}: ${err.message}`; | ||
throw err; | ||
} | ||
} | ||
|
||
/** Reads a JSON file and then parses it into an object */ | ||
export function readJsonSync(filePath: string): unknown { | ||
const decoder = new TextDecoder("utf-8"); | ||
|
||
const content = decoder.decode(Deno.readFileSync(filePath)); | ||
|
||
try { | ||
return JSON.parse(content); | ||
} catch (err) { | ||
err.message = `${filePath}: ${err.message}`; | ||
throw err; | ||
} | ||
} |
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