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

Fix CIP downloading script #1122

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Changes from all 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
37 changes: 22 additions & 15 deletions scripts/cip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,42 @@ const processCIPContentAsync = async (cip_name: string, content: string) => {
.replace(".jpeg)", ".jpeg")
.replace(".json)", ".json");

const buffer = await getBufferContentAsync(
`${cip_repo_raw_base_url}${cip_name}/${modified_file_name}`
);
// Use the path.join method to concatenate paths, which should resolve the issue with
// additional ./ segments in the paths and allow the script to correctly find the file locations
let relativePath = modified_file_name.replace("](", "");

const fullPath = path.join(cip_repo_raw_base_url, cip_name, relativePath);
const buffer = await getBufferContentAsync(fullPath);

fs.mkdirSync(`.${cip_static_resource_path}${cip_name}`, {
recursive: true,
});
const targetDir = path.join('.', cip_static_resource_path, cip_name, path.dirname(relativePath));
const targetFile = path.join(targetDir, path.basename(relativePath));

fs.writeFileSync(
`.${cip_static_resource_path}${cip_name}/${modified_file_name}`,
new Uint8Array(buffer)
);
try {
// Create the target directory if it doesn't exist
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}

fs.writeFileSync(targetFile, new Uint8Array(buffer));

console.log(`Processed CIP content downloaded to ${targetFile}`);

} catch (error) {
console.error(`ERROR: Failed to download and save resource ${modified_file_name} for CIP ${cip_name}: ${error.message}`);
}

// Rewrite link to static folder
content = content.replace(
modified_file_name,
`../../..${cip_static_resource_path}${cip_name}/${modified_file_name}`
);

console.log(
`Processed CIP content downloaded to .${cip_static_resource_path}${cip_name}/${modified_file_name}`
);
}
})
);
}

// Ensure compatibility
content = stringManipulation(content, cip_name);
content = stringManipulation(content, cip_name);

return content;
};
Expand Down
Loading