pull github folder

pull/4998/head
yann300 7 months ago
parent cd416d6183
commit c8de8b9700
  1. 11
      libs/remix-core-plugin/src/lib/compiler-content-imports.ts
  2. 11
      libs/remix-ui/workspace/src/lib/actions/workspace.ts
  3. 36
      libs/remix-url-resolver/src/github-folder-resolver.ts
  4. 1
      libs/remix-url-resolver/src/index.ts

@ -1,12 +1,12 @@
'use strict'
import { Plugin } from '@remixproject/engine'
import { RemixURLResolver } from '@remix-project/remix-url-resolver'
import { RemixURLResolver, githubFolderResolver } from '@remix-project/remix-url-resolver'
const profile = {
name: 'contentImport',
displayName: 'content import',
version: '0.0.1',
methods: ['resolve', 'resolveAndSave', 'isExternalUrl']
methods: ['resolve', 'resolveAndSave', 'isExternalUrl', 'resolveGithubFolder']
}
export type ResolvedImport = {
@ -212,4 +212,11 @@ export class CompilerImports extends Plugin {
throw new Error(e)
}
}
async resolveGithubFolder (url) {
const ghFolder = {}
const token = await this.call('settings', 'get', 'settings/gist-access-token')
await githubFolderResolver(url, ghFolder, token)
return ghFolder
}
}

@ -238,6 +238,7 @@ export type UrlParametersType = {
shareCode: string
url: string
language: string
ghfolder: string
}
export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDefault', opts?) => {
@ -284,11 +285,16 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe
await workspaceProvider.set(path, content)
}
if (params.url) {
if (params.ghfolder === 'true') {
const files = await plugin.call('contentImport', 'resolveGithubFolder', params.url)
console.log(files)
for (const [path, content] of Object.entries(files)) {
await workspaceProvider.set(path, content)
}
} else {
const data = await plugin.call('contentImport', 'resolve', params.url)
path = data.cleanUrl
content = data.content
try {
content = JSON.parse(content) as any
if (content.language && content.language === 'Solidity' && content.sources) {
@ -307,6 +313,7 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe
await workspaceProvider.set(path, content)
}
}
}
return path
} catch (e) {
console.error(e)

@ -0,0 +1,36 @@
// eslint-disable-next-line no-unused-vars
import axios, { AxiosResponse } from 'axios'
export const githubFolderResolver = async (url, obj = {}, token) => {
const child = await pullFolder(url, token)
for (const item of child) {
console.log(item)
if (item.type === 'file') {
const response: AxiosResponse = await axios.get(item.download_url, { transformResponse: res => res })
obj[item.path] = response.data
} else {
// dir
await githubFolderResolver(item.html_url, obj, token)
}
}
return obj
}
const pullFolder = async (url, token) => {
url = new URL(url);
const pathname = url.pathname;
const pathParts = pathname.split('/');
const username = pathParts[1];
const repo = pathParts[2];
const folderPath = pathParts.slice(5).join('/');
const apiUrl = `https://api.github.com/repos/${username}/${repo}/contents/${folderPath}`;
const response = await axios.get(apiUrl,
{
headers: {
Authorization: `Bearer ${token}`
}
});
const data = await response.data;
return data
}

@ -1 +1,2 @@
export { RemixURLResolver } from './resolve'
export { githubFolderResolver } from './github-folder-resolver'

Loading…
Cancel
Save