{"id":698,"date":"2023-11-16T09:01:01","date_gmt":"2023-11-16T01:01:01","guid":{"rendered":""},"modified":"2023-11-16T09:01:01","modified_gmt":"2023-11-16T01:01:01","slug":"\u4eceJSON\u5230\u63a5\u53e3\u6587\u6863\uff0c\u6559\u4f60\u5982\u4f55\u4f18\u96c5\u751f\u6210\u7ec4\u4ef6API\uff01","status":"publish","type":"post","link":"https:\/\/mushiming.com\/698.html","title":{"rendered":"\u4eceJSON\u5230\u63a5\u53e3\u6587\u6863\uff0c\u6559\u4f60\u5982\u4f55\u4f18\u96c5\u751f\u6210\u7ec4\u4ef6API\uff01"},"content":{"rendered":"

\u80cc\u666f<\/h1>\n

\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u7ec4\u4ef6\u5e93\u7684\u9879\u76ee\uff0c\u5bf9\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u4e8c\u6b21\u5c01\u88c5\u5e76\u66b4\u9732\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u6bcf\u4e2a\u7ec4\u4ef6\u90fd\u6709\u4e00\u4efd\u9ed8\u8ba4\u6570\u636e\u7684 JSON\u683c\u5f0f<\/code>\u3002\u5982\u4f55\u6839\u636e\u9ed8\u8ba4\u6570\u636e\u751f\u6210\u7ec4\u4ef6 API \u6587\u6863<\/code>\uff1f\u672c\u6587\u5c06\u6559\u4f60\u5982\u4f55\u4f7f\u7528 json-to-ts\u3001prettier\u3001ShellJS\u3001TypeDoc\u3001fs-extra\u3001ora\u3001path \u7b49 JS \u5e93\u4f18\u96c5\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002<\/p>\n

\u65b9\u6848<\/h2>\n

\u65b9\u6848\u975e\u5e38\u76f4\u63a5\uff0c\u53ea\u9700\u8981\u4e24\u6b65\uff1a<\/p>\n

    \n
  1. JSON \u8f6c TypeScript<\/li>\n
  2. \u901a\u8fc7 TypeScript \u751f\u6210\u7ec4\u4ef6 API \u6587\u6863<\/li>\n<\/ol>\n
    \n

    \u539f\u672c\u4ee5\u4e3a\u8fd9\u4e2a\u9700\u6c42\u5df2\u7ecf\u6709\u73b0\u6210\u7684\u8f6e\u5b50\uff0c\u4f46\u627e\u4e86\u4e00\u4e0b\u5348\u90fd\u6ca1\u6709\u627e\u5230\uff0c\u53ea\u80fd\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a<\/p>\n<\/blockquote>\n

    \u51c6\u5907\u5de5\u4f5c<\/h2>\n

    \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4ee5\u4e0b\u4f9d\u8d56\uff1a<\/p>\n

    npm install json-to-ts prettier shelljs typedoc fs-extra ora path --save-dev\n<\/code><\/pre>\n
    \n

    \u6211\u4eec\u6240\u6709\u7684\u811a\u672c\u6587\u4ef6\u90fd\u7528 ts \u6765\u5199\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5f88\u591a \u7c7b\u578b \u548c \u5f15\u7528 \u95ee\u9898<\/p>\n<\/blockquote>\n

    \u751f\u6210 TypeScript \u63a5\u53e3<\/h2>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 json-to-ts \u5e93 \u5c06 JSON \u6570\u636e\u8f6c\u6362\u4e3a TypeScript \u63a5\u53e3\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a json-to-interface.ts<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

    import<\/span> * as<\/span> jsonToTs from<\/span> \"json-to-ts\"<\/span>;\nimport<\/span> * as<\/span> fs from<\/span> \"fs-extra\"<\/span>;\nimport<\/span> * as<\/span> path from<\/span> \"path\"<\/span>;\nimport<\/span> prettier from<\/span> \"prettier\"<\/span>;\n\n\/\/ \u83b7\u53d6\u76ee\u6807\u6587\u4ef6\u5939\u4e0b\u7684\u5b9a\u4e49\u7684 \u9ed8\u8ba4\u6570\u636e\u6587\u4ef6<\/span>\nexport<\/span> const<\/span> getTargetFiles<\/span> = (targetPath<\/span>) => {\n  let<\/span> components = [];\n\n  const<\/span> files = fs.readdirSync<\/span>(targetPath);\n  files.forEach<\/span>((item, index<\/span>) =><\/span> {\n    let<\/span> stat = fs.statSync<\/span>(`${targetPath}<\/span>\/`<\/span> + item);\n\n    let<\/span> isBool = true<\/span>;\n    try<\/span> {\n      fs.statSync<\/span>(`${targetPath}<\/span>\/`<\/span> + item + \"\/defaultProps.ts\"<\/span>);\n    } catch<\/span> (error) {\n      isBool = false<\/span>;\n    }\n\n    if<\/span> (stat.isDirectory<\/span>() === true<\/span> && isBool) {\n      components.push<\/span>(item);\n    }\n  });\n\n  return<\/span> components;\n};\n\nconst<\/span> transform<\/span> = (<\/span>) => {\n   \/\/ \u83b7\u53d6\u5f85\u5904\u7406\u7684\u7ec4\u4ef6\u540d. targetPath \u4e3a\u5b58\u653e\u9ed8\u8ba4\u6570\u636e\u7684\u6587\u4ef6\u5939\u8def\u5f84<\/span>\n  let<\/span> components = getTargetFiles<\/span>(targetPath);\n\n  \/\/ \u5f00\u59cb\u5904\u7406\u7ec4\u4ef6<\/span>\n  components.forEach<\/span>((name<\/span>) =><\/span> {\n    console<\/span>.log<\/span>(`\u5f53\u524d\u5904\u7406\u7ec4\u4ef6\u7684 - ${name}<\/span>`<\/span>);\n\n    const<\/span> jsonFilePath = path.resolve<\/span>(\n      __dirname,\n      `${targetPath}<\/span>\/${name}<\/span>\/defaultProps.ts`<\/span>\n    );\n   \n    \/\/ \u83b7\u53d6\u6570\u636e<\/span>\n    const<\/span> { defaultProps<\/span>: json } = require<\/span>(jsonFilePath);\n\n    \/\/ \u53ef\u4ee5\u5148\u5bf9\u9ed8\u8ba4\u6570\u636e\u505a\u4e00\u4e0b\u524d\u7f6e\u5904\u7406<\/span>\n    \/\/ ...<\/span>\n    \n    \/\/ \u83b7\u53d6 interface \u6570\u636e<\/span>\n    const<\/span> interfaceData<\/span>: string<\/span>[] = JsonToTS<\/span>(json, { rootName<\/span>: \"DefaultProps\"<\/span> });\n    \n    \/\/ \u53ef\u4ee5\u5bf9\u751f\u6210\u7684 interfaceData \u8fdb\u884c\u81ea\u5b9a\u4e49\u914d\u7f6e\u5904\u7406<\/span>\n    \/\/ ...<\/span>\n\n    \/\/ interface \u6587\u4ef6\u8def\u5f84<\/span>\n    let<\/span> filePath = `${targetPath}<\/span>\/${name}<\/span>\/interface.ts`<\/span>;\n    filePath = path.resolve<\/span>(__dirname, filePath);\n\n    \/\/ \u4f7f\u7528 Prettier \u683c\u5f0f\u5316\u4ee3\u7801<\/span>\n    const<\/span> formatData = prettier.format<\/span>(interfaceData, {\n      singleQuote<\/span>: true<\/span>,\n      parser<\/span>: \"typescript\"<\/span>,\n    });\n\n    \/\/ \u5199 interface \u6587\u4ef6<\/span>\n    fs.outputFileSync<\/span>(filePath, formatData);\n });\n}\n<\/code><\/pre>\n

    \u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u4ece\u6211\u4eec\u7684\u76ee\u6807\u6587\u4ef6\u5939\u4e2d\u8bfb\u53d6\u540d\u4e3a defaultProps.ts<\/code> \u7684 \u9ed8\u8ba4\u6570\u636e<\/code> \u6587\u4ef6\uff08json \u683c\u5f0f\uff09\uff0c\u7136\u540e\u4f7f\u7528 json-to-ts<\/code> \u5e93\u5c06 JSON \u6570\u636e\u8f6c\u6362\u4e3a TypeScript \u63a5\u53e3\uff0c\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u540d\u4e3a interfaces.ts<\/code> \u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n

    \u7531\u4e8e json-to-ts<\/code> \u63d0\u4f9b\u7684 api \u8f83\u5c11\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u7ed9\u751f\u6210\u7684\u63a5\u53e3\u4e0a\u6dfb\u52a0 \u81ea\u5b9a\u4e49\u7684\u6570\u636e<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386 interfaceData<\/code> \u6765\u52a8\u6001\u6dfb\u52a0<\/p>\n

    interfaceData.map<\/span>(item<\/span> =><\/span> {\n    if<\/span> (item.includes<\/span>(\"interface DefaultProps {\"<\/span>)) {\n      const<\/span> linkName = item.split<\/span>(\" \"<\/span>)[1<\/span>];\n      return<\/span> ` \/** * @module ${title}<\/span> * @see \u70b9\u51fb\u6b64\u5904\u67e5\u770b\u533a\u5757\u8be6\u7ec6 props -> {@link ${linkName}<\/span>} *\/ ${item}<\/span> `<\/span>;\n    }\n    return<\/span> item;\n  })\n<\/code><\/pre>\n

    \u751f\u6210\u7684 interfaces.ts<\/code> \u5185\u5bb9\u5982\u4e0b<\/p>\n

    \"\u4eceJSON\u5230\u63a5\u53e3\u6587\u6863\uff0c\u6559\u4f60\u5982\u4f55\u4f18\u96c5\u751f\u6210\u7ec4\u4ef6API\uff01<\/p>\n

    \u751f\u6210\u63a5\u53e3\u6587\u6863<\/h2>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 TypeDoc \u5e93 \u5c06\u751f\u6210\u7684 TypeScript \u63a5\u53e3\u8f6c\u6362\u4e3a HTML \u6587\u6863\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a generate-doc.ts<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

    \/\/ \u4ece json-to-interface.ts \u4e2d\u5bfc\u51fa\u76f8\u5173\u7684\u65b9\u6cd5\u548c\u5c5e\u6027<\/span>\n...\n\nconst<\/span> generateDoc<\/span> = (<\/span>) => {\n  \/\/ \u83b7\u53d6 typeDoc \u914d\u7f6e\u6587\u4ef6<\/span>\n  const<\/span> typeDocConfigPath = path.resolve<\/span>(__dirname, \"..\/..\/typedoc.json\"<\/span>);\n  let<\/span> configJson = require<\/span>(typeDocConfigPath);\n\n  \/** \u5c06\u5f85\u751f\u6210\u6587\u6863\u7684\u7ec4\u4ef6\u5730\u5740\uff0c\u6ce8\u5165\u5230 typeDoc \u914d\u7f6e\u6587\u4ef6\u4e2d * basePath\u3001name\u3001components\uff1a\u662f\u5728\u4e0a\u4e2a\u9636\u6bb5\u3010\u751f\u6210 TypeScript \u63a5\u53e3\u3011\u83b7\u53d6\u7684 *\/<\/span>\n  configJson[\"entryPoints\"<\/span>] = components.map<\/span>(\n    (name<\/span>) =><\/span> `.${basePath}<\/span>\/${name}<\/span>\/interface.ts`<\/span>\n  );\n\n  \/\/ \u683c\u5f0f\u5316 json \u4ee3\u7801<\/span>\n  configJson = prettier.format<\/span>(JSON<\/span>.stringify<\/span>(configJson), { parser<\/span>: \"json\"<\/span> });\n\n  \/\/ \u66f4\u65b0 typeDoc \u914d\u7f6e\u6587\u4ef6<\/span>\n  fs.writeFileSync<\/span>(typeDocConfigPath, configJson);\n\n  \/\/ \u751f\u6210\u6587\u6863<\/span>\n  shell.exec<\/span>(\"typedoc --tsconfig .\/tsconfig.json\"<\/span>);\n}\n<\/code><\/pre>\n

    \u6700\u7ec8\u751f\u6210\u7684 typedoc.json<\/code> \u5927\u6982\u5982\u4e0b\u56fe\uff1a<\/p>\n

    {<\/span>\n  \"entryPoints\"<\/span>:<\/span> [<\/span>\n    \".\/src\/components\/AboutUs\/interface.ts\"<\/span>,<\/span>\n    \".\/src\/components\/Contact\/interface.ts\"<\/span>,<\/span>\n    ...\n  ]<\/span>,<\/span>\n  \"out\"<\/span>:<\/span> \"demo-docs\"<\/span>,<\/span>\n  \"name\"<\/span>:<\/span> \"demo \u7ec4\u4ef6 api\"<\/span>,<\/span>\n  \"exclude\"<\/span>:<\/span> [<\/span>\n    \"**\/__tests__\/**\/*\"<\/span>,<\/span>\n    \"**\/node_modules\/**\/*\"<\/span>\n  ]<\/span>,<\/span>\n  \"theme\"<\/span>:<\/span> \"default\"<\/span>,<\/span>\n  \"excludeExternals\"<\/span>:<\/span> true<\/span><\/span>,<\/span>\n  \"excludePrivate\"<\/span>:<\/span> true<\/span><\/span>,<\/span>\n  \"excludeProtected\"<\/span>:<\/span> true<\/span><\/span>,<\/span>\n  \"skipErrorChecking\"<\/span>:<\/span> true<\/span><\/span>,<\/span>\n  \"disableSources\"<\/span>:<\/span> true<\/span><\/span>,<\/span>\n  \"readme\"<\/span>:<\/span> \".\/scripts\/generate\/readme.md\"<\/span>\n}<\/span>\n<\/code><\/pre>\n

    \u4fee\u6539 package.json<\/code> \u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u811a\u672c\uff1a<\/p>\n

    \"scripts\"<\/span>:<\/span> {<\/span>\n  \"docs\"<\/span>:<\/span> \"npx tsx .\/generate-doc.ts\"<\/span>,<\/span>\n}<\/span>\n<\/code><\/pre>\n

    \u7136\u540e\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u63a5\u53e3\u6587\u6863\uff1a<\/p>\n

    npm run docs\n<\/code><\/pre>\n

    \u8fd9\u5c06\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u540d\u4e3a demo-docs<\/code> \u7684\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u63a5\u53e3\u6587\u6863\u7684 HTML \u6587\u4ef6\u3002<\/p>\n

    \"\u4eceJSON\u5230\u63a5\u53e3\u6587\u6863\uff0c\u6559\u4f60\u5982\u4f55\u4f18\u96c5\u751f\u6210\u7ec4\u4ef6API\uff01<\/p>\n

    \n

    \u53ef\u4ee5\u5b9a\u5236 \u6587\u6863\u9996\u9875 \u7684\u663e\u793a\u5185\u5bb9\uff0c\u901a\u8fc7\u4e00\u4efd readme.md<\/code> \u6765\u5b9a\u5236<\/p>\n<\/blockquote>\n

    \u6700\u540e<\/h2>\n

    \u901a\u8fc7 json-to-ts<\/code>\u3001TypeDoc<\/code> \u4e24\u4e2a\u7ec4\u4ef6\u5e93\u7684\u914d\u5408\uff0c\u4ee5\u53ca\u591a\u9879\u4ea4\u4e92\u4f18\u5316\uff0c\u8ba9\u4f60\u8f7b\u677e\u751f\u6210\u7ec4\u4ef6 API<\/p>\n","protected":false},"excerpt":{"rendered":"\u4eceJSON\u5230\u63a5\u53e3\u6587\u6863\uff0c\u6559\u4f60\u5982\u4f55\u4f18\u96c5\u751f\u6210\u7ec4\u4ef6API\uff01\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u7ec4\u4ef6\u5e93\u7684\u9879\u76ee\uff0c\u5bf9\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u4e8c\u6b21\u5c01\u88c5\u5e76\u66b4\u9732\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u6bcf\u4e2a\u7ec4\u4ef6...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/698"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=698"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/698\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}