Skip to content

Commit bd1c614

Browse files
committed
feat: support reading from stdin and clean-up implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent dddf657 commit bd1c614

28 files changed

+1129
-350
lines changed

.github/workflows/scripts/lint_package_json_files

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ if [ -n "${files}" ]; then
5252
printf '%s' "${files}" | "${lint_package_json}" --split=" "
5353

5454
echo "Validating package names..."
55-
for file in ${files}; do
56-
if ! node "${validate_package_names}" --pattern "${file}"; then
57-
echo "ERROR: Package name validation failed"
58-
needs_changes=1
59-
fi
60-
done
55+
if ! printf '%s' "${files}" | "${validate_package_names}" --split=" "; then
56+
echo "ERROR: Package name validation failed"
57+
needs_changes=1
58+
fi
6159
else
6260
echo "No package.json files to lint."
6361
fi

lib/node_modules/@stdlib/_tools/lint/pkg-json-names/README.md

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# Lint
2222

23-
> Lint @stdlib package.json names to ensure they match their directory location.
23+
> Lint package.json names to ensure they match their directory location.
2424
2525
<section class="usage">
2626

@@ -32,7 +32,7 @@ var lint = require( '@stdlib/_tools/lint/pkg-json-names' );
3232

3333
#### lint( \[options,] clbk )
3434

35-
Asynchronously lints @stdlib package.json names to ensure that each package name matches its directory location within the stdlib project structure.
35+
Asynchronously lints package.json names to ensure that each package name matches its directory location within the project structure.
3636

3737
```javascript
3838
lint( onLint );
@@ -41,7 +41,7 @@ function onLint( error, errs ) {
4141
if ( error ) {
4242
throw error;
4343
}
44-
if ( errs.length ) {
44+
if ( errs && errs.length ) {
4545
console.error( JSON.stringify( errs ) );
4646
} else {
4747
console.log( 'No detected errors.' );
@@ -51,22 +51,22 @@ function onLint( error, errs ) {
5151

5252
The function accepts the following `options`:
5353

54-
- **dir**: root directory from which to search for @stdlib package.json files to lint. May be either an absolute file path or a path relative to the `stdlib/lib/node_modules/` directory. Default: `/path/to/stdlib/lib/node_modules/`.
55-
- **pattern**: glob pattern for finding @stdlib package.json files. Default: `**/@stdlib/**/package.json`.
54+
- **dir**: root directory from which to search for packages. May be either an absolute file path or a path relative to the current working directory. Default: current working directory.
55+
- **pattern**: glob pattern for finding package.json files. Default: `**/@stdlib/**/package.json`.
5656
- **ignore**: glob pattern(s) to exclude.
5757

5858
Each lint error is represented by an `object` having the following fields:
5959

60-
- **file**: @stdlib package.json file path.
61-
- **expected**: expected @stdlib package name based on directory location.
60+
- **file**: package.json file path.
61+
- **expected**: expected package name based on directory location.
6262
- **actual**: actual package name found in package.json.
6363
- **message**: error message describing the mismatch.
6464

6565
To lint starting from a descendant directory, set the `dir` option.
6666

6767
```javascript
6868
var opts = {
69-
'dir': './@stdlib/math/base'
69+
'dir': '/foo/bar/baz'
7070
};
7171

