Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 3, 2021
1 parent 4452a7c commit 9b35f2a
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 42 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
6 changes: 2 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ Block users from running your app with root permissions. When a file containing
@example
```
import sudoBlock = require('sudo-block');
import sudoBlock from 'sudo-block';
sudoBlock();
```
*/
declare function sudoBlock(message?: string): void;

export = sudoBlock;
export default function sudoBlock(message?: string): void;
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const chalk = require('chalk');
const isRoot = require('is-root');
const isDocker = require('is-docker');
import chalk from 'chalk';
import isRoot from 'is-root';
import isDocker from 'is-docker';

module.exports = message => {
export default function sudoBlock(message) {
const defaultMessage = chalk`
{red.bold You are not allowed to run this app with root permissions.}
If running without {bold sudo} doesn't work, you can either fix your permission problems or change where npm stores global packages by putting {bold ~/npm/bin} in your PATH and running:
Expand All @@ -15,4 +14,4 @@ See: {underline https://github.com/sindresorhus/guides/blob/main/npm-global-with
console.error(message || defaultMessage);
process.exit(77); // eslint-disable-line unicorn/no-process-exit
}
};
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sudoBlock = require('.');
import sudoBlock from './index.js';

sudoBlock();
sudoBlock('hi');
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
"description": "Block users from running your app with root permissions",
"license": "MIT",
"repository": "sindresorhus/sudo-block",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
"//test": "xo && ava && tsd",
"test": "xo && tsd"
},
"files": [
"index.js",
Expand All @@ -30,15 +34,15 @@
"stop"
],
"dependencies": {
"chalk": "^2.4.2",
"is-docker": "^2.0.0",
"is-root": "^2.1.0"
"chalk": "^4.1.1",
"is-docker": "^2.2.1",
"is-root": "^3.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"import-fresh": "^3.0.0",
"sinon": "^7.3.2",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"import-fresh": "^3.3.0",
"sinon": "^10.0.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
}
}
12 changes: 2 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
<img src="screenshot.png" width="660">


## Install

```
$ npm install sudo-block
```


## Usage

```js
const sudoBlock = require('sudo-block');
import sudoBlock from 'sudo-block';

sudoBlock();
```


## API

### sudoBlock([message])
### sudoBlock(message?)

When a file containing this function is run with root permissions it will exit and show an error message telling the user how to fix the problem, so they don't have to run it with `sudo`.

Expand All @@ -32,8 +29,3 @@ When a file containing this function is run with root permissions it will exit a
Type: `string`

Custom message.


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
4 changes: 2 additions & 2 deletions test/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ test.after(() => {
console.error.restore();
});

test('doesn\'t prevent docker users', t => {
importFresh('..')();
test('doesn not prevent docker users', t => {
importFresh('../index.js')();
t.false(process.exit.called);
t.false(console.error.called);
});
4 changes: 2 additions & 2 deletions test/sudo.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ test.after(() => {
});

test('prevent sudo', t => {
importFresh('..')();
importFresh('../index.js')();
t.true(process.exit.calledOnce);
t.true(process.exit.calledWith(77));
t.true(console.error.calledOnce);
t.true(console.error.calledWithMatch(/You are not allowed/));
});

test('accept custom messages', t => {
importFresh('..')('Thou shalt not sudo!');
importFresh('../index.js')('Thou shalt not sudo!');
t.true(console.error.calledWithMatch(/Thou shalt not sudo!/));
});
4 changes: 2 additions & 2 deletions test/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ test.after(() => {
console.error.restore();
});

test('doesn\'t prevent users', t => {
importFresh('..')();
test('doesn not prevent users', t => {
importFresh('../index.js')();
t.false(process.exit.called);
t.false(console.error.called);
});

0 comments on commit 9b35f2a

Please sign in to comment.