@worldware/msg-cli 0.0.4 → 0.0.5
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.
|
@@ -117,7 +117,7 @@ class CreateResource extends core_1.Command {
|
|
|
117
117
|
}
|
|
118
118
|
try {
|
|
119
119
|
const url = (0, url_1.pathToFileURL)(outPath).href;
|
|
120
|
-
yield
|
|
120
|
+
yield (0, create_resource_helpers_js_1.dynamicImportFromUrl)(url);
|
|
121
121
|
}
|
|
122
122
|
catch (err) {
|
|
123
123
|
try {
|
|
@@ -42,6 +42,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
42
42
|
});
|
|
43
43
|
};
|
|
44
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.dynamicImportFromUrl = dynamicImportFromUrl;
|
|
45
46
|
exports.readPackageJsonForCreateResource = readPackageJsonForCreateResource;
|
|
46
47
|
exports.importMsgProjectForResource = importMsgProjectForResource;
|
|
47
48
|
exports.generateMsgResourceContent = generateMsgResourceContent;
|
|
@@ -50,6 +51,22 @@ const fs_1 = require("fs");
|
|
|
50
51
|
const path_1 = require("path");
|
|
51
52
|
const url_1 = require("url");
|
|
52
53
|
const init_helpers_js_1 = require("./init-helpers.js");
|
|
54
|
+
/**
|
|
55
|
+
* Dynamically import a module from a file URL.
|
|
56
|
+
* Tries native import() first (works in ESM/Vitest); falls back to Function-based
|
|
57
|
+
* import when running as compiled CJS (where import() would become require()).
|
|
58
|
+
*/
|
|
59
|
+
function dynamicImportFromUrl(url) {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
try {
|
|
62
|
+
return yield Promise.resolve(`${url}`).then(s => __importStar(require(s)));
|
|
63
|
+
}
|
|
64
|
+
catch (_a) {
|
|
65
|
+
const load = new Function("url", "return import(url)");
|
|
66
|
+
return load(url);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
53
70
|
/**
|
|
54
71
|
* Reads package.json and returns i18n directory and module type info.
|
|
55
72
|
* @param cwd - Directory to start from (e.g. process.cwd())
|
|
@@ -82,7 +99,7 @@ function dirFromSourceLocale(sourceLocale) {
|
|
|
82
99
|
*/
|
|
83
100
|
function importMsgProjectForResource(projectsDir, projectName) {
|
|
84
101
|
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
var _a, _b;
|
|
102
|
+
var _a, _b, _c;
|
|
86
103
|
const basePath = (0, path_1.join)(projectsDir, projectName);
|
|
87
104
|
const exts = [".ts", ".js"];
|
|
88
105
|
for (const ext of exts) {
|
|
@@ -90,19 +107,20 @@ function importMsgProjectForResource(projectsDir, projectName) {
|
|
|
90
107
|
if ((0, fs_1.existsSync)(p)) {
|
|
91
108
|
try {
|
|
92
109
|
const url = (0, url_1.pathToFileURL)(p).href;
|
|
93
|
-
const mod = yield
|
|
110
|
+
const mod = yield dynamicImportFromUrl(url);
|
|
94
111
|
const data = ((_a = mod === null || mod === void 0 ? void 0 : mod.default) !== null && _a !== void 0 ? _a : mod);
|
|
95
112
|
const sourceLocale = (_b = data === null || data === void 0 ? void 0 : data.locales) === null || _b === void 0 ? void 0 : _b.sourceLocale;
|
|
96
113
|
if (!sourceLocale || typeof sourceLocale !== "string") {
|
|
97
|
-
|
|
114
|
+
throw new Error(`Project file must export a default with locales.sourceLocale (got ${typeof ((_c = data === null || data === void 0 ? void 0 : data.locales) === null || _c === void 0 ? void 0 : _c.sourceLocale)}).`);
|
|
98
115
|
}
|
|
99
116
|
return {
|
|
100
117
|
sourceLocale,
|
|
101
118
|
dir: dirFromSourceLocale(sourceLocale),
|
|
102
119
|
};
|
|
103
120
|
}
|
|
104
|
-
catch (
|
|
105
|
-
|
|
121
|
+
catch (err) {
|
|
122
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
123
|
+
throw new Error(`Project file '${projectName}${ext}' could not be loaded: ${message}`);
|
|
106
124
|
}
|
|
107
125
|
}
|
|
108
126
|
}
|