UNPKG

5.63 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const chalk_1 = require("chalk");
4var dumper_js_1 = require("dumper.js");
5exports.dd = dumper_js_1.dd;
6const chrono = require("chrono-node");
7const execa = require("execa");
8const fs = require("fs-extra");
9const hyperlinker = require("hyperlinker");
10const inquirer = require("inquirer");
11const path = require("path");
12const supportsHyperlinks = require("supports-hyperlinks");
13const util = require("util");
14exports.inspect = (obj) => util.inspect(obj, { colors: true, depth: 3 });
15const decorateRepoPathDefaults = {
16 reset: true,
17 hyperlink: true,
18 basenameOnly: false,
19};
20function decorateRepoPath(repoPath, options) {
21 const { reset, hyperlink, basenameOnly } = Object.assign({}, decorateRepoPathDefaults, options);
22 const doHyperlink = hyperlink && supportsHyperlinks.stdout && path.isAbsolute(repoPath);
23 let ret = repoPath;
24 if (basenameOnly) {
25 ret = path.basename(ret);
26 }
27 ret = ret
28 .replace(/(lms-)(core)(\/|$)/g, chalk_1.default `${reset ? chalk_1.default.reset('$1') : '$1'}{cyan $2}$3`)
29 .replace(/(\/)(Custom)/g, chalk_1.default `$1{bold $2}`)
30 .replace(/(lms-custom-)(\w+)/g, chalk_1.default `${reset ? chalk_1.default.reset('$1') : '$1'}{cyan $2}`)
31 .replace(/(lms-core-legacy-)(\w+)/g, chalk_1.default `${reset ? chalk_1.default.reset('$1') : '$1'}{cyan $2}`);
32 if (doHyperlink) {
33 ret = hyperlinker(ret, `file://${repoPath}`);
34 }
35 return ret;
36}
37exports.decorateRepoPath = decorateRepoPath;
38async function promptForConfig() {
39 const prompt = inquirer.createPromptModule();
40 const flags = this.flags;
41 const cfgPath = `${this.config.configDir}/config.json`;
42 const defaultConfigValues = await this.getConfigDefaults();
43 const existingCfg = await (() => {
44 return fs.readJSON(cfgPath)
45 .catch(() => {
46 return {};
47 });
48 })();
49 if (flags.list) {
50 console.log(exports.inspect(existingCfg));
51 process.exit();
52 }
53 const defaults = Object.assign({}, defaultConfigValues, existingCfg);
54 return prompt([
55 {
56 type: 'input',
57 message: 'What is your GitHub username?',
58 default: defaults.username || path.basename(this.config.home),
59 name: 'username',
60 },
61 {
62 type: 'input',
63 message: 'What is your GitHub auth token?',
64 name: 'authToken',
65 validate: val => val.length < 2 ? 'Token must be 41 characters' : true,
66 default: defaults.authToken,
67 },
68 {
69 type: 'input',
70 message: 'Where do you want to store your repos?',
71 name: 'reposDir',
72 filter: userInp => {
73 let val = userInp;
74 if (val.charAt(0) === '~') {
75 val = val.replace('~', this.config.home);
76 }
77 return path.resolve(process.cwd(), path.normalize(val));
78 },
79 default: defaults.reposDir,
80 },
81 {
82 type: 'input',
83 message: 'Database server name',
84 name: 'server',
85 default: defaults.server,
86 },
87 {
88 type: 'input',
89 message: 'Database name',
90 name: 'database',
91 default: defaults.database,
92 },
93 {
94 type: 'input',
95 message: 'Maildrop location',
96 name: 'maildrop',
97 default: defaults.maildrop,
98 },
99 ])
100 .then((answers) => {
101 console.log('');
102 console.log(exports.inspect(answers));
103 console.log(`\nThis configuration will be written to:\n\t${cfgPath}`);
104 return prompt([
105 {
106 type: 'confirm',
107 message: 'Look good?',
108 default: true,
109 name: 'confirmed',
110 },
111 ])
112 .then(({ confirmed }) => {
113 if (!confirmed) {
114 this.log('KBAI.');
115 process.exit(0);
116 }
117 return answers;
118 });
119 })
120 .then((answers) => {
121 return fs.ensureDir(this.config.configDir)
122 .then(() => {
123 return fs.writeFile(cfgPath, JSON.stringify(answers, undefined, ' '))
124 .then(() => {
125 return answers;
126 });
127 });
128 });
129}
130exports.promptForConfig = promptForConfig;
131async function getFileDateByFilenameOrGitAuthorDate(filepath) {
132 let filedate;
133 try {
134 const fn = path.basename(filepath);
135 const match = fn.match(/(\d{4})-?(\d{2})-?(\d{2})/);
136 if (match) {
137 if (match[1] === '9000') {
138 filedate = new Date(9000, 0);
139 }
140 else {
141 filedate = chrono.parseDate(match.slice(1, 4).join('-'));
142 }
143 }
144 }
145 catch (_a) {
146 //pass
147 }
148 if (!filedate) {
149 try {
150 const gitDateOut = await execa.shell(`git -C "${path.dirname(filepath)}" log --format=%aI ${filepath}`);
151 const gitDate = gitDateOut.stdout.split('\n').reverse()[0];
152 if (gitDate) {
153 filedate = new Date(gitDate);
154 }
155 }
156 catch (_b) {
157 // console.log('Could not get file date from git')
158 }
159 }
160 if (!filedate) {
161 // ¯\_(ツ)_/¯
162 throw new Error(`Could not get date for file "${filepath}"`);
163 filedate = new Date();
164 }
165 return filedate;
166}
167exports.getFileDateByFilenameOrGitAuthorDate = getFileDateByFilenameOrGitAuthorDate;