Deno 中 writeJson 的格式化输出

Format output of writeJson in Deno

我正在使用 Deno 1.7 并希望将 JSON 文件的输出格式化为看起来像具有 return 行和 indentions/tabs.然而,这不是我在传递 JSON 样式字符串时看到的。


问题:为了获得不在一行中的文件(正确格式化 JSON),我应该更改什么?


注意:

供您参考,以下是最新的、更新的代码,它根据下面给出的答案实际工作。

应用程序的结构是:

  • app.ts (entry point)
  • write-json.ts (module that writes package.json)
  • deps.ts
  • .env
  • package.json (this is the output of the application)

deno 命令我 运行 是:

$ deno run --allow-read --allow-env --allow-write app.ts

// app.ts

import { config } from './deps.ts';
import { writePackageJsonFile } from './write-json.ts';

config({ export: true, safe: true });

const packageName: string = Deno.env.get('PACKAGE_NAME')!;
const content: string = Deno.env.get('PACKAGE_CONTENTS')!;
if(!packageName || !content){
  throw new Error("You must provide a valid file name and file content");
}

try {
  const parsedContent: any = JSON.parse(content);
  console.log(parsedContent)
  await writePackageJsonFile(packageName, parsedContent);
} catch (_) {
  throw new Error("The content provided for the file is not a valid JSON object");
}

// deps.ts

import { config } from 'https://deno.land/x/dotenv/mod.ts';
 
export { config };

//写-json.ts

import { writeJson, writeJsonSync } from 'https://deno.land/x/jsonfile/mod.ts';

export async function writePackageJsonFile(filename: string, contents: any) {
  await writeJson(filename, contents, { spaces: 2 });
}

//.env

PACKAGE_NAME="package.json"
PACKAGE_CONTENTS='{"name": "app", "version": "1.0.0", "scripts": { "start": "METEOR_SETTINGS=$(cat settings.json) node main.js" } }'

尝试 await writePackageJsonFile(packageName, JSON.parse(packageContents));,并将 contents: string 更改为 contents: any,以便它接受一个对象(或重新创建对象结构并分配该类型)。

你不需要任何库来做到这一点,Deno 有内置的函数来编写和格式化 JSON 就像下面这样简单

// Use whatever package you want here to read the .env file

const file_name = Deno.env.get('PACKAGE_NAME');
const content = Deno.env.get('PACKAGE_CONTENTS');

if(!file_name || !content){
  throw new Error("You must provide a valid file name and file content");
}

let json_object;
try{
  json_object = JSON.parse(content);
}catch(_){
  throw new Error("The content provided for the file is not a valid JSON object");
}

// Using two spaces to format
await Deno.writeTextFile(
  new URL(file_name, import.meta.url),
  JSON.stringify(json_object, null, 2),
);

// Using tab to format
await Deno.writeTextFile(
  new URL(file_name, import.meta.url),
  JSON.stringify(json_object, null, "\t"),
);

您可以在以下内容中阅读有关 Deno 内置函数的更多信息 link https://doc.deno.land/builtin/stable

关于 JSON.stringify 这里 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

这是工作代码

import { config } from 'https://deno.land/x/dotenv/mod.ts';
import { writeJson } from 'https://deno.land/x/jsonfile/mod.ts';

// replace with correct type
type PackageJson = object

config({ export: true, safe: true });

async function writePackageJsonFile(filename: string, contents: PackageJson) {
  await writeJson(filename, contents, { spaces: 2 });
}

const packageName: string = Deno.env.get('PACKAGE_NAME')!;
const content: string = Deno.env.get('PACKAGE_CONTENTS')!;

const parsedContent: PackageJson = JSON.parse(content);
await writePackageJsonFile(packageName, parsedContent);
PACKAGE_NAME="package.json"
PACKAGE_CONTENTS='{"name": "app", "version": "1.0.0", "scripts": { "start": "METEOR_SETTINGS=$(cat settings.json) node main.js" } }'

问题是 writeJson 将对象作为要写入的内容而不是字符串。

因此,在最原始的问题中,您需要更改引号并解析 packageContents 并将解析的内容传递给 writePackageJsonFile 函数。