@sprucelabs/resolve-path-aliases 2.0.0 → 2.0.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/build/index.js +3 -87
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -4,94 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.resolvePathAliases = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
10
7
|
const globby_1 = __importDefault(require("globby"));
|
|
11
|
-
const
|
|
12
|
-
const posixPath_1 = __importDefault(require("./posixPath"));
|
|
13
|
-
const logStub = {
|
|
14
|
-
info: () => { },
|
|
15
|
-
warning: () => { },
|
|
16
|
-
error: () => { },
|
|
17
|
-
};
|
|
18
|
-
const logLive = {
|
|
19
|
-
info: (...message) => console.log(chalk_1.default.italic(...message)),
|
|
20
|
-
warning: (...message) => console.log(chalk_1.default.yellow(...message)),
|
|
21
|
-
error: (...message) => console.log(chalk_1.default.red(...message)),
|
|
22
|
-
};
|
|
23
|
-
function buildResolvers(destination) {
|
|
24
|
-
const config = (0, tsconfig_paths_1.loadConfig)(destination);
|
|
25
|
-
if (config.resultType === 'failed') {
|
|
26
|
-
throw new Error(config.message);
|
|
27
|
-
}
|
|
28
|
-
const { paths, absoluteBaseUrl } = config;
|
|
29
|
-
const srcResolver = (0, tsconfig_paths_1.createMatchPath)(absoluteBaseUrl, paths);
|
|
30
|
-
const outResolver = buildOutResolver(config);
|
|
31
|
-
return { outResolver, srcResolver };
|
|
32
|
-
}
|
|
8
|
+
const PathResolve_1 = require("./PathResolve");
|
|
33
9
|
function resolvePathAliases(destination, options = {}, globUtil = globby_1.default) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const log = isVerbose ? logLive : logStub;
|
|
37
|
-
let totalMappedPaths = 0;
|
|
38
|
-
let totalFilesWithMappedPaths = 0;
|
|
39
|
-
const files = globUtil.sync(patterns.map((pattern) => (0, posixPath_1.default)(path_1.default.posix.join(destination, '/', pattern))), {
|
|
40
|
-
dot: true,
|
|
41
|
-
});
|
|
42
|
-
log.info(`Checking ${files.length} files for path aliases...`);
|
|
43
|
-
files.forEach((file) => {
|
|
44
|
-
let contents = fs_1.default.readFileSync(file).toString();
|
|
45
|
-
let found = false;
|
|
46
|
-
contents = `${contents}`.replace(/(from |import |import\(|require\()['"](#spruce\/(.*?))['"]/gi, (_, requireOrImport, match) => {
|
|
47
|
-
found = true;
|
|
48
|
-
const search = match;
|
|
49
|
-
let resolved;
|
|
50
|
-
log.info('Found', search, 'in', file);
|
|
51
|
-
if (outResolver) {
|
|
52
|
-
resolved = outResolver(search, undefined, undefined, [
|
|
53
|
-
'.ts',
|
|
54
|
-
'.js',
|
|
55
|
-
]);
|
|
56
|
-
}
|
|
57
|
-
if (!resolved) {
|
|
58
|
-
resolved = srcResolver(search, undefined, undefined, [
|
|
59
|
-
'.ts',
|
|
60
|
-
'.js',
|
|
61
|
-
]);
|
|
62
|
-
}
|
|
63
|
-
if (!resolved) {
|
|
64
|
-
throw new Error(`Could not map ${search} in ${file}.`);
|
|
65
|
-
}
|
|
66
|
-
totalMappedPaths++;
|
|
67
|
-
const relative = absoluteOrRelative === 'relative'
|
|
68
|
-
? './' +
|
|
69
|
-
path_1.default.relative(path_1.default.dirname(file), resolved)
|
|
70
|
-
: resolved;
|
|
71
|
-
return (0, posixPath_1.default)(`${requireOrImport}"${relative}"`);
|
|
72
|
-
});
|
|
73
|
-
if (found) {
|
|
74
|
-
totalFilesWithMappedPaths++;
|
|
75
|
-
fs_1.default.writeFileSync(file, contents);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
return {
|
|
79
|
-
totalMappedPaths,
|
|
80
|
-
totalFilesWithMappedPaths,
|
|
81
|
-
};
|
|
10
|
+
const resolver = PathResolve_1.PathResolve.Resolver(globUtil);
|
|
11
|
+
return resolver.resolvePathAliases(destination, options);
|
|
82
12
|
}
|
|
83
13
|
exports.resolvePathAliases = resolvePathAliases;
|
|
84
|
-
function buildOutResolver(config) {
|
|
85
|
-
const fullTsConfig = JSON.parse(fs_1.default.readFileSync(config.configFileAbsolutePath).toString());
|
|
86
|
-
const { compilerOptions: { baseUrl, outDir }, } = fullTsConfig;
|
|
87
|
-
let outResolver;
|
|
88
|
-
if (outDir) {
|
|
89
|
-
const resolver = (0, tsconfig_paths_1.createMatchPath)(config.absoluteBaseUrl, config.paths);
|
|
90
|
-
outResolver = (requested, readJson, fileExists, extensions) => {
|
|
91
|
-
let resolved = resolver(requested, readJson, fileExists, extensions);
|
|
92
|
-
resolved = resolved?.replace(`${path_1.default.sep}${baseUrl}${path_1.default.sep}`, `${path_1.default.sep}${outDir}${path_1.default.sep}`);
|
|
93
|
-
return resolved;
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
return outResolver;
|
|
97
|
-
}
|
package/package.json
CHANGED