UNPKG

7.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.cmd = exports.args = exports.description = exports.alias = exports.name = void 0;
4var tslib_1 = require("tslib");
5var path_1 = require("path");
6var common_1 = require("../common");
7exports.name = 'tsconfig';
8exports.alias = 'ts';
9exports.description = "Common transformations across typescript configuration files.";
10exports.args = {
11 '-i': 'Include ignored modules.',
12};
13function cmd(args) {
14 return tslib_1.__awaiter(this, void 0, void 0, function () {
15 var options, includeIgnored, settings, response, paths, parts, _a;
16 return tslib_1.__generator(this, function (_b) {
17 switch (_b.label) {
18 case 0:
19 options = (args && args.options) || {};
20 includeIgnored = options.i || false;
21 return [4, common_1.loadSettings()];
22 case 1:
23 settings = _b.sent();
24 if (!settings) {
25 common_1.log.warn.yellow(common_1.constants.CONFIG_NOT_FOUND_ERROR);
26 return [2];
27 }
28 return [4, common_1.inquirer.prompt([
29 {
30 type: 'list',
31 name: 'type',
32 message: 'Change?',
33 choices: ['noUnusedLocals: true', 'noUnusedLocals: false'],
34 },
35 ])];
36 case 2:
37 response = (_b.sent());
38 common_1.log.info();
39 return [4, getTsconfigPaths(settings, { includeIgnored: includeIgnored })];
40 case 3:
41 paths = _b.sent();
42 parts = toChoiceParts(response.type);
43 _a = response.type;
44 switch (_a) {
45 case 'noUnusedLocals: true': return [3, 4];
46 case 'noUnusedLocals: false': return [3, 4];
47 }
48 return [3, 6];
49 case 4: return [4, saveChangesWithPrompt(paths, { noUnusedLocals: parts.value })];
50 case 5:
51 _b.sent();
52 return [3, 7];
53 case 6:
54 common_1.log.error("'" + response.type + "' not supported.");
55 return [3, 7];
56 case 7:
57 common_1.log.info();
58 return [2];
59 }
60 });
61 });
62}
63exports.cmd = cmd;
64function getTsconfigPaths(settings, options) {
65 return tslib_1.__awaiter(this, void 0, void 0, function () {
66 var _a, includeIgnored, paths;
67 return tslib_1.__generator(this, function (_b) {
68 _a = options.includeIgnored, includeIgnored = _a === void 0 ? false : _a;
69 paths = settings.modules
70 .filter(function (pkg) { return common_1.filter.includeIgnored(pkg, includeIgnored); })
71 .map(function (m) { return m.dir; })
72 .map(function (dir) { return common_1.fs.join(dir, 'tsconfig.json'); });
73 return [2, common_1.filter.fileExists(paths)];
74 });
75 });
76}
77function saveChangesWithPrompt(paths, changes) {
78 return tslib_1.__awaiter(this, void 0, void 0, function () {
79 var response, _a;
80 return tslib_1.__generator(this, function (_b) {
81 switch (_b.label) {
82 case 0:
83 if (paths.length === 0) {
84 common_1.log.info.gray('No files to change.');
85 return [2, false];
86 }
87 common_1.log.info.cyan("\nChange files:");
88 paths.forEach(function (path) {
89 common_1.log.info(" " + toDisplayPath(path));
90 });
91 common_1.log.info();
92 return [4, common_1.inquirer.prompt([
93 {
94 type: 'list',
95 name: 'confirm',
96 message: 'Are you sure?',
97 choices: ['No', 'Yes'],
98 },
99 ])];
100 case 1:
101 response = (_b.sent());
102 _a = response.confirm;
103 switch (_a) {
104 case 'No': return [3, 2];
105 case 'Yes': return [3, 3];
106 }
107 return [3, 5];
108 case 2:
109 common_1.log.info.gray("Nothing changed.");
110 return [2, false];
111 case 3: return [4, saveChanges(paths, changes)];
112 case 4:
113 _b.sent();
114 return [2, true];
115 case 5: return [2, false];
116 }
117 });
118 });
119}
120function toDisplayPath(path) {
121 var root = path_1.parse(path);
122 var dir = path_1.parse(root.dir);
123 return common_1.log.gray(dir.dir + "/" + common_1.log.magenta(dir.base) + "/" + common_1.log.cyan(root.base));
124}
125function saveChanges(paths, changes) {
126 return tslib_1.__awaiter(this, void 0, void 0, function () {
127 var saveChange, tasks, error_1;
128 var _this = this;
129 return tslib_1.__generator(this, function (_a) {
130 switch (_a.label) {
131 case 0:
132 saveChange = function (path) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
133 var json, compilerOptions, tsConfig, text;
134 return tslib_1.__generator(this, function (_a) {
135 switch (_a.label) {
136 case 0: return [4, common_1.fs.readJson(path)];
137 case 1:
138 json = _a.sent();
139 compilerOptions = tslib_1.__assign(tslib_1.__assign({}, json.compilerOptions), changes);
140 tsConfig = tslib_1.__assign(tslib_1.__assign({}, json), { compilerOptions: compilerOptions });
141 text = JSON.stringify(tsConfig, null, ' ') + "\n";
142 return [4, common_1.fs.writeFile(path, text)];
143 case 2:
144 _a.sent();
145 return [2];
146 }
147 });
148 }); };
149 tasks = paths.map(function (path) {
150 return {
151 title: common_1.log.cyan('Updated') + " " + toDisplayPath(path),
152 task: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
153 return [2, saveChange(path)];
154 }); }); },
155 };
156 });
157 _a.label = 1;
158 case 1:
159 _a.trys.push([1, 3, , 4]);
160 return [4, common_1.listr(tasks, { concurrent: true, exitOnError: false }).run()];
161 case 2:
162 _a.sent();
163 return [3, 4];
164 case 3:
165 error_1 = _a.sent();
166 return [3, 4];
167 case 4: return [2];
168 }
169 });
170 });
171}
172function toChoiceParts(choice) {
173 var parts = choice.split(':');
174 var key = parts[0].trim();
175 var value = common_1.value.toType(parts[1].trim());
176 return { key: key, value: value };
177}