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

use format:raw for files outside format:json limit #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 31 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,43 @@ function getContentParams() {
return params;
}

function getContent(path) {
octokit.rest.repos.getContent({ path, ...getContentParams() })
.then(data => {
if (Array.isArray(data.data)) {
data.data.forEach(fileData => getContent(fileData.path))
} else {
saveContent(data.data)
async function getContent(path) {
const { data } = await octokit.rest.repos.getContent({ path, ...getContentParams() });
if (Array.isArray(data)) {
data.forEach(fileData => getContent(fileData.path))
} else {
let fileContent = Buffer.from(data.content, 'base64').toString('utf-8');
if (fileContent.length === 0 && data.size > 0) {
// File was over json content byte limit.
// Use the Raw endpoint will provide files data up to 10MB.
fileContent = await getRawFile(path);
}
})
saveContent({ path, fileContent })
}
}

function saveContent(data) {
const fileContent = Buffer.from(data.content, 'base64').toString('utf-8');
if (data.path.includes('/')) {
let foldersPath = data.path.split('/')


async function getRawFile(path) {
const { data } = await octokit.rest.repos.getContent({
...getContentParams(),
path,
mediaType: {
format: "raw"
},
});

return data;
}


function saveContent({ path, fileContent }) {
if (path.includes('/')) {
const foldersPath = path.split('/')
foldersPath.pop()
fs.mkdirSync(foldersPath.join('/'), { recursive: true });
}
fs.writeFile(data.path, fileContent, err => { if (err) throw err });
fs.writeFile(path, fileContent, err => { if (err) throw err });
}

files.forEach(file => {
Expand Down