7272
lint( opts, onLint );
@@ -75,7 +75,7 @@ function onLint( error, errs ) {
7575
if ( error ) {
7676
throw error;
7777
}
78-
if ( errs.length ) {
78+
if ( errs && errs.length ) {
7979
console.error( JSON.stringify( errs ) );
8080
} else {
8181
console.log( 'No detected errors.' );
@@ -85,11 +85,11 @@ function onLint( error, errs ) {
8585

8686
#### lint.sync( \[options] )
8787

88-
Synchronously lints @stdlib package.json names to ensure that each package name matches its directory location within the stdlib project structure.
88+
Synchronously lints package.json names to ensure that each package name matches its directory location within the project structure.
8989

9090
```javascript
9191
var errs = lint.sync();
92-
if ( errs.length ) {
92+
if ( errs && errs.length ) {
9393
console.error( JSON.stringify( errs ) );
9494
} else {
9595
console.log( 'No detected errors.' );
@@ -107,9 +107,8 @@ The function accepts the same `options` as `lint()` above.
107107
## Notes
108108

109109
- The function only lints packages under the `@stdlib` scope.
110-
- @stdlib package names must start with `@stdlib/` and match their directory location exactly.
110+
- Package names must start with `@stdlib/` and match their directory location exactly.
111111
- For example, a package located at `lib/node_modules/@stdlib/math/base/special/abs/` must be named `@stdlib/math/base/special/abs`.
112-
- The tool validates that the package name corresponds to the actual directory structure within the stdlib project.
113112

114113
</section>
115114

@@ -125,7 +124,8 @@ The function accepts the same `options` as `lint()` above.
125124
var lint = require( '@stdlib/_tools/lint/pkg-json-names' );
126125

127126
var opts = {
128-
'dir': './'
127+
'dir': './',
128+
'pattern': '**/@stdlib/utils/**/package.json'
129129
};
130130

131131
lint( opts, onLint );
@@ -134,7 +134,7 @@ function onLint( error, errors ) {
134134
if ( error ) {
135135
throw error;
136136
}
137-
if ( errors.length ) {
137+
if ( errors && errors.length ) {
138138
console.error( JSON.stringify( errors ) );
139139
} else {
140140
console.log( 'No detected errors.' );
@@ -157,14 +157,16 @@ function onLint( error, errors ) {
157157
### Usage
158158

159159
```text
160-
Usage: stdlib-lint-pkg-json-names [options] [<dir>]
160+
Usage: lint-pkg-json-names [options] [<dir>]
161161
162162
Options:
163163
164164
-h, --help Print this message.
165165
-V, --version Print the package version.
166166
--pattern pattern Pattern to match files for linting.
167167
--ignore pattern Exclusion glob pattern.
168+
--split sep Separator used to split stdin data. Default: /\\r?\\n/.
169+
--format fmt Output format: 'pretty' or 'ndjson'. Default: 'pretty'.
168170
```
169171

170172
</section>
@@ -175,7 +177,28 @@ Options:
175177

176178
### Notes
177179

178-
- If not provided a `dir` argument, the search directory is the current working directory.
180+
181+
- If part of a [standard stream][standard-stream] pipeline, results are written to `stdout` as newline-delimited JSON ([NDJSON][ndjson]). Otherwise, results are pretty printed by default.
182+
183+
- If not provided a `dir` argument, the current working directory is the search directory.
184+
185+
- To provide multiple exclusion glob patterns, set multiple `--ignore` option arguments.
186+
187+
```bash
188+
$ lint-pkg-json-names --ignore=node_modules/** --ignore=build/** --ignore=reports/**
189+
```
190+
191+
- If the split separator is a [regular expression][regexp], ensure that the `split` option is properly **escaped**.
192+
193+
```bash
194+
# Not escaped...
195+
$ <stdout> | lint-pkg-json-names --split /\r?\n/
196+
197+
# Escaped...
198+
$ <stdout> | lint-pkg-json-names --split /\\r?\\n/
199+
```
200+
201+
- If provided a list of filenames via `stdin`, each filename is resolved relative to the current working directory. To specify an alternative directory, provide a `dir` argument.
179202

180203
</section>
181204

@@ -186,7 +209,30 @@ Options:
186209
### Examples
187210

188211
```bash
189-
$ stdlib-lint-pkg-json-names
212+
$ lint-pkg-json-names
213+
214+
/path/to/lib/node_modules/@stdlib/math/base/special/abs/package.json
215+
216+
message: Package name "@stdlib/math/base/special/sin" does not match directory structure (expected: "@stdlib/math/base/special/abs")
217+
expected: @stdlib/math/base/special/abs
218+
actual: @stdlib/math/base/special/sin
219+
220+
1 errors
221+
```
222+
223+
To output results as newline-delimited JSON ([NDJSON][ndjson]),
224+
225+
```bash
226+
$ lint-pkg-json-names --format ndjson
227+
{"file":"/path/to/package.json","message":"Package name \"@stdlib/math/base/special/sin\" does not match directory structure (expected: \"@stdlib/math/base/special/abs\")","expected":"@stdlib/math/base/special/abs","actual":"@stdlib/math/base/special/sin"}
228+
...
229+
```
230+
231+
To use as a part of a [standard stream][standard-stream] pipeline,
232+
233+
```bash
234+
$ echo -n $'lib/node_modules/@stdlib/math/base/special/abs/package.json' | lint-pkg-json-names
235+
...
190236
```
191237

192238
</section>
@@ -209,6 +255,12 @@ $ stdlib-lint-pkg-json-names
209255

210256
<section class="links">
211257

258+
[ndjson]: http://ndjson.org/
259+
260+
[regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
261+
262+
[standard-stream]: https://en.wikipedia.org/wiki/Pipeline_%28Unix%29
263+
212264
</section>
213265

214266
<!-- /.links -->

0 commit comments

Comments
 (0)