UNPKG

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