@inlang/sdk 0.36.0 → 0.36.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/dist/resolve-modules/cache.d.ts +1 -1
- package/dist/resolve-modules/cache.d.ts.map +1 -1
- package/dist/resolve-modules/cache.js +13 -3
- package/dist/resolve-modules/import.d.ts +1 -1
- package/dist/resolve-modules/import.d.ts.map +1 -1
- package/dist/resolve-modules/import.test.js +1 -0
- package/package.json +6 -6
- package/src/resolve-modules/cache.ts +15 -4
- package/src/resolve-modules/import.test.ts +1 -0
- package/src/resolve-modules/import.ts +2 -2
|
@@ -2,5 +2,5 @@ import type { NodeishFilesystemSubset } from "@inlang/plugin";
|
|
|
2
2
|
/**
|
|
3
3
|
* Implements a "Network-First" caching strategy.
|
|
4
4
|
*/
|
|
5
|
-
export declare function withCache(moduleLoader: (uri: string) => Promise<string>, projectPath: string, nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile">): (uri: string) => Promise<string>;
|
|
5
|
+
export declare function withCache(moduleLoader: (uri: string) => Promise<string>, projectPath: string, nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile" | "mkdir">): (uri: string) => Promise<string>;
|
|
6
6
|
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/resolve-modules/cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/resolve-modules/cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAkD7D;;GAEG;AACH,wBAAgB,SAAS,CACxB,YAAY,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,EAC9C,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,IAAI,CAAC,uBAAuB,EAAE,UAAU,GAAG,WAAW,GAAG,OAAO,CAAC,GAC1E,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAelC"}
|
|
@@ -12,10 +12,20 @@ async function readModuleFromCache(moduleURI, projectPath, readFile) {
|
|
|
12
12
|
const filePath = projectPath + `/cache/modules/${moduleHash}`;
|
|
13
13
|
return await tryCatch(async () => await readFile(filePath, { encoding: "utf-8" }));
|
|
14
14
|
}
|
|
15
|
-
async function writeModuleToCache(moduleURI, moduleContent, projectPath, writeFile) {
|
|
15
|
+
async function writeModuleToCache(moduleURI, moduleContent, projectPath, writeFile, mkdir) {
|
|
16
16
|
const moduleHash = escape(moduleURI);
|
|
17
17
|
const filePath = projectPath + `/cache/modules/${moduleHash}`;
|
|
18
|
-
|
|
18
|
+
try {
|
|
19
|
+
await writeFile(filePath, moduleContent);
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
// if ENONET -> likely means the parent directory does not exist yet
|
|
23
|
+
if (!(e instanceof Error) || !e.message.includes("ENONET"))
|
|
24
|
+
return;
|
|
25
|
+
// create the parent directory & retry
|
|
26
|
+
await mkdir(projectPath + `/cache/modules`, { recursive: true });
|
|
27
|
+
await writeFile(filePath, moduleContent);
|
|
28
|
+
}
|
|
19
29
|
}
|
|
20
30
|
/**
|
|
21
31
|
* Implements a "Network-First" caching strategy.
|
|
@@ -33,7 +43,7 @@ export function withCache(moduleLoader, projectPath, nodeishFs) {
|
|
|
33
43
|
}
|
|
34
44
|
else {
|
|
35
45
|
const moduleAsText = networkResult.data;
|
|
36
|
-
await writeModuleToCache(uri, moduleAsText, projectPath, nodeishFs.writeFile);
|
|
46
|
+
await writeModuleToCache(uri, moduleAsText, projectPath, nodeishFs.writeFile, nodeishFs.mkdir);
|
|
37
47
|
return moduleAsText;
|
|
38
48
|
}
|
|
39
49
|
};
|
|
@@ -15,5 +15,5 @@ export type ImportFunction = (uri: string) => Promise<any>;
|
|
|
15
15
|
* const $import = createImport({ readFile: fs.readFile, fetch });
|
|
16
16
|
* const module = await _import('./some-module.js');
|
|
17
17
|
*/
|
|
18
|
-
export declare function createImport(projectPath: string, nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile">): (uri: string) => Promise<any>;
|
|
18
|
+
export declare function createImport(projectPath: string, nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile" | "mkdir">): (uri: string) => Promise<any>;
|
|
19
19
|
//# sourceMappingURL=import.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"import.d.ts","sourceRoot":"","sources":["../../src/resolve-modules/import.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAK7D;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;AAE1D;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC3B,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,IAAI,CAAC,uBAAuB,EAAE,UAAU,GAAG,WAAW,CAAC,
|
|
1
|
+
{"version":3,"file":"import.d.ts","sourceRoot":"","sources":["../../src/resolve-modules/import.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAK7D;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;AAE1D;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC3B,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,IAAI,CAAC,uBAAuB,EAAE,UAAU,GAAG,WAAW,GAAG,OAAO,CAAC,SAE/D,MAAM,kBACnB"}
|
|
@@ -20,6 +20,7 @@ describe("$import", async () => {
|
|
|
20
20
|
const _import = createImport("/project.inlang", {
|
|
21
21
|
writeFile: fs.writeFile,
|
|
22
22
|
readFile: fs.readFile,
|
|
23
|
+
mkdir: fs.mkdir,
|
|
23
24
|
});
|
|
24
25
|
it("should import a module from a local path", async () => {
|
|
25
26
|
const module = await _import("./mock-module.js");
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inlang/sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.36.
|
|
4
|
+
"version": "0.36.1",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -36,15 +36,15 @@
|
|
|
36
36
|
"throttle-debounce": "^5.0.0",
|
|
37
37
|
"@inlang/json-types": "1.1.0",
|
|
38
38
|
"@inlang/language-tag": "1.5.1",
|
|
39
|
-
"@inlang/message-lint-rule": "1.4.7",
|
|
40
|
-
"@inlang/plugin": "2.4.13",
|
|
41
39
|
"@inlang/message": "2.1.0",
|
|
42
40
|
"@inlang/module": "1.2.13",
|
|
41
|
+
"@inlang/message-lint-rule": "1.4.7",
|
|
42
|
+
"@inlang/plugin": "2.4.13",
|
|
43
43
|
"@inlang/project-settings": "2.4.2",
|
|
44
|
-
"@inlang/result": "1.1.0",
|
|
45
|
-
"@inlang/translatable": "1.3.1",
|
|
46
44
|
"@lix-js/client": "2.2.0",
|
|
47
|
-
"@lix-js/fs": "2.1.0"
|
|
45
|
+
"@lix-js/fs": "2.1.0",
|
|
46
|
+
"@inlang/result": "1.1.0",
|
|
47
|
+
"@inlang/translatable": "1.3.1"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@types/debug": "^4.1.12",
|
|
@@ -30,11 +30,22 @@ async function writeModuleToCache(
|
|
|
30
30
|
moduleURI: string,
|
|
31
31
|
moduleContent: string,
|
|
32
32
|
projectPath: string,
|
|
33
|
-
writeFile: NodeishFilesystemSubset["writeFile"]
|
|
33
|
+
writeFile: NodeishFilesystemSubset["writeFile"],
|
|
34
|
+
mkdir: NodeishFilesystemSubset["mkdir"]
|
|
34
35
|
): Promise<void> {
|
|
35
36
|
const moduleHash = escape(moduleURI)
|
|
36
37
|
const filePath = projectPath + `/cache/modules/${moduleHash}`
|
|
37
|
-
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
await writeFile(filePath, moduleContent)
|
|
41
|
+
} catch (e) {
|
|
42
|
+
// if ENONET -> likely means the parent directory does not exist yet
|
|
43
|
+
if (!(e instanceof Error) || !e.message.includes("ENONET")) return
|
|
44
|
+
|
|
45
|
+
// create the parent directory & retry
|
|
46
|
+
await mkdir(projectPath + `/cache/modules`, { recursive: true })
|
|
47
|
+
await writeFile(filePath, moduleContent)
|
|
48
|
+
}
|
|
38
49
|
}
|
|
39
50
|
|
|
40
51
|
/**
|
|
@@ -43,7 +54,7 @@ async function writeModuleToCache(
|
|
|
43
54
|
export function withCache(
|
|
44
55
|
moduleLoader: (uri: string) => Promise<string>,
|
|
45
56
|
projectPath: string,
|
|
46
|
-
nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile">
|
|
57
|
+
nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile" | "mkdir">
|
|
47
58
|
): (uri: string) => Promise<string> {
|
|
48
59
|
return async (uri: string) => {
|
|
49
60
|
const cachePromise = readModuleFromCache(uri, projectPath, nodeishFs.readFile)
|
|
@@ -55,7 +66,7 @@ export function withCache(
|
|
|
55
66
|
else throw networkResult.error
|
|
56
67
|
} else {
|
|
57
68
|
const moduleAsText = networkResult.data
|
|
58
|
-
await writeModuleToCache(uri, moduleAsText, projectPath, nodeishFs.writeFile)
|
|
69
|
+
await writeModuleToCache(uri, moduleAsText, projectPath, nodeishFs.writeFile, nodeishFs.mkdir)
|
|
59
70
|
return moduleAsText
|
|
60
71
|
}
|
|
61
72
|
}
|
|
@@ -23,7 +23,7 @@ export type ImportFunction = (uri: string) => Promise<any>
|
|
|
23
23
|
*/
|
|
24
24
|
export function createImport(
|
|
25
25
|
projectPath: string,
|
|
26
|
-
nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile">
|
|
26
|
+
nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile" | "mkdir">
|
|
27
27
|
) {
|
|
28
28
|
return (uri: string) => $import(uri, projectPath, nodeishFs)
|
|
29
29
|
}
|
|
@@ -31,7 +31,7 @@ export function createImport(
|
|
|
31
31
|
async function $import(
|
|
32
32
|
uri: string,
|
|
33
33
|
projectPath: string,
|
|
34
|
-
nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile">
|
|
34
|
+
nodeishFs: Pick<NodeishFilesystemSubset, "readFile" | "writeFile" | "mkdir">
|
|
35
35
|
): Promise<any> {
|
|
36
36
|
const moduleAsText = uri.startsWith("http")
|
|
37
37
|
? await withCache(readModuleFromCDN, projectPath, nodeishFs)(uri)
|