Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 4, 2022
1 parent 21ae944 commit c5e8e5c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 89 deletions.
21 changes: 6 additions & 15 deletions .github/workflows/main.yml
Expand Up @@ -4,28 +4,19 @@ on:
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
- 12
- 10
- 8
os:
- macos-latest
- ubuntu-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
if: matrix.os == 'ubuntu-latest' && matrix.node-version == 14
with:
fail_ci_if_error: true
2 changes: 0 additions & 2 deletions .gitignore
@@ -1,4 +1,2 @@
node_modules
yarn.lock
.nyc_output
coverage
29 changes: 14 additions & 15 deletions index.js
@@ -1,24 +1,23 @@
'use strict';
const path = require('path');
const fs = require('fs');
const commonDir = require('commondir');
const pkgDir = require('pkg-dir');
const makeDir = require('make-dir');
import process from 'node:process';
import path from 'node:path';
import fs from 'node:fs';
import commonPathPrefix from 'common-path-prefix';
import {packageDirectorySync} from 'pkg-dir';

const {env, cwd} = process;

const isWritable = path => {
try {
fs.accessSync(path, fs.constants.W_OK);
return true;
} catch (_) {
} catch {
return false;
}
};

function useDirectory(directory, options) {
if (options.create) {
makeDir.sync(directory);
fs.mkdirSync(directory, {recursive: true});
}

if (options.thunk) {
Expand All @@ -32,36 +31,36 @@ function getNodeModuleDirectory(directory) {
const nodeModules = path.join(directory, 'node_modules');

if (
!isWritable(nodeModules) &&
(fs.existsSync(nodeModules) || !isWritable(path.join(directory)))
!isWritable(nodeModules)
&& (fs.existsSync(nodeModules) || !isWritable(path.join(directory)))
) {
return;
}

return nodeModules;
}

module.exports = (options = {}) => {
export default function findCacheDirectory(options = {}) {
if (env.CACHE_DIR && !['true', 'false', '1', '0'].includes(env.CACHE_DIR)) {
return useDirectory(path.join(env.CACHE_DIR, options.name), options);
}

let {cwd: directory = cwd()} = options;

if (options.files) {
directory = commonDir(directory, options.files);
directory = commonPathPrefix(options.files.map(file => path.resolve(directory, file)));
}

directory = pkgDir.sync(directory);
directory = packageDirectorySync({cwd: directory});

if (!directory) {
return;
}

const nodeModules = getNodeModuleDirectory(directory);
if (!nodeModules) {
return undefined;
return;
}

return useDirectory(path.join(directory, 'node_modules', '.cache', options.name), options);
};
}
1 change: 1 addition & 0 deletions license
@@ -1,6 +1,7 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
Copyright (c) James Talmage <james@talmage.io> (https://github.com/jamestalmage)

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
37 changes: 19 additions & 18 deletions package.json
Expand Up @@ -3,13 +3,20 @@
"version": "3.3.2",
"description": "Finds the common standard cache directory",
"license": "MIT",
"repository": "avajs/find-cache-dir",
"funding": "https://github.com/avajs/find-cache-dir?sponsor=1",
"repository": "sindresorhus/find-cache-dir",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=14.16"
},
"scripts": {
"test": "xo && nyc ava"
"test": "xo && ava"
},
"files": [
"index.js"
Expand All @@ -23,22 +30,16 @@
"search"
],
"dependencies": {
"commondir": "^1.0.1",
"make-dir": "^3.0.2",
"pkg-dir": "^4.1.0"
"common-path-prefix": "^3.0.0",
"pkg-dir": "^7.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"coveralls": "^3.0.9",
"del": "^4.0.0",
"nyc": "^15.0.0",
"tempy": "^0.4.0",
"xo": "^0.25.3"
"ava": "^5.0.1",
"del": "^7.0.0",
"tempy": "^3.0.0",
"xo": "^0.52.4"
},
"nyc": {
"reporter": [
"lcov",
"text"
]
"ava": {
"workerThreads": false
}
}
26 changes: 6 additions & 20 deletions readme.md
@@ -1,4 +1,4 @@
# find-cache-dir [![Coverage Status](https://codecov.io/gh/avajs/find-cache-dir/branch/master/graph/badge.svg)](https://codecov.io/gh/avajs/find-cache-dir/branch/master)
# find-cache-dir

> Finds the common standard cache directory
Expand All @@ -21,26 +21,24 @@ This module makes it easy to correctly locate the cache directory according to t
rm -rf ./node_modules/.cache
```

If you decide to adopt this pattern, please file a PR adding your name to the list of adopters below.

## Install

```
$ npm install find-cache-dir
```sh
npm install find-cache-dir
```

## Usage

```js
const findCacheDir = require('find-cache-dir');
import findCacheDirectory from 'find-cache-dir';

findCacheDir({name: 'unicorns'});
findCacheDirectory({name: 'unicorns'});
//=> '/user/path/node-modules/.cache/unicorns'
```

## API

### findCacheDir(options?)
### findCacheDirectory(options?)

Finds the cache directory using the supplied options. The algorithm checks for the `CACHE_DIR` environmental variable and uses it if it is not set to `true`, `false`, `1` or `0`. If one is not found, it tries to find a `package.json` file, searching every parent directory of the `cwd` specified (or implied from other options). It returns a `string` containing the absolute path to the cache directory, or `undefined` if `package.json` was never found or if the `node_modules` directory is unwritable.

Expand Down Expand Up @@ -109,15 +107,3 @@ This is helpful for actually putting actual files in the cache!
- [`babel-loader`](https://github.com/babel/babel-loader)
- [`eslint-loader`](https://github.com/MoOx/eslint-loader)
- [More…](https://www.npmjs.com/browse/depended/find-cache-dir)

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-find_cache-dir?utm_source=npm-find-cache-dir&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
42 changes: 23 additions & 19 deletions test.js
@@ -1,57 +1,61 @@
import fs from 'fs';
import path from 'path';
import {serial as test} from 'ava';
import del from 'del';
import {directory as tempDirectory} from 'tempy';
import findCacheDir from '.';
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import {deleteSync} from 'del';
import {temporaryDirectory} from 'tempy';
import findCacheDirectory from './index.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

test('finds from a list of files', t => {
process.chdir(path.join(__dirname, '..'));
const files = ['foo/bar', 'baz/quz'].map(file => path.join(__dirname, file));
t.is(findCacheDir({files, name: 'blah'}), path.join(__dirname, 'node_modules', '.cache', 'blah'));
t.is(findCacheDirectory({files, name: 'blah'}), path.join(__dirname, 'node_modules', '.cache', 'blah'));
});

test('finds from process.cwd', t => {
process.chdir(path.join(__dirname));
t.is(findCacheDir({name: 'foo'}), path.join(__dirname, 'node_modules', '.cache', 'foo'));
t.is(findCacheDirectory({name: 'foo'}), path.join(__dirname, 'node_modules', '.cache', 'foo'));
});

test('finds from options.cwd', t => {
process.chdir(path.join(__dirname, '..'));
t.is(findCacheDir({cwd: __dirname, name: 'bar'}), path.join(__dirname, 'node_modules', '.cache', 'bar'));
t.is(findCacheDirectory({cwd: __dirname, name: 'bar'}), path.join(__dirname, 'node_modules', '.cache', 'bar'));
});

test('creates dir', t => {
const directory = path.join(__dirname, 'node_modules', '.cache', 'created');
del.sync(directory);
findCacheDir({create: true, name: 'created', cwd: __dirname});
deleteSync(directory);
findCacheDirectory({create: true, name: 'created', cwd: __dirname});
t.true(fs.existsSync(directory));
});

test('thunk', t => {
const directory = path.join(__dirname, 'node_modules', '.cache', 'thunked');
del.sync(directory);
const thunk = findCacheDir({thunk: true, name: 'thunked', cwd: __dirname});
deleteSync(directory);
const thunk = findCacheDirectory({thunk: true, name: 'thunked', cwd: __dirname});
t.is(thunk('foo'), path.join(directory, 'foo'));
t.is(thunk('bar'), path.join(directory, 'bar'));
});

test('returns undefined if it can\'t find package.json', t => {
process.chdir(path.join(__dirname, '..'));
t.is(findCacheDir({name: 'foo'}), undefined);
t.is(findCacheDirectory({name: 'foo'}), undefined);
});

test('supports CACHE_DIR environment variable', t => {
const newCacheDirectory = tempDirectory();
const newCacheDirectory = temporaryDirectory();
const finalDirectory = path.join(newCacheDirectory, 'some-package');
process.env.CACHE_DIR = newCacheDirectory;

t.is(findCacheDir({name: 'some-package'}), finalDirectory);
t.is(findCacheDirectory({name: 'some-package'}), finalDirectory);

findCacheDir({name: 'some-package', create: true});
findCacheDirectory({name: 'some-package', create: true});
t.true(fs.existsSync(finalDirectory));

const thunk = findCacheDir({name: 'some-package', thunk: true});
const thunk = findCacheDirectory({name: 'some-package', thunk: true});
t.is(thunk('foo'), path.join(finalDirectory, 'foo'));
t.is(thunk('bar'), path.join(finalDirectory, 'bar'));

Expand All @@ -61,5 +65,5 @@ test('supports CACHE_DIR environment variable', t => {
test('ignores `false` for CACHE_DIR environment variable', t => {
process.env.CACHE_DIR = 'false';

t.not(findCacheDir(), path.resolve(__dirname, 'false', 'find-cache-dir'));
t.not(findCacheDirectory(), path.resolve(__dirname, 'false', 'find-cache-dir'));
});

0 comments on commit c5e8e5c

Please sign in to comment.