Skip to content

Commit

Permalink
refactoring and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
reebayroo committed Oct 6, 2024
1 parent 1ef6d73 commit de6cc65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
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
28 changes: 8 additions & 20 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 All @@ -54,19 +54,6 @@ export const LocalFile = z.object({
})
.transform(ref => ref.url!);


export const CurrrentWorkDirectorReferenceFile = z.object({
projectDir: z.string(),
sanitized: z.string(),
}).transform(val => <LocalFileRef>{projectDir:val.projectDir, original: val.sanitized, fullPath: path.resolve(process.cwd(), val.sanitized ) })
.transform(ref => <LocalFileRef>({ ...ref, url: new URL(`file://${ref.fullPath}`) }))
.refine((ref: LocalFileRef) => fs.existsSync(ref.fullPath),
(ref: LocalFileRef) => {
console.error(`File not found from current directory ${ref.original}: ${ref.fullPath}`)
return { message: `File not found ${ref.original}` };
})
.transform(ref => ref.url!);

const SUPPORTED_PROTOCOLS = new Set(["http:", "https:", "ftp:"]);

export const Url = z.string().url().transform((url) => new URL(url))
Expand Down Expand Up @@ -208,18 +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: 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);

}

let { success: localFileCWDSuccess, data: localUrlCWDData } = CurrrentWorkDirectorReferenceFile.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", localUrlCWDData);
console.info("Loading local file url from current working directory ", localUrlCWDData);
return fetchUriToJson(localUrlCWDData);

}
Expand Down

0 comments on commit de6cc65

Please sign in to comment.