Skip to content

Commit

Permalink
feat: 选择远程Git仓库逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
AKclown committed May 29, 2022
1 parent 3a9d898 commit 32656d8
Show file tree
Hide file tree
Showing 6 changed files with 553 additions and 76 deletions.
97 changes: 53 additions & 44 deletions commands/publish/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,65 @@ const log = require('@ak-clown/log');
const path = require('path');
const fse = require('fs-extra');
class PublishCommand extends Command {
// 准备阶段
init() {
// $ 处理参数
console.log('this._argv', this._argv)
}
// 执行阶段
async exec() {
try {
// 记录发布时间
const startTime = new Date().getTime();
// 1. 初始化检查
this.prepare();
// 2. Git Flow自动化
const git = new Git(this.projectInfo);
git.prepare();
console.log('git: ', git);
// 3. 云构建 和 云发布
const endTime = new Date().getTime();
log.info('本次发布耗时:', Math.floor((endTime - startTime) / 1000) + '秒')
} catch (error) {
log.error(error.message);
if (process.env.LOG_LEVEL === 'verbose') {
console.log(error);
}
}
// 准备阶段
init() {
// $ 处理参数
log.verbose('publish', this._argv, this._cmd);
this.options = {
refreshServer: this._cmd.refreshServer,
};
}

// 执行阶段
async exec() {
try {
// 记录发布时间
const startTime = new Date().getTime();
// 1. 初始化检查
this.prepare();
// 2. Git Flow自动化
const git = new Git(this.projectInfo, this.options);
git.prepare();
console.log('git: ', git);
// 3. 云构建 和 云发布
const endTime = new Date().getTime();
log.info(
'本次发布耗时:',
Math.floor((endTime - startTime) / 1000) + '秒'
);
} catch (error) {
log.error(error.message);
if (process.env.LOG_LEVEL === 'verbose') {
console.log(error);
}
}
}

// 初始化检查
prepare() {
// 1. 确认项目是否为npm项目 (package.json是否存在)
const projectPath = process.cwd();
const pkgPath = path.resolve(projectPath, 'package.json')
if (!fse.pathExistsSync(pkgPath)) {
throw new Error('package.json不存在');
}
// 2. 确认是否包含name、version属性以及build命令
const pkg = fse.readJsonSync(pkgPath);
const { name, version, script } = pkg;
if (!name || !version || !script || !script.build) {
log.verbose('package.json信息不全,请检查是否存在name、version和script (需要提供build命令)')
}
// 将项目信息保存
this.projectInfo = { name, version, dir: projectPath }
// 初始化检查
prepare() {
// 1. 确认项目是否为npm项目 (package.json是否存在)
const projectPath = process.cwd();
const pkgPath = path.resolve(projectPath, 'package.json');
if (!fse.pathExistsSync(pkgPath)) {
throw new Error('package.json不存在');
}
// 2. 确认是否包含name、version属性以及build命令
const pkg = fse.readJsonSync(pkgPath);
const { name, version, script } = pkg;
if (!name || !version || !script || !script.build) {
log.verbose(
'package.json信息不全,请检查是否存在name、version和script (需要提供build命令)'
);
}
// 将项目信息保存
this.projectInfo = { name, version, dir: projectPath };
}
}

function init(args) {
// $ 这个args就是执行脚手架传入的参数
return new PublishCommand(args);
// $ 这个args就是执行脚手架传入的参数
return new PublishCommand(args);
}

module.exports = init;
module.exports.PublishCommand = PublishCommand;
module.exports.PublishCommand = PublishCommand;
5 changes: 4 additions & 1 deletion core/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function registerCommand() {
.action(exec);

// 注册发布命令
program.command('publish').action(exec);
program
.command('publish')
.option('--refreshServer', '强制更新远程Git仓库')
.action(exec);

// $ 指定targetPath
program.on('option:targetPath', function () {
Expand Down
120 changes: 90 additions & 30 deletions models/git/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,103 @@ const path = require('path');
const userHome = require('user-home');
const fse = require('fs-extra');
const fs = require('fs');
const { readFile, writeFile } = require('"@ak-clown/utils');
const inquirer = require('inquirer');

// 主目录
const DEFAULT_CLI_HOME = 'ak-cli';
// git主目录
const GIT_ROOT_DIR = '.git';
// git server文件
const GIT_SERVER_FILE = '.git_server';

// Git托管平台
const GITHUB = 'github';
const GITEE = 'gitee';

// Git平台类型列表
const GIT_SERVER_TYPE = [
{
name: 'Github',
value: GITHUB,
},
{
name: 'Gitee',
value: GITEE,
},
];

class Git {
constructor({ name, version, dir }) {
this.name = name;
this.version = version;
this.dir = dir;
this.git = SimpleGit(dir);
this.gitServer = null;
this.homePath = null;
constructor({ name, version, dir }, { refreshServer = false }) {
this.name = name;
this.version = version;
this.dir = dir;
this.git = SimpleGit(dir);
this.gitServer = null;
this.homePath = null;
this.refreshServer = refreshServer;
}
// 准备工作,创建gitServer对象
async prepare() {
// $ 检查缓存主目录
this.checkHomePath();
// $ 检查用户远程仓库类型
await this.checkGitServer();
}

// 检查缓存主目录
checkHomePath() {
log.verbose('env', process.env);
if (!this.homePath) {
if (process.env.CLI_HOME_PATH) {
this.homePath = process.env.CLI_HOME_PATH;
} else {
this.homePath = path.resolve(userHome, DEFAULT_CLI_HOME);
}
}
// 准备工作,创建gitServer对象
async prepare() {
// $ 检查缓存主目录
this.checkHomePath()
log.verbose('homePath', this.homePath);
// 确保当前目录存在 (如果目录不存在就创建)
fse.ensureDirSync(this.homePath);
if (!fs.existsSync(this.homePath)) {
throw new Error('用户主目录获取数据失败!');
}
}

// 检查缓存主目录
checkHomePath() {
log.verbose('env', process.env)
if (!this.homePath) {
if (process.env.CLI_HOME_PATH) {
this.homePath = process.env.CLI_HOME_PATH;
} else {
this.homePath = path.resolve(userHome, DEFAULT_CLI_HOME);
}
}
log.verbose('homePath', this.homePath);
// 确保当前目录存在 (如果目录不存在就创建)
fse.ensureDirSync(this.homePath);
if (!fs.existsSync(this.homePath)) {
throw new Error('用户主目录获取数据失败!')
}
// 检查用户远程仓库类型
async checkGitServer() {
const gitServerPath = this.createPath(GIT_SERVER_FILE);
// 读取到GIT_SERVER_FILE的文件内容
let gitServer = readFile(gitServerPath);
// 不存在文件内容,提供选择 给 用户选择git平台
if (!gitServer || this.refreshServer) {
// 选择托管的git平台
const { gitServer } = await inquirer.prompt([
{
type: 'list',
name: 'gitServer',
message: '请选择你想要托管的Git平台',
default: GITHUB,
choices: GIT_SERVER_TYPE,
},
]);
writeFile(gitServerPath, gitServer);
log.success('git server 写入成功', `${gitServer} --> ${gitServerPath}`);
} else {
log.success('git server 获取成功', gitServer);
}
}

// 获取git server 的文件路径
createPath(file) {
const rootDir = path.resolve(this.homePath, GIT_ROOT_DIR);
const filePath = path.resolve(rootDir, file);
// 确保GIT_ROOT_DIR目录存在,不存在就创建
fse.ensureDirSync(rootDir);
return filePath;
}

// 初始化操作
init() { }
// 初始化操作
init() {}
}

module.exports = Git;
module.exports = Git;
Loading

0 comments on commit 32656d8

Please sign in to comment.