1 | "use strict";
|
2 |
|
3 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
4 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
5 | };
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.loadTs = exports.loadTsSync = exports.loadYaml = exports.loadJson = exports.loadJs = exports.loadJsSync = void 0;
|
8 | const fs_1 = require("fs");
|
9 | const promises_1 = require("fs/promises");
|
10 | const path_1 = __importDefault(require("path"));
|
11 | const url_1 = require("url");
|
12 | let importFresh;
|
13 | const loadJsSync = function loadJsSync(filepath) {
|
14 | if (importFresh === undefined) {
|
15 | importFresh = require('import-fresh');
|
16 | }
|
17 | return importFresh(filepath);
|
18 | };
|
19 | exports.loadJsSync = loadJsSync;
|
20 | const loadJs = async function loadJs(filepath) {
|
21 | try {
|
22 | const { href } = (0, url_1.pathToFileURL)(filepath);
|
23 | return (await import(href)).default;
|
24 | }
|
25 | catch (error) {
|
26 | return (0, exports.loadJsSync)(filepath, '');
|
27 | }
|
28 | };
|
29 | exports.loadJs = loadJs;
|
30 | let parseJson;
|
31 | const loadJson = function loadJson(filepath, content) {
|
32 | if (parseJson === undefined) {
|
33 | parseJson = require('parse-json');
|
34 | }
|
35 | try {
|
36 | return parseJson(content);
|
37 | }
|
38 | catch (error) {
|
39 | error.message = `JSON Error in ${filepath}:\n${error.message}`;
|
40 | throw error;
|
41 | }
|
42 | };
|
43 | exports.loadJson = loadJson;
|
44 | let yaml;
|
45 | const loadYaml = function loadYaml(filepath, content) {
|
46 | if (yaml === undefined) {
|
47 | yaml = require('js-yaml');
|
48 | }
|
49 | try {
|
50 | return yaml.load(content);
|
51 | }
|
52 | catch (error) {
|
53 | error.message = `YAML Error in ${filepath}:\n${error.message}`;
|
54 | throw error;
|
55 | }
|
56 | };
|
57 | exports.loadYaml = loadYaml;
|
58 | let typescript;
|
59 | const loadTsSync = function loadTsSync(filepath, content) {
|
60 |
|
61 | if (typescript === undefined) {
|
62 | typescript = require('typescript');
|
63 | }
|
64 | const compiledFilepath = `${filepath.slice(0, -2)}cjs`;
|
65 | try {
|
66 | const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {};
|
67 | config.compilerOptions = {
|
68 | ...config.compilerOptions,
|
69 | module: typescript.ModuleKind.NodeNext,
|
70 | moduleResolution: typescript.ModuleResolutionKind.NodeNext,
|
71 | target: typescript.ScriptTarget.ES2022,
|
72 | noEmit: false,
|
73 | };
|
74 | content = typescript.transpileModule(content, config).outputText;
|
75 | (0, fs_1.writeFileSync)(compiledFilepath, content);
|
76 | return (0, exports.loadJsSync)(compiledFilepath, content).default;
|
77 | }
|
78 | catch (error) {
|
79 | error.message = `TypeScript Error in ${filepath}:\n${error.message}`;
|
80 | throw error;
|
81 | }
|
82 | finally {
|
83 | if ((0, fs_1.existsSync)(compiledFilepath)) {
|
84 | (0, fs_1.rmSync)(compiledFilepath);
|
85 | }
|
86 | }
|
87 | };
|
88 | exports.loadTsSync = loadTsSync;
|
89 | const loadTs = async function loadTs(filepath, content) {
|
90 | if (typescript === undefined) {
|
91 | typescript = (await import('typescript')).default;
|
92 | }
|
93 | const compiledFilepath = `${filepath.slice(0, -2)}mjs`;
|
94 | try {
|
95 | const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {};
|
96 | config.compilerOptions = {
|
97 | ...config.compilerOptions,
|
98 | module: typescript.ModuleKind.ES2022,
|
99 | moduleResolution: typescript.ModuleResolutionKind.Bundler,
|
100 | target: typescript.ScriptTarget.ES2022,
|
101 | noEmit: false,
|
102 | };
|
103 | content = typescript.transpileModule(content, config).outputText;
|
104 | await (0, promises_1.writeFile)(compiledFilepath, content);
|
105 | const { href } = (0, url_1.pathToFileURL)(compiledFilepath);
|
106 | return (await import(href)).default;
|
107 | }
|
108 | catch (error) {
|
109 | error.message = `TypeScript Error in ${filepath}:\n${error.message}`;
|
110 | throw error;
|
111 | }
|
112 | finally {
|
113 | if ((0, fs_1.existsSync)(compiledFilepath)) {
|
114 | await (0, promises_1.rm)(compiledFilepath);
|
115 | }
|
116 | }
|
117 | };
|
118 | exports.loadTs = loadTs;
|
119 |
|
120 | function resolveTsConfig(directory) {
|
121 | const filePath = typescript.findConfigFile(directory, (fileName) => {
|
122 | return typescript.sys.fileExists(fileName);
|
123 | });
|
124 | if (filePath !== undefined) {
|
125 | const { config, error } = typescript.readConfigFile(filePath, (path) => typescript.sys.readFile(path));
|
126 | if (error) {
|
127 | throw new Error(`Error in ${filePath}: ${error.messageText.toString()}`);
|
128 | }
|
129 | return config;
|
130 | }
|
131 | return;
|
132 | }
|
133 |
|
\ | No newline at end of file |