@sprucelabs/resolve-path-aliases 2.0.1 → 2.0.3

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.
@@ -0,0 +1,127 @@
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.logLive = exports.logStub = exports.PathResolver = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const chalk_1 = __importDefault(require("chalk"));
10
+ const globby_1 = __importDefault(require("globby"));
11
+ const tsconfig_paths_1 = require("tsconfig-paths");
12
+ const posixPath_1 = __importDefault(require("./posixPath"));
13
+ class PathResolver {
14
+ static Class;
15
+ outResolver;
16
+ srcResolver;
17
+ globby;
18
+ totalMappedPaths = 0;
19
+ totalFilesWithMappedPaths = 0;
20
+ log = exports.logLive;
21
+ constructor(globbyUtil) {
22
+ this.globby = globbyUtil;
23
+ }
24
+ static Resolver(globbyUtil = globby_1.default) {
25
+ return new (this.Class ?? this)(globbyUtil);
26
+ }
27
+ buildResolvers(destination) {
28
+ const config = (0, tsconfig_paths_1.loadConfig)(destination);
29
+ if (config.resultType === 'failed') {
30
+ throw new Error(config.message);
31
+ }
32
+ const { paths, absoluteBaseUrl } = config;
33
+ const srcResolver = (0, tsconfig_paths_1.createMatchPath)(absoluteBaseUrl, paths);
34
+ const outResolver = this.buildOutResolver(config);
35
+ this.outResolver = outResolver;
36
+ this.srcResolver = srcResolver;
37
+ }
38
+ resolvePathAliases(destination, options = {}) {
39
+ this.buildResolvers(destination);
40
+ const { patterns = ['**/*.js'], absoluteOrRelative = 'relative', beVerbose: isVerbose = false, } = options;
41
+ this.log = isVerbose ? exports.logLive : exports.logStub;
42
+ this.totalMappedPaths = 0;
43
+ this.totalFilesWithMappedPaths = 0;
44
+ const files = this.findFiles(patterns, destination);
45
+ this.log.info(`Checking ${files.length} files for path aliases...`);
46
+ files.forEach((file) => this.replacePaths(file, absoluteOrRelative));
47
+ return {
48
+ totalMappedPaths: this.totalMappedPaths,
49
+ totalFilesWithMappedPaths: this.totalFilesWithMappedPaths,
50
+ };
51
+ }
52
+ replacePaths(file, absoluteOrRelative) {
53
+ const contents = fs_1.default.readFileSync(file).toString();
54
+ let { found, updated } = this.replaceHashPaths(contents, file, absoluteOrRelative);
55
+ if (found) {
56
+ this.totalFilesWithMappedPaths++;
57
+ fs_1.default.writeFileSync(file, updated);
58
+ }
59
+ }
60
+ replaceHashPaths(contents, filepath, absoluteOrRelative) {
61
+ const directoryPath = path_1.default.dirname(filepath);
62
+ let found = false;
63
+ const updated = `${contents}`.replace(/(['"]?).*?(from |import |import\(|require\()['"](#spruce\/(.*?))['"]/gi,
64
+ //@ts-ignore
65
+ (possibleMatchWithQuote, _, requireOrImport, match) => {
66
+ if (possibleMatchWithQuote.trim().startsWith('"')) {
67
+ return;
68
+ }
69
+ found = true;
70
+ const search = match;
71
+ let resolved;
72
+ this.log.info('Found', search, 'in', filepath);
73
+ if (this.outResolver) {
74
+ resolved = this.outResolver(search, undefined, undefined, [
75
+ '.ts',
76
+ '.js',
77
+ ]);
78
+ }
79
+ if (!resolved && this.srcResolver) {
80
+ resolved = this.srcResolver(search, undefined, undefined, [
81
+ '.ts',
82
+ '.js',
83
+ ]);
84
+ }
85
+ if (!resolved) {
86
+ throw new Error(`Could not map ${search} in ${filepath}.`);
87
+ }
88
+ const relative = absoluteOrRelative === 'relative'
89
+ ? './' + path_1.default.relative(directoryPath, resolved)
90
+ : resolved;
91
+ const replaced = possibleMatchWithQuote.replace(search, (0, posixPath_1.default)(relative));
92
+ this.totalMappedPaths++;
93
+ return replaced;
94
+ });
95
+ return { found, updated };
96
+ }
97
+ findFiles(patterns, destination) {
98
+ return this.globby.sync(patterns.map((pattern) => (0, posixPath_1.default)(path_1.default.posix.join(destination, '/', pattern))), {
99
+ dot: true,
100
+ });
101
+ }
102
+ buildOutResolver(config) {
103
+ const fullTsConfig = JSON.parse(fs_1.default.readFileSync(config.configFileAbsolutePath).toString());
104
+ const { compilerOptions: { baseUrl, outDir }, } = fullTsConfig;
105
+ let outResolver;
106
+ if (outDir) {
107
+ const resolver = (0, tsconfig_paths_1.createMatchPath)(config.absoluteBaseUrl, config.paths);
108
+ outResolver = (requested, readJson, fileExists, extensions) => {
109
+ let resolved = resolver(requested, readJson, fileExists, extensions);
110
+ resolved = resolved?.replace(`${path_1.default.sep}${baseUrl}${path_1.default.sep}`, `${path_1.default.sep}${outDir}${path_1.default.sep}`);
111
+ return resolved;
112
+ };
113
+ }
114
+ return outResolver;
115
+ }
116
+ }
117
+ exports.PathResolver = PathResolver;
118
+ exports.logStub = {
119
+ info: () => { },
120
+ warning: () => { },
121
+ error: () => { },
122
+ };
123
+ exports.logLive = {
124
+ info: (...message) => console.log(chalk_1.default.italic(...message)),
125
+ warning: (...message) => console.log(chalk_1.default.yellow(...message)),
126
+ error: (...message) => console.log(chalk_1.default.red(...message)),
127
+ };
package/build/index.js CHANGED
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.resolvePathAliases = void 0;
7
7
  const globby_1 = __importDefault(require("globby"));
8
- const PathResolve_1 = require("./PathResolve");
8
+ const PathResolver_1 = require("./PathResolver");
9
9
  function resolvePathAliases(destination, options = {}, globUtil = globby_1.default) {
10
- const resolver = PathResolve_1.PathResolve.Resolver(globUtil);
10
+ const resolver = PathResolver_1.PathResolver.Resolver(globUtil);
11
11
  return resolver.resolvePathAliases(destination, options);
12
12
  }
13
13
  exports.resolvePathAliases = resolvePathAliases;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sprucelabs/resolve-path-aliases",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Script that resolves paths from your tsconfig",
5
5
  "main": "build/index.js",
6
6
  "repository": "git@github.com:sprucelabsai-community/resolve-path-aliases.git",
@@ -20,7 +20,8 @@
20
20
  "files": [
21
21
  "build/index.js",
22
22
  "build/resolve-path-aliases.js",
23
- "build/posixPath.js"
23
+ "build/posixPath.js",
24
+ "build/PathResolver.js"
24
25
  ],
25
26
  "scripts": {
26
27
  "build.ci": "yarn build.tsc && yarn copy-for-tests && yarn lint",