Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
huangxingguang committed Apr 3, 2021
1 parent 86df5df commit aad162f
Show file tree
Hide file tree
Showing 14 changed files with 4,442 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port


.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 starxg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Sync Config

### For the Terminus terminal

This plugin can Sync configuration files to GitHub Gist or Gitee Gist.

![](./screenshot.png)
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "terminus-sync-config",
"version": "1.0.0",
"description": "Sync configuration files to GitHub Gist or Gitee Gist",
"keywords": [
"terminus-plugin"
],
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"build": "webpack --progress --color",
"watch": "webpack --progress --color --watch",
"prepublishOnly": "npm run build"
},
"files": [
"dist"
],
"author": "starxg",
"license": "MIT",
"devDependencies": {
"@angular/animations": "^7.2.8",
"@angular/common": "^7.2.8",
"@angular/core": "^7.2.8",
"@angular/forms": "^7.2.8",
"@angular/platform-browser": "^7.2.8",
"@ng-bootstrap/ng-bootstrap": "^2.2.0",
"@types/webpack-env": "^1.16.0",
"apply-loader": "^2.0.0",
"awesome-typescript-loader": "^5.2.1",
"css-loader": "^5.1.1",
"electron": "^7.0.0",
"ngx-toastr": "^8.8.0",
"node-sass": "^5.0.0",
"pug": "^2.0.3",
"pug-loader": "^2.4.0",
"rxjs": "^6.2.1",
"sanitize-filename": "^1.6.3",
"sass-loader": "^11.0.1",
"strip-ansi": "^5.0.0",
"style-loader": "^2.0.0",
"terminus-core": "^1.0.135-nightly.0",
"terminus-settings": "^1.0.135-nightly.0",
"terminus-ssh": "^1.0.135-nightly.1",
"terminus-terminal": "^1.0.135-nightly.0",
"typescript": "^4.2.3",
"webpack": "^5.24.4",
"webpack-cli": "^4.5.0",
"axios": "^0.21.1"
},
"peerDependencies": {
"@angular/core": "^4.0.1"
},
"repository": "starxg/terminus-sync-config"
}
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import axios, { AxiosResponse } from "axios";

function resolveGist(response: AxiosResponse): any {
if (response.data.files["config.json"] && response.data.files["config.json"].content) {
return response.data.files["config.json"].content;
}
throw 'the config file is bad.';
}

async function syncGitee(token: string, gist: string, config: string): Promise<string> {
const url = gist ? `https://gitee.com/api/v5/gists/${gist}` : "https://gitee.com/api/v5/gists";
const data = {
access_token: token,
files: {
"config.json": { content: config }
},
description: "sync terminus config",
public: false,
id: gist || ''
};
const method = gist ? 'PATCH' : 'POST';

return new Promise(async (resolve, reject) => {
try {
const result = await axios.request({
method,
url,
data,
})
resolve(result.data.id);
} catch (error) {
if (error.response) {
if (error.response.status === 404) {
reject(error.message);
} else {
reject(error.response.data.message);
}
} else {
reject(error.message);
}
}
});

}

async function syncGithub(token: string, gist: string, config: string): Promise<any> {


const url = gist ? `https://api.github.com/gists/${gist}` : "https://api.github.com/gists";
const data = {
files: {
"config.json": { content: config }
},
description: "sync terminus config",
public: false
};
const method = gist ? 'PATCH' : 'POST';

return new Promise(async (resolve, reject) => {
try {
const result = await axios.request({
method,
url,
data,
headers: {
Authorization: `Bearer ${token}`
}
})
resolve(result.data.id);
} catch (error) {
if (error.response) {
if (error.response.status === 404) {
reject(error.message);
} else {
reject(error.response.data.message);
}
} else {
reject(error.message);
}
}
});
}

export function syncGist(type: string, token: string, gist: string, config: string): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
if (type === 'Gitee') {
resolve(await syncGitee(token, gist, config));
} else if (type === 'GitHub') {
resolve(await syncGithub(token, gist, config));
} else {
throw "unknown the type " + type;
}
} catch (error) {
reject(error);
}
});
}

