UNPKG

5.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.RepairCommand = void 0;
4const utils_fs_1 = require("@ionic/utils-fs");
5const utils_terminal_1 = require("@ionic/utils-terminal");
6const path = require("path");
7const color_1 = require("../lib/color");
8const command_1 = require("../lib/command");
9const errors_1 = require("../lib/errors");
10const executor_1 = require("../lib/executor");
11class RepairCommand extends command_1.Command {
12 async getMetadata() {
13 return {
14 name: 'repair',
15 type: 'project',
16 summary: 'Remove and recreate dependencies and generated files',
17 description: `
18This command may be useful when obscure errors or issues are encountered. It removes and recreates dependencies of your project.
19
20For Cordova apps, it removes and recreates the generated native project and the native dependencies of your project.
21`,
22 options: [
23 {
24 name: 'cordova',
25 summary: 'Only perform the repair steps for Cordova platforms and plugins.',
26 type: Boolean,
27 },
28 ],
29 };
30 }
31 async run(inputs, options, runinfo) {
32 if (!this.project) {
33 throw new errors_1.FatalException(`Cannot run ${color_1.input('ionic repair')} outside a project directory.`);
34 }
35 const { pkgManagerArgs } = await Promise.resolve().then(() => require('../lib/utils/npm'));
36 const [installer, ...installerArgs] = await pkgManagerArgs(this.env.config.get('npmClient'), { command: 'install' });
37 const cordovaOnly = !!options['cordova'];
38 const cordova = this.project.getIntegration('cordova');
39 if (cordovaOnly && !cordova) {
40 throw new errors_1.FatalException(`${color_1.input('--cordova')} was specified, but Cordova has not been added to this project.`);
41 }
42 if (cordova && !cordova.enabled) {
43 this.env.log.warn(`Cordova integration found, but is disabled--not running repair for Cordova.`);
44 }
45 if (this.env.flags.interactive) {
46 const steps = [];
47 if (!cordovaOnly) {
48 steps.push(`- Remove ${color_1.strong('node_modules/')} and ${color_1.strong('package-lock.json')}\n` +
49 `- Run ${color_1.input([installer, ...installerArgs].join(' '))} to restore dependencies\n`);
50 }
51 if (cordova && cordova.enabled) {
52 steps.push(`- Remove ${color_1.strong('platforms/')} and ${color_1.strong('plugins/')}\n` +
53 `- Run ${color_1.input('cordova prepare')} to restore platforms and plugins\n`);
54 }
55 if (steps.length === 0) {
56 this.env.log.ok(`${color_1.input('ionic repair')} has nothing to do.`);
57 throw new errors_1.FatalException('', 0);
58 }
59 this.env.log.info(`${color_1.input('ionic repair')} will do the following:\n\n` + steps.join(''));
60 }
61 const confirm = await this.env.prompt({
62 type: 'confirm',
63 name: 'confirm',
64 message: 'Continue?',
65 default: false,
66 });
67 if (!confirm) {
68 throw new errors_1.FatalException(`Not running ${color_1.input('ionic repair')}.`);
69 }
70 this.env.log.nl();
71 if (!cordovaOnly) {
72 await this.npmRepair(this.project);
73 }
74 if (cordova && cordova.enabled) {
75 await this.cordovaRepair(cordova, runinfo);
76 }
77 }
78 async npmRepair(project) {
79 const { pkgManagerArgs } = await Promise.resolve().then(() => require('../lib/utils/npm'));
80 const [installer, ...installerArgs] = await pkgManagerArgs(this.env.config.get('npmClient'), { command: 'install' });
81 const tasks = this.createTaskChain();
82 const packageLockFile = path.resolve(project.directory, 'package-lock.json');
83 const nodeModulesDir = path.resolve(project.directory, 'node_modules');
84 tasks.next(`Removing ${color_1.strong(utils_terminal_1.prettyPath(packageLockFile))}`);
85 const packageLockFileExists = await utils_fs_1.pathExists(packageLockFile);
86 if (packageLockFileExists) {
87 await utils_fs_1.unlink(packageLockFile);
88 }
89 tasks.next(`Removing ${color_1.strong(utils_terminal_1.prettyPath(nodeModulesDir))}`);
90 await utils_fs_1.remove(nodeModulesDir);
91 tasks.end();
92 await this.env.shell.run(installer, installerArgs, { cwd: project.directory, stdio: 'inherit' });
93 }
94 async cordovaRepair(cordova, runinfo) {
95 const tasks = this.createTaskChain();
96 const platformsDir = path.resolve(cordova.root, 'platforms');
97 const pluginsDir = path.resolve(cordova.root, 'plugins');
98 tasks.next(`Removing ${color_1.strong(utils_terminal_1.prettyPath(platformsDir))}`);
99 await utils_fs_1.remove(platformsDir);
100 tasks.next(`Removing ${color_1.strong(utils_terminal_1.prettyPath(pluginsDir))}`);
101 await utils_fs_1.remove(pluginsDir);
102 tasks.end();
103 await executor_1.runCommand(runinfo, ['cordova', 'prepare', '--no-build']);
104 }
105}
106exports.RepairCommand = RepairCommand;