Skip to content

Commit

Permalink
Initial implementation, tests, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 6, 2024
1 parent 48899d8 commit 6b2fcec
Show file tree
Hide file tree
Showing 22 changed files with 335 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .eslintrc
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",
},
}
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
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
10 changes: 10 additions & 0 deletions .github/workflows/node-pretest.yml
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
14 changes: 14 additions & 0 deletions .github/workflows/node.yml
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
22 changes: 22 additions & 0 deletions .github/workflows/rebase.yml
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 }}
18 changes: 18 additions & 0 deletions .github/workflows/require-allow-edits.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ dist
npm-shrinkwrap.json
package-lock.json
yarn.lock

.npmignore
10 changes: 10 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"exclude": [
"test/**"
],
"reporter": [
"html",
"lcov",
"text"
]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
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).
53 changes: 52 additions & 1 deletion README.md
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
42 changes: 42 additions & 0 deletions nvmrc.mjs
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, ' '));
48 changes: 46 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
"name": "nvmrc",
"version": "0.0.0",
"description": "command-line tool to validate a `.nvmrc` file",
"bin": "./nvmrc.mjs",
"main": false,
"exports": {
"./package.json": "./package.json"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prepublishOnly": "safe-publish-latest",
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint",
"tests-only": "NODE_OPTIONS='--no-warnings' c8 tape 'test/*.mjs'",
"test": "npm run tests-only",
"posttest": "aud --production",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
"repository": {
"type": "git",
Expand All @@ -23,5 +33,39 @@
"bugs": {
"url": "https://github.com/nvm-sh/nvmrc/issues"
},
"homepage": "https://github.com/nvm-sh/nvmrc#readme"
"homepage": "https://github.com/nvm-sh/nvmrc#readme",
"engines": {
"node": "^20 || >= 22"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.1",
"aud": "^2.0.4",
"auto-changelog": "^2.4.0",
"c8": "^9.1.0",
"eslint": "^8.8.0",
"in-publish": "^2.0.1",
"npmignore": "^0.3.1",
"safe-publish-latest": "^2.0.0",
"strip-color": "^0.1.0",
"tape": "^5.7.5"
},
"testling": {
"files": "test/*.js"
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true
},
"publishConfig": {
"ignore": [
".github/workflows",
"test",
".nycrc",
".eslintrc"
]
}
}
3 changes: 3 additions & 0 deletions test/fixtures/basic-comments/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
v20

# this is a comment
2 changes: 2 additions & 0 deletions test/fixtures/basic-invalid/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
bar
1 change: 1 addition & 0 deletions test/fixtures/basic-with-comment/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
21.2 # this is an inline comment
2 changes: 2 additions & 0 deletions test/fixtures/basic-with-npm/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node
npm=10
1 change: 1 addition & 0 deletions test/fixtures/basic/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node
4 changes: 4 additions & 0 deletions test/fixtures/duplicate-pairs/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node

npm=3
npm=4
1 change: 1 addition & 0 deletions test/fixtures/invalid/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=b
2 changes: 2 additions & 0 deletions test/fixtures/only-comments/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this is a comment
# nothing here but us comments!
2 changes: 2 additions & 0 deletions test/fixtures/pre-normalized/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node=foo
npm=bar
73 changes: 73 additions & 0 deletions test/index.mjs
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();
});
}
});

0 comments on commit 6b2fcec

Please sign in to comment.