Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 19683c4

Browse files
committed
feat: refactor gpm
1 parent 77e3d13 commit 19683c4

19 files changed

+496
-493
lines changed

app/command/add.ts

+35-51
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,31 @@
22
* Created by axetroy on 17-2-14.
33
*/
44

5-
const path = require('path');
6-
const gitUrlParse = require('git-url-parse');
7-
const fs = require('fs-extra');
8-
const _ = require('lodash');
9-
const inquirer = require('inquirer');
5+
const path = require("path");
6+
const gitUrlParse = require("git-url-parse");
7+
const fs = require("fs-extra");
8+
const _ = require("lodash");
9+
const inquirer = require("inquirer");
1010
const prompt = inquirer.createPromptModule();
11-
const which = require('which');
12-
const uniqueString = require('unique-string');
13-
const clipboardy = require('clipboardy');
14-
import chalk from 'chalk';
15-
const __ = require('i18n').__;
16-
17-
18-
import { isExistPath, isLink, normalizePath, runShell } from '../utils';
19-
import config from '../config';
20-
import registry from '../registry';
21-
import globalConfig from '../global-config';
22-
import Gpmrc from '../gpmrc';
23-
import { info, warn } from '../logger';
24-
25-
interface Argv$ {
26-
repo: string;
27-
}
28-
29-
interface Options$ {
30-
nolog?: boolean;
31-
unixify?: boolean;
32-
force?: boolean;
33-
name?: string;
34-
ignoreRc?: boolean;
35-
}
36-
37-
async function add(repo: string, options: Options$) {
11+
const which = require("which");
12+
const uniqueString = require("unique-string");
13+
const clipboardy = require("clipboardy");
14+
import chalk from "chalk";
15+
const __ = require("i18n").__;
16+
17+
import { isExistPath, isLink, normalizePath, runShell } from "../utils";
18+
import config from "../config";
19+
import registry from "../registry";
20+
import globalConfig from "../global-config";
21+
import Gpmrc from "../gpmrc";
22+
import { info, warn } from "../logger";
23+
import { IAddOption } from "../type";
24+
25+
export default async function add(repo: string, options: IAddOption) {
3826
const gitInfo = gitUrlParse(repo);
3927

4028
if (!gitInfo || !gitInfo.owner || !gitInfo.name) {
41-
throw new Error(__('commands.add.log.invalid_url', { repo }));
29+
throw new Error(__("commands.add.log.invalid_url", { repo }));
4230
}
4331

4432
const randomTemp: string = path.join(config.paths.temp, uniqueString());
@@ -51,7 +39,7 @@ async function add(repo: string, options: Options$) {
5139
const ownerDir: string = path.join(sourceDir, gitInfo.owner);
5240
let repoDir: string = path.join(
5341
ownerDir,
54-
typeof options.name === 'string' ? options.name : gitInfo.name
42+
typeof options.name === "string" ? options.name : gitInfo.name
5543
);
5644

5745
let confirmCover: boolean = false;
@@ -60,18 +48,18 @@ async function add(repo: string, options: Options$) {
6048
confirmCover = true;
6149
} else {
6250
confirmCover = (await prompt({
63-
type: 'confirm',
64-
name: 'result',
51+
type: "confirm",
52+
name: "result",
6553
message: chalk.white(
66-
__('commands.add.log.confirm_cover', {
54+
__("commands.add.log.confirm_cover", {
6755
path: chalk.yellow.underline(normalizePath(repoDir, options))
6856
})
6957
),
70-
['default']: false
58+
["default"]: false
7159
})).result;
7260
}
7361
if (!confirmCover) {
74-
!options.nolog && info(__('global.tips.good_bye'));
62+
!options.nolog && info(__("global.tips.good_bye"));
7563
return process.exit(1);
7664
}
7765
}
@@ -82,11 +70,11 @@ async function add(repo: string, options: Options$) {
8270
await fs.ensureDir(randomTemp);
8371

8472
try {
85-
const git = which.sync('git');
73+
const git = which.sync("git");
8674
if (!git) {
8775
return Promise.reject(
8876
new Error(
89-
__('commands.add.log.make_sure_install', { bin: chalk.green('Git') })
77+
__("commands.add.log.make_sure_install", { bin: chalk.green("Git") })
9078
)
9179
);
9280
}
@@ -96,7 +84,7 @@ async function add(repo: string, options: Options$) {
9684

9785
await runShell(`git clone ${gitInfo.href}`, {
9886
cwd: randomTemp,
99-
stdio: 'inherit'
87+
stdio: "inherit"
10088
});
10189

10290
// if it's a link, then unlink first
@@ -125,7 +113,7 @@ async function add(repo: string, options: Options$) {
125113
await fs.move(repoDir, newRepoDir);
126114
repoDir = newRepoDir;
127115
}
128-
await gpmrc.runHook('add', { cwd: repoDir }).catch(err => {
116+
await gpmrc.runHook("add", { cwd: repoDir }).catch(err => {
129117
console.error(err);
130118
return Promise.resolve();
131119
});
@@ -139,23 +127,19 @@ async function add(repo: string, options: Options$) {
139127
if (!options.nolog) {
140128
let finallyPath = normalizePath(repoDir, options);
141129
info(
142-
__('commands.add.log.info_add_success', {
130+
__("commands.add.log.info_add_success", {
143131
path: chalk.green.underline(finallyPath)
144132
})
145133
);
146134
try {
147135
clipboardy.writeSync(finallyPath);
148136
info(
149-
__('commands.add.log.info_copy_clipboard', {
150-
key: chalk.green('<CTRL+V>')
137+
__("commands.add.log.info_copy_clipboard", {
138+
key: chalk.green("<CTRL+V>")
151139
})
152140
);
153141
} catch (err) {
154-
warn(__('commands.add.log.warn_copy_clipboard'));
142+
warn(__("commands.add.log.warn_copy_clipboard"));
155143
}
156144
}
157145
}
158-
159-
export default async function(argv: Argv$, options: Options$) {
160-
return await add(argv.repo, options);
161-
}

app/command/clean.ts

+11-20
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,26 @@
22
* Created by axetroy on 17-2-14.
33
*/
44

5-
const fs = require('fs-extra');
6-
import chalk from 'chalk';
7-
import config from '../config';
8-
import { info } from '../logger';
5+
const fs = require("fs-extra");
6+
import chalk from "chalk";
7+
import config from "../config";
8+
import { info } from "../logger";
99

10-
import relinkHandler from './relink';
10+
import relinkHandler from "./relink";
11+
import { ICleanOption } from "../type";
1112

12-
interface Argv$ {}
13-
14-
interface Options$ {
15-
nolog?: boolean;
16-
unixify?: boolean;
17-
force?: boolean;
18-
}
19-
20-
export default async function clean(
21-
argv: Argv$,
22-
options: Options$
23-
): Promise<void> {
13+
export default async function clean(options: ICleanOption): Promise<void> {
2414
try {
2515
await fs.emptyDir(config.paths.temp);
2616
} catch (err) {
2717
throw new Error(
28-
`${err + ''}\n May be you don't have permission to access ${config.paths
29-
.temp}, try to delete in manual`
18+
`${err + ""}\n May be you don't have permission to access ${
19+
config.paths.temp
20+
}, try to delete in manual`
3021
);
3122
}
3223
// auto generate file again
33-
await relinkHandler(argv, { nolog: options.nolog });
24+
await relinkHandler({ nolog: options.nolog });
3425
!options.nolog &&
3526
info(`clean ${chalk.green.underline(config.paths.temp)} success`);
3627
}

app/command/config.ts

+30-54
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,70 @@
11
/**
22
* Created by axetroy on 17-2-15.
33
*/
4-
import chalk from 'chalk';
5-
const prettyjson = require('prettyjson');
6-
const __ = require('i18n').__;
4+
import chalk from "chalk";
5+
const prettyjson = require("prettyjson");
6+
const __ = require("i18n").__;
77

8-
import { info, error } from '../logger';
9-
import config from '../config';
10-
import globalConfig from '../global-config';
8+
import { info, error } from "../logger";
9+
import config from "../config";
10+
import globalConfig from "../global-config";
11+
import { IConfigAction, IConfigArgv, IConfigOption } from "../type";
1112

12-
type Action$ =
13-
| 'list'
14-
| 'LIST'
15-
| 'get'
16-
| 'GET'
17-
| 'set'
18-
| 'SET'
19-
| 'remove'
20-
| 'REMOVE'
21-
| 'reset'
22-
| 'RESET';
23-
24-
export interface Argv$ {
25-
key?: string;
26-
value?: string;
27-
action: Action$;
28-
}
29-
30-
interface Options$ {
31-
nolog?: boolean;
32-
unixify?: boolean;
33-
force?: boolean;
34-
}
35-
36-
async function configHandler(argv: Argv$, options: Options$): Promise<any> {
13+
export default async function configHandler(
14+
argv: IConfigArgv,
15+
options: IConfigOption
16+
): Promise<any> {
3717
const { action, key, value } = argv;
3818
let output = void 0;
3919

40-
const actionUpperCase: Action$ = <Action$>action.toUpperCase();
20+
const actionUpperCase: IConfigAction = <IConfigAction>action.toUpperCase();
4121

4222
switch (actionUpperCase) {
43-
case 'LIST':
23+
case "LIST":
4424
output = globalConfig.entity;
4525
!options.nolog &&
46-
process.stdout.write(prettyjson.render(globalConfig.entity) + '\n');
26+
process.stdout.write(prettyjson.render(globalConfig.entity) + "\n");
4727
break;
48-
case 'GET':
28+
case "GET":
4929
if (!key)
50-
return !options.nolog && error(__('commands.config.log.require_key'));
30+
return !options.nolog && error(__("commands.config.log.require_key"));
5131
output = globalConfig.get(key);
5232
!options.nolog && info(`${key}: ${output}`);
5333
break;
54-
case 'SET':
34+
case "SET":
5535
if (!key)
56-
return !options.nolog && error(__('commands.config.log.require_key'));
36+
return !options.nolog && error(__("commands.config.log.require_key"));
5737
if (!value)
58-
return !options.nolog && error(__('commands.config.log.require_val'));
38+
return !options.nolog && error(__("commands.config.log.require_val"));
5939
output = await globalConfig.set(key, value);
6040
!options.nolog &&
61-
process.stdout.write(prettyjson.render(globalConfig.entity) + '\n');
41+
process.stdout.write(prettyjson.render(globalConfig.entity) + "\n");
6242
break;
63-
case 'REMOVE':
43+
case "REMOVE":
6444
if (!key)
65-
return !options.nolog && error(__('commands.config.log.require_key'));
45+
return !options.nolog && error(__("commands.config.log.require_key"));
6646
output = await globalConfig.remove(key);
6747
!options.nolog &&
68-
process.stdout.write(prettyjson.render(globalConfig.entity) + '\n');
48+
process.stdout.write(prettyjson.render(globalConfig.entity) + "\n");
6949
break;
70-
case 'RESET':
50+
case "RESET":
7151
output = await globalConfig.reset();
72-
!options.nolog && info(__('commands.config.log.info_reset'));
52+
!options.nolog && info(__("commands.config.log.info_reset"));
7353
!options.nolog &&
74-
process.stdout.write(prettyjson.render(globalConfig.entity) + '\n');
54+
process.stdout.write(prettyjson.render(globalConfig.entity) + "\n");
7555
break;
7656
default:
7757
!options.nolog &&
7858
info(
79-
__('commands.config.log.help', {
59+
__("commands.config.log.help", {
8060
cmd:
8161
config.name +
82-
' config ' +
83-
chalk.yellow.underline('list|get|set|remove|reset') +
84-
chalk.green(' [key] [value]')
62+
" config " +
63+
chalk.yellow.underline("list|get|set|remove|reset") +
64+
chalk.green(" [key] [value]")
8565
})
8666
);
8767
break;
8868
}
8969
return output;
9070
}
91-
92-
export default async function(argv: Argv$, options: Options$): Promise<any> {
93-
return configHandler(argv, options);
94-
}

app/command/find.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@ import config from "../config";
1515
import registry, { Target$ } from "../registry";
1616
import { normalizePath } from "../utils";
1717
import { info, warn } from "../logger";
18-
19-
interface Argv$ {}
20-
21-
interface Options$ {
22-
nolog?: boolean;
23-
unixify?: boolean;
24-
force?: boolean;
25-
}
18+
import { IFindOption } from "../type";
2619

2720
export interface ExtendTarget$ extends Target$ {
2821
__index__: string;
@@ -36,7 +29,7 @@ export function decoratorIndex<T>(repo: any): T {
3629
return repo;
3730
}
3831

39-
export default async function search(argv: Argv$, options: Options$) {
32+
export default async function search(options: IFindOption) {
4033
let repositories = registry.repositories.map(decoratorIndex);
4134

4235
const answer = await inquirer.prompt([

0 commit comments

Comments
 (0)