Skip to content

Commit

Permalink
Local Dependencies property support for Current Working Directory (#1196
Browse files Browse the repository at this point in the history
)

* ignore all .DS_Store

* Support to Current Working Directory

* refactoring and clean up

* Fetch cwd files before project dir files

* attempt to fix vulnerability

* attempt to fix vulnerability

* putting package-lock back
  • Loading branch information
reebayroo authored Oct 7, 2024
1 parent 4b36eb7 commit 23aaff1
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 94 deletions.
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,
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) {

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
Loading

0 comments on commit 23aaff1

Please sign in to comment.