tuture
Version:
Write tutorials from the future, with the power of Git and community.
128 lines (127 loc) • 4.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const crypto_1 = tslib_1.__importDefault(require("crypto"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const command_1 = require("@oclif/command");
const inquirer_1 = require("inquirer");
const logger_1 = tslib_1.__importDefault(require("../utils/logger"));
const base_1 = tslib_1.__importDefault(require("../base"));
const utils_1 = require("../utils");
const collection_1 = require("../utils/collection");
const git_1 = require("../utils/git");
class Init extends base_1.default {
async promptInitGit(yes) {
const response = yes
? { answer: true }
: await inquirer_1.prompt([
{
name: 'answer',
type: 'confirm',
message: 'You are not in a Git repository, do you want to initialize one?',
default: false,
},
]);
if (!response.answer) {
this.exit(0);
}
else {
await git_1.git.init();
logger_1.default.log('success', 'Git repository is initialized!');
}
}
async promptMetaData(yes) {
const answer = yes
? { name: 'My Awesome Tutorial' }
: await inquirer_1.prompt([
{
name: 'name',
message: 'Tutorial Name',
default: 'My Awesome Tutorial',
},
{
name: 'description',
message: 'Description',
},
{
name: 'topics',
message: 'Topics',
},
{
name: 'categories',
message: 'Categories',
},
]);
answer.id = crypto_1.default.randomBytes(16).toString('hex');
// TODO: process user input with inquirer built-ins
const { topics, categories } = answer;
if (topics) {
answer.topics = topics.split(/\W+/);
}
else {
delete answer.topics;
}
if (categories) {
answer.categories = categories.split(/\W+/);
}
else {
delete answer.categories;
}
return answer;
}
async run() {
const { flags } = this.parse(Init);
if (fs_extra_1.default.existsSync(collection_1.collectionPath)) {
logger_1.default.log('success', 'Tuture tutorial has already been initialized!');
this.exit(0);
}
if (!(await git_1.git.checkIsRepo())) {
await this.promptInitGit(flags.yes);
}
const meta = await this.promptMetaData(flags.yes);
try {
const steps = await utils_1.makeSteps(this.userConfig.ignoredFiles);
steps.forEach((step) => {
step.articleId = meta.id;
});
const collection = Object.assign(Object.assign({}, meta), { created: new Date(), articles: [meta], steps });
const github = await git_1.inferGithubField();
if (github) {
logger_1.default.log('info', `Inferred github repository: ${chalk_1.default.underline(github)}. Feel free to revise or delete it.`);
collection.github = github;
}
const remotes = await git_1.git.getRemotes(true);
if (remotes.length > 0) {
if (flags.yes) {
collection.remotes = [remotes[0]];
}
else {
collection.remotes = await git_1.selectRemotes(remotes);
}
}
collection_1.saveCollection(collection);
git_1.appendGitignore();
git_1.appendGitHook();
logger_1.default.log('success', 'Tuture tutorial has been initialized!');
}
catch (err) {
await utils_1.removeTutureSuite();
logger_1.default.log({
level: 'error',
message: err.message,
error: err,
});
this.exit(1);
}
}
}
exports.default = Init;
Init.description = 'Initialize a tuture tutorial';
Init.flags = {
help: command_1.flags.help({ char: 'h' }),
yes: command_1.flags.boolean({
char: 'y',
description: 'do not ask for prompts',
}),
};