@inlang/sdk
Advanced tools
Comparing version 0.25.0 to 0.26.0
@@ -6,4 +6,3 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { loadProject } from "../loadProject.js"; | ||
import { createNodeishMemoryFs } from "@lix-js/fs"; | ||
import { openRepository } from "@lix-js/client"; | ||
import { mockRepo } from "@lix-js/client"; | ||
// ------------------------------------------------------------------------------------------------ | ||
@@ -76,6 +75,6 @@ const config = { | ||
it("should react to changes in config", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(config)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = solidAdapter(await loadProject({ | ||
@@ -102,6 +101,6 @@ projectPath: "/user/project.inlang", | ||
it("react to changes that are unrelated to installed items", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(config)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = solidAdapter(await loadProject({ | ||
@@ -131,3 +130,4 @@ projectPath: "/user/project.inlang", | ||
it("should react to changes in config", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
const mockConfig = { | ||
@@ -161,3 +161,2 @@ sourceLanguageTag: "en", | ||
await fs.writeFile("/user/project.inlang.inlang/settings.json", JSON.stringify(mockConfig)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = solidAdapter(await loadProject({ | ||
@@ -181,6 +180,6 @@ projectPath: "/user/project.inlang.inlang", | ||
it("should react to changes in messages", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang.inlang/settings.json", JSON.stringify(config)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = solidAdapter(await loadProject({ | ||
@@ -229,6 +228,6 @@ projectPath: "/user/project.inlang.inlang", | ||
await createRoot(async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("./project.inlang", { recursive: true }); | ||
await fs.writeFile("./project.inlang/settings.json", JSON.stringify(config)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = solidAdapter(await loadProject({ | ||
@@ -259,5 +258,5 @@ projectPath: "./project.inlang", | ||
await createRoot(async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.writeFile("./project.config.json", JSON.stringify(config)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = solidAdapter(await loadProject({ | ||
@@ -264,0 +263,0 @@ projectPath: "./project.config.json", |
@@ -5,30 +5,7 @@ import type { InlangProject, Subscribable } from "./api.js"; | ||
/** | ||
* Creates an inlang instance. | ||
* | ||
* @param projectPath - Absolute path to the inlang settings file. | ||
* @param @deprecated nodeishFs - Filesystem that implements the NodeishFilesystemSubset interface. | ||
* @param _import - Use `_import` to pass a custom import function for testing, | ||
* and supporting legacy resolvedModules such as CJS. | ||
* | ||
*/ | ||
export declare function loadProject(args: { | ||
projectPath: string; | ||
nodeishFs: Repository["nodeishFs"]; | ||
/** | ||
* The app id is used to identify the app that is using the SDK. | ||
* | ||
* We use the app id to group events in telemetry to answer questions | ||
* like "Which apps causes these errors?" or "Which apps are used more than others?". | ||
* | ||
* @example | ||
* appId: "app.inlang.badge" | ||
*/ | ||
appId?: string; | ||
_import?: ImportFunction; | ||
}): Promise<InlangProject>; | ||
/** | ||
* @param projectPath - Absolute path to the inlang settings file. | ||
* @param repo - An instance of a lix repo as returned by `openRepository`. | ||
* @param _import - Use `_import` to pass a custom import function for testing, | ||
* and supporting legacy resolvedModules such as CJS. | ||
* @param appId - The app id to use for telemetry e.g "app.inlang.badge" | ||
* | ||
@@ -35,0 +12,0 @@ */ |
@@ -20,2 +20,10 @@ import { resolveModules } from "./resolve-modules/index.js"; | ||
const settingsCompiler = TypeCompiler.Compile(ProjectSettings); | ||
/** | ||
* @param projectPath - Absolute path to the inlang settings file. | ||
* @param repo - An instance of a lix repo as returned by `openRepository`. | ||
* @param _import - Use `_import` to pass a custom import function for testing, | ||
* and supporting legacy resolvedModules such as CJS. | ||
* @param appId - The app id to use for telemetry e.g "app.inlang.badge" | ||
* | ||
*/ | ||
export async function loadProject(args) { | ||
@@ -33,13 +41,3 @@ const projectPath = normalizePath(args.projectPath); | ||
} | ||
let fs; | ||
if (args.nodeishFs) { | ||
// TODO: deprecate | ||
fs = args.nodeishFs; | ||
} | ||
else if (args.repo) { | ||
fs = args.repo.nodeishFs; | ||
} | ||
else { | ||
throw new LoadProjectInvalidArgument(`Repo missing from arguments.`, { argument: "repo" }); | ||
} | ||
const fs = args.repo.nodeishFs; | ||
const nodeishFs = createNodeishFsWithAbsolutePaths({ | ||
@@ -46,0 +44,0 @@ projectPath, |
@@ -5,6 +5,6 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { LoadProjectInvalidArgument, ProjectSettingsFileJSONSyntaxError, ProjectSettingsFileNotFoundError, ProjectSettingsInvalidError, } from "./errors.js"; | ||
import { createNodeishMemoryFs, normalizePath } from "@lix-js/fs"; | ||
import { normalizePath } from "@lix-js/fs"; | ||
import { createMessage } from "./test-utilities/createMessage.js"; | ||
import { tryCatch } from "@inlang/result"; | ||
import { mockRepo, openRepository } from "@lix-js/client"; | ||
import { mockRepo } from "@lix-js/client"; | ||
import {} from "@lix-js/fs"; | ||
@@ -89,9 +89,5 @@ // eslint-disable-next-line no-restricted-imports -- test | ||
}); | ||
async function openCiTestRepo() { | ||
return await mockRepo({ | ||
fromSnapshot: JSON.parse(readFileSync(resolve(__dirname, "../mocks/ci-test-repo-no-shallow.json"), { | ||
encoding: "utf-8", | ||
})), | ||
}); | ||
} | ||
const ciTestRepoSnapshot = JSON.parse(readFileSync(resolve(__dirname, "../mocks/ci-test-repo-no-shallow.json"), { | ||
encoding: "utf-8", | ||
})); | ||
// ------------------------------------------------------------------------------------------------ | ||
@@ -105,4 +101,3 @@ /** | ||
it("should throw if a project (path) does not have a name", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const repo = await mockRepo(); | ||
const project = await tryCatch(() => loadProject({ | ||
@@ -116,4 +111,3 @@ projectPath: "/source-code/.inlang", | ||
it("should throw if a project path does not end with .inlang", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const repo = await mockRepo(); | ||
const invalidPaths = [ | ||
@@ -135,4 +129,3 @@ "/source-code/frontend.inlang/settings", | ||
it("should throw if projectPath is not an absolute path", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const repo = await mockRepo(); | ||
const result = await tryCatch(() => loadProject({ | ||
@@ -147,3 +140,3 @@ projectPath: "relative/path", | ||
it("should generate projectId on missing projectid", async () => { | ||
const repo = await openCiTestRepo(); | ||
const repo = await mockRepo({ fromSnapshot: ciTestRepoSnapshot }); | ||
const existing = await repo.nodeishFs | ||
@@ -175,3 +168,3 @@ .readFile("/project.inlang/project_id", { | ||
it("should reuse projectId on existing projectid", async () => { | ||
const repo = await openCiTestRepo(); | ||
const repo = await mockRepo({ fromSnapshot: ciTestRepoSnapshot }); | ||
repo.nodeishFs.writeFile("/project.inlang/project_id", "testId"); | ||
@@ -195,6 +188,6 @@ const result = await tryCatch(() => loadProject({ | ||
it("should resolve from a windows path", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
fs.mkdir("C:\\Users\\user\\project.inlang", { recursive: true }); | ||
fs.writeFile("C:\\Users\\user\\project.inlang\\settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const result = await tryCatch(() => loadProject({ | ||
@@ -210,5 +203,5 @@ projectPath: "C:\\Users\\user\\project.inlang", | ||
it("should return an error if settings file is not found", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
fs.mkdir("/user/project", { recursive: true }); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -222,6 +215,6 @@ projectPath: "/user/non-existend-project.inlang", | ||
it("should return an error if settings file is not a valid JSON", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", "invalid json"); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -235,6 +228,6 @@ projectPath: "/user/project.inlang", | ||
it("should return an error if settings file is does not match schema", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify({})); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -248,6 +241,6 @@ projectPath: "/user/project.inlang", | ||
it("should return the parsed settings", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -261,7 +254,7 @@ projectPath: "/user/project.inlang", | ||
it("should not re-write the settings to disk when initializing", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
const settingsWithDeifferentFormatting = JSON.stringify(settings, undefined, 4); | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", settingsWithDeifferentFormatting); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -290,6 +283,6 @@ projectPath: "/user/project.inlang", | ||
}); | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -320,6 +313,6 @@ projectPath: "/user/project.inlang", | ||
it("should return the settings", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -333,6 +326,6 @@ projectPath: "/user/project.inlang", | ||
it("should set a new settings", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -357,6 +350,6 @@ projectPath: "/user/project.inlang", | ||
it("should fail if settings are not valid", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -372,3 +365,4 @@ projectPath: "/user/project.inlang", | ||
it("should throw an error if sourceLanguageTag is not in languageTags", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
@@ -381,3 +375,2 @@ const settings = { | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -392,6 +385,6 @@ projectPath: "/user/project.inlang", | ||
it("should write settings to disk", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -416,3 +409,4 @@ projectPath: "/user/project.inlang", | ||
it("should return the installed items", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
const settings = { | ||
@@ -425,3 +419,2 @@ sourceLanguageTag: "en", | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -447,3 +440,4 @@ projectPath: "/user/project.inlang", | ||
it("should apply 'warning' as default lint level to lint rules that have no lint level defined in the settings", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
const settings = { | ||
@@ -456,3 +450,2 @@ sourceLanguageTag: "en", | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -486,3 +479,4 @@ projectPath: "/user/project.inlang", | ||
}; | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
@@ -494,3 +488,2 @@ await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify({ | ||
})); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const _import = async (name) => { | ||
@@ -531,3 +524,4 @@ return { | ||
}; | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
@@ -539,3 +533,2 @@ await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify({ | ||
})); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const _import = async (name) => { | ||
@@ -557,6 +550,6 @@ return { | ||
it("should return the errors", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -574,6 +567,6 @@ projectPath: "/user/project.inlang", | ||
it("should return the app specific api", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -591,6 +584,6 @@ projectPath: "/user/project.inlang", | ||
it("should return the messages", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -606,3 +599,4 @@ projectPath: "/user/project.inlang", | ||
it("should call saveMessages() on updates", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
const settings = { | ||
@@ -619,3 +613,2 @@ sourceLanguageTag: "en", | ||
await fs.mkdir("./resources"); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const mockSaveFn = vi.fn(); | ||
@@ -771,3 +764,4 @@ const _mockPlugin = { | ||
it("should pass all messages, regardless of which message changed, to saveMessages()", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
const settings = { | ||
@@ -783,3 +777,2 @@ sourceLanguageTag: "en", | ||
await fs.writeFile("./project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const mockSaveFn = vi.fn(); | ||
@@ -815,6 +808,6 @@ const _mockPlugin = { | ||
it.todo("should throw if lint reports are not initialized yet", async () => { | ||
const fs = await createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project", { recursive: true }); | ||
await fs.writeFile("/user/project/project.inlang.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -840,6 +833,6 @@ projectPath: "/user/project/project.inlang.json", | ||
}; | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
await fs.mkdir("/user/project.inlang", { recursive: true }); | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
const project = await loadProject({ | ||
@@ -858,3 +851,4 @@ projectPath: "/user/project.inlang", | ||
it("changing files in resources should trigger callback of message query", async () => { | ||
const fs = createNodeishMemoryFs(); | ||
const repo = await mockRepo(); | ||
const fs = repo.nodeishFs; | ||
const messages = { | ||
@@ -903,3 +897,2 @@ $schema: "https://inlang.com/schema/inlang-message-format", | ||
await fs.writeFile("./project.inlang/settings.json", JSON.stringify(settings)); | ||
const repo = await openRepository("file://", { nodeishFs: fs }); | ||
// establish watcher | ||
@@ -906,0 +899,0 @@ const project = await loadProject({ |
{ | ||
"name": "@inlang/sdk", | ||
"type": "module", | ||
"version": "0.25.0", | ||
"version": "0.26.0", | ||
"license": "Apache-2.0", | ||
@@ -28,7 +28,7 @@ "publishConfig": { | ||
"throttle-debounce": "^5.0.0", | ||
"@inlang/json-types": "1.1.0", | ||
"@inlang/language-tag": "1.4.0", | ||
"@inlang/message-lint-rule": "1.4.1", | ||
"@inlang/module": "1.2.3", | ||
"@inlang/message": "2.0.1", | ||
"@inlang/json-types": "1.1.0", | ||
"@inlang/module": "1.2.3", | ||
"@inlang/message-lint-rule": "1.4.1", | ||
"@inlang/plugin": "2.4.3", | ||
@@ -35,0 +35,0 @@ "@inlang/project-settings": "2.2.1", |
@@ -7,4 +7,3 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { loadProject } from "../loadProject.js" | ||
import { createNodeishMemoryFs } from "@lix-js/fs" | ||
import { openRepository } from "@lix-js/client" | ||
import { mockRepo } from "@lix-js/client" | ||
import type { | ||
@@ -94,6 +93,6 @@ Message, | ||
it("should react to changes in config", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(config)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = solidAdapter( | ||
@@ -129,6 +128,6 @@ await loadProject({ | ||
it("react to changes that are unrelated to installed items", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(config)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = solidAdapter( | ||
@@ -167,3 +166,4 @@ await loadProject({ | ||
it("should react to changes in config", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
const mockConfig: ProjectSettings = { | ||
@@ -200,3 +200,2 @@ sourceLanguageTag: "en", | ||
await fs.writeFile("/user/project.inlang.inlang/settings.json", JSON.stringify(mockConfig)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = solidAdapter( | ||
@@ -229,6 +228,6 @@ await loadProject({ | ||
it("should react to changes in messages", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang.inlang/settings.json", JSON.stringify(config)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = solidAdapter( | ||
@@ -293,6 +292,6 @@ await loadProject({ | ||
await createRoot(async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("./project.inlang", { recursive: true }) | ||
await fs.writeFile("./project.inlang/settings.json", JSON.stringify(config)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = solidAdapter( | ||
@@ -334,5 +333,5 @@ await loadProject({ | ||
await createRoot(async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.writeFile("./project.config.json", JSON.stringify(config)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = solidAdapter( | ||
@@ -339,0 +338,0 @@ await loadProject({ |
@@ -19,6 +19,6 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
} from "./errors.js" | ||
import { createNodeishMemoryFs, normalizePath } from "@lix-js/fs" | ||
import { normalizePath } from "@lix-js/fs" | ||
import { createMessage } from "./test-utilities/createMessage.js" | ||
import { tryCatch } from "@inlang/result" | ||
import { mockRepo, openRepository } from "@lix-js/client" | ||
import { mockRepo } from "@lix-js/client" | ||
import { type Snapshot } from "@lix-js/fs" | ||
@@ -113,11 +113,7 @@ // eslint-disable-next-line no-restricted-imports -- test | ||
async function openCiTestRepo() { | ||
return await mockRepo({ | ||
fromSnapshot: JSON.parse( | ||
readFileSync(resolve(__dirname, "../mocks/ci-test-repo-no-shallow.json"), { | ||
encoding: "utf-8", | ||
}) | ||
) as Snapshot, | ||
const ciTestRepoSnapshot = JSON.parse( | ||
readFileSync(resolve(__dirname, "../mocks/ci-test-repo-no-shallow.json"), { | ||
encoding: "utf-8", | ||
}) | ||
} | ||
) as Snapshot | ||
@@ -133,4 +129,3 @@ // ------------------------------------------------------------------------------------------------ | ||
it("should throw if a project (path) does not have a name", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const repo = await mockRepo() | ||
const project = await tryCatch(() => | ||
@@ -147,4 +142,3 @@ loadProject({ | ||
it("should throw if a project path does not end with .inlang", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const repo = await mockRepo() | ||
@@ -171,4 +165,3 @@ const invalidPaths = [ | ||
it("should throw if projectPath is not an absolute path", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const repo = await mockRepo() | ||
@@ -187,3 +180,3 @@ const result = await tryCatch(() => | ||
it("should generate projectId on missing projectid", async () => { | ||
const repo = await openCiTestRepo() | ||
const repo = await mockRepo({ fromSnapshot: ciTestRepoSnapshot }) | ||
@@ -224,3 +217,3 @@ const existing = await repo.nodeishFs | ||
it("should reuse projectId on existing projectid", async () => { | ||
const repo = await openCiTestRepo() | ||
const repo = await mockRepo({ fromSnapshot: ciTestRepoSnapshot }) | ||
@@ -252,6 +245,6 @@ repo.nodeishFs.writeFile("/project.inlang/project_id", "testId") | ||
it("should resolve from a windows path", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
fs.mkdir("C:\\Users\\user\\project.inlang", { recursive: true }) | ||
fs.writeFile("C:\\Users\\user\\project.inlang\\settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -272,5 +265,5 @@ const result = await tryCatch(() => | ||
it("should return an error if settings file is not found", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
fs.mkdir("/user/project", { recursive: true }) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -287,6 +280,6 @@ const project = await loadProject({ | ||
it("should return an error if settings file is not a valid JSON", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", "invalid json") | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -303,6 +296,6 @@ const project = await loadProject({ | ||
it("should return an error if settings file is does not match schema", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify({})) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -319,6 +312,6 @@ const project = await loadProject({ | ||
it("should return the parsed settings", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -334,7 +327,7 @@ projectPath: "/user/project.inlang", | ||
it("should not re-write the settings to disk when initializing", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
const settingsWithDeifferentFormatting = JSON.stringify(settings, undefined, 4) | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", settingsWithDeifferentFormatting) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -370,6 +363,6 @@ const project = await loadProject({ | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -405,6 +398,6 @@ const project = await loadProject({ | ||
it("should return the settings", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -420,6 +413,6 @@ projectPath: "/user/project.inlang", | ||
it("should set a new settings", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -448,6 +441,6 @@ projectPath: "/user/project.inlang", | ||
it("should fail if settings are not valid", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -465,3 +458,4 @@ projectPath: "/user/project.inlang", | ||
it("should throw an error if sourceLanguageTag is not in languageTags", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
@@ -476,3 +470,2 @@ | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -490,6 +483,6 @@ const project = await loadProject({ | ||
it("should write settings to disk", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -519,3 +512,4 @@ projectPath: "/user/project.inlang", | ||
it("should return the installed items", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
const settings: ProjectSettings = { | ||
@@ -528,3 +522,2 @@ sourceLanguageTag: "en", | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -553,3 +546,4 @@ projectPath: "/user/project.inlang", | ||
it("should apply 'warning' as default lint level to lint rules that have no lint level defined in the settings", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
@@ -564,3 +558,2 @@ const settings: ProjectSettings = { | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -598,3 +591,4 @@ const project = await loadProject({ | ||
} | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
@@ -609,3 +603,2 @@ await fs.writeFile( | ||
) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -652,3 +645,4 @@ const _import: ImportFunction = async (name) => { | ||
} | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
@@ -663,3 +657,2 @@ await fs.writeFile( | ||
) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const _import: ImportFunction = async (name) => { | ||
@@ -687,6 +680,6 @@ return { | ||
it("should return the errors", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -705,6 +698,6 @@ projectPath: "/user/project.inlang", | ||
it("should return the app specific api", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -724,6 +717,6 @@ projectPath: "/user/project.inlang", | ||
it("should return the messages", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -741,3 +734,4 @@ projectPath: "/user/project.inlang", | ||
it("should call saveMessages() on updates", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
@@ -757,3 +751,2 @@ const settings: ProjectSettings = { | ||
await fs.mkdir("./resources") | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -921,3 +914,4 @@ const mockSaveFn = vi.fn() | ||
it("should pass all messages, regardless of which message changed, to saveMessages()", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
@@ -935,3 +929,2 @@ const settings: ProjectSettings = { | ||
await fs.writeFile("./project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -976,6 +969,6 @@ const mockSaveFn = vi.fn() | ||
it.todo("should throw if lint reports are not initialized yet", async () => { | ||
const fs = await createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project", { recursive: true }) | ||
await fs.writeFile("/user/project/project.inlang.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -1000,6 +993,6 @@ projectPath: "/user/project/project.inlang.json", | ||
} | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
await fs.mkdir("/user/project.inlang", { recursive: true }) | ||
await fs.writeFile("/user/project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
const project = await loadProject({ | ||
@@ -1019,3 +1012,4 @@ projectPath: "/user/project.inlang", | ||
it("changing files in resources should trigger callback of message query", async () => { | ||
const fs = createNodeishMemoryFs() | ||
const repo = await mockRepo() | ||
const fs = repo.nodeishFs | ||
@@ -1071,3 +1065,2 @@ const messages = { | ||
await fs.writeFile("./project.inlang/settings.json", JSON.stringify(settings)) | ||
const repo = await openRepository("file://", { nodeishFs: fs }) | ||
@@ -1074,0 +1067,0 @@ // establish watcher |
@@ -38,31 +38,7 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
/** | ||
* Creates an inlang instance. | ||
* | ||
* @param projectPath - Absolute path to the inlang settings file. | ||
* @param @deprecated nodeishFs - Filesystem that implements the NodeishFilesystemSubset interface. | ||
* @param _import - Use `_import` to pass a custom import function for testing, | ||
* and supporting legacy resolvedModules such as CJS. | ||
* | ||
*/ | ||
export async function loadProject(args: { | ||
projectPath: string | ||
nodeishFs: Repository["nodeishFs"] | ||
/** | ||
* The app id is used to identify the app that is using the SDK. | ||
* | ||
* We use the app id to group events in telemetry to answer questions | ||
* like "Which apps causes these errors?" or "Which apps are used more than others?". | ||
* | ||
* @example | ||
* appId: "app.inlang.badge" | ||
*/ | ||
appId?: string | ||
_import?: ImportFunction | ||
}): Promise<InlangProject> | ||
/** | ||
* @param projectPath - Absolute path to the inlang settings file. | ||
* @param repo - An instance of a lix repo as returned by `openRepository`. | ||
* @param _import - Use `_import` to pass a custom import function for testing, | ||
* and supporting legacy resolvedModules such as CJS. | ||
* @param appId - The app id to use for telemetry e.g "app.inlang.badge" | ||
* | ||
@@ -75,10 +51,2 @@ */ | ||
_import?: ImportFunction | ||
}): Promise<InlangProject> | ||
export async function loadProject(args: { | ||
projectPath: string | ||
repo?: Repository | ||
appId?: string | ||
_import?: ImportFunction | ||
nodeishFs?: Repository["nodeishFs"] | ||
}): Promise<InlangProject> { | ||
@@ -104,11 +72,3 @@ const projectPath = normalizePath(args.projectPath) | ||
let fs: Repository["nodeishFs"] | ||
if (args.nodeishFs) { | ||
// TODO: deprecate | ||
fs = args.nodeishFs | ||
} else if (args.repo) { | ||
fs = args.repo.nodeishFs | ||
} else { | ||
throw new LoadProjectInvalidArgument(`Repo missing from arguments.`, { argument: "repo" }) | ||
} | ||
const fs = args.repo.nodeishFs | ||
@@ -115,0 +75,0 @@ const nodeishFs = createNodeishFsWithAbsolutePaths({ |
Sorry, the diff of this file is not supported yet
430354
11084