UNPKG

6.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const command_1 = require("@oclif/command");
4const fs = require("fs-extra");
5const fuzzy = require("fuzzaldrin");
6const globby = require("globby");
7const inquirer = require("inquirer");
8const inquirerAutocompletePrompt = require("inquirer-autocomplete-prompt");
9const path = require("path");
10const PrettyError = require("pretty-error");
11const utils_1 = require("./utils");
12const prompt = inquirer.createPromptModule();
13prompt.registerPrompt('autocomplete', inquirerAutocompletePrompt);
14const pe = new PrettyError();
15pe.skipNodeFiles();
16pe.start();
17class BaseCommand extends command_1.default {
18 constructor() {
19 super(...arguments);
20 this.fuzzyMatchCustomDir = async (partialCustomName) => {
21 const { customerName } = this.args;
22 if (!customerName)
23 return;
24 const customRepoRegex = new RegExp(this.flags['custom-repo-regex']);
25 const repoDirs = await this.getCustomRepos();
26 const matches = await Promise.resolve(repoDirs)
27 .then(choices => choices.filter(p => customRepoRegex.test(path.basename(p))))
28 .then(choices => fuzzy.filter(choices, partialCustomName));
29 if (matches.lengh === 0) {
30 return;
31 }
32 return matches[0];
33 };
34 this.getCorePath = async () => {
35 const userConfig = await this.getConfig();
36 return path.resolve(path.join(userConfig.reposDir, this.flags['core-repo-name']));
37 };
38 this.getCoreCustomSubdirPath = async () => {
39 const coreRepo = await this.getCorePath();
40 return path.resolve(path.join(coreRepo, 'Site', 'LmsSite', 'Custom'));
41 };
42 this.getCustomRepos = async () => {
43 const userConfig = await this.getConfig();
44 return globby([userConfig.reposDir + '/*/'], { onlyDirectories: true });
45 };
46 this.getCurrentlyLinkedCustomDir = async () => {
47 const coreSubdir = await this.getCoreCustomSubdirPath();
48 return fs.realpath(coreSubdir)
49 .catch(_err => {
50 return;
51 });
52 };
53 this.promptForCustomerDir = async () => {
54 const repoDirs = await this.getCustomRepos();
55 const customRepoRegex = new RegExp(this.flags['custom-repo-regex']);
56 const customRepoShortnameRegex = new RegExp(this.flags['custom-repo-shortname-regex']);
57 //@ts-ignore
58 return prompt([
59 {
60 type: 'autocomplete',
61 message: 'Which custom repo do you want to link?',
62 name: 'customDir',
63 pageSize: 20,
64 source: (_answers, input) => {
65 return Promise.resolve(repoDirs)
66 .then(choices => choices.filter(p => customRepoRegex.test(path.basename(p))))
67 .then(choices => fuzzy.filter(choices, input))
68 .then(choices => {
69 return choices.map(p => {
70 const bname = path.basename(p);
71 return {
72 name: bname,
73 value: p,
74 short: bname.replace(customRepoShortnameRegex, ''),
75 };
76 });
77 });
78 },
79 },
80 ])
81 .then(({ customDir }) => customDir);
82 };
83 }
84 getConfigPath() {
85 return path.join(this.config.configDir, 'config.json');
86 }
87 getConfig() {
88 const cfgPath = this.getConfigPath();
89 return fs.readJSON(cfgPath)
90 .then(contents => {
91 return contents;
92 });
93 }
94 getConfigDefaults() {
95 const ret = {
96 reposDir: path.join(this.config.home, 'Projects', 'lms'),
97 server: '10.0.0.130\\SQL2012',
98 database: 'TestLms',
99 maildrop: path.join(this.config.home, 'maildrop'),
100 };
101 return Promise.resolve(ret);
102 }
103 configExists() {
104 return fs.pathExistsSync(this.getConfigPath());
105 }
106 async requireConfig() {
107 if (!this.configExists()) {
108 if (!process.stdout.isTTY) {
109 throw new Error('Config not found. Run init to create a config.');
110 }
111 console.error('Config not found. Run init to create a config.');
112 await inquirer.prompt([
113 {
114 type: 'confirm',
115 message: 'Do you want to run configuration now?',
116 name: 'runconfig',
117 },
118 ])
119 .then(({ runconfig }) => {
120 if (runconfig) {
121 return utils_1.promptForConfig.call(this);
122 }
123 this.log('KBAI.');
124 this.exit(-1);
125 });
126 }
127 return this.getConfig();
128 }
129 async init() {
130 const { flags, args } = this.parse();
131 this.flags = flags;
132 this.args = args;
133 }
134 async finally() {
135 // called after run and catch regardless of whether or not the command errored
136 }
137}
138BaseCommand.flags = {
139 help: command_1.flags.help({ char: 'h' }),
140 // Hidden/advanced flags
141 'core-repo-name': command_1.flags.string({
142 hidden: true,
143 description: 'name of the lms core repo',
144 required: true,
145 default: 'lms-core',
146 }),
147 'custom-repo-regex': command_1.flags.string({
148 hidden: true,
149 description: 'regex for which repos are considered custom',
150 required: true,
151 default: '^lms-custom-',
152 }),
153 'custom-repo-shortname-regex': command_1.flags.string({
154 description: 'regex used to shorten customer repo names',
155 hidden: true,
156 required: true,
157 default: '^lms-custom-',
158 })
159};
160exports.default = BaseCommand;