This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
add.ts
161 lines (144 loc) · 4.12 KB
/
add.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Created by axetroy on 17-2-14.
*/
const path = require('path');
const gitUrlParse = require('git-url-parse');
const fs = require('fs-extra');
const _ = require('lodash');
const inquirer = require('inquirer');
const prompt = inquirer.createPromptModule();
const which = require('which');
const uniqueString = require('unique-string');
const clipboardy = require('clipboardy');
import chalk from 'chalk';
const __ = require('i18n').__;
import { isExistPath, isLink, normalizePath, runShell } from '../utils';
import config from '../config';
import registry from '../registry';
import globalConfig from '../global-config';
import Gpmrc from '../gpmrc';
import { info, warn } from '../logger';
interface Argv$ {
repo: string;
}
interface Options$ {
nolog?: boolean;
unixify?: boolean;
force?: boolean;
name?: string;
ignoreRc?: boolean;
}
async function add(repo: string, options: Options$) {
const gitInfo = gitUrlParse(repo);
if (!gitInfo || !gitInfo.owner || !gitInfo.name) {
throw new Error(__('commands.add.log.invalid_url', { repo }));
}
const randomTemp: string = path.join(config.paths.temp, uniqueString());
const tempDir: string = path.join(randomTemp, gitInfo.name);
const baseDir: string = path.join(
config.paths.home,
globalConfig.entity.base || config.defaults.base
);
const sourceDir: string = path.join(baseDir, gitInfo.source);
const ownerDir: string = path.join(sourceDir, gitInfo.owner);
let repoDir: string = path.join(
ownerDir,
typeof options.name === 'string' ? options.name : gitInfo.name
);
let confirmCover: boolean = false;
if (await isExistPath(repoDir)) {
if (options.force) {
confirmCover = true;
} else {
confirmCover = (await prompt({
type: 'confirm',
name: 'result',
message: chalk.white(
__('commands.add.log.confirm_cover', {
path: chalk.yellow.underline(normalizePath(repoDir, options))
})
),
['default']: false
})).result;
}
if (!confirmCover) {
!options.nolog && info(__('global.tips.good_bye'));
return process.exit(1);
}
}
await fs.ensureDir(baseDir);
await fs.ensureDir(sourceDir);
await fs.ensureDir(ownerDir);
await fs.ensureDir(randomTemp);
try {
const git = which.sync('git');
if (!git) {
return Promise.reject(
new Error(
__('commands.add.log.make_sure_install', { bin: chalk.green('Git') })
)
);
}
} catch (err) {
throw err;
}
await runShell(`git clone ${gitInfo.href}`, {
cwd: randomTemp,
stdio: 'inherit'
});
// if it's a link, then unlink first
if (await isLink(repoDir)) await fs.unlink(repoDir);
await fs.remove(repoDir);
await fs.move(tempDir, repoDir);
await fs.remove(randomTemp);
/**
* parse the .gpmrc
* valid argument:
* name: the repo's name
* hooks:
* add: run command after add repo
*/
if (!options.ignoreRc) {
const gpmrc = new Gpmrc();
await gpmrc.load(repoDir);
if (gpmrc.exist) {
const alias: string = gpmrc.rc.name;
if (alias && alias !== gitInfo.name) {
let newRepoDir: string = path.join(
ownerDir,
_.isString(options.name) ? options.name : alias
);
await fs.move(repoDir, newRepoDir);
repoDir = newRepoDir;
}
await gpmrc.runHook('add', { cwd: repoDir }).catch(err => {
console.error(err);
return Promise.resolve();
});
}
}
const entity = _.extend({}, gitInfo, { path: repoDir });
delete entity.toString;
await registry.add(entity);
if (!options.nolog) {
let finallyPath = normalizePath(repoDir, options);
info(
__('commands.add.log.info_add_success', {
path: chalk.green.underline(finallyPath)
})
);
try {
clipboardy.writeSync(finallyPath);
info(
__('commands.add.log.info_copy_clipboard', {
key: chalk.green('<CTRL+V>')
})
);
} catch (err) {
warn(__('commands.add.log.warn_copy_clipboard'));
}
}
}
export default async function(argv: Argv$, options: Options$) {
return await add(argv.repo, options);
}