From 930ea2a044c0b5258a25cdb9be1210ae1ce705ed Mon Sep 17 00:00:00 2001 From: Lee Robinson Date: Tue, 12 Jul 2022 21:26:09 -0500 Subject: [PATCH] Fix chalk and set up vitest. --- package.json | 7 +- pnpm-lock.yaml | 235 ++++++++++++++++++++++++++- source/main.ts | 3 +- source/utilities/cli.ts | 4 +- test/__snapshots__/help.test.ts.snap | 75 +++++++++ test/help.test.ts | 8 + 6 files changed, 321 insertions(+), 11 deletions(-) create mode 100644 test/__snapshots__/help.test.ts.snap create mode 100644 test/help.test.ts diff --git a/package.json b/package.json index 2736b124..b0b496eb 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "start": "node ./build/main.js", "compile": "tsup ./source/main.ts", "test:tsc": "tsc --project tsconfig.json", - "test": "pnpm test:tsc", + "test:unit": "vitest", + "test": "pnpm test:tsc && pnpm test:unit", "lint:code": "eslint --max-warnings 0 source/**/*.ts", "lint:style": "prettier --check --ignore-path .gitignore .", "lint": "pnpm lint:code && pnpm lint:style", @@ -38,6 +39,7 @@ "arg": "5.0.2", "boxen": "7.0.0", "chalk": "5.0.1", + "chalk-template": "0.4.0", "clipboardy": "3.0.0", "compression": "1.7.4", "is-port-reachable": "4.0.0", @@ -54,7 +56,8 @@ "prettier": "2.7.1", "tsup": "6.1.3", "tsx": "3.7.1", - "typescript": "4.6.4" + "typescript": "4.6.4", + "vitest": "0.18.0" }, "tsup": { "target": "esnext", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 829a873a..0b166d17 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,6 +9,7 @@ specifiers: arg: 5.0.2 boxen: 7.0.0 chalk: 5.0.1 + chalk-template: 0.4.0 clipboardy: 3.0.0 compression: 1.7.4 eslint: 8.19.0 @@ -21,6 +22,7 @@ specifiers: tsx: 3.7.1 typescript: 4.6.4 update-check: 1.5.4 + vitest: 0.18.0 dependencies: '@zeit/schemas': 2.21.0 @@ -28,6 +30,7 @@ dependencies: arg: 5.0.2 boxen: 7.0.0 chalk: 5.0.1 + chalk-template: 0.4.0 clipboardy: 3.0.0 compression: 1.7.4 is-port-reachable: 4.0.0 @@ -45,6 +48,7 @@ devDependencies: tsup: 6.1.3_typescript@4.6.4 tsx: 3.7.1 typescript: 4.6.4 + vitest: 0.18.0 packages: /@babel/code-frame/7.18.6: @@ -260,6 +264,22 @@ packages: '@types/node': 18.0.3 dev: true + /@types/chai-subset/1.3.3: + resolution: + { + integrity: sha512-frBecisrNGz+F4T6bcc+NLeolfiojh5FxW2klu669+8BARtyQv2C/GkNW6FUodVe4BroGMP/wER/YDGc7rEllw==, + } + dependencies: + '@types/chai': 4.3.1 + dev: true + + /@types/chai/4.3.1: + resolution: + { + integrity: sha512-/zPMqDkzSZ8t3VtxOa4KPq7uzzW978M9Tvh+j7GHKuo6k6GTLxPJ4J5gE5cjfJ26pnXst0N5Hax8Sr0T2Mi9zQ==, + } + dev: true + /@types/compression/1.7.2: resolution: { @@ -754,7 +774,6 @@ packages: engines: { node: '>=8' } dependencies: color-convert: 2.0.1 - dev: true /ansi-styles/6.1.0: resolution: @@ -861,6 +880,13 @@ packages: es-shim-unscopables: 1.0.0 dev: true + /assertion-error/1.1.0: + resolution: + { + integrity: sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==, + } + dev: true + /ast-types-flow/0.0.7: resolution: { @@ -1008,6 +1034,32 @@ packages: engines: { node: '>=14.16' } dev: false + /chai/4.3.6: + resolution: + { + integrity: sha512-bbcp3YfHCUzMOvKqsztczerVgBKSsEijCySNlHHbX3VG1nskvqjz5Rfso1gGwD6w6oOV3eI60pKuMOV5MV7p3Q==, + } + engines: { node: '>=4' } + dependencies: + assertion-error: 1.1.0 + check-error: 1.0.2 + deep-eql: 3.0.1 + get-func-name: 2.0.0 + loupe: 2.3.4 + pathval: 1.1.1 + type-detect: 4.0.8 + dev: true + + /chalk-template/0.4.0: + resolution: + { + integrity: sha512-/ghrgmhfY8RaSdeo43hNXxpoHAtxdbskUHjPpfqUWGttFgycUhYPGx3YZBCnUCvOa7Doivn1IZec3DEGFoMgLg==, + } + engines: { node: '>=12' } + dependencies: + chalk: 4.1.2 + dev: false + /chalk/2.4.1: resolution: { @@ -1029,7 +1081,6 @@ packages: dependencies: ansi-styles: 4.3.0 supports-color: 7.2.0 - dev: true /chalk/5.0.1: resolution: @@ -1039,6 +1090,13 @@ packages: engines: { node: ^12.17.0 || ^14.13 || >=16.0.0 } dev: false + /check-error/1.0.2: + resolution: + { + integrity: sha512-BrgHpW9NURQgzoNyjfq0Wu6VFO6D7IZEmJNdtgNqpzGG8RuNFHt2jQxWlAs4HMe119chBnv+34syEZtc6IhLtA==, + } + dev: true + /chokidar/3.5.3: resolution: { @@ -1151,7 +1209,6 @@ packages: engines: { node: '>=7.0.0' } dependencies: color-name: 1.1.4 - dev: true /color-name/1.1.3: resolution: @@ -1165,7 +1222,6 @@ packages: { integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==, } - dev: true /colorette/2.0.19: resolution: @@ -1294,6 +1350,16 @@ packages: ms: 2.1.2 dev: true + /deep-eql/3.0.1: + resolution: + { + integrity: sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==, + } + engines: { node: '>=0.12' } + dependencies: + type-detect: 4.0.8 + dev: true + /deep-extend/0.6.0: resolution: { @@ -2342,6 +2408,13 @@ packages: } dev: true + /get-func-name/2.0.0: + resolution: + { + integrity: sha512-Hm0ixYtaSZ/V7C8FJrtZIuBBI+iSgL+1Aq82zSu8VQNB4S3Gk8e7Qs3VwBDJAhmRZcFqkl3tQu36g/Foh5I5ig==, + } + dev: true + /get-intrinsic/1.1.2: resolution: { @@ -2486,7 +2559,6 @@ packages: integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==, } engines: { node: '>=8' } - dev: true /has-property-descriptors/1.0.0: resolution: @@ -3036,6 +3108,14 @@ packages: engines: { node: ^12.20.0 || ^14.13.1 || >=16.0.0 } dev: true + /local-pkg/0.4.2: + resolution: + { + integrity: sha512-mlERgSPrbxU3BP4qBqAvvwlgW4MTg78iwJdGGnv7kibKjWcJksrG3t6LB5lXI93wXRDvG4NpUgJFmTG4T6rdrg==, + } + engines: { node: '>=14' } + dev: true + /locate-path/2.0.0: resolution: { @@ -3101,6 +3181,15 @@ packages: js-tokens: 4.0.0 dev: true + /loupe/2.3.4: + resolution: + { + integrity: sha512-OvKfgCC2Ndby6aSTREl5aCCPTNIzlDfQZvZxNUrBrihDhL3xcrYegTblhmEiCrg2kKQz4XsFIaemE5BF4ybSaQ==, + } + dependencies: + get-func-name: 2.0.0 + dev: true + /lru-cache/6.0.0: resolution: { @@ -3250,6 +3339,15 @@ packages: thenify-all: 1.6.0 dev: true + /nanoid/3.3.4: + resolution: + { + integrity: sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==, + } + engines: { node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1 } + hasBin: true + dev: true + /natural-compare/1.4.0: resolution: { @@ -3591,6 +3689,20 @@ packages: engines: { node: '>=8' } dev: true + /pathval/1.1.1: + resolution: + { + integrity: sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==, + } + dev: true + + /picocolors/1.0.0: + resolution: + { + integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==, + } + dev: true + /picomatch/2.3.1: resolution: { @@ -3643,6 +3755,18 @@ packages: yaml: 1.10.2 dev: true + /postcss/8.4.14: + resolution: + { + integrity: sha512-E398TUmfAYFPBSdzgeieK2Y1+1cpdxJx8yXbK/m57nRhKSmk1GB2tO4lbLBtlkfPQTDKfe4Xqv1ASWPpayPEig==, + } + engines: { node: ^10 || ^12 || >=14 } + dependencies: + nanoid: 3.3.4 + picocolors: 1.0.0 + source-map-js: 1.0.2 + dev: true + /prelude-ls/1.2.1: resolution: { @@ -4075,6 +4199,14 @@ packages: is-fullwidth-code-point: 4.0.0 dev: true + /source-map-js/1.0.2: + resolution: + { + integrity: sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==, + } + engines: { node: '>=0.10.0' } + dev: true + /source-map-support/0.5.21: resolution: { @@ -4306,7 +4438,6 @@ packages: engines: { node: '>=8' } dependencies: has-flag: 4.0.0 - dev: true /supports-preserve-symlinks-flag/1.0.0: resolution: @@ -4349,6 +4480,22 @@ packages: } dev: true + /tinypool/0.2.2: + resolution: + { + integrity: sha512-tp4n5OARNL3v8ntdJUyo5NsDfwvUtu8isB43USjrsQxQrADDKY6UGBkmFaw/2vNmEt8S/uSm2U5FhkiK1eAFGw==, + } + engines: { node: '>=14.0.0' } + dev: true + + /tinyspy/0.3.3: + resolution: + { + integrity: sha512-gRiUR8fuhUf0W9lzojPf1N1euJYA30ISebSfgca8z76FOvXtVXqd5ojEIaKLWbDQhAaC3ibxZIjqbyi4ybjcTw==, + } + engines: { node: '>=14.0.0' } + dev: true + /to-regex-range/5.0.1: resolution: { @@ -4485,6 +4632,14 @@ packages: prelude-ls: 1.2.1 dev: true + /type-detect/4.0.8: + resolution: + { + integrity: sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==, + } + engines: { node: '>=4' } + dev: true + /type-fest/0.20.2: resolution: { @@ -4589,6 +4744,74 @@ packages: engines: { node: '>= 0.8' } dev: false + /vite/2.9.14: + resolution: + { + integrity: sha512-P/UCjSpSMcE54r4mPak55hWAZPlyfS369svib/gpmz8/01L822lMPOJ/RYW6tLCe1RPvMvOsJ17erf55bKp4Hw==, + } + engines: { node: '>=12.2.0' } + hasBin: true + peerDependencies: + less: '*' + sass: '*' + stylus: '*' + peerDependenciesMeta: + less: + optional: true + sass: + optional: true + stylus: + optional: true + dependencies: + esbuild: 0.14.48 + postcss: 8.4.14 + resolve: 1.22.1 + rollup: 2.76.0 + optionalDependencies: + fsevents: 2.3.2 + dev: true + + /vitest/0.18.0: + resolution: + { + integrity: sha512-ryAtlh5Gvg3+aLNuOQ8YOHxgQCCu46jx40X5MBL0K0/ejB9i5zsr8fV8LTGXbXex80UMHlzceI9F+ouGaiR+mQ==, + } + engines: { node: '>=v14.16.0' } + hasBin: true + peerDependencies: + '@edge-runtime/vm': '*' + '@vitest/ui': '*' + c8: '*' + happy-dom: '*' + jsdom: '*' + peerDependenciesMeta: + '@edge-runtime/vm': + optional: true + '@vitest/ui': + optional: true + c8: + optional: true + happy-dom: + optional: true + jsdom: + optional: true + dependencies: + '@types/chai': 4.3.1 + '@types/chai-subset': 1.3.3 + '@types/node': 18.0.3 + chai: 4.3.6 + debug: 4.3.4 + local-pkg: 0.4.2 + tinypool: 0.2.2 + tinyspy: 0.3.3 + vite: 2.9.14 + transitivePeerDependencies: + - less + - sass + - stylus + - supports-color + dev: true + /webidl-conversions/4.0.2: resolution: { diff --git a/source/main.ts b/source/main.ts index e3afb265..c462fa72 100755 --- a/source/main.ts +++ b/source/main.ts @@ -5,6 +5,7 @@ import path from 'node:path'; import chalk from 'chalk'; +import chalkTemplate from 'chalk-template'; import boxen from 'boxen'; import clipboard from 'clipboardy'; import checkForUpdate from 'update-check'; @@ -36,7 +37,7 @@ const printUpdateNotification = async (debugMode?: boolean) => { if (!update) return; logger.log( - chalk` {bgRed.white UPDATE } The latest version of \`serve\` is ${update.latest}`, + chalkTemplate` {bgRed.white UPDATE } The latest version of \`serve\` is ${update.latest}`, ); }; diff --git a/source/utilities/cli.ts b/source/utilities/cli.ts index f32aa8e0..be7cb976 100644 --- a/source/utilities/cli.ts +++ b/source/utilities/cli.ts @@ -1,7 +1,7 @@ // source/utilities/cli.ts // CLI-related utility functions. -import chalk from 'chalk'; +import chalkTemplate from 'chalk-template'; import parseArgv from 'arg'; import { parseEndpoint } from './http.js'; import type { Arguments } from '../types.js'; @@ -40,7 +40,7 @@ const options = { }; // The help text for the CLI. -const helpText = chalk` +const helpText = chalkTemplate` {bold.cyan serve} - Static file serving and directory listing {bold USAGE} diff --git a/test/__snapshots__/help.test.ts.snap b/test/__snapshots__/help.test.ts.snap new file mode 100644 index 00000000..c7e9a24d --- /dev/null +++ b/test/__snapshots__/help.test.ts.snap @@ -0,0 +1,75 @@ +// Vitest Snapshot v1 + +exports[`--help > snapshot 1`] = ` +" + serve - Static file serving and directory listing + + USAGE + + $ serve --help + $ serve --version + $ serve folder_name + $ serve [-l listen_uri [-l ...]] [directory] + + By default, serve will listen on 0.0.0.0:3000 and serve the + current working directory on that address. + + Specifying a single --listen argument will overwrite the default, not supplement it. + + OPTIONS + + --help Shows this help message + + -v, --version Displays the current version of serve + + -l, --listen listen_uri Specify a URI endpoint on which to listen (see below) - + more than one may be specified to listen in multiple places + + -p Specify custom port + + -d, --debug Show debugging information + + -s, --single Rewrite all not-found requests to \`index.html\` + + -c, --config Specify custom path to \`serve.json\` + + -C, --cors Enable CORS, sets \`Access-Control-Allow-Origin\` to \`*\` + + -n, --no-clipboard Do not copy the local address to the clipboard + + -u, --no-compression Do not compress files + + --no-etag Send \`Last-Modified\` header instead of \`ETag\` + + -S, --symlinks Resolve symlinks instead of showing 404 errors + + --ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS + + --ssl-key Optional path to the SSL/TLS certificate's private key + + --ssl-pass Optional path to the SSL/TLS certificate's passphrase + + --no-port-switching Do not open a port other than the one specified when it's taken. + + ENDPOINTS + + Listen endpoints (specified by the --listen or -l options above) instruct serve + to listen on one or more interfaces/ports, UNIX domain sockets, or Windows named pipes. + + For TCP ports on hostname \\"localhost\\": + + $ serve -l 1234 + + For TCP (traditional host/port) endpoints: + + $ serve -l tcp://hostname:1234 + + For UNIX domain socket endpoints: + + $ serve -l unix:/path/to/socket.sock + + For Windows named pipe endpoints: + + $ serve -l pipe:\\\\\\\\.\\\\pipe\\\\PipeName +" +`; diff --git a/test/help.test.ts b/test/help.test.ts new file mode 100644 index 00000000..1a678002 --- /dev/null +++ b/test/help.test.ts @@ -0,0 +1,8 @@ +import { describe, expect, it } from 'vitest'; +import { getHelpText } from '../source/utilities/cli'; + +describe('--help', () => { + it('snapshot', () => { + expect(getHelpText()).toMatchSnapshot(); + }); +});