11#!/usr/bin/env node
2- import { globSync } from 'tinyglobby'
32import fs from 'node:fs'
4- import getStdin from 'get-stdin'
3+ import streamConsumers from 'node:stream/consumers'
4+ import { parseArgs } from 'node:util'
5+ import { globSync } from 'tinyglobby'
56import sortPackageJson from './index.js'
67import Reporter from './reporter.js'
78
@@ -30,6 +31,32 @@ If file/glob is omitted, './package.json' file will be processed.
3031 )
3132}
3233
34+ function parseCliArguments ( ) {
35+ const { values : options , positionals : patterns } = parseArgs ( {
36+ options : {
37+ check : { type : 'boolean' , short : 'c' , default : false } ,
38+ quiet : { type : 'boolean' , short : 'q' , default : false } ,
39+ stdin : { type : 'boolean' , default : false } ,
40+ ignore : {
41+ type : 'string' ,
42+ short : 'i' ,
43+ multiple : true ,
44+ default : [ 'node_modules/**' ] ,
45+ } ,
46+ version : { type : 'boolean' , short : 'v' , default : false } ,
47+ help : { type : 'boolean' , short : 'h' , default : false } ,
48+ } ,
49+ allowPositionals : true ,
50+ strict : true ,
51+ } )
52+
53+ if ( patterns . length === 0 ) {
54+ patterns [ 0 ] = 'package.json'
55+ }
56+
57+ return { options, patterns }
58+ }
59+
3360function sortPackageJsonFile ( file , reporter , isCheck ) {
3461 const original = fs . readFileSync ( file , 'utf8' )
3562 const sorted = sortPackageJson ( original )
@@ -46,6 +73,7 @@ function sortPackageJsonFile(file, reporter, isCheck) {
4673
4774function sortPackageJsonFiles ( patterns , { ignore, ...options } ) {
4875 const files = globSync ( patterns , { ignore } )
76+
4977 const reporter = new Reporter ( files , options )
5078 const { isCheck } = options
5179
@@ -60,65 +88,44 @@ function sortPackageJsonFiles(patterns, { ignore, ...options }) {
6088}
6189
6290async function sortPackageJsonFromStdin ( ) {
63- process . stdout . write ( sortPackageJson ( await getStdin ( ) ) )
91+ process . stdout . write (
92+ sortPackageJson ( await streamConsumers . text ( process . stdin ) ) ,
93+ )
6494}
6595
6696function run ( ) {
67- const cliArguments = process . argv
68- . slice ( 2 )
69- . map ( ( arg ) => arg . split ( '=' ) )
70- . flat ( )
71-
72- if (
73- cliArguments . some ( ( argument ) => argument === '--help' || argument === '-h' )
74- ) {
97+ let options , patterns
98+ try {
99+ ; ( { options, patterns } = parseCliArguments ( ) )
100+ } catch ( error ) {
101+ process . exitCode = 2
102+ console . error ( error . message )
103+ if (
104+ error . code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION' ||
105+ error . code === 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
106+ ) {
107+ console . error ( `Try 'sort-package-json --help' for more information.` )
108+ }
109+ return
110+ }
111+
112+ if ( options . help ) {
75113 return showHelpInformation ( )
76114 }
77115
78- if (
79- cliArguments . some (
80- ( argument ) => argument === '--version' || argument === '-v' ,
81- )
82- ) {
116+ if ( options . version ) {
83117 return showVersion ( )
84118 }
85119
86- if ( cliArguments . some ( ( argument ) => argument === '-- stdin' ) ) {
120+ if ( options . stdin ) {
87121 return sortPackageJsonFromStdin ( )
88122 }
89123
90- const patterns = [ ]
91- const ignore = [ ]
92- let isCheck = false
93- let shouldBeQuiet = false
94-
95- let lastArg
96- for ( const argument of cliArguments ) {
97- if ( lastArg === '--ignore' || lastArg === '-i' ) {
98- ignore . push ( argument )
99- lastArg = undefined
100- continue
101- }
102- if ( argument === '--check' || argument === '-c' ) {
103- isCheck = true
104- } else if ( argument === '--quiet' || argument === '-q' ) {
105- shouldBeQuiet = true
106- } else if ( argument === '--ignore' || argument === '-i' ) {
107- lastArg = argument
108- } else {
109- patterns . push ( argument )
110- }
111- }
112-
113- if ( ! patterns . length ) {
114- patterns [ 0 ] = 'package.json'
115- }
116-
117- if ( ! ignore . length ) {
118- ignore [ 0 ] = 'node_modules'
119- }
120-
121- sortPackageJsonFiles ( patterns , { ignore, isCheck, shouldBeQuiet } )
124+ sortPackageJsonFiles ( patterns , {
125+ ignore : options . ignore ,
126+ isCheck : options . check ,
127+ shouldBeQuiet : options . quiet ,
128+ } )
122129}
123130
124131run ( )
0 commit comments