Skip to content

Commit c901e69

Browse files
Merge pull request #44 from appworks-lab/release-next
Release 0.2.0
2 parents c5d93d8 + 6aed7a2 commit c901e69

File tree

86 files changed

+2539
-9986
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+2539
-9986
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = getESLintConfig('react-ts', {
88
'@iceworks/best-practices/recommend-polyfill': 0,
99
'import/order': 1,
1010
'no-param-reassign': 0,
11-
'@typescript-eslint/no-require-imports': 0
11+
'@typescript-eslint/no-require-imports': 0,
12+
'no-await-in-loop': 0
1213
},
1314
});

main/constants.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const PACKAGE_JSON_FILE_NAME = 'package.json';
88
export const TOOLKIT_DIR = path.join(process.env.HOME, '.toolkit');
99
export const TOOLKIT_TMP_DIR = path.join(TOOLKIT_DIR, 'tmp');
1010
export const TOOLKIT_PACKAGES_DIR = path.join(TOOLKIT_DIR, 'packages');
11+
export const TOOLKIT_USER_GIT_CONFIG_DIR = path.join(TOOLKIT_DIR, 'git');
1112

1213
export const DEFAULT_LOCAL_PACKAGE_INFO: ILocalPackageInfo = {
1314
localVersion: null,
@@ -25,12 +26,15 @@ export const INSTALL_COMMAND_PACKAGES = [
2526
commandRelativePath: './Contents/Resources/app/bin/code',
2627
},
2728
];
28-
29-
export const NOT_REINSTALL_PACKAGES = ['npm'];
30-
29+
export const NOT_REINSTALL_DEPENDENCIES = ['npm'];
30+
// bash profile
31+
export const PROFILE_FILES = ['.bash_profile', '.bashrc', '.zshrc'];
32+
export const DEFAULT_PROFILE_FILE = '.bash_profile';
33+
// npm
34+
export const NPMRC_PATH = path.join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.npmrc');
35+
export const NPM_REGISTRY = 'https://registry.npmjs.org/';
3136
export const TAOBAO_NPM_REGISTRY = 'https://registry.npm.taobao.org';
3237
export const ALI_NPM_REGISTRY = 'https://registry.npm.alibaba-inc.com/';
3338
export const TAOBAO_NODE_MIRROR = 'https://npm.taobao.org/mirrors/node';
34-
35-
export const PROFILE_FILES = ['.bash_profile', '.bashrc', '.zshrc'];
36-
export const DEFAULT_PROFILE_FILE = '.bash_profile';
39+
// git
40+
export const GLOBAL_GITCONFIG_PATH = path.join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.gitconfig');

