├── README.md ├── index.js ├── package.json └── strings ├── en.strings └── zh-Hans.strings /README.md: -------------------------------------------------------------------------------- 1 | # gitCloneDownloader 2 | 使用`git clone`命令从Github安装JSBox应用 3 | 只支持形如`https://github.com/github-tools/github`或`https://github.com/github-tools/github.git`的链接 4 | 5 | ## Requirements 6 | - JSBox>=2.0.0 7 | - 必要模块:`isomorphic-git`,点击右上角按钮并选择“解决模块依赖”即可安装 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs"); 2 | const path = require("path"); 3 | 4 | const l10n = require("l10n"); 5 | const input = require("input"); 6 | const ui = require("ui"); 7 | const quicklook = require("quicklook"); 8 | const clipboard = require("clipboard"); 9 | const detector = require("detector"); 10 | 11 | const git = require("isomorphic-git"); 12 | 13 | const shared = $jsbox.path.shared; 14 | 15 | const dir = path.join(shared, ".tmp"); 16 | 17 | 18 | if (fs.existsSync(dir)) { 19 | fs.rmdirSync(dir, recursive=true); 20 | } 21 | fs.mkdirSync(dir); 22 | 23 | 24 | 25 | async function gitclone(url) { 26 | await git.clone({ 27 | fs, 28 | dir: dir, 29 | url: url, 30 | singleBranch: true, 31 | depth: 10 32 | }); 33 | } 34 | 35 | function install(name) { 36 | $jsbox.run(` 37 | const name = "${name}" 38 | const path = "shared://.tmp" 39 | const zipPath = path + ".zip" 40 | await $archiver.zip({ 41 | directory: path, 42 | dest: zipPath 43 | }) 44 | $addin.save({ 45 | name: name, 46 | data: $file.read(zipPath) 47 | }) 48 | $file.delete(path) 49 | $file.delete(zipPath) 50 | console.log("${l10n("FINISHED")}") 51 | `) 52 | } 53 | 54 | function getRepoName(url) { 55 | const patt = /^https:\/\/github.com\/[^/]+\/([^/]+)$/ 56 | const found = patt.exec(url) 57 | if (!found) { 58 | return 59 | } else { 60 | let name = found[1] 61 | if (name.lastIndexOf(".git") === name.length - 4) { 62 | name = name.slice(0, -4) 63 | } 64 | return name 65 | } 66 | } 67 | 68 | 69 | async function prompt() { 70 | const text = await input.text({ 71 | placeholder: l10n("ENTER_URL"), 72 | text: detector.link(clipboard.text()) 73 | }); 74 | 75 | if (text == undefined) { 76 | return; 77 | } 78 | 79 | const link = detector.link(text); 80 | const name = getRepoName(link); 81 | if (link && name) { 82 | gitclone(link).then(()=>install(name)); 83 | } else { 84 | console.error(l10n("CANNOT_FIND_URL")); 85 | setTimeout(prompt, 500); 86 | } 87 | } 88 | 89 | prompt() 90 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "index.js", 3 | "name": "gitCloneDownloader", 4 | "dependencies": { 5 | "isomorphic-git": "^0.72.0" 6 | }, 7 | "version": "1.0.0" 8 | } -------------------------------------------------------------------------------- /strings/en.strings: -------------------------------------------------------------------------------- 1 | "ENTER_URL" = "Enter a Github Repo URL"; 2 | "CANNOT_FIND_URL" = "Cannot find the link, please retry"; 3 | "FINISHED" = "Finished"; 4 | -------------------------------------------------------------------------------- /strings/zh-Hans.strings: -------------------------------------------------------------------------------- 1 | "ENTER_URL" = "输入 Github Repo 链接"; 2 | "CANNOT_FIND_URL" = "无法找到链接,请重试"; 3 | "FINISHED" = "完成"; 4 | --------------------------------------------------------------------------------