mili
Version:
Scaffolding with continuous control over the development of the project.
81 lines (80 loc) • 2.94 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = void 0;
const simple_git_1 = require("simple-git");
const isUrl = require("is-url");
function parseRepo(url) {
const githubSSHRegExp = /^git@github.com:([-a-zA-Z0-9@:%._+~#=]+)\/([-a-zA-Z0-9@:%._+~#=]+)\.git$/;
const githubHttpRegExp = /https:\/\/github.com\/([-a-zA-Z0-9@:%._+~#=]+)\/([-a-zA-Z0-9@:%._+~#=]+)\.git$/;
let isGitHubRepo = false;
let gitHubOwner;
let gitHubRepoName;
if (isUrl(url)) {
const matched = githubHttpRegExp.exec(url);
if (matched) {
isGitHubRepo = true;
gitHubOwner = matched[1];
gitHubRepoName = matched[2];
}
}
else {
const matched = githubSSHRegExp.exec(url);
if (matched) {
isGitHubRepo = true;
gitHubOwner = matched[1];
gitHubRepoName = matched[2];
}
}
return {
isGitHubRepo,
gitHubOwner,
gitHubRepoName,
};
}
const exec = function (cwd, options) {
return __awaiter(this, void 0, void 0, function* () {
const git = (0, simple_git_1.default)(cwd);
if (!(yield git.checkIsRepo())) {
return {
repository: {
isRepo: false,
isGitHubRepo: false,
},
};
}
const remotes = yield git.getRemotes(true);
if (options.remote) {
const remote = remotes.find(remote => remote.name === options.remote);
if (!remote)
throw new Error(`The remote ${options.remote} not existed.`);
const url = remote.refs.push;
return {
repository: Object.assign({ isRepo: true, url }, parseRepo(url)),
};
}
else if (remotes[0]) {
return {
repository: Object.assign({ isRepo: true, url: remotes[0].refs.push }, parseRepo(remotes[0].refs.push)),
};
}
else {
return {
repository: {
isRepo: true,
url: '',
isGitHubRepo: false,
},
};
}
});
};
exports.exec = exec;