1 | "use strict";
|
2 | var __importStar = (this && this.__importStar) || function (mod) {
|
3 | if (mod && mod.__esModule) return mod;
|
4 | var result = {};
|
5 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
6 | result["default"] = mod;
|
7 | return result;
|
8 | };
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const path = __importStar(require("path"));
|
11 | const fs = __importStar(require("fs"));
|
12 | class Config {
|
13 | constructor() {
|
14 | this.data = new ConfigData();
|
15 | this.configRootDir = '';
|
16 | this.reposDir = '';
|
17 | this.configFile = '';
|
18 | }
|
19 | initialize() {
|
20 | this.checkDirs();
|
21 | this.data = this.loadConfig();
|
22 | }
|
23 | checkDirs() {
|
24 | const userDir = require('os').homedir();
|
25 | this.configRootDir = userDir + path.sep + '.xrefcli';
|
26 | this.reposDir = this.configRootDir + path.sep + 'repos';
|
27 | if (!fs.existsSync(this.configRootDir)) {
|
28 | fs.mkdirSync(this.configRootDir);
|
29 | }
|
30 | if (!fs.existsSync(this.reposDir)) {
|
31 | fs.mkdirSync(this.reposDir);
|
32 | }
|
33 | }
|
34 | loadConfig() {
|
35 | this.configFile = this.configRootDir + path.sep + 'xrefconfig.json';
|
36 | let config = new ConfigData();
|
37 | if (fs.existsSync(this.configFile)) {
|
38 | config = require(this.configFile);
|
39 | }
|
40 | return config;
|
41 | }
|
42 | saveConfig() {
|
43 | fs.writeFileSync(this.configFile, JSON.stringify(this.data, null, 4));
|
44 | }
|
45 | addRepo(repo) {
|
46 | if (this.data.repos.findIndex(item => item.name === repo.name) === -1) {
|
47 | this.data.repos.push(repo);
|
48 | }
|
49 | }
|
50 | removeRepo(reponame) {
|
51 | reponame = reponame.toLowerCase();
|
52 | if (this.data.repos.findIndex(item => item.name === reponame) === -1) {
|
53 | console.error(`Error: repo '${reponame}' not found`);
|
54 | process.exit(1);
|
55 | }
|
56 | const repofile = this.getRepoFilename(reponame);
|
57 | fs.unlinkSync(repofile);
|
58 | this.data.repos = this.data.repos.filter(item => item.name !== reponame);
|
59 | }
|
60 | repoExists(reponame) {
|
61 | return (this.data.repos.findIndex(item => item.name === reponame) !== -1);
|
62 | }
|
63 | writeRepoData(reponame, xrefdata) {
|
64 | const repofilename = this.getRepoFilename(reponame);
|
65 | fs.writeFileSync(repofilename, JSON.stringify(xrefdata, undefined, 2));
|
66 | return true;
|
67 | }
|
68 | getRepo(reponame) {
|
69 | const repo = this.data.repos.filter(item => item.name === reponame.toLowerCase())[0];
|
70 | return repo;
|
71 | }
|
72 | getRepoFilename(reponame) {
|
73 | const repofilename = this.reposDir + path.sep + reponame + '.json';
|
74 | return repofilename;
|
75 | }
|
76 | loadRepo(reponame) {
|
77 | const repofile = this.getRepoFilename(reponame);
|
78 | const xreffile = require(repofile);
|
79 | return xreffile;
|
80 | }
|
81 | }
|
82 | exports.Config = Config;
|
83 | class ConfigData {
|
84 | constructor() {
|
85 | this.current = '';
|
86 | this.repos = [];
|
87 | }
|
88 | }
|
89 | exports.ConfigData = ConfigData;
|
90 |
|
\ | No newline at end of file |