@lexmin0412/gcm
Version:
160 lines (159 loc) • 7.97 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pushConfig = exports.writeConfigIntoLocalRepo = exports.cloneConfigRepo = void 0;
var fs_1 = __importDefault(require("fs"));
var inquirer_1 = __importDefault(require("inquirer"));
var const_1 = require("./const");
var child_process_1 = require("child_process");
var gcm_api_1 = require("@lexmin0412/gcm-api");
var config_1 = require("../config");
var path_1 = __importDefault(require("path"));
/**
* 执行命令行脚本的统一选项
*/
var EXEC_OPTIONS = {
cwd: const_1.REPO_FOLDER_PATH,
};
/**
* clone 远程配置仓库到本地
*/
var cloneConfigRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
var sync, isContinue, newSyncConfig;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log("开始下载远程配置");
// 如果已存在,则递归清除目录
if (fs_1.default.existsSync(const_1.TEMP_SYNC_DIR)) {
fs_1.default.rmSync(const_1.TEMP_SYNC_DIR, { recursive: true });
}
// 创建临时工作目录
fs_1.default.mkdirSync(const_1.TEMP_SYNC_DIR);
sync = (0, gcm_api_1.readConfigs)().sync;
if (!!(sync === null || sync === void 0 ? void 0 : sync.repoUrl)) return [3 /*break*/, 3];
return [4 /*yield*/, inquirer_1.default.prompt([
{
type: "confirm",
name: "isContinue",
message: "检测到同步配置不存在, 是否立即配置?",
default: true,
}
])];
case 1:
isContinue = (_a.sent()).isContinue;
if (!isContinue) {
console.log("已取消同步配置");
process.exit(0);
}
return [4 /*yield*/, (0, config_1.setConfig)('sync')];
case 2:
_a.sent();
_a.label = 3;
case 3:
newSyncConfig = (0, gcm_api_1.readConfigs)().sync;
(0, child_process_1.execSync)("git clone ".concat(newSyncConfig.repoUrl, " ").concat(const_1.CONFIG_REPO_NAME), {
cwd: const_1.TEMP_SYNC_DIR,
});
console.log("下载远程配置成功");
return [2 /*return*/];
}
});
}); };
exports.cloneConfigRepo = cloneConfigRepo;
/**
* 将本地用户配置写入到临时仓库的配置文件中
*/
var writeConfigIntoLocalRepo = function (config) { return __awaiter(void 0, void 0, void 0, function () {
var sync, CONFIG_FOLDER_PATH, GCM_CONFIG_FILE_PATH;
return __generator(this, function (_a) {
console.log("开始写入配置到本地仓库");
sync = (0, gcm_api_1.readConfigs)().sync;
CONFIG_FOLDER_PATH = path_1.default.join(const_1.REPO_FOLDER_PATH, sync.dir);
if (!fs_1.default.existsSync(CONFIG_FOLDER_PATH)) {
fs_1.default.mkdirSync(CONFIG_FOLDER_PATH);
}
GCM_CONFIG_FILE_PATH = path_1.default.join(CONFIG_FOLDER_PATH, sync.filename);
// 判断 configPath 是否存在,否则新建文件
if (!fs_1.default.existsSync(GCM_CONFIG_FILE_PATH)) {
fs_1.default.writeFileSync(GCM_CONFIG_FILE_PATH, "{}");
}
fs_1.default.writeFileSync(GCM_CONFIG_FILE_PATH, JSON.stringify(config, null, 2));
console.log("写入配置到本地仓库成功");
return [2 /*return*/];
});
}); };
exports.writeConfigIntoLocalRepo = writeConfigIntoLocalRepo;
/**
* 推送配置到远程
*/
var pushConfig = function () { return __awaiter(void 0, void 0, void 0, function () {
var _a, name, email;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
console.log("开始推送配置到远程");
return [4 /*yield*/, inquirer_1.default.prompt([
{
type: "input",
name: "name",
message: "请输入你的 git 用户名",
},
{
type: "input",
name: "email",
message: "请输入你的 git 邮箱",
},
])];
case 1:
_a = _b.sent(), name = _a.name, email = _a.email;
console.log("\u7528\u6237: ".concat(name, ", ").concat(email));
(0, child_process_1.execSync)("git config user.name ".concat(name), EXEC_OPTIONS);
(0, child_process_1.execSync)("git config user.email ".concat(email), EXEC_OPTIONS);
(0, child_process_1.execSync)("git add .", EXEC_OPTIONS);
(0, child_process_1.execSync)('git commit -m "update gcm config"', EXEC_OPTIONS);
(0, child_process_1.execSync)("git push origin main", EXEC_OPTIONS);
console.log("推送配置到远程成功");
return [2 /*return*/];
}
});
}); };
exports.pushConfig = pushConfig;