Skip to content

Commit d755125

Browse files
Cria plugin para sincronizar workspaces do insomnia em um repositório local
1 parent 1ab1b0b commit d755125

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

icon.svg

Lines changed: 3 additions & 0 deletions
Loading

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "insomnia-plugin-repo-sync",
3+
"version": "0.1.0",
4+
"author": "Rafael Simao <[email protected]>",
5+
"description": "Repo Sync Plugin for Insomnia",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/joaostroher/insomnia-plugin-repo-sync.git"
10+
},
11+
"keywords": [
12+
"insomnia",
13+
"plugin",
14+
"git"
15+
],
16+
"bugs": {
17+
"url": "https://github.com/joaostroher/insomnia-plugin-repo-sync/issues"
18+
},
19+
"main": "src/index.js",
20+
"insomnia": {
21+
"name": "repo-sync",
22+
"displayName": "Repo Sync Plugin",
23+
"description": "This plugin allows users to sync workspaces in a configured repository.",
24+
"images": {
25+
"icon": "icon.svg"
26+
},
27+
"applications": {
28+
"core": "*"
29+
}
30+
},
31+
"dependencies": {},
32+
"homepage": "https://github.com/joaostroher/insomnia-plugin-repo-sync#readme",
33+
"devDependencies": {}
34+
}

src/ScreenHelper.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class ScreenHelper {
2+
static async alertError(context, message) {
3+
return await context.app.alert('Erro!', message);
4+
}
5+
6+
static async askRepoPath(context, options={}) {
7+
await context.app.alert(
8+
'Selecione Repo',
9+
`Escolha o repositorio para sincronizar workspaces\nWorkspace Atual: ${options.currentPath}`
10+
);
11+
const path = await context.app.showSaveDialog({defaultPath: options.workspaceName});
12+
13+
return normalizePath(path);
14+
}
15+
}
16+
17+
const normalizePath = (path) => {
18+
if (path == null || path == 'undefined') return null;
19+
20+
return path.substr(0, path.lastIndexOf('/'));
21+
};
22+
23+
module.exports = ScreenHelper;

src/WorkspaceRepo.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const storeKey = 'insomnia-plugin-repo-sync-workspace';
2+
3+
class WorkspaceRepo {
4+
constructor(context) {
5+
this.context = context;
6+
}
7+
8+
async getPath() {
9+
return await this.context.store.getItem(storeKey);
10+
}
11+
12+
async setPath(path) {
13+
return await this.context.store.setItem(storeKey, path);
14+
}
15+
16+
async isConfigured() {
17+
return await this.context.store.hasItem(storeKey);
18+
}
19+
}
20+
21+
module.exports = WorkspaceRepo;

src/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const fs = require('fs');
2+
const WorkspaceRepo = require('./WorkspaceRepo.js');
3+
const ScreenHelper = require('./ScreenHelper.js');
4+
5+
const verifyRepoConfig = async (repo, context) => {
6+
if (await repo.isConfigured(context)) return true;
7+
8+
ScreenHelper.alertError(context, 'Workspace não foi setado!');
9+
return false;
10+
};
11+
12+
module.exports.workspaceActions = [{
13+
label: 'Repo Sync - Export Workspace',
14+
icon: 'fa-download',
15+
action: async (context, models) => {
16+
const repo = new WorkspaceRepo(context);
17+
if (!verifyRepoConfig(repo, context)) return;
18+
19+
const path = await repo.getPath();
20+
const ex = await context.data.export.insomnia({
21+
includePrivate: false,
22+
format: 'yaml',
23+
workspace: models.workspace,
24+
});
25+
26+
fs.writeFileSync(`${path}/${models.workspace.name}.yml`, ex);
27+
},
28+
},
29+
{
30+
label: 'Repo Sync - Import Workspace',
31+
icon: 'fa-upload',
32+
action: async (context, models) => {
33+
const repo = new WorkspaceRepo(context);
34+
if (!verifyRepoConfig(repo, context)) return;
35+
36+
const path = await repo.getPath();
37+
const imported = fs.readFileSync(`${path}/${models.workspace.name}.yml`, 'utf8');
38+
39+
await context.data.import.raw(imported);
40+
},
41+
},
42+
{
43+
label: 'Repo Sync - Configure',
44+
icon: 'fa-cog',
45+
action: async (context, models) => {
46+
const repo = new WorkspaceRepo(context);
47+
48+
const repoPath = await ScreenHelper.askRepoPath(context, {
49+
currentPath: await repo.getPath(),
50+
workspaceName: models.workspace.name,
51+
});
52+
if (repoPath == null) return;
53+
54+
await repo.setPath(repoPath);
55+
},
56+
}];

0 commit comments

Comments
 (0)