11import test from 'ava'
2+ import sortPackageJson from '../index.js'
23import { macro } from './_helpers.js'
34
45const fixture = {
@@ -35,37 +36,77 @@ const expectAllSorted = {
3536 watch : 'watch things' ,
3637}
3738
38- const expectPreAndPostSorted = {
39- pretest : 'xyz' ,
40- test : 'node test.js' ,
41- posttest : 'abc' ,
42- multiply : '2 * 3' ,
43- prewatch : 'echo "about to watch"' ,
44- watch : 'watch things' ,
45- preinstall : 'echo "Installing"' ,
46- postinstall : 'echo "Installed"' ,
47- start : 'node server.js' ,
48- preprettier : 'echo "not pretty"' ,
49- prettier : 'prettier -l "**/*.js"' ,
50- postprettier : 'echo "so pretty"' ,
51- prepare : 'npm run build' ,
52- 'pre-fetch-info' : 'foo' ,
53- }
54-
5539for ( const field of [ 'scripts' , 'betterScripts' ] ) {
56- test ( `${ field } when npm-run-all is not a dev dependency` , macro . sortObject , {
40+ test ( `${ field } when npm-run-all is NOT a dev dependency` , macro . sortObject , {
5741 value : { [ field ] : fixture } ,
5842 expect : { [ field ] : expectAllSorted } ,
5943 } )
60- test ( `${ field } when npm-run-all is a dev dependency` , macro . sortObject , {
61- value : {
62- [ field ] : fixture ,
63- devDependencies : { 'npm-run-all' : '^1.0.0' } ,
64- } ,
65- expect : {
66- [ field ] : expectPreAndPostSorted ,
67- devDependencies : { 'npm-run-all' : '^1.0.0' } ,
44+
45+ test (
46+ `${ field } when npm-run-all IS a dev dependency, but is NOT used in scripts` ,
47+ macro . sortObject ,
48+ {
49+ value : {
50+ [ field ] : { z : 'z' , a : 'a' } ,
51+ devDependencies : { 'npm-run-all' : '^1.0.0' } ,
52+ } ,
53+ expect : {
54+ [ field ] : { a : 'a' , z : 'z' } ,
55+ devDependencies : { 'npm-run-all' : '^1.0.0' } ,
56+ } ,
6857 } ,
58+ )
59+ }
60+
61+ // `run-s` command
62+ function sortScriptsWithNpmRunAll ( script ) {
63+ const packageJson = {
64+ scripts : { z : 'z' , a : 'a' , maybeRunS : script } ,
65+ devDependencies : { 'npm-run-all' : '^1.0.0' } ,
66+ }
67+
68+ return Object . keys ( sortPackageJson ( packageJson ) . scripts )
69+ }
70+ const sortedScripts = [ 'a' , 'maybeRunS' , 'z' ]
71+ const unsortedScripts = [ 'z' , 'a' , 'maybeRunS' ]
72+ for ( const { script, expected } of [
73+ // Should NOT sort
74+ { script : 'run-s "lint:*"' , expected : unsortedScripts } ,
75+ { script : 'npm-run-all -s "lint:*"' , expected : unsortedScripts } ,
76+ { script : 'npm-run-all --sequential "lint:*"' , expected : unsortedScripts } ,
77+ { script : 'npm-run-all --serial "lint:*"' , expected : unsortedScripts } ,
78+ { script : 'npm-run-all "lint:*" --sequential' , expected : unsortedScripts } ,
79+ { script : 'foo&&npm-run-all --serial "lint:*"' , expected : unsortedScripts } ,
80+ { script : 'foo||npm-run-all --serial "lint:*"' , expected : unsortedScripts } ,
81+ { script : 'foo|npm-run-all --serial "lint:*"' , expected : unsortedScripts } ,
82+ { script : 'foo>npm-run-all --serial "lint:*"' , expected : unsortedScripts } ,
83+ { script : 'foo<npm-run-all --serial "lint:*"' , expected : unsortedScripts } ,
84+ {
85+ script : 'cross-env FOO=1 npm-run-all --serial "lint:*"' ,
86+ expected : unsortedScripts ,
87+ } ,
88+ { script : 'npm-run-all "lint:*" --serial&&foo' , expected : unsortedScripts } ,
89+ { script : 'npm-run-all "lint:*" --serial|foo' , expected : unsortedScripts } ,
90+ { script : 'npm-run-all "lint:*" --serial||foo' , expected : unsortedScripts } ,
91+ { script : 'npm-run-all "lint:*" --serial>foo' , expected : unsortedScripts } ,
92+ { script : 'npm-run-all "lint:*" --serial<foo' , expected : unsortedScripts } ,
93+ { script : 'npm-run-all --serial "lint:*"&&foo' , expected : unsortedScripts } ,
94+ { script : 'npm-run-all "lint:*" --serial;foo' , expected : unsortedScripts } ,
95+ { script : '(npm-run-all "lint:*" --serial)|foo' , expected : unsortedScripts } ,
96+
97+ // Should sort
98+ { script : 'run-s lint:a lint:b' , expected : sortedScripts } ,
99+ { script : 'not-run-s *' , expected : sortedScripts } ,
100+ { script : 'npm-run-all * --serial!' , expected : sortedScripts } ,
101+ { script : 'looks like && run-s-but-its-not *' , expected : sortedScripts } ,
102+ { script : 'npm-run-all *' , expected : sortedScripts } ,
103+ { script : 'npm-run-all --parallel watch:*' , expected : sortedScripts } ,
104+
105+ // False positive
106+ { script : 'rm -rf dist/* && run-s lint:a lint:b' , expected : unsortedScripts } ,
107+ ] ) {
108+ test ( `command: '${ script } '` , ( t ) => {
109+ t . deepEqual ( sortScriptsWithNpmRunAll ( script ) , expected )
69110 } )
70111}
71112
@@ -80,7 +121,7 @@ for (const field of ['scripts', 'betterScripts']) {
80121 devDependencies : { 'npm-run-all2' : '^1.0.0' } ,
81122 } ,
82123 expect : {
83- [ field ] : expectPreAndPostSorted ,
124+ [ field ] : expectAllSorted ,
84125 devDependencies : { 'npm-run-all2' : '^1.0.0' } ,
85126 } ,
86127 } )
0 commit comments