-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: v1 logic for downloading plugin repository
- Loading branch information
1 parent
8fed344
commit cebedce
Showing
7 changed files
with
252 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import path from 'path'; | ||
import fetch from 'node-fetch'; | ||
import fs from 'fs'; | ||
import util from 'util'; | ||
import tar from 'tar'; | ||
import {PackageJsonMonoklePlugin} from '@models/plugin'; | ||
import {downloadFile} from '@utils/http'; | ||
|
||
const fsExistsPromise = util.promisify(fs.exists); | ||
const fsMkdirPromise = util.promisify(fs.mkdir); | ||
|
||
const GITHUB_URL = 'https://github.com'; | ||
const GITHUB_REPOSITORY_REGEX = /^https:\/\/github.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+/i; | ||
|
||
function isValidRepositoryUrl(repositoryUrl: string) { | ||
return GITHUB_REPOSITORY_REGEX.test(repositoryUrl); | ||
} | ||
|
||
function extractRepositoryOwnerAndName(pluginUrl: string) { | ||
if (!isValidRepositoryUrl(pluginUrl)) { | ||
throw new Error('Invalig repository URL'); | ||
} | ||
const repositoryPath = pluginUrl.split(`${GITHUB_URL}/`)[1]; | ||
const [repositoryOwner, repositoryName] = repositoryPath.split('/'); | ||
|
||
return { | ||
repositoryOwner, | ||
repositoryName, | ||
}; | ||
} | ||
|
||
function extractPluginInfo(packageJson: PackageJsonMonoklePlugin) { | ||
const {name, version, author, description, monoklePlugin} = packageJson; | ||
if (name === undefined || version === undefined || author === undefined || monoklePlugin === undefined) { | ||
throw new Error('Invalid plugin package.json'); | ||
} | ||
return {name, version, author, description, monoklePlugin}; | ||
} | ||
|
||
async function fetchLatestRelease(repositoryOwner: string, repositoryName: string) { | ||
const latestReleaseUrl = `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/releases/latest`; | ||
const latestReleaseResponse = await fetch(latestReleaseUrl); | ||
const latestReleaseJson: any = await latestReleaseResponse.json(); | ||
const targetCommitish = latestReleaseJson?.target_commitish; | ||
const tarballUrl = latestReleaseJson?.tarball_url; | ||
const tagName = latestReleaseJson?.tag_name; | ||
if (typeof targetCommitish !== 'string' || typeof tarballUrl !== 'string' || typeof tagName !== 'string') { | ||
throw new Error("Couldn't fetch the latest release of the plugin."); | ||
} | ||
return {targetCommitish, tarballUrl, tagName}; | ||
} | ||
|
||
async function fetchPackageJson(repositoryOwner: string, repositoryName: string, targetCommitish: string) { | ||
const packageJsonUrl = `https://raw.githubusercontent.com/${repositoryOwner}/${repositoryName}/${targetCommitish}/package.json`; | ||
const packageJsonResponse = await fetch(packageJsonUrl); | ||
if (!packageJsonResponse.ok) { | ||
throw new Error("Couldn't fetch package.json"); | ||
} | ||
const packageJson = await packageJsonResponse.json(); | ||
return packageJson as PackageJsonMonoklePlugin; | ||
} | ||
|
||
async function fetchLatestReleaseCommitSha(repositoryOwner: string, repositoryName: string, tagName: string) { | ||
const refTagUrl = `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/git/ref/tags/${tagName}`; | ||
const refTagResponse = await fetch(refTagUrl); | ||
if (!refTagResponse.ok) { | ||
throw new Error("Couldn't fetch git ref tag."); | ||
} | ||
const refTagJson: any = await refTagResponse.json(); | ||
|
||
let commitSha: string | undefined; | ||
if (typeof refTagJson?.object?.type !== 'string' || typeof refTagJson?.object?.sha !== 'string') { | ||
throw new Error("Couldn't find the ref tag object."); | ||
} | ||
|
||
if (refTagJson.object.type === 'commit') { | ||
commitSha = refTagJson.object.sha; | ||
} else { | ||
const tagUrl = `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/git/tags/${refTagJson.object.sha}`; | ||
const tagResponse = await fetch(tagUrl); | ||
const tagJson: any = tagResponse.json(); | ||
if (tagJson?.object && typeof tagJson?.object?.sha === 'string') { | ||
commitSha = tagJson.object.sha; | ||
} | ||
} | ||
|
||
if (commitSha === undefined) { | ||
throw new Error("Couldn't find the commit sha of the latest release."); | ||
} | ||
return commitSha; | ||
} | ||
|
||
export async function downloadPlugin(pluginUrl: string, pluginsDir: string) { | ||
const {repositoryOwner, repositoryName} = extractRepositoryOwnerAndName(pluginUrl); | ||
const {targetCommitish, tarballUrl, tagName} = await fetchLatestRelease(repositoryOwner, repositoryName); | ||
const packageJson = await fetchPackageJson(repositoryOwner, repositoryName, targetCommitish); | ||
const commitSha = await fetchLatestReleaseCommitSha(repositoryOwner, repositoryName, tagName); | ||
const pluginInfo = extractPluginInfo(packageJson); | ||
const doesPluginsDirExist = await fsExistsPromise(pluginsDir); | ||
if (!doesPluginsDirExist) { | ||
await fsMkdirPromise(pluginsDir); | ||
} | ||
const pluginFolderPath: string = path.join(pluginsDir, `${repositoryOwner}-${repositoryName}`); | ||
if (!fs.existsSync(pluginFolderPath)) { | ||
await fsMkdirPromise(pluginFolderPath); | ||
} | ||
const pluginTarballFilePath = path.join(pluginFolderPath, `${commitSha}.tgz`); | ||
const pluginCommitShaFolder: string = path.join(pluginFolderPath, commitSha); | ||
if (fs.existsSync(pluginTarballFilePath) || fs.existsSync(pluginCommitShaFolder)) { | ||
throw new Error('Plugin already exists.'); | ||
} | ||
await downloadFile(tarballUrl, pluginTarballFilePath); | ||
await fsMkdirPromise(pluginCommitShaFolder); | ||
await tar.extract({ | ||
file: pluginTarballFilePath, | ||
cwd: pluginCommitShaFolder, | ||
strip: 1, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 34 additions & 2 deletions
36
src/components/organisms/PluginManagerPane/PluginInstallModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const DOWNLOAD_PLUGIN = 'download-plugin'; | ||
export const DOWNLOAD_PLUGIN_RESULT = 'download-plugin-result'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
import {Merge, PackageJson} from 'type-fest'; | ||
|
||
type PackageJsonMonoklePlugin = Merge< | ||
PackageJson, | ||
{ | ||
monoklePlugin?: { | ||
modules: MonoklePluginModule[]; | ||
}; | ||
} | ||
>; | ||
|
||
interface MonoklePluginRepositoryLatestRelease { | ||
commitHash: string; | ||
tagName: string; | ||
tarballUrl: string; | ||
} | ||
|
||
interface MonoklePluginRepository { | ||
owner: string; | ||
name: string; | ||
url: string; | ||
latestRelease: MonoklePluginRepositoryLatestRelease; | ||
} | ||
|
||
interface MonoklePluginModule { | ||
id: string; | ||
type: string; | ||
source: string; | ||
description?: string; | ||
} | ||
|
||
interface MonoklePlugin { | ||
name: string; | ||
description: string; | ||
version: string; | ||
owner: string; | ||
repository: { | ||
name: string; | ||
url: string; | ||
}; | ||
author: string; | ||
description?: string; | ||
location: string; | ||
repository: MonoklePluginRepository; | ||
isActive: boolean; | ||
isInstalled: boolean; | ||
modules: MonoklePluginModule[]; | ||
} | ||
|
||
export type {MonoklePlugin}; | ||
export type { | ||
MonoklePlugin, | ||
MonoklePluginModule, | ||
MonoklePluginRepository, | ||
MonoklePluginRepositoryLatestRelease, | ||
PackageJsonMonoklePlugin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fetch from 'node-fetch'; | ||
import fs from 'fs'; | ||
|
||
export async function downloadFile(sourceUrl: string, destinationFilePath: string): Promise<void> { | ||
const response = await fetch(sourceUrl); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Downdload error: ${response.status} ${response.statusText}`); | ||
} | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
const fileStream = fs.createWriteStream(destinationFilePath); | ||
|
||
if (!response.body) { | ||
throw new Error(`Download error: missing response body.`); | ||
} | ||
|
||
response.body.pipe(fileStream); | ||
|
||
response.body.on('error', err => { | ||
fileStream.close(); | ||
if (fs.existsSync(destinationFilePath) && fs.statSync(destinationFilePath).isFile()) { | ||
fs.unlinkSync(destinationFilePath); | ||
} | ||
reject(err); | ||
}); | ||
|
||
fileStream.on('finish', () => { | ||
fileStream.close(); | ||
resolve(); | ||
}); | ||
}); | ||
} |