@atlaspack/utils 2.19.3 → 3.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/DefaultMap.js +41 -0
  3. package/dist/Deferred.js +16 -0
  4. package/dist/PromiseQueue.js +107 -0
  5. package/dist/TapStream.js +23 -0
  6. package/dist/alternatives.js +97 -0
  7. package/dist/ansi-html.js +12 -0
  8. package/dist/blob.js +29 -0
  9. package/dist/bundle-url.js +32 -0
  10. package/dist/collection.js +106 -0
  11. package/dist/config.js +138 -0
  12. package/dist/countLines.js +12 -0
  13. package/dist/debounce.js +15 -0
  14. package/dist/debug-tools.js +37 -0
  15. package/dist/dependency-location.js +22 -0
  16. package/dist/escape-html.js +19 -0
  17. package/dist/generateBuildMetrics.js +111 -0
  18. package/dist/generateCertificate.js +124 -0
  19. package/dist/getCertificate.js +13 -0
  20. package/dist/getExisting.js +20 -0
  21. package/dist/getModuleParts.js +27 -0
  22. package/dist/getRootDir.js +46 -0
  23. package/dist/glob.js +133 -0
  24. package/dist/hash.js +45 -0
  25. package/dist/http-server.js +55 -0
  26. package/dist/index.js +146 -0
  27. package/dist/is-url.js +15 -0
  28. package/dist/isDirectoryInside.js +11 -0
  29. package/dist/objectHash.js +20 -0
  30. package/dist/openInBrowser.js +61 -0
  31. package/dist/parseCSSImport.js +14 -0
  32. package/dist/path.js +36 -0
  33. package/dist/prettifyTime.js +6 -0
  34. package/dist/prettyDiagnostic.js +104 -0
  35. package/dist/progress-message.js +31 -0
  36. package/dist/relativeBundlePath.js +13 -0
  37. package/dist/relativeUrl.js +11 -0
  38. package/dist/replaceBundleReferences.js +140 -0
  39. package/dist/schema.js +389 -0
  40. package/dist/shared-buffer.js +24 -0
  41. package/dist/sourcemap.js +121 -0
  42. package/dist/stream.js +69 -0
  43. package/dist/throttle.js +12 -0
  44. package/dist/urlJoin.js +22 -0
  45. package/lib/prettyDiagnostic.js +17 -1
  46. package/lib/sourcemap.js +13 -2
  47. package/lib/types/sourcemap.d.ts +1 -1
  48. package/package.json +6 -7
  49. package/src/prettyDiagnostic.ts +11 -1
  50. package/src/sourcemap.ts +10 -1
  51. package/tsconfig.json +31 -2
  52. package/tsconfig.tsbuildinfo +1 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @atlaspack/utils
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [#800](https://github.com/atlassian-labs/atlaspack/pull/800) [`1180103`](https://github.com/atlassian-labs/atlaspack/commit/118010351ed444f8178988afb3f77807154dd933) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Fix errors when creating diagnostics with relative file paths
8
+
9
+ BREAKING CHANGE: The `remapSourceLocation` API in `@atlaspack/utils` now requires the project root
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [[`0207171`](https://github.com/atlassian-labs/atlaspack/commit/0207171e59e985c51e105aec2e0a99de174374bd), [`10ee3fa`](https://github.com/atlassian-labs/atlaspack/commit/10ee3fa5b75a92acde8973673d9b3c5b6f3958e5), [`bbe4e28`](https://github.com/atlassian-labs/atlaspack/commit/bbe4e28c89e5e9284bd730075d4d6f2a3cb37b21), [`1180103`](https://github.com/atlassian-labs/atlaspack/commit/118010351ed444f8178988afb3f77807154dd933), [`2bc93b1`](https://github.com/atlassian-labs/atlaspack/commit/2bc93b17cea07fd7cbb68acec84d8471345a22b4)]:
14
+ - @atlaspack/feature-flags@2.25.2
15
+ - @atlaspack/rust@3.8.2
16
+ - @atlaspack/types-internal@2.20.2
17
+ - @atlaspack/logger@2.14.24
18
+ - @atlaspack/codeframe@2.13.14
19
+
3
20
  ## 2.19.3
4
21
 
5
22
  ### Patch Changes
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultWeakMap = exports.DefaultMap = void 0;
4
+ class DefaultMap extends Map {
5
+ constructor(getDefault, entries) {
6
+ super(entries);
7
+ this._getDefault = getDefault;
8
+ }
9
+ get(key) {
10
+ let ret;
11
+ if (this.has(key)) {
12
+ ret = super.get(key);
13
+ }
14
+ else {
15
+ ret = this._getDefault(key);
16
+ this.set(key, ret);
17
+ }
18
+ return ret;
19
+ }
20
+ }
21
+ exports.DefaultMap = DefaultMap;
22
+ // Duplicated from DefaultMap implementation for Flow
23
+ // Roughly mirrors https://github.com/facebook/flow/blob/2eb5a78d92c167117ba9caae070afd2b9f598599/lib/core.js#L617
24
+ class DefaultWeakMap extends WeakMap {
25
+ constructor(getDefault, entries) {
26
+ super(entries);
27
+ this._getDefault = getDefault;
28
+ }
29
+ get(key) {
30
+ let ret;
31
+ if (this.has(key)) {
32
+ ret = super.get(key);
33
+ }
34
+ else {
35
+ ret = this._getDefault(key);
36
+ this.set(key, ret);
37
+ }
38
+ return ret;
39
+ }
40
+ }
41
+ exports.DefaultWeakMap = DefaultWeakMap;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.makeDeferredWithPromise = makeDeferredWithPromise;
7
+ const assert_1 = __importDefault(require("assert"));
8
+ function makeDeferredWithPromise() {
9
+ let deferred;
10
+ let promise = new Promise((resolve, reject) => {
11
+ deferred = { resolve, reject };
12
+ });
13
+ // Promise constructor callback executes synchronously, so this is defined
14
+ (0, assert_1.default)(deferred != null);
15
+ return { deferred, promise };
16
+ }
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Deferred_1 = require("./Deferred");
4
+ class PromiseQueue {
5
+ constructor(opts = { maxConcurrent: Infinity }) {
6
+ this._numRunning = 0;
7
+ this._queue = [];
8
+ this._runPromise = null;
9
+ this._count = 0;
10
+ this._results = [];
11
+ this._addSubscriptions = new Set();
12
+ if (opts.maxConcurrent <= 0) {
13
+ throw new TypeError('maxConcurrent must be a positive, non-zero value');
14
+ }
15
+ this._maxConcurrent = opts.maxConcurrent;
16
+ }
17
+ getNumWaiting() {
18
+ return this._queue.length;
19
+ }
20
+ add(fn) {
21
+ new Promise((resolve, reject) => {
22
+ let i = this._count++;
23
+ let wrapped = () => fn().then((result) => {
24
+ this._results[i] = result;
25
+ resolve(result);
26
+ }, (err) => {
27
+ reject(err);
28
+ throw err;
29
+ });
30
+ this._queue.push(wrapped);
31
+ for (const addFn of this._addSubscriptions) {
32
+ addFn();
33
+ }
34
+ if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
35
+ this._next();
36
+ }
37
+ }).catch(() => { });
38
+ }
39
+ subscribeToAdd(fn) {
40
+ this._addSubscriptions.add(fn);
41
+ return () => {
42
+ this._addSubscriptions.delete(fn);
43
+ };
44
+ }
45
+ run() {
46
+ if (this._runPromise != null) {
47
+ return this._runPromise;
48
+ }
49
+ if (this._queue.length === 0) {
50
+ return Promise.resolve([]);
51
+ }
52
+ let { deferred, promise } = (0, Deferred_1.makeDeferredWithPromise)();
53
+ this._deferred = deferred;
54
+ this._runPromise = promise;
55
+ while (this._queue.length && this._numRunning < this._maxConcurrent) {
56
+ this._next();
57
+ }
58
+ return promise;
59
+ }
60
+ async _next() {
61
+ let fn = this._queue.shift();
62
+ await this._runFn(fn);
63
+ if (this._queue.length) {
64
+ this._next();
65
+ }
66
+ else if (this._numRunning === 0) {
67
+ this._done();
68
+ }
69
+ }
70
+ async _runFn(fn) {
71
+ this._numRunning++;
72
+ try {
73
+ await fn();
74
+ }
75
+ catch (e) {
76
+ // Only store the first error that occurs.
77
+ // We don't reject immediately so that any other concurrent
78
+ // requests have time to complete.
79
+ if (this._error == null) {
80
+ this._error = e;
81
+ }
82
+ }
83
+ finally {
84
+ this._numRunning--;
85
+ }
86
+ }
87
+ _resetState() {
88
+ this._queue = [];
89
+ this._count = 0;
90
+ this._results = [];
91
+ this._runPromise = null;
92
+ this._numRunning = 0;
93
+ this._deferred = null;
94
+ }
95
+ _done() {
96
+ if (this._deferred != null) {
97
+ if (this._error != null) {
98
+ this._deferred.reject(this._error);
99
+ }
100
+ else {
101
+ this._deferred.resolve(this._results);
102
+ }
103
+ }
104
+ this._resetState();
105
+ }
106
+ }
107
+ exports.default = PromiseQueue;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const stream_1 = require("stream");
4
+ /*
5
+ * "Taps" into the contents of a flowing stream, yielding chunks to the passed
6
+ * callback. Continues to pass data chunks down the stream.
7
+ */
8
+ class TapStream extends stream_1.Transform {
9
+ constructor(tap, options) {
10
+ super({ ...options });
11
+ this._tap = tap;
12
+ }
13
+ _transform(chunk, encoding, callback) {
14
+ try {
15
+ this._tap(Buffer.from(chunk));
16
+ callback(null, chunk);
17
+ }
18
+ catch (err) {
19
+ callback(err);
20
+ }
21
+ }
22
+ }
23
+ exports.default = TapStream;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.findAlternativeNodeModules = findAlternativeNodeModules;
7
+ exports.findAlternativeFiles = findAlternativeFiles;
8
+ const path_1 = __importDefault(require("path"));
9
+ const schema_1 = require("./schema");
10
+ const path_2 = require("./path");
11
+ const config_1 = require("./config");
12
+ async function findAlternativeNodeModules(fs, moduleName, dir) {
13
+ let potentialModules = [];
14
+ let root = path_1.default.parse(dir).root;
15
+ let isOrganisationModule = moduleName.startsWith('@');
16
+ while (dir !== root) {
17
+ // Skip node_modules directories
18
+ if (path_1.default.basename(dir) === 'node_modules') {
19
+ dir = path_1.default.dirname(dir);
20
+ }
21
+ try {
22
+ let modulesDir = path_1.default.join(dir, 'node_modules');
23
+ let stats = await fs.stat(modulesDir);
24
+ if (stats.isDirectory()) {
25
+ let dirContent = (await fs.readdir(modulesDir)).sort();
26
+ // Filter out the modules that interest us
27
+ let modules = dirContent.filter((i) => isOrganisationModule ? i.startsWith('@') : !i.startsWith('@'));
28
+ // If it's an organisation module, loop through all the modules of that organisation
29
+ if (isOrganisationModule) {
30
+ await Promise.all(modules.map(async (item) => {
31
+ let orgDirPath = path_1.default.join(modulesDir, item);
32
+ let orgDirContent = (await fs.readdir(orgDirPath)).sort();
33
+ // Add all org packages
34
+ potentialModules.push(...orgDirContent.map((i) => `${item}/${i}`));
35
+ }));
36
+ }
37
+ else {
38
+ potentialModules.push(...modules);
39
+ }
40
+ }
41
+ }
42
+ catch (err) {
43
+ // ignore
44
+ }
45
+ // Move up a directory
46
+ dir = path_1.default.dirname(dir);
47
+ }
48
+ return (0, schema_1.fuzzySearch)(potentialModules.sort(), moduleName).slice(0, 2);
49
+ }
50
+ async function findAllFilesUp({ fs, dir, root, basedir, maxlength, collected, leadingDotSlash = true, includeDirectories = true, }) {
51
+ let dirContent = (await fs.readdir(dir)).sort();
52
+ return Promise.all(dirContent.map(async (item) => {
53
+ let fullPath = path_1.default.join(dir, item);
54
+ let relativeFilePath = (0, path_2.relativePath)(basedir, fullPath, leadingDotSlash);
55
+ if (relativeFilePath.length < maxlength) {
56
+ let stats = await fs.stat(fullPath);
57
+ let isDir = stats.isDirectory();
58
+ if ((isDir && includeDirectories) || stats.isFile()) {
59
+ collected.push(relativeFilePath);
60
+ }
61
+ // If it's a directory, run over each item within said directory...
62
+ if (isDir) {
63
+ return findAllFilesUp({
64
+ fs,
65
+ dir: fullPath,
66
+ root,
67
+ basedir,
68
+ maxlength,
69
+ collected,
70
+ });
71
+ }
72
+ }
73
+ }));
74
+ }
75
+ async function findAlternativeFiles(fs, fileSpecifier, dir, projectRoot, leadingDotSlash = true, includeDirectories = true, includeExtension = false) {
76
+ let potentialFiles = [];
77
+ // Find our root, we won't recommend files above the package root as that's bad practise
78
+ let pkg = await (0, config_1.resolveConfig)(fs, path_1.default.join(dir, 'index'), ['package.json'], projectRoot);
79
+ let pkgRoot = pkg ? path_1.default.dirname(pkg) : projectRoot;
80
+ await findAllFilesUp({
81
+ fs,
82
+ dir: pkgRoot,
83
+ root: pkgRoot,
84
+ basedir: dir,
85
+ maxlength: fileSpecifier.length + 10,
86
+ collected: potentialFiles,
87
+ leadingDotSlash,
88
+ includeDirectories,
89
+ });
90
+ if (path_1.default.extname(fileSpecifier) === '' && !includeExtension) {
91
+ potentialFiles = potentialFiles.map((p) => {
92
+ let ext = path_1.default.extname(p);
93
+ return ext.length > 0 ? p.slice(0, -ext.length) : p;
94
+ });
95
+ }
96
+ return (0, schema_1.fuzzySearch)(potentialFiles.sort(), fileSpecifier).slice(0, 2);
97
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ansiHtml = ansiHtml;
7
+ // @ts-expect-error ansi-html-community is not typed
8
+ const ansi_html_community_1 = __importDefault(require("ansi-html-community"));
9
+ const escape_html_1 = require("./escape-html");
10
+ function ansiHtml(ansi) {
11
+ return (0, ansi_html_community_1.default)((0, escape_html_1.escapeHTML)(ansi));
12
+ }
package/dist/blob.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.blobToBuffer = blobToBuffer;
4
+ exports.blobToString = blobToString;
5
+ const buffer_1 = require("buffer");
6
+ const _1 = require("./");
7
+ const stream_1 = require("stream");
8
+ function blobToBuffer(blob) {
9
+ if (blob instanceof stream_1.Readable) {
10
+ return (0, _1.bufferStream)(blob);
11
+ }
12
+ else if (blob instanceof buffer_1.Buffer) {
13
+ return Promise.resolve(buffer_1.Buffer.from(blob));
14
+ }
15
+ else {
16
+ return Promise.resolve(buffer_1.Buffer.from(blob, 'utf8'));
17
+ }
18
+ }
19
+ async function blobToString(blob) {
20
+ if (blob instanceof stream_1.Readable) {
21
+ return (await (0, _1.bufferStream)(blob)).toString();
22
+ }
23
+ else if (blob instanceof buffer_1.Buffer) {
24
+ return blob.toString();
25
+ }
26
+ else {
27
+ return blob;
28
+ }
29
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBundleURL = void 0;
4
+ exports.getBaseURL = getBaseURL;
5
+ let bundleURL = null;
6
+ function getBundleURLCached() {
7
+ if (bundleURL == null) {
8
+ bundleURL = _getBundleURL();
9
+ }
10
+ return bundleURL;
11
+ }
12
+ function _getBundleURL() {
13
+ // Attempt to find the URL of the current script and use that as the base URL
14
+ try {
15
+ throw new Error();
16
+ }
17
+ catch (err) {
18
+ let stack = typeof err.stack === 'string' ? err.stack : '';
19
+ let matches = stack.match(/(https?|file|ftp):\/\/[^)\n]+/g);
20
+ if (matches) {
21
+ return getBaseURL(matches[0]);
22
+ }
23
+ }
24
+ return '/';
25
+ }
26
+ function getBaseURL(url) {
27
+ if (url == null) {
28
+ return '/';
29
+ }
30
+ return url.replace(/^((?:https?|file|ftp):\/\/.+)\/[^/]+$/, '$1') + '/';
31
+ }
32
+ exports.getBundleURL = getBundleURLCached;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unique = unique;
4
+ exports.objectSortedEntries = objectSortedEntries;
5
+ exports.objectSortedEntriesDeep = objectSortedEntriesDeep;
6
+ exports.setDifference = setDifference;
7
+ exports.setSymmetricDifference = setSymmetricDifference;
8
+ exports.setIntersect = setIntersect;
9
+ exports.setIntersectStatic = setIntersectStatic;
10
+ exports.setUnion = setUnion;
11
+ exports.setEqual = setEqual;
12
+ function unique(array) {
13
+ return [...new Set(array)];
14
+ }
15
+ function objectSortedEntries(obj) {
16
+ return Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
17
+ }
18
+ function objectSortedEntriesDeep(object) {
19
+ let sortedEntries = objectSortedEntries(object);
20
+ for (let i = 0; i < sortedEntries.length; i++) {
21
+ sortedEntries[i][1] = sortEntry(sortedEntries[i][1]);
22
+ }
23
+ return sortedEntries;
24
+ }
25
+ function sortEntry(entry) {
26
+ if (Array.isArray(entry)) {
27
+ return entry.map(sortEntry);
28
+ }
29
+ if (typeof entry === 'object' && entry != null) {
30
+ return objectSortedEntriesDeep(entry);
31
+ }
32
+ return entry;
33
+ }
34
+ /**
35
+ * Get the difference of A and B sets
36
+ *
37
+ * This is the set of elements which are in A but not in B
38
+ * For example, the difference of the sets {1,2,3} and {3,4} is {1,2}
39
+ *
40
+ * @param {*} a Set A
41
+ * @param {*} b Set B
42
+ * @returns A \ B
43
+ */
44
+ function setDifference(a, b) {
45
+ let difference = new Set();
46
+ for (let e of a) {
47
+ if (!b.has(e)) {
48
+ difference.add(e);
49
+ }
50
+ }
51
+ return difference;
52
+ }
53
+ /**
54
+ * Get the symmetric difference of A and B sets
55
+ *
56
+ * This is the set of elements which are in either of the sets, but not in their intersection.
57
+ * For example, the symmetric difference of the sets {1,2,3} and {3,4} is {1,2,4}
58
+ *
59
+ * @param {*} a Set A
60
+ * @param {*} b Set B
61
+ * @returns A Δ B
62
+ */
63
+ function setSymmetricDifference(a, b) {
64
+ let difference = new Set();
65
+ for (let e of a) {
66
+ if (!b.has(e)) {
67
+ difference.add(e);
68
+ }
69
+ }
70
+ for (let d of b) {
71
+ if (!a.has(d)) {
72
+ difference.add(d);
73
+ }
74
+ }
75
+ return difference;
76
+ }
77
+ function setIntersect(a, b) {
78
+ for (let entry of a) {
79
+ if (!b.has(entry)) {
80
+ a.delete(entry);
81
+ }
82
+ }
83
+ }
84
+ function setIntersectStatic(a, b) {
85
+ let intersection = new Set();
86
+ for (let entry of a) {
87
+ if (b.has(entry)) {
88
+ intersection.add(entry);
89
+ }
90
+ }
91
+ return intersection;
92
+ }
93
+ function setUnion(a, b) {
94
+ return new Set([...a, ...b]);
95
+ }
96
+ function setEqual(a, b) {
97
+ if (a.size != b.size) {
98
+ return false;
99
+ }
100
+ for (let entry of a) {
101
+ if (!b.has(entry)) {
102
+ return false;
103
+ }
104
+ }
105
+ return true;
106
+ }
package/dist/config.js ADDED
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.resolveConfig = resolveConfig;
7
+ exports.resolveConfigSync = resolveConfigSync;
8
+ exports.loadConfig = loadConfig;
9
+ exports.readConfig = readConfig;
10
+ const diagnostic_1 = __importDefault(require("@atlaspack/diagnostic"));
11
+ const path_1 = __importDefault(require("path"));
12
+ // @ts-expect-error TS7016
13
+ const clone_1 = __importDefault(require("clone"));
14
+ const json5_1 = __importDefault(require("json5"));
15
+ const toml_1 = require("@iarna/toml");
16
+ const lru_cache_1 = __importDefault(require("lru-cache"));
17
+ // @ts-expect-error TS2351
18
+ const configCache = new lru_cache_1.default({ max: 500 });
19
+ const resolveCache = new Map();
20
+ function resolveConfig(fs, filepath, filenames, projectRoot) {
21
+ // Cache the result of resolving config for this directory.
22
+ // This is automatically invalidated at the end of the current build.
23
+ let key = path_1.default.dirname(filepath) + filenames.join(',');
24
+ let cached = resolveCache.get(key);
25
+ if (cached !== undefined) {
26
+ return Promise.resolve(cached);
27
+ }
28
+ let resolved = fs.findAncestorFile(filenames, path_1.default.dirname(filepath), projectRoot);
29
+ resolveCache.set(key, resolved);
30
+ return Promise.resolve(resolved);
31
+ }
32
+ function resolveConfigSync(fs, filepath, filenames, projectRoot) {
33
+ return fs.findAncestorFile(filenames, path_1.default.dirname(filepath), projectRoot);
34
+ }
35
+ async function loadConfig(fs, filepath, filenames, projectRoot, opts) {
36
+ let parse = opts?.parse ?? true;
37
+ let configFile = await resolveConfig(fs, filepath, filenames, projectRoot);
38
+ if (configFile) {
39
+ let cachedOutput = configCache.get(String(parse) + configFile);
40
+ if (cachedOutput) {
41
+ return cachedOutput;
42
+ }
43
+ try {
44
+ let extname = path_1.default.extname(configFile).slice(1);
45
+ if (extname === 'js' || extname === 'cjs') {
46
+ let output = {
47
+ config: (0, clone_1.default)(module.require(configFile)),
48
+ files: [{ filePath: configFile }],
49
+ };
50
+ configCache.set(configFile, output);
51
+ return output;
52
+ }
53
+ return readConfig(fs, configFile, opts);
54
+ }
55
+ catch (err) {
56
+ if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
57
+ return null;
58
+ }
59
+ throw err;
60
+ }
61
+ }
62
+ return null;
63
+ }
64
+ loadConfig.clear = () => {
65
+ configCache.reset();
66
+ resolveCache.clear();
67
+ };
68
+ async function readConfig(fs, configFile, opts) {
69
+ let parse = opts?.parse ?? true;
70
+ let cachedOutput = configCache.get(String(parse) + configFile);
71
+ if (cachedOutput) {
72
+ return cachedOutput;
73
+ }
74
+ try {
75
+ let configContent = await fs.readFile(configFile, 'utf8');
76
+ let config;
77
+ if (parse === false) {
78
+ config = configContent;
79
+ }
80
+ else {
81
+ let extname = path_1.default.extname(configFile).slice(1);
82
+ let parse = opts?.parser ?? getParser(extname);
83
+ try {
84
+ config = parse(configContent);
85
+ }
86
+ catch (e) {
87
+ if (extname !== '' && extname !== 'json') {
88
+ throw e;
89
+ }
90
+ let pos = {
91
+ line: e.lineNumber,
92
+ column: e.columnNumber,
93
+ };
94
+ throw new diagnostic_1.default({
95
+ diagnostic: {
96
+ message: `Failed to parse ${path_1.default.basename(configFile)}`,
97
+ origin: '@atlaspack/utils',
98
+ codeFrames: [
99
+ {
100
+ language: 'json5',
101
+ filePath: configFile,
102
+ code: configContent,
103
+ codeHighlights: [
104
+ {
105
+ start: pos,
106
+ end: pos,
107
+ message: e.message,
108
+ },
109
+ ],
110
+ },
111
+ ],
112
+ },
113
+ });
114
+ }
115
+ }
116
+ let output = {
117
+ config,
118
+ files: [{ filePath: configFile }],
119
+ };
120
+ configCache.set(String(parse) + configFile, output);
121
+ return output;
122
+ }
123
+ catch (err) {
124
+ if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
125
+ return null;
126
+ }
127
+ throw err;
128
+ }
129
+ }
130
+ function getParser(extname) {
131
+ switch (extname) {
132
+ case 'toml':
133
+ return toml_1.parse;
134
+ case 'json':
135
+ default:
136
+ return json5_1.default.parse;
137
+ }
138
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = countLines;
4
+ function countLines(string, startIndex = 0) {
5
+ let lines = 1;
6
+ for (let i = startIndex; i < string.length; i++) {
7
+ if (string.charAt(i) === '\n') {
8
+ lines++;
9
+ }
10
+ }
11
+ return lines;
12
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = debounce;
4
+ function debounce(fn, delay) {
5
+ let timeout;
6
+ return function (...args) {
7
+ if (timeout) {
8
+ clearTimeout(timeout);
9
+ }
10
+ timeout = setTimeout(() => {
11
+ timeout = null;
12
+ fn(...args);
13
+ }, delay);
14
+ };
15
+ }