require-it 1.0.5 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,49 +1,73 @@
1
- # require-it
2
-
3
- [![Build Status](https://travis-ci.org/szikszail/require-it.svg?branch=master)](https://travis-ci.org/szikszail/require-it) [![dependency Status](https://david-dm.org/szikszail/require-it.svg)](https://david-dm.org/szikszail/require-it) [![devDependency Status](https://david-dm.org/szikszail/require-it/dev-status.svg)](https://david-dm.org/szikszail/require-it#info=devDependencies)
4
-
5
- This module extends the default nodejs require with capabilities to require nested modules, independent on where they are nested.
6
-
7
- **This module is for npm < 3.0, because new npm versions flatten the dependency tree and the problem is solved :)**
8
-
9
- ## Install
10
-
11
- npm install require-it --save
12
-
13
- ## Example
14
-
15
- myPackage
16
- + node_modules
17
- | + direct-dependecy
18
- | | + node_modules
19
- | | | \ nested-module
20
- | | | + index.js
21
- | | | \ package.json
22
- | | + index.js
23
- | | \ package.json
24
- | + index.js
25
- | \ package.json
26
- + index.js
27
- \ package.json
28
-
29
- `myPackage/index.js`:
30
-
31
- ```javascript
32
- var requireIt = require('require-it');
33
- // it will work and won't throw error
34
- var nestedModule = requireIt('nested-module');
35
- ```
36
-
37
- ## API
38
-
39
- ### `requireIt(moduleName)`
40
-
41
- Requires the given module, indenpendent on which level is it.
42
-
43
- ### `requireIt.resolve(moduleName)`
44
-
45
- Returns the path which could be required by `require`.
46
-
47
- ### `requireIt.directory(moduleName)`
48
-
49
- Returns the path wich could be required by `require` without the main JS file, ie. the path to the module's directory.
1
+ # require-it
2
+
3
+ ![Downloads](https://img.shields.io/npm/dw/require-it?style=flat-square)
4
+ ![Version@npm](https://img.shields.io/npm/v/require-it?label=version%40npm&style=flat-square)
5
+ ![Version@git](https://img.shields.io/github/package-json/v/szikszail/require-it/master?label=version%40git&style=flat-square)
6
+
7
+ This module extends the default Node.js require with capabilities to require nested modules, independent of where they are nested.
8
+
9
+ ## Install
10
+
11
+ npm install require-it --save
12
+
13
+ ## Example
14
+
15
+ myPackage
16
+ + node_modules
17
+ | + direct-dependecy
18
+ | | + node_modules
19
+ | | | \ nested-module
20
+ | | | + index.js
21
+ | | | \ package.json
22
+ | | + index.js
23
+ | | \ package.json
24
+ | + index.js
25
+ | \ package.json
26
+ + index.js
27
+ \ package.json
28
+
29
+ `myPackage/index.js`:
30
+
31
+ ```javascript
32
+ const { requireIt } = require('require-it');
33
+ // it will work and won't throw error
34
+ const nestedModule = requireIt('nested-module');
35
+ ```
36
+
37
+ ## API
38
+
39
+ ### `requireIt(moduleName)`
40
+
41
+ Requires the given local module, independent of which level it is.
42
+
43
+ #### `requireIt.resolve(moduleName)`
44
+
45
+ Returns the local path, which could be required by `require`.
46
+
47
+ #### `requireIt.directory(moduleName)`
48
+
49
+ Returns the local path, which could be required by `require` without the main JS file, i.e., the path to the module's directory.
50
+
51
+ ### `requireGlobal(globalModuleName)`
52
+
53
+ Requires the given global module.
54
+
55
+ #### `requireGlobal.resolve(globalModuleName)`
56
+
57
+ Returns the global path, which could be required by `require`.
58
+
59
+ #### `requireGlobal.directory(globalModuleName)`
60
+
61
+ Returns the global path, which could be required by `require` without the main JS file, i.e., the path to the module's directory.
62
+
63
+ ### `requireFrom(moduleName, root)`
64
+
65
+ Requires the given module found in the given root directory.
66
+
67
+ #### `requireFrom.resolve(moduleName, root)`
68
+
69
+ Returns the module's path, which could be required by `require`, found in the given root directory.
70
+
71
+ #### `requireFrom.directory(moduleName, root)`
72
+
73
+ Returns the module's path, which could be required by `require` without the main JS file, i.e., the path to the module's directory, found in the given root directory.
package/index.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export declare type ResolveFunction = (name: string) => string;
2
+ export declare type ResolveFromFunction = (name: string, root?: string) => string;
3
+ export declare type RequireFunction = (name: string) => object;
4
+ export declare type RequireFromFunction = (name: string, root?: string) => object;
5
+ export interface RequireObject extends RequireFunction {
6
+ resolve: ResolveFunction;
7
+ directory: ResolveFunction;
8
+ }
9
+ export interface RequireFromObject extends RequireFromFunction {
10
+ resolve: ResolveFromFunction;
11
+ directory: ResolveFromFunction;
12
+ }
13
+ export declare const requireIt: RequireObject;
14
+ export declare const requireGlobal: RequireObject;
15
+ export declare const requireFrom: RequireFromObject;
package/index.js CHANGED
@@ -1,67 +1,91 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const fs = require('fs');
5
-
6
- const utils = require('./utils');
7
-
8
- const requireIt = function (module) {
9
- return require(requireIt.resolve(module));
10
- };
11
-
12
- requireIt.resolve = module => {
13
- let pathToModule;
14
-
15
- const checkScopedNodeModulesOfFolder = (folder, module) => {
16
- const directModules = utils.getNodeModulesOfFolder(folder);
17
- for (let i = 0; i < directModules.length; ++i) {
18
- if (utils.getFolder(directModules[i]) === module) {
19
- pathToModule = path.join(folder, directModules[i]);
20
- break;
21
- }
22
- }
23
- }
24
- const checkNodeModulesOfFolder = (folder, module) => {
25
- const root = path.join(folder, 'node_modules');
26
- const nodeModules = utils.getNodeModulesOfFolder(root);
27
- for (let i = 0; !pathToModule && i < nodeModules.length; ++i) {
28
- if (utils.getFolder(nodeModules[i]) === module) {
29
- pathToModule = path.join(root, nodeModules[i]);
30
- } else {
31
- utils.getNodeModulesOfFolder(path.join(root, nodeModules[i], 'node_modules')).forEach(subModule => {
32
- nodeModules.push(path.join(nodeModules[i], 'node_modules', subModule));
33
- });
34
- }
35
- }
36
- }
37
-
38
- try {
39
- pathToModule = require.resolve(module);
40
- } catch (e) {
41
- let names = module.match(/^(@[^/]+)\/(.+)$/);
42
- if (names) {
43
- checkNodeModulesOfFolder(process.cwd(), names[1]);
44
- checkScopedNodeModulesOfFolder(pathToModule, names[2]);
45
- } else {
46
- checkNodeModulesOfFolder(process.cwd(), module);
47
- }
48
- }
49
- return pathToModule;
50
- };
51
-
52
- requireIt.directory = module => {
53
- let pathToModule = requireIt.resolve(module);
54
- if (!pathToModule) {
55
- throw Error(`Cannot find module: '${module}'`);
56
- }
57
- module = module.replace('/', path.sep);
58
- const pathPieces = pathToModule.split(module)
59
- .filter(p => !/^[/\\]$/.test(p))
60
- .filter(p => !/\.[^\.\\/]+$/.test(p));
61
- if (pathPieces.length > 1) {
62
- return pathPieces.join(module);
63
- }
64
- return path.join(pathPieces[0], module);
65
- };
66
-
67
- module.exports = requireIt;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireFrom = exports.requireGlobal = exports.requireIt = void 0;
4
+ const child_process_1 = require("child_process");
5
+ const path_1 = require("path");
6
+ const utils_1 = require("./utils");
7
+ let globalRoot;
8
+ const getGlobalRoot = () => {
9
+ if (!globalRoot) {
10
+ globalRoot = (0, child_process_1.execSync)("npm root -g", { encoding: "utf8" }).trim().replace(/node_modules$/, "");
11
+ }
12
+ return globalRoot;
13
+ };
14
+ const checkScopedNodeModulesOfFolder = (folder, name) => {
15
+ const directModules = (0, utils_1.getNodeModulesOfFolder)(folder);
16
+ const found = directModules.find((subfolder) => {
17
+ return (0, utils_1.getFolder)(subfolder) === name;
18
+ });
19
+ if (!found) {
20
+ throw Error(`Cannot find module: "${name}"`);
21
+ }
22
+ return (0, path_1.join)(folder, found);
23
+ };
24
+ const checkNodeModulesOfFolder = (folder, name) => {
25
+ const root = (0, path_1.join)(folder, "node_modules");
26
+ const nodeModules = (0, utils_1.getNodeModulesOfFolder)(root);
27
+ for (const nodeModule of nodeModules) {
28
+ if ((0, utils_1.getFolder)(nodeModule) === name) {
29
+ return (0, path_1.join)(root, nodeModule);
30
+ }
31
+ (0, utils_1.getNodeModulesOfFolder)((0, path_1.join)(root, nodeModule, "node_modules")).forEach((subModule) => {
32
+ nodeModules.push((0, path_1.join)(nodeModule, "node_modules", subModule));
33
+ });
34
+ }
35
+ };
36
+ const resolveMainFile = (name, root) => {
37
+ const names = name.match(/^(@[^/]+)\/(.+)$/);
38
+ let pathToFolder;
39
+ if (names) {
40
+ const pathToModule = checkNodeModulesOfFolder(root, names[1]);
41
+ pathToFolder = checkScopedNodeModulesOfFolder(pathToModule, names[2]);
42
+ }
43
+ else {
44
+ pathToFolder = checkNodeModulesOfFolder(root, name);
45
+ }
46
+ if (pathToFolder) {
47
+ return (0, path_1.join)(pathToFolder, (0, utils_1.readPackageJSON)(pathToFolder).main);
48
+ }
49
+ };
50
+ const resolve = (name, root) => {
51
+ if (!root) {
52
+ try {
53
+ return require.resolve(name);
54
+ }
55
+ catch (e) {
56
+ return resolveMainFile(name, process.cwd());
57
+ }
58
+ }
59
+ return resolveMainFile(name, root);
60
+ };
61
+ const directory = (name, root) => {
62
+ const pathToModule = resolve(name, root);
63
+ if (!pathToModule) {
64
+ throw Error(`Cannot find module: "${name}"`);
65
+ }
66
+ name = name.replace("/", path_1.sep);
67
+ const pathPieces = pathToModule
68
+ .split(name + path_1.sep)
69
+ .filter((p) => !/^[/\\]$/.test(p))
70
+ .filter((p) => !/\.[^\.\\/]+$/.test(p));
71
+ if (pathPieces.length > 1) {
72
+ return pathPieces.join(name);
73
+ }
74
+ return (0, path_1.join)(pathPieces[0], name);
75
+ };
76
+ exports.requireIt = ((name) => {
77
+ return require(exports.requireIt.resolve(name));
78
+ });
79
+ exports.requireIt.resolve = (name) => resolve(name);
80
+ exports.requireIt.directory = (name) => directory(name);
81
+ exports.requireGlobal = ((name) => {
82
+ return require(exports.requireGlobal.resolve(name));
83
+ });
84
+ exports.requireGlobal.resolve = (name) => resolve(name, getGlobalRoot());
85
+ exports.requireGlobal.directory = (name) => directory(name, getGlobalRoot());
86
+ exports.requireFrom = ((module, root) => {
87
+ return require(resolve(module, root));
88
+ });
89
+ exports.requireFrom.resolve = resolve;
90
+ exports.requireFrom.directory = directory;
91
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAAyC;AACzC,+BAAiC;AACjC,mCAA6E;AAe7E,IAAI,UAAkB,CAAC;AACvB,MAAM,aAAa,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CAAC,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;AAExE,QAAA,WAAW,GAAsB,CAAC,CAAC,MAAc,EAAE,IAAa,EAAU,EAAE;IACrF,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;AAC1C,CAAC,CAAkB,CAAC;AACpB,mBAAW,CAAC,OAAO,GAAG,OAAO,CAAC;AAC9B,mBAAW,CAAC,SAAS,GAAG,SAAS,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,19 @@
1
1
  {
2
2
  "name": "require-it",
3
- "version": "1.0.5",
4
- "description": "This module extends the default nodejs require with capabilities to require nested modules, independent on where they are nested.",
3
+ "version": "2.1.1",
4
+ "description": "This module extends the default Node.js require with capabilities to require nested modules, independent on where they are nested.",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
6
7
  "scripts": {
7
- "test": "node ./node_modules/jasmine-node/bin/jasmine-node ./test --verbose --color"
8
+ "tsc:watch": "tsc -w",
9
+ "build": "npm run clean && npm run compile && npm run copyToDist && npm test -- --coverage && npm run typedoc",
10
+ "buildUpdate": "npm run compile && npm run copyToDist",
11
+ "copyToDist": "copyfiles -f *.txt *.md package.json dist",
12
+ "typedoc": "typedoc --exclude **/bin/**/*.ts --exclude **/cli.ts --out ./dist/docs/api ./src",
13
+ "clean": "rimraf ./dist",
14
+ "test": "cross-env JEST_JUNIT_OUTPUT=./dist/reports/junit.xml jest",
15
+ "tslint": "tslint -c tslint.json ./src/**/*.ts ./tests/**/*.ts",
16
+ "compile": "tsc && npm run tslint"
8
17
  },
9
18
  "repository": {
10
19
  "type": "git",
@@ -18,16 +27,63 @@
18
27
  ],
19
28
  "author": "Laszlo Szikszai <sziklaszlo@gmail.com>",
20
29
  "license": "MIT",
30
+ "files": [
31
+ "*.js",
32
+ "*.d.ts",
33
+ "*.js.map"
34
+ ],
21
35
  "engines": {
22
- "node": ">=4.7.0"
36
+ "node": ">=12.0.0"
23
37
  },
24
38
  "bugs": {
25
39
  "url": "https://github.com/szikszail/require-it/issues"
26
40
  },
27
41
  "homepage": "https://github.com/szikszail/require-it#readme",
28
42
  "devDependencies": {
29
- "copy-dir": "^0.4.0",
30
- "jasmine-node": "^1.14.5",
31
- "sinon": "^6.0.0"
43
+ "@types/jest": "^26.0.20",
44
+ "@types/node": "^14.14.22",
45
+ "copyfiles": "^2.4.1",
46
+ "cross-env": "^7.0.3",
47
+ "jest": "^26.6.3",
48
+ "jest-junit": "^13.0.0",
49
+ "rimraf": "^3.0.2",
50
+ "ts-jest": "^26.4.4",
51
+ "tslint": "^6.1.3",
52
+ "typedoc": "^0.22.11",
53
+ "typescript": "4.4.4"
54
+ },
55
+ "jest": {
56
+ "transform": {
57
+ "^.+\\.ts?$": "ts-jest"
58
+ },
59
+ "testEnvironment": "node",
60
+ "testMatch": [
61
+ "**/tests/**/*.test.ts"
62
+ ],
63
+ "testPathIgnorePatterns": [
64
+ "/node_modules/",
65
+ "dist"
66
+ ],
67
+ "reporters": [
68
+ "default",
69
+ "jest-junit"
70
+ ],
71
+ "coverageDirectory": "./dist/reports/coverage",
72
+ "moduleFileExtensions": [
73
+ "ts",
74
+ "js"
75
+ ],
76
+ "collectCoverageFrom": [
77
+ "src/index.ts",
78
+ "src/utils.ts"
79
+ ],
80
+ "coverageThreshold": {
81
+ "global": {
82
+ "branches": 85,
83
+ "functions": 85,
84
+ "lines": 85,
85
+ "statements": 85
86
+ }
87
+ }
32
88
  }
33
89
  }
package/utils.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export declare const isFile: (file: string) => boolean;
2
+ export declare const isFolder: (folder: string) => boolean;
3
+ export declare const isScopedModule: (name: string) => boolean;
4
+ export declare const isNodeModule: (name: string) => boolean;
5
+ export declare const getNodeModulesOfFolder: (folder: string) => string[];
6
+ export interface PackageJSON {
7
+ main: string;
8
+ }
9
+ export declare const getFolder: (path: string) => string;
10
+ export declare const readPackageJSON: (folder: string) => PackageJSON;
package/utils.js CHANGED
@@ -1,39 +1,48 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
-
6
- const self = {
7
- isFile(file) {
8
- try {
9
- return fs.statSync(file).isFile();
10
- } catch (e) {
11
- return false;
12
- }
13
- },
14
- isFolder(folder) {
15
- try {
16
- return fs.statSync(folder).isDirectory();
17
- } catch (e) {
18
- return false;
19
- }
20
- },
21
- isNodeModule(folder) {
22
- return this.isFolder(folder)
23
- && [this.isFolder(path.join(folder, 'node_modules'))
24
- || this.isFile(path.join(folder, 'package.json'))];
25
- },
26
- getNodeModulesOfFolder(folder) {
27
- try {
28
- return fs.readdirSync(folder).filter(file => {
29
- return this.isNodeModule(path.join(folder, file));
30
- });
31
- } catch (e) {
32
- return [];
33
- }
34
- },
35
- getFolder(folder) {
36
- return folder.split(/[\/\\]/).pop();
37
- }
38
- };
39
- module.exports = self;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readPackageJSON = exports.getFolder = exports.getNodeModulesOfFolder = exports.isNodeModule = exports.isScopedModule = exports.isFolder = exports.isFile = void 0;
4
+ const fs_1 = require("fs");
5
+ const path_1 = require("path");
6
+ const isFile = (file) => {
7
+ try {
8
+ return (0, fs_1.statSync)(file).isFile();
9
+ }
10
+ catch (e) {
11
+ return false;
12
+ }
13
+ };
14
+ exports.isFile = isFile;
15
+ const isFolder = (folder) => {
16
+ try {
17
+ return (0, fs_1.statSync)(folder).isDirectory();
18
+ }
19
+ catch (e) {
20
+ return false;
21
+ }
22
+ };
23
+ exports.isFolder = isFolder;
24
+ const isScopedModule = (name) => {
25
+ return (0, exports.isFolder)(name) && /@[^/\\@]+$/.test(name);
26
+ };
27
+ exports.isScopedModule = isScopedModule;
28
+ const isNodeModule = (name) => {
29
+ return (0, exports.isFolder)(name) && ((0, exports.isFolder)((0, path_1.join)(name, "node_modules")) || (0, exports.isFile)((0, path_1.join)(name, "package.json")));
30
+ };
31
+ exports.isNodeModule = isNodeModule;
32
+ const getNodeModulesOfFolder = (folder) => {
33
+ try {
34
+ return (0, fs_1.readdirSync)(folder).filter((subfolder) => {
35
+ const path = (0, path_1.join)(folder, subfolder);
36
+ return (0, exports.isNodeModule)(path) || (0, exports.isScopedModule)(path);
37
+ });
38
+ }
39
+ catch (e) {
40
+ return [];
41
+ }
42
+ };
43
+ exports.getNodeModulesOfFolder = getNodeModulesOfFolder;
44
+ const getFolder = (path) => path.split(/[\/\\]/).pop();
45
+ exports.getFolder = getFolder;
46
+ const readPackageJSON = (folder) => require((0, path_1.join)(folder, "package.json"));
47
+ exports.readPackageJSON = readPackageJSON;
48
+ //# sourceMappingURL=utils.js.map
package/utils.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,2BAA2C;AAC3C,+BAA4B;AAErB,MAAM,MAAM,GAAG,CAAC,IAAY,EAAW,EAAE;IAC5C,IAAI;QACA,OAAO,IAAA,aAAQ,EAAC,IAAI,CAAC,CAAC,MAAM,EAAE,CAAC;KAClC;IAAC,OAAO,CAAC,EAAE;QACR,OAAO,KAAK,CAAC;KAChB;AACL,CAAC,CAAC;AANW,QAAA,MAAM,UAMjB;AAEK,MAAM,QAAQ,GAAG,CAAC,MAAc,EAAW,EAAE;IAChD,IAAI;QACA,OAAO,IAAA,aAAQ,EAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;KACzC;IAAC,OAAO,CAAC,EAAE;QACR,OAAO,KAAK,CAAC;KAChB;AACL,CAAC,CAAC;AANW,QAAA,QAAQ,YAMnB;AAEK,MAAM,cAAc,GAAG,CAAC,IAAY,EAAW,EAAE;IACpD,OAAO,IAAA,gBAAQ,EAAC,IAAI,CAAC,IAAI,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACrD,CAAC,CAAC;AAFW,QAAA,cAAc,kBAEzB;AAEK,MAAM,YAAY,GAAG,CAAC,IAAY,EAAW,EAAE;IAClD,OAAO,IAAA,gBAAQ,EAAC,IAAI,CAAC,IAAI,CAAC,IAAA,gBAAQ,EAAC,IAAA,WAAI,EAAC,IAAI,EAAE,cAAc,CAAC,CAAC,IAAI,IAAA,cAAM,EAAC,IAAA,WAAI,EAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC;AAC1G,CAAC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEK,MAAM,sBAAsB,GAAG,CAAC,MAAc,EAAY,EAAE;IAC/D,IAAI;QACA,OAAO,IAAA,gBAAW,EAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,SAAiB,EAAW,EAAE;YAC7D,MAAM,IAAI,GAAG,IAAA,WAAI,EAAC,MAAM,EAAE,SAAS,CAAC,CAAC;YACrC,OAAO,IAAA,oBAAY,EAAC,IAAI,CAAC,IAAI,IAAA,sBAAc,EAAC,IAAI,CAAC,CAAC;QACtD,CAAC,CAAC,CAAC;KACN;IAAC,OAAO,CAAC,EAAE;QACR,OAAO,EAAE,CAAC;KACb;AACL,CAAC,CAAC;AATW,QAAA,sBAAsB,0BASjC;AAMK,MAAM,SAAS,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,GAAG,EAAE,CAAC;AAAjE,QAAA,SAAS,aAAwD;AACvE,MAAM,eAAe,GAAG,CAAC,MAAc,EAAe,EAAE,CAAC,OAAO,CAAC,IAAA,WAAI,EAAC,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC;AAAzF,QAAA,eAAe,mBAA0E"}
package/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - 4
4
- - 6
5
- - 8
6
- - 10
7
- scripts:
8
- - npm test
9
- notifications:
10
- email: false
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2016 Szikszai László
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.