diff options
Diffstat (limited to 'node_modules/get-package-type')
-rw-r--r-- | node_modules/get-package-type/CHANGELOG.md | 10 | ||||
-rw-r--r-- | node_modules/get-package-type/LICENSE | 21 | ||||
-rw-r--r-- | node_modules/get-package-type/README.md | 32 | ||||
-rw-r--r-- | node_modules/get-package-type/async.cjs | 52 | ||||
-rw-r--r-- | node_modules/get-package-type/cache.cjs | 3 | ||||
-rw-r--r-- | node_modules/get-package-type/index.cjs | 7 | ||||
-rw-r--r-- | node_modules/get-package-type/is-node-modules.cjs | 15 | ||||
-rw-r--r-- | node_modules/get-package-type/package.json | 35 | ||||
-rw-r--r-- | node_modules/get-package-type/sync.cjs | 42 |
9 files changed, 217 insertions, 0 deletions
diff --git a/node_modules/get-package-type/CHANGELOG.md b/node_modules/get-package-type/CHANGELOG.md new file mode 100644 index 0000000..5f2c4cc --- /dev/null +++ b/node_modules/get-package-type/CHANGELOG.md @@ -0,0 +1,10 @@ +# Changelog + +All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +## 0.1.0 (2020-05-19) + + +### Features + +* Initial implementation ([52863f4](https://github.com/cfware/get-package-type/commit/52863f4b2b7b287fe1adcd97331231a2911312dc)) diff --git a/node_modules/get-package-type/LICENSE b/node_modules/get-package-type/LICENSE new file mode 100644 index 0000000..971e3b7 --- /dev/null +++ b/node_modules/get-package-type/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 CFWare, LLC + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/node_modules/get-package-type/README.md b/node_modules/get-package-type/README.md new file mode 100644 index 0000000..8e1ebf2 --- /dev/null +++ b/node_modules/get-package-type/README.md @@ -0,0 +1,32 @@ +# get-package-type [![NPM Version][npm-image]][npm-url] + +Determine the `package.json#type` which applies to a location. + +## Usage + +```js +const getPackageType = require('get-package-type'); + +(async () => { + console.log(await getPackageType('file.js')); + console.log(getPackageType.sync('file.js')); +})(); +``` + +This function does not validate the value found in `package.json#type`. Any truthy value +found will be returned. Non-truthy values will be reported as `commonjs`. + +The argument must be a filename. +```js +// This never looks at `dir1/`, first attempts to load `./package.json`. +const type1 = await getPackageType('dir1/'); + +// This attempts to load `dir1/package.json`. +const type2 = await getPackageType('dir1/index.cjs'); +``` + +The extension of the filename does not effect the result. The primary use case for this +module is to determine if `myapp.config.js` should be loaded with `require` or `import`. + +[npm-image]: https://img.shields.io/npm/v/get-package-type.svg +[npm-url]: https://npmjs.org/package/get-package-type diff --git a/node_modules/get-package-type/async.cjs b/node_modules/get-package-type/async.cjs new file mode 100644 index 0000000..fa7fd5c --- /dev/null +++ b/node_modules/get-package-type/async.cjs @@ -0,0 +1,52 @@ +'use strict'; + +const path = require('path'); +const {promisify} = require('util'); +const readFile = promisify(require('fs').readFile); + +const isNodeModules = require('./is-node-modules.cjs'); +const resultsCache = require('./cache.cjs'); + +const promiseCache = new Map(); + +async function getDirectoryTypeActual(directory) { + if (isNodeModules(directory)) { + return 'commonjs'; + } + + try { + return JSON.parse(await readFile(path.resolve(directory, 'package.json'))).type || 'commonjs'; + } catch (_) { + } + + const parent = path.dirname(directory); + if (parent === directory) { + return 'commonjs'; + } + + return getDirectoryType(parent); +} + +async function getDirectoryType(directory) { + if (resultsCache.has(directory)) { + return resultsCache.get(directory); + } + + if (promiseCache.has(directory)) { + return promiseCache.get(directory); + } + + const promise = getDirectoryTypeActual(directory); + promiseCache.set(directory, promise); + const result = await promise; + resultsCache.set(directory, result); + promiseCache.delete(directory); + + return result; +} + +function getPackageType(filename) { + return getDirectoryType(path.resolve(path.dirname(filename))); +} + +module.exports = getPackageType; diff --git a/node_modules/get-package-type/cache.cjs b/node_modules/get-package-type/cache.cjs new file mode 100644 index 0000000..4fd928a --- /dev/null +++ b/node_modules/get-package-type/cache.cjs @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = new Map(); diff --git a/node_modules/get-package-type/index.cjs b/node_modules/get-package-type/index.cjs new file mode 100644 index 0000000..b5b0734 --- /dev/null +++ b/node_modules/get-package-type/index.cjs @@ -0,0 +1,7 @@ +'use strict'; + +const getPackageType = require('./async.cjs'); +const getPackageTypeSync = require('./sync.cjs'); + +module.exports = filename => getPackageType(filename); +module.exports.sync = getPackageTypeSync; diff --git a/node_modules/get-package-type/is-node-modules.cjs b/node_modules/get-package-type/is-node-modules.cjs new file mode 100644 index 0000000..5a37a77 --- /dev/null +++ b/node_modules/get-package-type/is-node-modules.cjs @@ -0,0 +1,15 @@ +'use strict'; + +const path = require('path'); + +function isNodeModules(directory) { + let basename = path.basename(directory); + /* istanbul ignore next: platform specific branch */ + if (path.sep === '\\') { + basename = basename.toLowerCase(); + } + + return basename === 'node_modules'; +} + +module.exports = isNodeModules; diff --git a/node_modules/get-package-type/package.json b/node_modules/get-package-type/package.json new file mode 100644 index 0000000..dcb0ea8 --- /dev/null +++ b/node_modules/get-package-type/package.json @@ -0,0 +1,35 @@ +{ + "name": "get-package-type", + "version": "0.1.0", + "description": "Determine the `package.json#type` which applies to a location", + "type": "module", + "main": "index.cjs", + "exports": "./index.cjs", + "scripts": { + "pretest": "if-ver -ge 10 || exit 0; cfware-lint .", + "tests-only": "nyc -s node test.cjs", + "test": "npm run -s tests-only", + "posttest": "nyc report --check-coverage" + }, + "engines": { + "node": ">=8.0.0" + }, + "author": "Corey Farrell", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/cfware/get-package-type.git" + }, + "bugs": { + "url": "https://github.com/cfware/get-package-type/issues" + }, + "homepage": "https://github.com/cfware/get-package-type#readme", + "dependencies": {}, + "devDependencies": { + "@cfware/lint": "^1.4.3", + "@cfware/nyc": "^0.7.0", + "if-ver": "^1.1.0", + "libtap": "^0.3.0", + "nyc": "^15.0.1" + } +} diff --git a/node_modules/get-package-type/sync.cjs b/node_modules/get-package-type/sync.cjs new file mode 100644 index 0000000..85090a6 --- /dev/null +++ b/node_modules/get-package-type/sync.cjs @@ -0,0 +1,42 @@ +'use strict'; + +const path = require('path'); +const {readFileSync} = require('fs'); + +const isNodeModules = require('./is-node-modules.cjs'); +const resultsCache = require('./cache.cjs'); + +function getDirectoryTypeActual(directory) { + if (isNodeModules(directory)) { + return 'commonjs'; + } + + try { + return JSON.parse(readFileSync(path.resolve(directory, 'package.json'))).type || 'commonjs'; + } catch (_) { + } + + const parent = path.dirname(directory); + if (parent === directory) { + return 'commonjs'; + } + + return getDirectoryType(parent); +} + +function getDirectoryType(directory) { + if (resultsCache.has(directory)) { + return resultsCache.get(directory); + } + + const result = getDirectoryTypeActual(directory); + resultsCache.set(directory, result); + + return result; +} + +function getPackageTypeSync(filename) { + return getDirectoryType(path.resolve(path.dirname(filename))); +} + +module.exports = getPackageTypeSync; |