-
Notifications
You must be signed in to change notification settings - Fork 314
/
cloneCommand.ts
94 lines (91 loc) · 3.17 KB
/
cloneCommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { CommandIDs, IGitExtension, Level } from './tokens';
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
import { Dialog, showDialog } from '@jupyterlab/apputils';
import { GitCloneForm } from './widgets/GitCloneForm';
import { logger } from './logger';
import {
addFileBrowserContextMenu,
IGitCloneArgs,
Operation,
showGitOperationDialog
} from './commandsAndMenu';
import { GitExtension } from './model';
import { addCloneButton } from './widgets/gitClone';
export const gitCloneCommandPlugin: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/git:clone',
requires: [ITranslator, IGitExtension, IFileBrowserFactory],
activate: (
app: JupyterFrontEnd,
translator: ITranslator,
gitModel: IGitExtension,
fileBrowserFactory: IFileBrowserFactory
) => {
translator = translator || nullTranslator;
const trans = translator.load('jupyterlab_git');
const fileBrowser = fileBrowserFactory.defaultBrowser;
const fileBrowserModel = fileBrowser.model;
/** Add git clone command */
app.commands.addCommand(CommandIDs.gitClone, {
label: trans.__('Clone a Repository'),
caption: trans.__('Clone a repository from a URL'),
isEnabled: () => gitModel.pathRepository === null,
execute: async () => {
const result = await showDialog({
title: trans.__('Clone a repo'),
body: new GitCloneForm(trans),
focusNodeSelector: 'input',
buttons: [
Dialog.cancelButton({ label: trans.__('Cancel') }),
Dialog.okButton({ label: trans.__('Clone') })
]
});
if (result.button.accept && result.value.url) {
logger.log({
level: Level.RUNNING,
message: trans.__('Cloning…')
});
try {
const details = await showGitOperationDialog<IGitCloneArgs>(
gitModel as GitExtension,
Operation.Clone,
trans,
{
path: fileBrowserModel.path,
url: result.value.url,
versioning: result.value.versioning,
submodules: result.value.submodules
}
);
logger.log({
message: trans.__('Successfully cloned'),
level: Level.SUCCESS,
details
});
await fileBrowserModel.refresh();
} catch (error) {
console.error(
'Encountered an error when cloning the repository. Error: ',
error
);
logger.log({
message: trans.__('Failed to clone'),
level: Level.ERROR,
error: error as Error
});
throw error;
}
}
}
});
// Add a clone button to the file browser extension toolbar
addCloneButton(gitModel, fileBrowser, app.commands, trans);
// Add the context menu items for the default file browser
addFileBrowserContextMenu(gitModel, fileBrowser, app.contextMenu, trans);
},
autoStart: true
};