{"id":10006807,"date":"2022-11-04T15:35:17","date_gmt":"2022-11-04T07:35:17","guid":{"rendered":"https:\/\/ityet.com\/?p=10006807"},"modified":"2022-11-04T15:35:17","modified_gmt":"2022-11-04T07:35:17","slug":"%e4%bd%bf%e7%94%a8nodejs%e4%bf%ae%e6%94%b9%e9%a1%b9%e7%9b%aepackage-json%e7%89%88%e6%9c%ac%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006807","title":{"rendered":"\u4f7f\u7528nodejs\u4fee\u6539\u9879\u76eepackage.json\u7248\u672c\u53f7"},"content":{"rendered":"<blockquote><p>\u80cc\u666f\uff1a\u81ea\u5df1\u7684\u9879\u76ee\u5728\u90e8\u7f72\u4e0a\u4f20\u4e4b\u524d\u9700\u8981\u66f4\u65b0\u7248\u672c\u53f7\u624d\u80fd\u6210\u529f\u90e8\u7f72<\/p><\/blockquote>\n<h4>\u4ee3\u7801<\/h4>\n<p>\u8be6\u7ec6\u4ee3\u7801\u5982\u4e0b\uff08nodejs\u7b80\u964b\uff09; \u6ce8\u91ca\u4e86<code>\u81ea\u52a8\u521b\u5efa\u5206\u652f+\u63d0\u4ea4\u52a8\u4f5c<\/code>\uff1b\u53ef\u6839\u636e\u9700\u6c42\u81ea\u884c\u4f7f\u7528<\/p>\n<pre><code class=\"javascript\">\/\/build.js\u6587\u4ef6\nvar exec = require('child_process').exec \/\/ \u5f02\u6b65\u5b50\u8fdb\u7a0b\nvar fs = require('fs')\n\nvar packageJSON = require('.\/package.json')\n\/** package.json\u6587\u4ef6\u7684version\u53c2\u6570 *\/\nvar version = packageJSON.version\n\/** \u547d\u4ee4\u884c\u7684\u6240\u6709\u53c2\u6570 *\/\nvar options = process.argv\n\/** \u547d\u4ee4\u884c\u7684type\u53c2\u6570 *\/\nvar type = null\n\/** \u65b0\u7684version\u53c2\u6570 *\/\nvar newVersion = null\n\n\/\/\u5224\u65ad\u547d\u4ee4\u884c\u662f\u5426\u5b58\u5728type\u53c2\u6570\u6216version\u53c2\u6570\u8fdb\u884c\u903b\u8f91\u5904\u7406\nfor (let i = 0; i &lt; options.length; i++) {\n  if (options[i].indexOf('type') &gt; -1) {\n    \/\/\u5b58\u5728type\u53c2\u6570\n    type = options[i].split('=')[1]\n  } else if (options[i].indexOf('version') &gt; -1) {\n    \/\/\u5b58\u5728version\u53c2\u6570\n    newVersion = options[i].split('=')[1]\n  } else {\n    \/\/code\n  }\n}\n\nif (newVersion) {\n  \/\/\u5b58\u5728\u8bbe\u7f6eversion\u53c2\u6570\u5219\u6539\u53d8\u539f\u6765\u7684version\n  version = newVersion\n} else if (type) {\n  \/\/\u4e0d\u8bbe\u7f6eversion\u5219\u6839\u636etype\u6765\u8fdb\u884c\u4fee\u6539version\n  version = handleType(version, type)\n} else {\n  version = null\n  console.log('-----------\u6ca1\u6709\u6539\u53d8version-----------')\n}\n\n\/\/\u4fee\u6539\u4e86version\u5219\u5199\u5165\nif (version) {\n  packageJSON.version = version\n  \/\/\u540c\u6b65\u5199\u5165package.json\u6587\u4ef6\n  fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 2))\n  console.log('-----------\u66f4\u65b0package\u7684version\u4e3a\uff1a%s\u53c2\u6570\u6210\u529f-----------', version)\n  \/\/ handleGitAdd('.\/package.json')\n  \/\/ pullRemote()\n}\n\n\/**\n * \u6839\u636e\u5206\u652f\u7c7b\u578b\u5904\u7406\u7248\u672c\u53f7version\n * @param {string} oldVersion \u65e7\u7684\u7248\u672c\u53f7\n * @param {string} type \u5206\u652f\u7c7b\u578b\n * @private\n *\/\nfunction handleType(oldVersion, type) {\n  var oldVersionArr = oldVersion.split('.')\n  \/\/\u7248\u672c\u53f7\u7b2c\u4e00\u4f4d \u5982\uff1a1.2.3 \u5219\u4e3a 1\n  var firstNum = +oldVersionArr[0]\n  \/\/\u7248\u672c\u53f7\u7b2c\u4e8c\u4f4d \u5982\uff1a1.2.3 \u5219\u4e3a 2\n  var secondNum = +oldVersionArr[1]\n  \/\/\u7248\u672c\u53f7\u7b2c\u4e09\u4f4d \u5982\uff1a1.2.3 \u5219\u4e3a 3\n  var thirdNum = +oldVersionArr[2]\n  switch (type) {\n    case 'release':\n      \/\/release\u5206\u652f\u7684\u5904\u7406\u903b\u8f91\n      ++secondNum\n      thirdNum = 0\n      break\n\n    case 'hotfix':\n      \/\/hotfix\u5206\u652f\u7684\u5904\u7406\u903b\u8f91\n      ++thirdNum\n      break\n\n    default:\n      \/\/ \u9ed8\u8ba4\u6309\u7167\u6700\u5c0f\u7248\u672c\u5904\u7406\n      ++thirdNum\n      break\n  }\n\n  return firstNum + '.' + secondNum + '.' + thirdNum\n}\n\n\/\/ \/**\n\/\/  * \u5c06package.json\u63a8\u9001\u5230\u8fdc\u7a0b\u65b0\u521b\u5efa\u7684\u5206\u652f\n\/\/  *\/\n\/\/ function pullRemote() {\n\/\/   var branch = type + '\/' + version\n\/\/   \/\/\u521b\u5efa\u65b0\u5206\u652f\n\/\/   handleGitCreate(branch)\n\/\/ }\n\n\/\/ \/**\n\/\/  * \u521b\u5efa\u65b0\u5206\u652f\n\/\/  * @param {string} branch \u5206\u652f\u540d\n\/\/  *\/\n\/\/ function handleGitCreate(branch) {\n\/\/   exec('git branch ' + branch, function (error, stdout, stderr) {\n\/\/     console.log('-----------\u521b\u5efa\u65b0\u5206\u652f:%s DONE-----------', branch)\n\/\/     \/\/\u5207\u6362\u5206\u652f\n\/\/     handleGitCheckOut(branch)\n\/\/   })\n\/\/ }\n\n\/\/ \/**\n\/\/  * \u5207\u6362\u5206\u652f\n\/\/  * @param {string} branch \u5206\u652f\u540d\n\/\/  *\/\n\/\/ function handleGitCheckOut(branch) {\n\/\/   exec('git checkout ' + branch, function (error, stdout, stderr) {\n\/\/     console.log('-----------\u5207\u6362\u65b0\u5206\u652f:%s DONE-----------', branch)\n\/\/     \/\/\u6dfb\u52a0\u4fee\u6539\u6587\u4ef6\n\/\/     handleGitAdd('.\/package.json')\n\/\/   })\n\/\/ }\n\n\/**\n * \u6dfb\u52a0\u4fee\u6539\u6587\u4ef6\n * @param {string} filePath \u6587\u4ef6\u8def\u5f84\n *\/\nfunction handleGitAdd(filePath) {\n  exec('git add ' + filePath, function (error, stdout, stderr) {\n    console.log('[\u6dfb\u52a0\u4fee\u6539\u6587\u4ef6\u8f93\u51fa\uff1a%s]', stdout)\n    \/\/\u63d0\u4ea4\u6587\u4ef6\n    handleGitCommit('\u66f4\u65b0package.json\u6587\u4ef6')\n  })\n}\n\n\/**\n * \u63d0\u4ea4\u6587\u4ef6\n * @param {string} prompt commit\u6587\u5b57\u5907\u6ce8\n *\/\nfunction handleGitCommit(prompt) {\n  \/\/ var branch = type + '\/' + version\n  exec('git commit -m &quot;' + prompt + '&quot;', function (error, stdout, stderr) {\n    console.log('[\u63d0\u4ea4\u4fee\u6539\u6587\u4ef6\u8f93\u51fa\uff1a%s]', stdout)\n    \/\/\u63a8\u9001\u5206\u652f\n    handleGitPush()\n  })\n}\n\n\/**\n * \u63a8\u9001\u5206\u652f\n *\/\nfunction handleGitPush() {\n  exec('git push ', function (error, stdout, stderr) {\n    console.log('[\u63a8\u9001\u81f3\u5206\u652f:%s\u8f93\u51fa\uff1a%s]', stdout || error || stderr)\n    console.log('-----------\u63d0\u4ea4\u4fee\u6539\u6587\u4ef6\u5b8c\u6210-----------')\n  })\n}\n\n\/**\n * \u81ea\u52a8\u751f\u6210\u7248\u672c\u53f7\u811a\u672c\u601d\u8def\n * 1.\u83b7\u53d6\u4f20\u8fdb\u6765\u7684\u53c2\u6570 \u221a\n * 2.\u6839\u636e\u53c2\u6570\u8fdb\u884c\u903b\u8f91\u5904\u7406 \u221a\n * 3.\u83b7\u53d6package.json\u4e2d\u7684version\u53c2\u6570 \u221a\n * 4.\u4fee\u6539version\u7684\u503c\u5199\u5165package.json\u6587\u4ef6 \u221a\n * 5.git\u63d0\u4ea4package.json\u6587\u4ef6 \u221a\n *\/\n<\/code><\/pre>\n<h4>\u4f7f\u7528<\/h4>\n<ul>\n<li><code>node build.js --type=hotfix <\/code> \u53ea\u4fee\u6539\u6700\u5c0f\u7248\u672c<\/li>\n<li><code>node build.js --type=release<\/code> \u4fee\u6539\u529f\u80fd\u7248\u672c<\/li>\n<li><code>node build.js --type=release --version=0.0.8<\/code> \u4fee\u6539\u4e3a\u6307\u5b9a\u7248\u672c<\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042741814\">https:\/\/segmentfault.com\/a\/1190000042741814<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f\uff1a\u81ea\u5df1\u7684\u9879\u76ee\u5728\u90e8\u7f72\u4e0a\u4f20\u4e4b\u524d\u9700\u8981\u66f4\u65b0\u7248\u672c\u53f7\u624d\u80fd\u6210\u529f\u90e8\u7f72 \u4ee3\u7801 \u8be6\u7ec6\u4ee3\u7801\u5982\u4e0b\uff08nodejs\u7b80\u964b\uff09; \u6ce8\u91ca\u4e86\u81ea\u52a8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006807","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006807"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006807\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}