Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local Dependencies property support for Current Working Directory #1196

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ tests-integration/reference-model/Dockerfile
*Debug.log*
tests-integration/cli2-qa-test/test-data/.DS_Store
tests-integration/cli2-qa-test/test-data/business-terms/.DS_Store
*.DS_Store*
8 changes: 4 additions & 4 deletions cli2/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("the dependencies module", () => {
test("should fail if can't find the file.", () => {
try {
dep.LocalFile.parse({
projectDir: __dirname,
baseDir: __dirname,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renaming projectDir to baseDir for consistency

sanitized: "./shouldFail.ir",
});
} catch (error) {
Expand All @@ -39,7 +39,7 @@ describe("the dependencies module", () => {
let expectedUrl = new URL(`file://${expectedFile}`);

let { success: urlSuccess, data: urlData } = dep.LocalFile.safeParse({
projectDir: __dirname,
baseDir: __dirname,
sanitized: `./${fileName}`,
});
expect({ success: urlSuccess, data: urlData }).toStrictEqual({
Expand All @@ -53,7 +53,7 @@ describe("the dependencies module", () => {

let expectedUrl = new URL(`file://${expectedFile}`);
let { success: urlSuccess, data: urlData } = dep.LocalFile.safeParse({
projectDir: __dirname,
baseDir: __dirname,
sanitized: `../${fileName}`,
});
expect({ success: urlSuccess, data: urlData }).toStrictEqual({
Expand All @@ -67,7 +67,7 @@ describe("the dependencies module", () => {

let expectedUrl = new URL(`file://${expectedFile}`);
let { success: urlSuccess, data: urlData } = dep.LocalFile.safeParse({
projectDir: __dirname,
baseDir: __dirname,
sanitized: `../cli/${fileName}`,
});
expect({ success: urlSuccess, data: urlData }).toStrictEqual({
Expand Down
25 changes: 17 additions & 8 deletions cli2/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ export const FileUrl = z.string().trim().url().transform((val, ctx) => {
});

type LocalFileRef = {
projectDir: string,
baseDir: string,
original: string,
fullPath: string,
url?: URL,

}

export const LocalFile = z.object({
projectDir: z.string(),
baseDir: z.string(),
sanitized: z.string(),
}).transform(val => <LocalFileRef>{projectDir:val.projectDir, original: val.sanitized, fullPath: path.resolve(val.projectDir, val.sanitized ) })
}).transform(val => <LocalFileRef>{ baseDir: val.baseDir, original: val.sanitized, fullPath: path.resolve(val.baseDir, val.sanitized) })
.transform(ref => <LocalFileRef>({ ...ref, url: new URL(`file://${ref.fullPath}`) }))
.refine((ref: LocalFileRef) => fs.existsSync(ref.fullPath),
(ref: LocalFileRef) => {
Expand Down Expand Up @@ -87,7 +87,7 @@ const IncludeProvided = z.object({

const LocalDependencyProvided = z.object({
eventKind: z.literal('LocalDependencyProvided'),
payload: z.string()
payload: z.string()
})

const DependencyProvided = z.object({
Expand Down Expand Up @@ -148,7 +148,7 @@ export async function loadAllDependencies(config: DependencyConfig) {
});
}

const load = (config: DependencyConfig) => function(event: DependencyEvent) {
const load = (config: DependencyConfig) => function (event: DependencyEvent) {

//TODO: Clear this up
let source: "dependencies" | "localDependencies" | "includes";
Expand All @@ -173,7 +173,7 @@ const load = (config: DependencyConfig) => function(event: DependencyEvent) {
}
}
}
const loadDependenciesFromString = (config: DependencyConfig) => function(input: string, source: string) {
const loadDependenciesFromString = (config: DependencyConfig) => function (input: string, source: string) {
const doWork = async () => {
let sanitized = input.trim();
let { success, data } = DataUrl.safeParse(sanitized);
Expand All @@ -195,10 +195,19 @@ const loadDependenciesFromString = (config: DependencyConfig) => function(input:
console.info("Loading url", urlData);
return fetchUriToJson(urlData);
}
let { success: localFileSuccess, data: localUrlData } = LocalFile.safeParse({projectDir : config.projectDir, sanitized});

let { success: localFileCWDSuccess, data: localUrlCWDData } = LocalFile.safeParse({ baseDir: process.cwd(), sanitized });
if (localFileCWDSuccess && localUrlCWDData !== undefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using cwd first as baseDir


console.info("Loading local file url from current working directory ", localUrlCWDData);
return fetchUriToJson(localUrlCWDData);

}

let { success: localFileSuccess, data: localUrlData } = LocalFile.safeParse({ baseDir: config.projectDir, sanitized });
if (localFileSuccess && localUrlData !== undefined) {

console.info("Loading local file url", localUrlData);
console.info("Loading local file url from morphir.json directory", localUrlData);
return fetchUriToJson(localUrlData);

}
Expand Down
13 changes: 13 additions & 0 deletions tests-integration/cli2-qa-test/cli2.dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ describe('morphir dependencies', () => {

assertMorphirHashesExists();
});
test("should support local dependency from current working directory ", async () => {
let localInclude = 'tests-integration/cli2-qa-test/temp-local-dependencies/dependency-project/morphir-ir.json';
let newMorphir = { ...morphirJSON, localDependencies: [localInclude] };
await makeMorphirJson(newMorphir);



assertMorphirHashesIsMisssing();
const resultIR = await cli2.make(PATH_TO_PROJECT, CLI_OPTIONS)
expect(resultIR).not.toBeNull();

assertMorphirHashesExists();
});
})

describe('Should support data URL dependencies ', () => {
Expand Down
Loading