main/data/data.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
"registry": "https://registry.npmjs.org/",
115115
"isInternal": false
116116
},
117+
{
118+
"name": "yarn",
119+
"registry": "https://registry.yarnpkg.com/",
120+
"isInternal": false
121+
},
117122
{
118123
"name": "taobao",
119124
"registry": "https://registry.npm.taobao.org/",

main/git/config.ts

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import * as path from 'path';
2+
import ini = require('ini');
3+
import * as fse from 'fs-extra';
4+
import * as globby from 'globby';
5+
import { IAddUserConfig, IUserConfig } from '../types/git';
6+
import { GLOBAL_GITCONFIG_PATH, TOOLKIT_USER_GIT_CONFIG_DIR } from '../constants';
7+
import log from '../utils/log';
8+
import {
9+
updateSSHConfig,
10+
getSSHConfig,
11+
removeSSHConfig,
12+
addSSHConfig,
13+
} from './ssh';
14+
15+
const USER_GIT_CONFIG_FILENAME_PREFIX = '.gitconfig-';
16+
const IGNORE_CONFIG_KEYS = ['gitDir'];
17+
18+
export async function getGlobalGitConfig() {
19+
const globalGitConfig = await parseGitConfig(GLOBAL_GITCONFIG_PATH);
20+
log.info('get-global-git-config', globalGitConfig);
21+
return globalGitConfig;
22+
}
23+
24+
export async function updateGlobalGitConfig(gitConfig: object) {
25+
log.info('update-global-git-config', gitConfig);
26+
await writeGitConfig(GLOBAL_GITCONFIG_PATH, gitConfig);
27+
}
28+
29+
export async function getExistedUserGitConfigNames() {
30+
const filenames = await getUserGitConfigFilenames();
31+
return filenames.map((filename: string) => filename.replace(USER_GIT_CONFIG_FILENAME_PREFIX, ''));
32+
}
33+
34+
/**
35+
* get user git config list
36+
*/
37+
export async function getUserGitConfigs(): Promise<IUserConfig[]> {
38+
const gitConfigFilenames = await getUserGitConfigFilenames();
39+
const userGitDirs = await getUserGitDirs();
40+
41+
const userGitConfigs = [];
42+
for (const gitConfigFilename of gitConfigFilenames) {
43+
const configPath = path.join(TOOLKIT_USER_GIT_CONFIG_DIR, gitConfigFilename);
44+
if (!fse.pathExistsSync(configPath)) {
45+
continue;
46+
}
47+
const gitConfig = await parseGitConfig(configPath);
48+
const filename = path.basename(configPath);
49+
const configName = filename.replace(USER_GIT_CONFIG_FILENAME_PREFIX, '');
50+
const { SSHPublicKey } = await getSSHConfig(configName);
51+
userGitConfigs.push({
52+
...gitConfig,
53+
SSHPublicKey,
54+
configName,
55+
gitDirs: userGitDirs[configPath] || [],
56+
});
57+
}
58+
59+
return userGitConfigs;
60+
}
61+
62+
export async function addUserGitConfig(userGitConfig: IAddUserConfig) {
63+
const { configName, user: { name: userName, hostName } } = userGitConfig;
64+
const gitConfigPath = getGitConfigPath(configName);
65+
66+
checkUserGitConfigExists(configName, gitConfigPath);
67+
68+
await fse.createFile(gitConfigPath);
69+
// do not save the configName to the gitconfig file
70+
delete userGitConfig.configName;
71+
await writeGitConfig(gitConfigPath, userGitConfig);
72+
await addSSHConfig({ hostName, configName, userName });
73+
}
74+
75+
export async function updateUserGitConfig(gitConfig: any, configName: string) {
76+
const { user = {} } = gitConfig;
77+
const { name: userName = '', hostName = '' } = user;
78+
await updateSSHConfig(configName, hostName, userName);
79+
80+
IGNORE_CONFIG_KEYS.forEach((key) => {
81+
delete gitConfig[key];
82+
});
83+
// save to ~/.toolkit/git/.gitconfig-${configName}
84+
const gitConfigPath = `${path.join(TOOLKIT_USER_GIT_CONFIG_DIR, `${USER_GIT_CONFIG_FILENAME_PREFIX}${configName}`)}`;
85+
await writeGitConfig(gitConfigPath, gitConfig);
86+
87+
log.info('update-user-git-config', configName, gitConfig);
88+
}
89+
90+
async function getUserGitDirs() {
91+
const globalGitConfig = await getGlobalGitConfig();
92+
93+
const userGitDirs = {};
94+
95+
const configKeys = Object.keys(globalGitConfig);
96+
97+
for (const configKey of configKeys) {
98+
const { path: gitConfigPath } = globalGitConfig[configKey];
99+
if (!gitConfigPath) {
100+
continue;
101+
}
102+
if (!userGitDirs[gitConfigPath]) {
103+
userGitDirs[gitConfigPath] = [];
104+
}
105+
const gitDir = configKey.replace(/includeIf "gitdir:(.*)"/, (match, p1) => p1);
106+
userGitDirs[gitConfigPath].push(gitDir);
107+
}
108+
109+
return userGitDirs;
110+
}
111+
112+
export async function updateUserGitDir(
113+
originGitDir: string,
114+
currentGitDir: string,
115+
configName: string,
116+
) {
117+
const globalGitConfig = await parseGitConfig(GLOBAL_GITCONFIG_PATH);
118+
119+
const originIncludeIfKey = `includeIf "gitdir:${originGitDir}"`;
120+
const currentIncludeIfKey = `includeIf "gitdir:${currentGitDir}"`;
121+
122+
delete globalGitConfig[originIncludeIfKey];
123+
const gitConfigPath = getGitConfigPath(configName);
124+
globalGitConfig[currentIncludeIfKey] = { path: gitConfigPath };
125+
await writeGitConfig(GLOBAL_GITCONFIG_PATH, globalGitConfig);
126+
127+
log.info('update-user-git-dir: ', currentIncludeIfKey, globalGitConfig[currentIncludeIfKey]);
128+
}
129+
130+
export async function removeUserGitDir(gitDir: string, configName: string) {
131+
const gitConfigPath = getGitConfigPath(configName);
132+
const globalGitConfig = await parseGitConfig(GLOBAL_GITCONFIG_PATH);
133+
134+
const includeIfKey = `includeIf "gitdir:${gitDir}"`;
135+
const includeIfValue = globalGitConfig[includeIfKey];
136+
if (includeIfValue && includeIfValue.path === gitConfigPath) {
137+
delete globalGitConfig[includeIfKey];
138+
await writeGitConfig(GLOBAL_GITCONFIG_PATH, globalGitConfig);
139+
log.info('remove-user-git-dir: ', includeIfKey, gitConfigPath);
140+
} else {
141+
const error = new Error(`Can not remove ${gitDir}. The ${includeIfValue} is not found.`);
142+
log.error(error);
143+
throw error;
144+
}
145+
}
146+
147+
export async function removeUserGitConfig(configName: string, gitDirs = []) {
148+
await removeSSHConfig(configName);
149+
150+
for (const gitDir of gitDirs) {
151+
await removeUserGitDir(gitDir, configName);
152+
}
153+
154+
// remove the gitconfig file
155+
const gitConfigPath = getGitConfigPath(configName);
156+
await fse.remove(gitConfigPath);
157+
}
158+
159+
async function parseGitConfig(gitConfigPath: string) {
160+
const gitConfigContent = await fse.readFile(gitConfigPath, 'utf-8');
161+
return ini.parse(gitConfigContent);
162+
}
163+
164+
async function writeGitConfig(gitConfigPath: string, config: object) {
165+
await fse.writeFile(gitConfigPath, ini.stringify(config));
166+
log.info('write-git-config', config);
167+
}
168+
169+
function checkUserGitConfigExists(configName: string, gitConfigPath: string) {
170+
if (fse.pathExistsSync(gitConfigPath)) {
171+
const err = new Error(`${configName} config has existed,please use other config name.`);
172+
err.name = 'add-user-git-config';
173+
log.error(err);
174+
throw err;
175+
}
176+
}
177+
178+
async function getUserGitConfigFilenames() {
179+
return await globby([`${USER_GIT_CONFIG_FILENAME_PREFIX}*`], { cwd: TOOLKIT_USER_GIT_CONFIG_DIR, dot: true });
180+
}
181+
182+
/**
183+
* get absolute git config path in ~/.toolkit/git/
184+
*/
185+
function getGitConfigPath(configName: string) {
186+
return path.join(TOOLKIT_USER_GIT_CONFIG_DIR, `${USER_GIT_CONFIG_FILENAME_PREFIX}${configName}`);
187+
}

main/git/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './config';
2+
export * from './ssh';

0 commit comments

Comments
 (0)