From abc4ca0ec5d39468be1f9ac42a480330597df822 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 00:40:30 -0600 Subject: [PATCH 01/12] feat(cli): added --version and --help options added --version (-V) and --help (-h) options to the CLI for user interface. --- cli.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index b519b18d..f59d604c 100755 --- a/cli.js +++ b/cli.js @@ -1,12 +1,39 @@ #!/usr/bin/env node -import fs from 'node:fs' import { globbySync } from 'globby' +import fs from 'node:fs' import sortPackageJson from './index.js' const isCheckFlag = (argument) => argument === '--check' || argument === '-c' +const isHelpFlag = (argument) => argument === '--help' || argument === '-h' +const isVersionFlag = (argument) => + argument === '--version' || argument === '-V' const cliArguments = process.argv.slice(2) const isCheck = cliArguments.some(isCheckFlag) +const isHelp = cliArguments.some(isHelpFlag) +const isVersion = cliArguments.some(isVersionFlag) + +if (isHelp) { + console.log( + `Usage: sort-package-json [OPTION...] [FILE...] +Sort npm package.json files. Default: ./package.json +Strings passed as files are parsed as globs. + + -c, --check check if FILES are sorted + -q, --quiet don't output success messages + -h, --help display this help and exit + -V, --version display the version and exit + `, + ) + process.exit(0) +} + +if (isVersion) { + const packageBuffer = fs.readFileSync('./package.json') + const { version } = JSON.parse(packageBuffer) + console.log(`sort-package-json ${version}`) + process.exit(0) +} const patterns = cliArguments.filter((argument) => !isCheckFlag(argument)) From 582095f8f8611541c8ede0ff5a8cbded1a195c6c Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 01:34:18 -0600 Subject: [PATCH 02/12] fix(cli): updated the version option Fixed a major bug with the version option in order to work from any directory --- cli.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cli.js b/cli.js index f59d604c..dbf9a079 100755 --- a/cli.js +++ b/cli.js @@ -1,6 +1,7 @@ #!/usr/bin/env node import { globbySync } from 'globby' import fs from 'node:fs' +import path from 'node:path' import sortPackageJson from './index.js' const isCheckFlag = (argument) => argument === '--check' || argument === '-c' @@ -29,9 +30,17 @@ Strings passed as files are parsed as globs. } if (isVersion) { - const packageBuffer = fs.readFileSync('./package.json') - const { version } = JSON.parse(packageBuffer) - console.log(`sort-package-json ${version}`) + try { + const cliRealPath = fs.realpathSync(process.argv[1]) + const cliParentDir = path.dirname(cliRealPath) + const packageJsonBuffer = fs.readFileSync(`${cliParentDir}/package.json`) + const { version } = JSON.parse(packageJsonBuffer) + + console.log(`sort-package-json ${version}`) + } catch { + // If anything goes wrong, fall back to 'unknown version' + console.log(`sort-package-json unknown version`) + } process.exit(0) } From 1ba096754d058d7aa04ec1b7c657caf38462d8f8 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 01:56:44 -0600 Subject: [PATCH 03/12] fix(cli): delay path import until needed Uses dynamic import to import the node:path module when checking the version so it is not loaded when it is not needed. --- cli.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cli.js b/cli.js index dbf9a079..edceb800 100755 --- a/cli.js +++ b/cli.js @@ -1,7 +1,6 @@ #!/usr/bin/env node import { globbySync } from 'globby' import fs from 'node:fs' -import path from 'node:path' import sortPackageJson from './index.js' const isCheckFlag = (argument) => argument === '--check' || argument === '-c' @@ -32,7 +31,7 @@ Strings passed as files are parsed as globs. if (isVersion) { try { const cliRealPath = fs.realpathSync(process.argv[1]) - const cliParentDir = path.dirname(cliRealPath) + const cliParentDir = (await import('node:path')).dirname(cliRealPath) const packageJsonBuffer = fs.readFileSync(`${cliParentDir}/package.json`) const { version } = JSON.parse(packageJsonBuffer) From c59a9e5b5338e4efef263c4b27127733d857ec66 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 02:24:01 -0600 Subject: [PATCH 04/12] fix(cli): use path.join to make windows friendly --- cli.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index edceb800..e590f41f 100755 --- a/cli.js +++ b/cli.js @@ -30,9 +30,13 @@ Strings passed as files are parsed as globs. if (isVersion) { try { + const path = await import('node:path') + const cliRealPath = fs.realpathSync(process.argv[1]) - const cliParentDir = (await import('node:path')).dirname(cliRealPath) - const packageJsonBuffer = fs.readFileSync(`${cliParentDir}/package.json`) + const cliParentDir = path.dirname(cliRealPath) + const packageJsonBuffer = fs.readFileSync( + path.join(cliParentDir, 'package.json'), + ) const { version } = JSON.parse(packageJsonBuffer) console.log(`sort-package-json ${version}`) From eb20b83cc4413d2e86b55dd36307bcdf89bdf3a5 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 03:25:40 -0600 Subject: [PATCH 05/12] refactor(cli): changed manner of getting path --- cli.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/cli.js b/cli.js index e590f41f..ec941f4d 100755 --- a/cli.js +++ b/cli.js @@ -30,13 +30,8 @@ Strings passed as files are parsed as globs. if (isVersion) { try { - const path = await import('node:path') - - const cliRealPath = fs.realpathSync(process.argv[1]) - const cliParentDir = path.dirname(cliRealPath) - const packageJsonBuffer = fs.readFileSync( - path.join(cliParentDir, 'package.json'), - ) + const cliParentDir = new URL('package.json', import.meta.url) + const packageJsonBuffer = fs.readFileSync(cliParentDir.pathname) const { version } = JSON.parse(packageJsonBuffer) console.log(`sort-package-json ${version}`) From dd747f523b5a6fa1fce93c3cc40b9299f482b185 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 03:39:57 -0600 Subject: [PATCH 06/12] fix(cli): used fileURLToPath to fix windows support --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index ec941f4d..2d286b91 100755 --- a/cli.js +++ b/cli.js @@ -1,6 +1,7 @@ #!/usr/bin/env node import { globbySync } from 'globby' import fs from 'node:fs' +import { fileURLToPath } from 'url' import sortPackageJson from './index.js' const isCheckFlag = (argument) => argument === '--check' || argument === '-c' @@ -27,11 +28,10 @@ Strings passed as files are parsed as globs. ) process.exit(0) } - if (isVersion) { try { const cliParentDir = new URL('package.json', import.meta.url) - const packageJsonBuffer = fs.readFileSync(cliParentDir.pathname) + const packageJsonBuffer = fs.readFileSync(fileURLToPath(cliParentDir)) const { version } = JSON.parse(packageJsonBuffer) console.log(`sort-package-json ${version}`) From a365d4f9c9a3efcb97cd91e7bbec97c4335276a7 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 19:04:25 -0600 Subject: [PATCH 07/12] refactor(cli): removes fileURLToPath call removes not needed call to fileURLToPath, replacing with the URL object itself --- cli.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 2d286b91..1814046e 100755 --- a/cli.js +++ b/cli.js @@ -1,7 +1,6 @@ #!/usr/bin/env node import { globbySync } from 'globby' import fs from 'node:fs' -import { fileURLToPath } from 'url' import sortPackageJson from './index.js' const isCheckFlag = (argument) => argument === '--check' || argument === '-c' @@ -31,7 +30,7 @@ Strings passed as files are parsed as globs. if (isVersion) { try { const cliParentDir = new URL('package.json', import.meta.url) - const packageJsonBuffer = fs.readFileSync(fileURLToPath(cliParentDir)) + const packageJsonBuffer = fs.readFileSync(cliParentDir) const { version } = JSON.parse(packageJsonBuffer) console.log(`sort-package-json ${version}`) From 54bfeeb9d419095277996d474a35d21c41766618 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sat, 21 Jan 2023 19:16:58 -0600 Subject: [PATCH 08/12] refactor(cli): removed try-catch block Removed try-catch block in --version which provided a default response if the file system requests failed. --- cli.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/cli.js b/cli.js index 1814046e..3f31fd6e 100755 --- a/cli.js +++ b/cli.js @@ -28,16 +28,11 @@ Strings passed as files are parsed as globs. process.exit(0) } if (isVersion) { - try { - const cliParentDir = new URL('package.json', import.meta.url) - const packageJsonBuffer = fs.readFileSync(cliParentDir) - const { version } = JSON.parse(packageJsonBuffer) + const cliParentDir = new URL('package.json', import.meta.url) + const packageJsonBuffer = fs.readFileSync(cliParentDir) + const { version } = JSON.parse(packageJsonBuffer) - console.log(`sort-package-json ${version}`) - } catch { - // If anything goes wrong, fall back to 'unknown version' - console.log(`sort-package-json unknown version`) - } + console.log(`sort-package-json ${version}`) process.exit(0) } From 5e52ce490d7a417428b5aa6b6e7908fdb7f9dfb9 Mon Sep 17 00:00:00 2001 From: Aaron Dill <117116764+aarondill@users.noreply.github.com> Date: Sun, 22 Jan 2023 11:56:16 -0600 Subject: [PATCH 09/12] =?UTF-8?q?typo:=20cliParentDir=20=E2=80=94>=20packa?= =?UTF-8?q?geJsonUrl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 3f31fd6e..fe28235e 100755 --- a/cli.js +++ b/cli.js @@ -28,8 +28,8 @@ Strings passed as files are parsed as globs. process.exit(0) } if (isVersion) { - const cliParentDir = new URL('package.json', import.meta.url) - const packageJsonBuffer = fs.readFileSync(cliParentDir) + const packageJsonUrl = new URL('package.json', import.meta.url) + const packageJsonBuffer = fs.readFileSync(packageJsonUrl) const { version } = JSON.parse(packageJsonBuffer) console.log(`sort-package-json ${version}`) From 3ab3ff1b73517d146f7679582cb6c332abf94f1c Mon Sep 17 00:00:00 2001 From: Aaron Dill <117116764+aarondill@users.noreply.github.com> Date: Sun, 22 Jan 2023 11:56:56 -0600 Subject: [PATCH 10/12] fix(cli): change -v to -V for version --- cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.js b/cli.js index fe28235e..89adf7c6 100755 --- a/cli.js +++ b/cli.js @@ -6,7 +6,7 @@ import sortPackageJson from './index.js' const isCheckFlag = (argument) => argument === '--check' || argument === '-c' const isHelpFlag = (argument) => argument === '--help' || argument === '-h' const isVersionFlag = (argument) => - argument === '--version' || argument === '-V' + argument === '--version' || argument === '-v' const cliArguments = process.argv.slice(2) const isCheck = cliArguments.some(isCheckFlag) From 35c89d92502533117bb9f68a70b25436e7d75016 Mon Sep 17 00:00:00 2001 From: Aaron Dill <117116764+aarondill@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:00:49 -0600 Subject: [PATCH 11/12] Update cli.js Co-authored-by: fisker Cheung --- cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 89adf7c6..578d642c 100755 --- a/cli.js +++ b/cli.js @@ -22,7 +22,7 @@ Strings passed as files are parsed as globs. -c, --check check if FILES are sorted -q, --quiet don't output success messages -h, --help display this help and exit - -V, --version display the version and exit + -v, --version display the version and exit `, ) process.exit(0) From 6569a44deb31390dcf64cf78f8a7099036ef0d00 Mon Sep 17 00:00:00 2001 From: Aaron Dill <117116764+aarondill@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:02:51 -0600 Subject: [PATCH 12/12] =?UTF-8?q?fix:=20remove=20mention=20of=20=E2=80=94q?= =?UTF-8?q?uiet=20from=20help?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli.js | 1 - 1 file changed, 1 deletion(-) diff --git a/cli.js b/cli.js index 578d642c..370afd26 100755 --- a/cli.js +++ b/cli.js @@ -20,7 +20,6 @@ Sort npm package.json files. Default: ./package.json Strings passed as files are parsed as globs. -c, --check check if FILES are sorted - -q, --quiet don't output success messages -h, --help display this help and exit -v, --version display the version and exit `,