From 1216125b55849dfe24b944edf3560b990e5233c2 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 7 Apr 2020 04:18:31 +0800 Subject: [PATCH 1/2] feat: better scripts order for follow num run order --- index.js | 25 ++----------------------- package.json | 7 ++++--- tests/scripts.js | 14 +++++++------- tests/snapshots/cli.js.snap | Bin 1863 -> 1866 bytes 4 files changed, 13 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index fca5fe8e..226b0454 100755 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const detectIndent = require('detect-indent') const detectNewline = require('detect-newline').graceful const gitHooks = require('git-hooks-list') const isPlainObject = require('is-plain-obj') +const sortPackageJsonScripts = require('sort-package-json-scripts').default const hasOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property) @@ -132,29 +133,7 @@ const defaultNpmScripts = new Set([ ]) const sortScripts = onObject(scripts => { - const names = Object.keys(scripts) - const prefixable = new Set() - - const keys = names - .map(name => { - const omitted = name.replace(/^(?:pre|post)/, '') - if (defaultNpmScripts.has(omitted) || names.includes(omitted)) { - prefixable.add(omitted) - return omitted - } - return name - }) - .sort() - - const order = keys.reduce( - (order, key) => - order.concat( - prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key], - ), - [], - ) - - return sortObjectKeys(scripts, order) + return sortPackageJsonScripts(scripts) }) // fields marked `vscode` are for `Visual Studio Code extension manifest` only diff --git a/package.json b/package.json index 89df0e08..164a1fe6 100644 --- a/package.json +++ b/package.json @@ -27,10 +27,10 @@ ], "scripts": { "lint": "eslint .", - "semantic-release": "semantic-release", - "sort-package-json": "node cli.js package.json --check", "test": "ava", "test-coverage": "nyc ava", + "semantic-release": "semantic-release", + "sort-package-json": "node cli.js package.json --check", "update-snapshot": "ava -u && node cli.js package.json" }, "husky": { @@ -76,7 +76,8 @@ "git-hooks-list": "1.0.3", "globby": "11.0.0", "is-plain-obj": "2.1.0", - "sort-object-keys": "^1.1.3" + "sort-object-keys": "^1.1.3", + "sort-package-json-scripts": "^1.0.3" }, "devDependencies": { "@commitlint/cli": "8.3.5", diff --git a/tests/scripts.js b/tests/scripts.js index 0acde8b5..98db5ebe 100644 --- a/tests/scripts.js +++ b/tests/scripts.js @@ -18,19 +18,19 @@ const fixture = { } const expect = { + start: 'node server.js', + pretest: 'xyz', + test: 'node test.js', + posttest: 'abc', postinstall: 'echo "Installed"', - multiply: '2 * 3', - 'pre-fetch-info': 'foo', prepare: 'npm run build', + multiply: '2 * 3', preprettier: 'echo "not pretty"', prettier: 'prettier -l "**/*.js"', postprettier: 'echo "so pretty"', - start: 'node server.js', - pretest: 'xyz', - test: 'node test.js', - posttest: 'abc', + 'pre-fetch-info': 'foo', prewatch: 'echo "about to watch"', - watch: 'watch things', + watch: 'watch things' } for (const field of ['scripts', 'betterScripts']) { diff --git a/tests/snapshots/cli.js.snap b/tests/snapshots/cli.js.snap index 3efb5c67e6ea27283d2c10a306ff7faeb7375c74..14d796914e4f8e160883479c6f347b72cf7771e5 100644 GIT binary patch literal 1866 zcmV-Q2etS?RzVuqm%1Gz)fFFalaG5BgZNi1A*6BFlt@kC2OD!29-Me&NLn@J zNAu-`44XH1)!T8C{(c;q8C_Hszk%|juPO;?s`+KmwV5T;k}M-{DG!~UOZm}=KtevO zyr9)wd@)LTJ?!zZhKdBrkDgT%@@trC@z~Ri`%&!olt{T72AUUr*Zn(OY@}om_gk*0@3=hL4sakYhVYL}R5l)GV ztL6%u<8;V!eeqI#sWD-R)m*MMS+!QP#bz|bsQAeDB8UTA^iXW7L=?Hq)qC%FFJt9$ zTy~s}e@CU&#+K`s8Dl4CW4RmYguCmwyT`_Jmsc1qRufLZNKQyfNQzah7X4OPiK)!! zI0Q{h>K>Bf7jC6^&ZQKog?ZkS>Td~TxALzPTliPo7l`2_lF3~1ym+5WAi;V^?rwdNP z~m|3G+mfow?F+)Xm>yo&m(jK6X#>>;}+=+iCxotBy>IuumU^%Qu86= zhwEdy?;-n+0wIQ_`R74M2J(UBU9fK#nr`rK55I%(?gK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qgtsXl9jzi{MpoU`{L}ddwubIaYD;?gAmyWTLJ0X~ zn0ndxGcEhmYKx;AjJuCeKDuKFAzS7}F50rOaof4SZ|(U#{Jfs>(N`l08MUCO=8gE5 z{(Kyf6kYE=suPB*+bAD>H;oWE3Ff~; zs-lVv{GSQT1uB4{G$?`w-2mb+?pPT?6?#j#m5+Yb zC8{bjRokj8M*Ed>U9JQnSXE);K3w()aXA$}R5`_B(XXL0$B{;(;Yq_mh!&CLB9dY( z2noO}UHKCNy5O8E2S;=&zjthe+Ja&YN+hnDt89+blvVoD75Z{x;&Q9GQmszWn#;5)+Ld}+=`y21TV|>-YE4#c zrP-#nnk_b?AyLgQ_BW(=7s!Fy%`(wq6;k)iISFI+DqMD)PJC0X)yA$iTC65>W$Ywv zY)WEsVsdN(7p>HtYZLz&k*LSerpE+LVdyH7q{K z`3~@xDV4Ne8L#Xx8UJ14XflpCxqIWpvYx0sZ>ip(OL59n&4p5(L+Vt{qEnqL+N#xC zjIJhKfz&O)8Q=lq$pAfHj!qY71X|^uEueoqeTeH<)K`yFzb-M=Crk8sBi8Ds5gUYO zTElP)B{p0n9mo<~V;;*jN!CHg+eE{#;{s?&E*YV_x_58f zZgekm=-%VF!11migQfc^AiM&U3(Y@cT;P>?+Z+3~j>cc(^;Dq0Tl_3!Ivv;ve9s;N z#Gthw2S6NTZ!Zl20!_I8HsJ%Sd3#1;uITdob5nvzKryhU8_iqz?EKl;CD{ve3+EM< zeMErpA89aYJr zbPGUO08|6pyV0^qNSEETo#}5)KR^PHxEBG?b4@_gKGXCSH<}LRl}AV_YbHArgaW~7 z%YfHe`mOR_z`GjRi9r$xCw;7Gvmd5MzBlu zQHraNq=ASkkOz*;i6dRfP~Ck Date: Tue, 7 Apr 2020 21:19:29 +0800 Subject: [PATCH 2/2] chore: reduce code --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 226b0454..c2a362f0 100755 --- a/index.js +++ b/index.js @@ -132,9 +132,7 @@ const defaultNpmScripts = new Set([ 'version', ]) -const sortScripts = onObject(scripts => { - return sortPackageJsonScripts(scripts) -}) +const sortScripts = onObject(sortPackageJsonScripts) // fields marked `vscode` are for `Visual Studio Code extension manifest` only // https://code.visualstudio.com/api/references/extension-manifest