@servicetitan/restrict-imports 30.3.0 → 31.0.0
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/dist/config.js +60 -33
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -4
- package/dist/index.js.map +1 -1
- package/dist/restrict-imports.js +59 -52
- package/dist/restrict-imports.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +3 -2
package/dist/config.js
CHANGED
|
@@ -1,21 +1,45 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InvalidEntry = exports.InvalidConfig = void 0;
|
|
4
|
-
exports.validateRawConfig = validateRawConfig;
|
|
5
|
-
exports.validateRawEntry = validateRawEntry;
|
|
6
|
-
exports.canonicalizeConfig = canonicalizeConfig;
|
|
7
|
-
exports.canonicalizeEntry = canonicalizeEntry;
|
|
8
|
-
exports.canonicalizeAllow = canonicalizeAllow;
|
|
9
1
|
/**
|
|
10
2
|
* validateRawConfig
|
|
11
|
-
*/
|
|
3
|
+
*/ "use strict";
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
function _export(target, all) {
|
|
8
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
_export(exports, {
|
|
14
|
+
get InvalidConfig () {
|
|
15
|
+
return InvalidConfig;
|
|
16
|
+
},
|
|
17
|
+
get InvalidEntry () {
|
|
18
|
+
return InvalidEntry;
|
|
19
|
+
},
|
|
20
|
+
get canonicalizeAllow () {
|
|
21
|
+
return canonicalizeAllow;
|
|
22
|
+
},
|
|
23
|
+
get canonicalizeConfig () {
|
|
24
|
+
return canonicalizeConfig;
|
|
25
|
+
},
|
|
26
|
+
get canonicalizeEntry () {
|
|
27
|
+
return canonicalizeEntry;
|
|
28
|
+
},
|
|
29
|
+
get validateRawConfig () {
|
|
30
|
+
return validateRawConfig;
|
|
31
|
+
},
|
|
32
|
+
get validateRawEntry () {
|
|
33
|
+
return validateRawEntry;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
12
36
|
function validateRawConfig(maybeConfig) {
|
|
13
37
|
if (maybeConfig === undefined) {
|
|
14
38
|
return false;
|
|
15
39
|
}
|
|
16
40
|
if (!Array.isArray(maybeConfig)) {
|
|
17
41
|
if (validateRawEntry(maybeConfig)) {
|
|
18
|
-
return
|
|
42
|
+
return InvalidConfig.ExpectedAnArrayOrASingleEntry;
|
|
19
43
|
}
|
|
20
44
|
return false;
|
|
21
45
|
}
|
|
@@ -30,45 +54,46 @@ function validateRawConfig(maybeConfig) {
|
|
|
30
54
|
return false;
|
|
31
55
|
// ------------------
|
|
32
56
|
function isThereInvalidEntry(c0) {
|
|
33
|
-
for
|
|
57
|
+
for(let index = 0; index < c0.length; index += 1){
|
|
34
58
|
const entry = c0[index];
|
|
35
59
|
const error = validateRawEntry(entry);
|
|
36
60
|
if (error) {
|
|
37
|
-
return
|
|
61
|
+
return InvalidConfig.InvalidEntry(index, entry, error);
|
|
38
62
|
}
|
|
39
63
|
}
|
|
40
64
|
return false;
|
|
41
65
|
}
|
|
42
66
|
function isInvalidEntryOrder(c0) {
|
|
43
67
|
const available = new Set();
|
|
44
|
-
for
|
|
68
|
+
for(let index = 0; index < c0.length; index += 1){
|
|
45
69
|
const entry = c0[index];
|
|
46
70
|
available.add(entry.name);
|
|
47
71
|
const allow0 = canonicalizeAllow(entry.allow);
|
|
48
|
-
const allow = allow0 === '*' ? [
|
|
49
|
-
|
|
72
|
+
const allow = allow0 === '*' ? [
|
|
73
|
+
allow0
|
|
74
|
+
] : allow0;
|
|
75
|
+
const unavailable = allow.find((n)=>!available.has(n) && !available.has('*'));
|
|
50
76
|
if (unavailable) {
|
|
51
|
-
return
|
|
77
|
+
return InvalidConfig.InvalidOrder(index, entry, unavailable);
|
|
52
78
|
}
|
|
53
79
|
}
|
|
54
80
|
return false;
|
|
55
81
|
}
|
|
56
82
|
}
|
|
57
|
-
|
|
83
|
+
const InvalidConfig = {
|
|
58
84
|
ExpectedAnArrayOrASingleEntry: 'Expected an array of config entries, or a single entry',
|
|
59
|
-
InvalidEntry: (index, entry, error)
|
|
60
|
-
InvalidOrder: (index, entry, unavailable)
|
|
61
|
-
` '${unavailable}' is used before its description`,
|
|
85
|
+
InvalidEntry: (index, entry, error)=>`Invalid entry #${index} (${JSON.stringify(entry)}): ${error}`,
|
|
86
|
+
InvalidOrder: (index, entry, unavailable)=>`Invalid entries order, #${index} (${JSON.stringify(entry)}):` + ` '${unavailable}' is used before its description`
|
|
62
87
|
};
|
|
63
88
|
function validateRawEntry(e) {
|
|
64
89
|
if (!isPlainObject(e)) {
|
|
65
|
-
return
|
|
90
|
+
return "Expected a plain object with fields `name` and `allow`";
|
|
66
91
|
}
|
|
67
92
|
if (!isValidName(e.name)) {
|
|
68
|
-
return
|
|
93
|
+
return "The `name` field is expected to be `string`";
|
|
69
94
|
}
|
|
70
95
|
if (!isValidAllow(e.allow)) {
|
|
71
|
-
return
|
|
96
|
+
return "The `allow` field is expected to be `string|string[]|undefined`";
|
|
72
97
|
}
|
|
73
98
|
return false;
|
|
74
99
|
// ------------------
|
|
@@ -86,35 +111,37 @@ function validateRawEntry(e) {
|
|
|
86
111
|
if (!Array.isArray(allow)) {
|
|
87
112
|
return false;
|
|
88
113
|
}
|
|
89
|
-
return !allow.some(v
|
|
114
|
+
return !allow.some((v)=>typeof v !== 'string');
|
|
90
115
|
}
|
|
91
116
|
}
|
|
92
|
-
var InvalidEntry
|
|
93
|
-
(function (InvalidEntry) {
|
|
117
|
+
var InvalidEntry = /*#__PURE__*/ function(InvalidEntry) {
|
|
94
118
|
InvalidEntry["ExpectedAPlainObject"] = "Expected a plain object with fields `name` and `allow`";
|
|
95
119
|
InvalidEntry["InvalidName"] = "The `name` field is expected to be `string`";
|
|
96
120
|
InvalidEntry["InvalidAllow"] = "The `allow` field is expected to be `string|string[]|undefined`";
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
* canonicalizeConfig
|
|
100
|
-
*/
|
|
121
|
+
return InvalidEntry;
|
|
122
|
+
}({});
|
|
101
123
|
function canonicalizeConfig(c = []) {
|
|
102
124
|
if (Array.isArray(c)) {
|
|
103
125
|
return c.map(canonicalizeEntry);
|
|
104
126
|
}
|
|
105
|
-
return [
|
|
127
|
+
return [
|
|
128
|
+
canonicalizeEntry(c)
|
|
129
|
+
];
|
|
106
130
|
}
|
|
107
131
|
function canonicalizeEntry(e) {
|
|
108
132
|
return {
|
|
109
133
|
name: e.name,
|
|
110
|
-
allow: canonicalizeAllow(e.allow)
|
|
134
|
+
allow: canonicalizeAllow(e.allow)
|
|
111
135
|
};
|
|
112
136
|
}
|
|
113
137
|
function canonicalizeAllow(a) {
|
|
114
|
-
const allow = Array.isArray(a) ? a : a ? [
|
|
138
|
+
const allow = Array.isArray(a) ? a : a ? [
|
|
139
|
+
a
|
|
140
|
+
] : [];
|
|
115
141
|
if (allow.includes('*')) {
|
|
116
142
|
return '*';
|
|
117
143
|
}
|
|
118
144
|
return allow;
|
|
119
145
|
}
|
|
146
|
+
|
|
120
147
|
//# sourceMappingURL=config.js.map
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/config.ts"],"sourcesContent":["/**\n * validateRawConfig\n */\nexport function validateRawConfig(maybeConfig: any): false | string {\n if (maybeConfig === undefined) {\n return false;\n }\n\n if (!Array.isArray(maybeConfig)) {\n if (validateRawEntry(maybeConfig)) {\n return InvalidConfig.ExpectedAnArrayOrASingleEntry;\n }\n\n return false;\n }\n\n const invalidEntryError = isThereInvalidEntry(maybeConfig);\n if (invalidEntryError) {\n return invalidEntryError;\n }\n\n const invalidOrderError = isInvalidEntryOrder(maybeConfig);\n if (invalidOrderError) {\n return invalidOrderError;\n }\n\n return false;\n\n // ------------------\n\n function isThereInvalidEntry(c0: any[]): false | string {\n for (let index = 0; index < c0.length; index += 1) {\n const entry = c0[index];\n const error = validateRawEntry(entry);\n if (error) {\n return InvalidConfig.InvalidEntry(index, entry, error);\n }\n }\n\n return false;\n }\n\n function isInvalidEntryOrder(c0: RawEntry[]): false | string {\n const available = new Set<string>();\n\n for (let index = 0; index < c0.length; index += 1) {\n const entry = c0[index];\n\n available.add(entry.name);\n\n const allow0 = canonicalizeAllow(entry.allow);\n const allow = allow0 === '*' ? [allow0] : allow0;\n\n const unavailable = allow.find(n => !available.has(n) && !available.has('*'));\n if (unavailable) {\n return InvalidConfig.InvalidOrder(index, entry, unavailable);\n }\n }\n\n return false;\n }\n}\n\nexport const InvalidConfig = {\n ExpectedAnArrayOrASingleEntry: 'Expected an array of config entries, or a single entry',\n\n InvalidEntry: (index: number, entry: any, error: string) =>\n `Invalid entry #${index} (${JSON.stringify(entry)}): ${error}`,\n\n InvalidOrder: (index: number, entry: RawEntry, unavailable: string) =>\n `Invalid entries order, #${index} (${JSON.stringify(entry)}):` +\n ` '${unavailable}' is used before its description`,\n};\n\nexport function validateRawEntry(e: any): false | InvalidEntry {\n if (!isPlainObject(e)) {\n return InvalidEntry.ExpectedAPlainObject;\n }\n\n if (!isValidName(e.name)) {\n return InvalidEntry.InvalidName;\n }\n\n if (!isValidAllow(e.allow)) {\n return InvalidEntry.InvalidAllow;\n }\n\n return false;\n\n // ------------------\n\n // eslint-disable-next-line @typescript-eslint/no-wrapper-object-types\n function isPlainObject(v: any): v is Object {\n return v != null && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype;\n }\n\n function isValidName(name: any): name is string {\n return typeof name === 'string';\n }\n\n function isValidAllow(allow: any): allow is RawAllow {\n if (typeof allow === 'string' || allow === undefined) {\n return true;\n }\n\n if (!Array.isArray(allow)) {\n return false;\n }\n\n return !allow.some(v => typeof v !== 'string');\n }\n}\n\nexport enum InvalidEntry {\n ExpectedAPlainObject = 'Expected a plain object with fields `name` and `allow`',\n InvalidName = 'The `name` field is expected to be `string`',\n InvalidAllow = 'The `allow` field is expected to be `string|string[]|undefined`',\n}\n\n/**\n * RawConfig\n */\nexport type RawConfig = RawEntry | RawEntry[] | undefined;\n\nexport interface RawEntry {\n name: string;\n allow?: string | string[];\n}\n\nexport type RawAllow = RawEntry['allow'];\n\n/**\n * Config\n */\nexport type Config = Entry[];\n\nexport interface Entry extends RawEntry {\n name: string;\n allow: Allow;\n}\n\nexport type Allow = '*' | string[];\n\n/**\n * canonicalizeConfig\n */\nexport function canonicalizeConfig(c: RawConfig = []): Config {\n if (Array.isArray(c)) {\n return c.map(canonicalizeEntry);\n }\n return [canonicalizeEntry(c)];\n}\n\nexport function canonicalizeEntry(e: RawEntry): Entry {\n return {\n name: e.name,\n allow: canonicalizeAllow(e.allow),\n };\n}\n\nexport function canonicalizeAllow(a: RawAllow): Allow {\n const allow = Array.isArray(a) ? a : a ? [a] : [];\n\n if (allow.includes('*')) {\n return '*';\n }\n\n return allow;\n}\n"],"names":["InvalidConfig","InvalidEntry","canonicalizeAllow","canonicalizeConfig","canonicalizeEntry","validateRawConfig","validateRawEntry","maybeConfig","undefined","Array","isArray","ExpectedAnArrayOrASingleEntry","invalidEntryError","isThereInvalidEntry","invalidOrderError","isInvalidEntryOrder","c0","index","length","entry","error","available","Set","add","name","allow0","allow","unavailable","find","n","has","InvalidOrder","JSON","stringify","e","isPlainObject","isValidName","isValidAllow","v","Object","getPrototypeOf","prototype","some","c","map","a","includes"],"mappings":"AAAA;;CAEC;;;;;;;;;;;QA6DYA;eAAAA;;QAkDDC;eAAAA;;QA+CIC;eAAAA;;QAdAC;eAAAA;;QAOAC;eAAAA;;QAtJAC;eAAAA;;QAuEAC;eAAAA;;;AAvET,SAASD,kBAAkBE,WAAgB;IAC9C,IAAIA,gBAAgBC,WAAW;QAC3B,OAAO;I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IA,IAAI;QAACA;KAAE,GAAG,EAAE;IAEjD,IAAInB,MAAMoB,QAAQ,CAAC,MAAM;QACrB,OAAO;IACX;IAEA,OAAOpB;AACX"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { RawConfig as Config, RawEntry as Entry } from './config';
|
|
2
|
-
export {
|
|
1
|
+
export type { RawConfig as Config, RawEntry as Entry } from './config';
|
|
2
|
+
export type { RestrictionZone } from './restrict-imports';
|
|
3
|
+
export { restrictImports } from './restrict-imports';
|
|
3
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,SAAS,IAAI,MAAM,EAAE,QAAQ,IAAI,KAAK,EAAE,MAAM,UAAU,CAAC;AACvE,YAAY,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "restrictImports", {
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "restrictImports", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _restrictimports.restrictImports;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _restrictimports = require("./restrict-imports");
|
|
12
|
+
|
|
6
13
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type { RawConfig as Config, RawEntry as Entry } from './config';\nexport type { RestrictionZone } from './restrict-imports';\nexport { restrictImports } from './restrict-imports';\n"],"names":["restrictImports"],"mappings":";;;;+BAESA;;;eAAAA,gCAAe;;;iCAAQ"}
|
package/dist/restrict-imports.js
CHANGED
|
@@ -1,73 +1,74 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
*
|
|
33
|
-
* // 'settings' can import from every module
|
|
34
|
-
* { name: 'settings', allow: '*' },
|
|
35
|
-
* ]
|
|
36
|
-
*
|
|
37
|
-
* @description Config should follow the next rules:
|
|
38
|
-
* 1. The only available mask for both `name` and `allow` fields is '*'
|
|
39
|
-
* 2. Order of entries is important to avoid circular dependencies between modules, module's dependency should be described *above* the module description. In the example, the 'common' module should be described *above* other modules. This rule also applies to the '*' mask, it should be described *before* it is using in other config entries.
|
|
40
|
-
*/
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get allowZone () {
|
|
13
|
+
return allowZone;
|
|
14
|
+
},
|
|
15
|
+
get disallowZone () {
|
|
16
|
+
return disallowZone;
|
|
17
|
+
},
|
|
18
|
+
get getAllow () {
|
|
19
|
+
return getAllow;
|
|
20
|
+
},
|
|
21
|
+
get getDisallow () {
|
|
22
|
+
return getDisallow;
|
|
23
|
+
},
|
|
24
|
+
get restrictImports () {
|
|
25
|
+
return restrictImports;
|
|
26
|
+
},
|
|
27
|
+
get restrictImports_ () {
|
|
28
|
+
return restrictImports_;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const _config = require("./config");
|
|
41
32
|
function restrictImports(base, modules, config) {
|
|
42
|
-
const validationError = (0,
|
|
33
|
+
const validationError = (0, _config.validateRawConfig)(config);
|
|
43
34
|
if (validationError) {
|
|
44
35
|
throw new Error(validationError);
|
|
45
36
|
}
|
|
46
|
-
return restrictImports_(base, modules, (0,
|
|
37
|
+
return restrictImports_(base, modules, (0, _config.canonicalizeConfig)(config));
|
|
47
38
|
}
|
|
48
|
-
// suppose we have '*' entry in the `config`
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
50
39
|
function restrictImports_(base, modules, config) {
|
|
51
40
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
52
41
|
const getAllow_ = getAllow(config);
|
|
53
|
-
return modules.reduce((acc, name)
|
|
42
|
+
return modules.reduce((acc, name)=>{
|
|
54
43
|
const allow = getAllow_(name);
|
|
55
44
|
if (allow !== '*') {
|
|
56
|
-
return [
|
|
45
|
+
return [
|
|
46
|
+
...acc,
|
|
47
|
+
allowZone(base, name, allow)
|
|
48
|
+
];
|
|
57
49
|
}
|
|
58
50
|
const disallow = getDisallow(config, name);
|
|
59
51
|
if (disallow.length > 0) {
|
|
60
|
-
return [
|
|
52
|
+
return [
|
|
53
|
+
...acc,
|
|
54
|
+
disallowZone(base, name, disallow)
|
|
55
|
+
];
|
|
61
56
|
}
|
|
62
57
|
return acc;
|
|
63
58
|
}, []);
|
|
64
59
|
}
|
|
65
60
|
function getAllow(c) {
|
|
66
|
-
const m = new Map(c.map(e
|
|
67
|
-
|
|
61
|
+
const m = new Map(c.map((e)=>[
|
|
62
|
+
e.name,
|
|
63
|
+
e.allow
|
|
64
|
+
]));
|
|
65
|
+
return (name)=>{
|
|
66
|
+
var _m_get, _ref;
|
|
67
|
+
return (_ref = (_m_get = m.get(name)) !== null && _m_get !== void 0 ? _m_get : m.get('*')) !== null && _ref !== void 0 ? _ref : [];
|
|
68
|
+
};
|
|
68
69
|
}
|
|
69
70
|
function getDisallow(c, name) {
|
|
70
|
-
const names = c.map(c
|
|
71
|
+
const names = c.map((c)=>c.name);
|
|
71
72
|
const index = names.indexOf(name);
|
|
72
73
|
return names.slice(index + 1);
|
|
73
74
|
}
|
|
@@ -75,16 +76,21 @@ function allowZone(base, dirName, allow = []) {
|
|
|
75
76
|
return {
|
|
76
77
|
target: base + dirName,
|
|
77
78
|
from: `${base}**/*`,
|
|
78
|
-
except: [
|
|
79
|
+
except: [
|
|
80
|
+
`${base}${toGlobList([
|
|
81
|
+
dirName,
|
|
82
|
+
...allow
|
|
83
|
+
])}/**/*`
|
|
84
|
+
]
|
|
79
85
|
};
|
|
80
86
|
}
|
|
81
87
|
function disallowZone(base, dirName, disallow) {
|
|
82
88
|
return {
|
|
83
89
|
target: base + dirName,
|
|
84
|
-
from: `${base}${toGlobList(disallow)}
|
|
90
|
+
from: `${base}${toGlobList(disallow)}/**/*`
|
|
85
91
|
};
|
|
86
92
|
}
|
|
87
|
-
const toGlobList = (items)
|
|
93
|
+
const toGlobList = (items)=>{
|
|
88
94
|
if (!items.length) {
|
|
89
95
|
return '';
|
|
90
96
|
}
|
|
@@ -94,4 +100,5 @@ const toGlobList = (items) => {
|
|
|
94
100
|
}
|
|
95
101
|
return `{${uniqueItems.join(',')}}`;
|
|
96
102
|
};
|
|
103
|
+
|
|
97
104
|
//# sourceMappingURL=restrict-imports.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/restrict-imports.ts"],"sourcesContent":["import { canonicalizeConfig, Config, RawConfig, validateRawConfig } from './config';\n\n/**\n * @param {string} base an absolute path\n * @param {Array<string>} modules\n * @param {Object} [config] describes import restrictions between modules\n *\n * @example\n * [\n * // 'common' is self-containing, imports from other modules is restricted\n * { name: 'common' },\n *\n * // 'ach' can import only from 'common' module\n * { name: 'ach-modal', allow: 'common' },\n *\n * // both 'accounting' and 'onboarding' can import only from 'common' and 'ach-modal' modules\n * { name: 'accounting', allow: ['common', 'ach-modal'] },\n * { name: 'onboarding', allow: ['common', 'ach-modal'] },\n *\n * // '*' describes any module which has no a special config entry\n * { name: '*', allow: 'common' },\n *\n * // 'header' can import from every module, except for 'settings'\n * { name: 'header', allow: '*' },\n *\n * // 'settings' can import from every module\n * { name: 'settings', allow: '*' },\n * ]\n *\n * @description Config should follow the next rules:\n * 1. The only available mask for both `name` and `allow` fields is '*'\n * 2. Order of entries is important to avoid circular dependencies between modules, module's dependency should be described *above* the module description. In the example, the 'common' module should be described *above* other modules. This rule also applies to the '*' mask, it should be described *before* it is using in other config entries.\n */\n\nexport function restrictImports(\n base: string,\n modules: string[],\n config?: RawConfig\n): RestrictionZone[] {\n const validationError = validateRawConfig(config);\n if (validationError) {\n throw new Error(validationError);\n }\n\n return restrictImports_(base, modules, canonicalizeConfig(config));\n}\n\n// suppose we have '*' entry in the `config`\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport function restrictImports_(base: string, modules: string[], config: Config) {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n const getAllow_ = getAllow(config);\n\n return modules.reduce<RestrictionZone[]>((acc, name) => {\n const allow = getAllow_(name);\n if (allow !== '*') {\n return [...acc, allowZone(base, name, allow)];\n }\n\n const disallow = getDisallow(config, name);\n if (disallow.length > 0) {\n return [...acc, disallowZone(base, name, disallow)];\n }\n\n return acc;\n }, []);\n}\n\nexport function getAllow(c: Config) {\n const m = new Map(c.map(e => [e.name, e.allow]));\n\n return (name: string) => m.get(name) ?? m.get('*') ?? [];\n}\n\nexport function getDisallow(c: Config, name: string) {\n const names = c.map(c => c.name);\n const index = names.indexOf(name);\n\n return names.slice(index + 1);\n}\n\nexport function allowZone(base: string, dirName: string, allow: string[] = []): RestrictionZone {\n return {\n target: base + dirName,\n from: `${base}**/*`,\n except: [`${base}${toGlobList([dirName, ...allow])}/**/*`],\n };\n}\n\nexport function disallowZone(base: string, dirName: string, disallow: string[]): RestrictionZone {\n return {\n target: base + dirName,\n from: `${base}${toGlobList(disallow)}/**/*`,\n };\n}\n\nconst toGlobList = (items: string[]) => {\n if (!items.length) {\n return '';\n }\n\n const uniqueItems = Array.from(new Set(items));\n\n if (uniqueItems.length === 1) {\n return uniqueItems[0];\n }\n\n return `{${uniqueItems.join(',')}}`;\n};\n\nexport interface RestrictionZone {\n target: string;\n from: string;\n except?: string[];\n}\n"],"names":["allowZone","disallowZone","getAllow","getDisallow","restrictImports","restrictImports_","base","modules","config","validationError","validateRawConfig","Error","canonicalizeConfig","getAllow_","reduce","acc","name","allow","disallow","length","c","m","Map","map","e","get","names","index","indexOf","slice","dirName","target","from","except","toGlobList","items","uniqueItems","Array","Set","join"],"mappings":";;;;;;;;;;;QAiFgBA;eAAAA;;QAQAC;eAAAA;;QArBAC;eAAAA;;QAMAC;eAAAA;;QAxCAC;eAAAA;;QAeAC;eAAAA;;;wBAjDyD;AAkClE,SAASD,gBACZE,IAAY,EACZC,OAAiB,EACjBC,MAAkB;IAElB,MAAMC,kBAAkBC,IAAAA,yBAAiB,EAACF;IAC1C,IAAIC,iBAAiB;QACjB,MAAM,IAAIE,MAAMF;IACpB;IAEA,OAAOJ,iBAAiBC,MAAMC,SAASK,IAAAA,0BAAkB,EAACJ;AAC9D;AAIO,SAASH,iBAAiBC,IAAY,EAAEC,OAAiB,EAAEC,MAAc;IAC5E,gEAAgE;IAChE,MAAMK,YAAYX,SAASM;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AC;IAC/C;AACJ;AAEA,MAAMgB,aAAa,CAACC;IAChB,IAAI,CAACA,MAAMhB,MAAM,EAAE;QACf,OAAO;IACX;IAEA,MAAMiB,cAAcC,MAAML,IAAI,CAAC,IAAIM,IAAIH;IAEvC,IAAIC,YAAYjB,MAAM,KAAK,GAAG;QAC1B,OAAOiB,WAAW,CAAC,EAAE;IACzB;IAEA,OAAO,CAAC,CAAC,EAAEA,YAAYG,IAAI,CAAC,KAAK,CAAC,CAAC;AACvC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@servicetitan/restrict-imports",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "31.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
"cli": {
|
|
20
20
|
"webpack": false
|
|
21
21
|
},
|
|
22
|
-
"gitHead": "
|
|
22
|
+
"gitHead": "c52e188a1217df74052774a2eea6a5126089e3d5"
|
|
23
23
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { RawConfig as Config, RawEntry as Entry } from './config';
|
|
2
|
-
export {
|
|
1
|
+
export type { RawConfig as Config, RawEntry as Entry } from './config';
|
|
2
|
+
export type { RestrictionZone } from './restrict-imports';
|
|
3
|
+
export { restrictImports } from './restrict-imports';
|