-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation, tests, readme
- Loading branch information
Showing
22 changed files
with
335 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"root": true, | ||
|
||
"extends": "@ljharb/eslint-config/node/20", | ||
|
||
"rules": { | ||
"no-console": "off", | ||
"no-process-exit": "off", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# These are supported funding model platforms | ||
|
||
github: [ljharb] | ||
patreon: # Replace with a single Patreon username | ||
open_collective: # Replace with a single Open Collective username | ||
ko_fi: # Replace with a single Ko-fi username | ||
tidelift: npm/nvmrc | ||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry | ||
liberapay: # Replace with a single Liberapay username | ||
issuehunt: # Replace with a single IssueHunt username | ||
otechie: # Replace with a single Otechie username | ||
custom: # Replace with a single custom sponsorship URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: 'Tests: pretest/posttest' | ||
|
||
on: [pull_request, push] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/pretest.yml@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: 'Tests: node.js' | ||
|
||
on: [pull_request, push] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/node.yml@main | ||
with: | ||
range: '>= 22' | ||
type: minors | ||
command: npm run tests-only |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Automatic Rebase | ||
|
||
on: [pull_request_target] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
_: | ||
permissions: | ||
contents: write # for ljharb/rebase to push code to rebase | ||
pull-requests: read # for ljharb/rebase to get info about PR | ||
|
||
name: "Automatic Rebase" | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: ljharb/rebase@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Require “Allow Edits” | ||
|
||
on: [pull_request_target] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
_: | ||
permissions: | ||
pull-requests: read # for ljharb/require-allow-edits to check 'allow edits' on PR | ||
|
||
name: "Require “Allow Edits”" | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: ljharb/require-allow-edits@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,3 +133,5 @@ dist | |
npm-shrinkwrap.json | ||
package-lock.json | ||
yarn.lock | ||
|
||
.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"exclude": [ | ||
"test/**" | ||
], | ||
"reporter": [ | ||
"html", | ||
"lcov", | ||
"text" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,53 @@ | ||
# nvmrc | ||
# nvmrc <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
|
||
[![github actions][actions-image]][actions-url] | ||
[![coverage][codecov-image]][codecov-url] | ||
[![License][license-image]][license-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
|
||
[![npm badge][npm-badge-png]][package-url] | ||
|
||
command-line tool to validate a `.nvmrc` file | ||
|
||
## Example | ||
|
||
Run: | ||
```sh | ||
$ nvmrc | ||
``` | ||
|
||
When valid, the parsed JSON will be logged. | ||
|
||
When invalid: | ||
```sh | ||
invalid .nvmrc! | ||
all non-commented content (anything after # is a comment) must be either: | ||
- a single bare nvm-recognized version-ish | ||
- or, multiple distinct key-value pairs, each key/value separated by a single equals sign (=) | ||
|
||
non-commented content parsed: | ||
… | ||
``` | ||
## Tests | ||
Simply clone the repo, `npm install`, and run `npm test` | ||
## Security | ||
Please email [@ljharb](https://github.com/ljharb) or see https://tidelift.com/security if you have a potential security vulnerability to report. | ||
[package-url]: https://npmjs.org/package/nvmrc | ||
[npm-version-svg]: https://versionbadg.es/nvm-sh/nvmrc.svg | ||
[deps-svg]: https://david-dm.org/nvm-sh/nvmrc.svg | ||
[deps-url]: https://david-dm.org/nvm-sh/nvmrc | ||
[dev-deps-svg]: https://david-dm.org/nvm-sh/nvmrc/dev-status.svg | ||
[dev-deps-url]: https://david-dm.org/nvm-sh/nvmrc#info=devDependencies | ||
[npm-badge-png]: https://nodei.co/npm/nvmrc.png?downloads=true&stars=true | ||
[license-image]: https://img.shields.io/npm/l/nvmrc.svg | ||
[license-url]: LICENSE | ||
[downloads-image]: https://img.shields.io/npm/dm/nvmrc.svg | ||
[downloads-url]: https://npm-stat.com/charts.html?package=nvmrc | ||
[codecov-image]: https://codecov.io/gh/nvm-sh/nvmrc/branch/main/graphs/badge.svg | ||
[codecov-url]: https://app.codecov.io/gh/nvm-sh/nvmrc/ | ||
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/nvm-sh/nvmrc | ||
[actions-url]: https://github.com/nvm-sh/nvmrc/actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#! /usr/bin/env node | ||
|
||
import { readFile } from 'fs/promises'; | ||
import { join } from 'path'; | ||
|
||
const contentsP = readFile(join(process.cwd(), '.nvmrc')); | ||
|
||
const contentsStr = `${await contentsP}`; | ||
|
||
const rawOptions = contentsStr | ||
.split('\n') | ||
.map((x) => x.replace(/#.*$/, '').trim()) | ||
.filter(Boolean); | ||
|
||
const optionsEntries = rawOptions.map((x) => ((/[=]/).test(x) | ||
? x.split('=').map((y) => y.trim()) | ||
: ['node', x] | ||
)); | ||
|
||
const map = new Map(optionsEntries); | ||
|
||
if (map.size !== optionsEntries.length || !map.has('node') || rawOptions.filter((x) => !x.includes('=')).length !== 1) { | ||
console.error(` | ||
invalid .nvmrc! | ||
all non-commented content (anything after # is a comment) must be either: | ||
- a single bare nvm-recognized version-ish | ||
- or, multiple distinct key-value pairs, each key/value separated by a single equals sign (=) | ||
additionally, a single bare nvm-recognized version-ish must be present (after stripping comments). | ||
`); | ||
|
||
console.warn(` | ||
non-commented content parsed: | ||
${rawOptions.join('\n')} | ||
`); | ||
|
||
process.exit(1); | ||
} | ||
|
||
const options = Object.fromEntries(optionsEntries); | ||
|
||
console.log(JSON.stringify(options, null, '\t').replace(/\n\s*/g, ' ')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
v20 | ||
|
||
# this is a comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
foo | ||
bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
21.2 # this is an inline comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node | ||
npm=10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node | ||
|
||
npm=3 | ||
npm=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a=b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# this is a comment | ||
# nothing here but us comments! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node=foo | ||
npm=bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { spawnSync } from 'child_process'; | ||
import { join } from 'path'; | ||
import { readdirSync } from 'fs'; | ||
|
||
import test from 'tape'; | ||
|
||
import stripColors from 'strip-color'; | ||
|
||
const fixtureDir = join(import.meta.dirname, 'fixtures'); | ||
|
||
const fixtures = readdirSync(fixtureDir); | ||
|
||
test('nvmrc', async (t) => { | ||
const valid = [ | ||
'basic', | ||
'basic-comments', | ||
'basic-with-comment', | ||
'basic-with-npm', | ||
]; | ||
|
||
const invalid = [ | ||
'basic-invalid', | ||
'invalid', | ||
'only-comments', | ||
'pre-normalized', | ||
'duplicate-pairs', | ||
]; | ||
|
||
t.deepEqual(fixtures.sort(), valid.concat(invalid).sort(), 'all fixtures are accounted for'); | ||
|
||
const bin = join(import.meta.dirname, '../nvmrc.mjs'); | ||
|
||
for (const fixture of valid) { | ||
t.test(`fixture ${fixture}`, (st) => { | ||
const cwd = join(fixtureDir, fixture); | ||
|
||
const { stdout, status } = spawnSync(`${bin}`, { cwd }); | ||
|
||
st.equal(status, 0, 'yields a zero exit code'); | ||
|
||
const stripped = stripColors(`${stdout}`); | ||
|
||
st.doesNotThrow(() => JSON.parse(stripped), `fixture ${fixture} is valid, yields ${stripped.replace(/\n\s*/g, ' ')}`); | ||
|
||
st.end(); | ||
}); | ||
} | ||
|
||
for (const fixture of invalid) { | ||
t.test(`fixture ${fixture}`, (st) => { | ||
const cwd = join(fixtureDir, fixture); | ||
|
||
const { status, stderr } = spawnSync(`${bin}`, { cwd }); | ||
|
||
st.notEqual(status, 0, `fixture ${fixture} did not produce a zero exit code`); | ||
|
||
const stripped = stripColors(`${stderr}`); | ||
|
||
const lines = stripped.split('\n').map((x) => x.trim()).filter(Boolean); | ||
|
||
st.deepEqual(lines.slice(0, 6), [ | ||
'invalid .nvmrc!', | ||
'all non-commented content (anything after # is a comment) must be either:', | ||
'- a single bare nvm-recognized version-ish', | ||
'- or, multiple distinct key-value pairs, each key/value separated by a single equals sign (=)', | ||
'additionally, a single bare nvm-recognized version-ish must be present (after stripping comments).', | ||
'non-commented content parsed:', | ||
]); | ||
|
||
st.end(); | ||
}); | ||
} | ||
}); |