export function getGist(type: string, token: string, gist: string): Promise<string> {

const isGithub = type === 'GitHub';
const url = isGithub ? `https://api.github.com/gists/${gist}` : `https://gitee.com/api/v5/gists/${gist}`;

var config = <any>{
headers: {},
params: {}
};

if (isGithub) config.headers.Authorization = `Bearer ${token}`
else config.params.access_token = token;

return new Promise(async (resolve, reject) => {
try {

resolve(resolveGist(await axios.get(url, config)));

} catch (error) {
console.error(error);
if (typeof error === "string") {
reject(error);
} else if (error.response) {
if (error.response.status === 404) {
reject(error.message);
} else {
reject(error.response.data.message);
}
} else {
reject(error.message);
}
}
});
}
12 changes: 12 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ConfigProvider } from 'terminus-core';

export class SyncConfigProvider extends ConfigProvider {
defaults = {
syncConfig: {
type: 'Off',
token: '',
gist: '',
lastSyncTime: '-'
}
}
}
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { NgModule } from '@angular/core'
import { SyncConfigSettingsTabComponent } from 'settingsTab.component';
import { SettingsTabProvider } from 'terminus-settings'
import { SyncConfigSettingsTabProvider } from './settings'
import { CommonModule } from '@angular/common'
import { FormsModule } from '@angular/forms'
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'
import { ConfigProvider } from 'terminus-core';
import { SyncConfigProvider } from 'config';

@NgModule({
imports: [
CommonModule,
FormsModule,
NgbModule,
],
providers: [
{ provide: SettingsTabProvider, useClass: SyncConfigSettingsTabProvider, multi: true },
{ provide: ConfigProvider, useClass: SyncConfigProvider, multi: true },
],
entryComponents: [
SyncConfigSettingsTabComponent
],
declarations: [
SyncConfigSettingsTabComponent
],
})

export default class SyncConfigModule { }
14 changes: 14 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@angular/core'
import { SyncConfigSettingsTabComponent } from 'settingsTab.component'
import { SettingsTabProvider } from 'terminus-settings'

@Injectable()
export class SyncConfigSettingsTabProvider extends SettingsTabProvider {
id = 'sync-config'
icon = 'cloud'
title = 'Sync Config'

getComponentType(): any {
return SyncConfigSettingsTabComponent;
}
}
51 changes: 51 additions & 0 deletions src/settingsTab.component.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
h3 Sync Config

.row
.col-md-7
.form-group
label Type
select.form-control(
[(ngModel)]='config.store.syncConfig.type',
(ngModelChange)='config.save()'
)
option(ngValue='Off') Off
option(ngValue='GitHub') GitHub
option(ngValue='Gitee') Gitee

.row(*ngIf='config.store.syncConfig.type !== "Off"')
.col-md-7
.form-group
label Token
input.form-control(
type='text',
placeholder="token",
[(ngModel)]='config.store.syncConfig.token',
(ngModelChange)='config.save()',
)
.form-group
label Gist
input.form-control(
type='text',
placeholder="use existing gist id or automatic generation gist id",
[(ngModel)]='config.store.syncConfig.gist',
(ngModelChange)='config.save()',
)
.form-group
button.btn.btn-outline-primary.mr-3(
(click)='sync(true)',
[disabled]='isUploading || isDownloading',
)
div.fa.fa-fw.fa-upload(*ngIf='!isUploading')
div.fa.fa-cog.fa-spin(*ngIf='isUploading')
span.ml-2 Upload config
button.btn.btn-outline-warning(
(click)='sync(false)',
style={'margin-right':'10px'},
[disabled]='isUploading || isDownloading',
)
div.fa.fa-cog.fa-spin(*ngIf='isDownloading')
div.fa.fa-fw.fa-download(*ngIf='!isDownloading')
span.ml-2 Download config
.form-group
p.text-white(*ngIf='isUploading || isDownloading') Syncing
p.text-white(*ngIf='!(isUploading || isDownloading)') Last sync time : {{ config.store.syncConfig.lastSyncTime }}
Loading

0 comments on commit aad162f

Please sign in to comment.