1 | "use strict";
|
2 | const rimraf_1 = require("rimraf");
|
3 | const addConfig_1 = require("../fns/add-cmd/addConfig");
|
4 | const cmdName_1 = require("../fns/cmdName");
|
5 | const xSpawn_1 = require("../fns/xSpawn");
|
6 | const PackageManager_1 = require("../inc/PackageManager");
|
7 | const Log_1 = require("../lib/Log");
|
8 | const PromptableConfig_1 = require("../lib/PromptableConfig");
|
9 | const command = cmdName_1.cmdName(__filename);
|
10 | const cmd = {
|
11 | builder(argv) {
|
12 | return addConfig_1.addConfig(argv, command)
|
13 | .positional('pkgMgr', {
|
14 | describe: 'Package manager in use. Can be inferred from existing lockfile.',
|
15 | type: 'string'
|
16 | })
|
17 | .option('cwd', {
|
18 | hidden: true,
|
19 | type: 'string'
|
20 | })
|
21 | .option('keep-lockfile', {
|
22 | alias: ['k', 'keep'],
|
23 | default: false,
|
24 | describe: 'Don\'t remove the lockfile',
|
25 | type: 'boolean'
|
26 | });
|
27 | },
|
28 | command: `${command} [pkgMgr]`,
|
29 | describe: 'Perform a clean reinstallation of node modules',
|
30 | handler(c$) {
|
31 | const initialCwd = process.cwd();
|
32 | try {
|
33 | if (c$.cwd) {
|
34 | process.chdir(c$.cwd);
|
35 | }
|
36 | const pkgMgr = new PromptableConfig_1.PromptableConfig(c$).promptedPkgMgr();
|
37 | Log_1.Log.info('Removing node_modules');
|
38 | rimraf_1.sync('node_modules');
|
39 | Log_1.Log.success('Removed node_modules');
|
40 | if (!c$.keepLockfile) {
|
41 | if (pkgMgr === PackageManager_1.PackageManager.YARN) {
|
42 | rimraf_1.sync('yarn.lock');
|
43 | }
|
44 | else {
|
45 | rimraf_1.sync('package-lock.json');
|
46 | rimraf_1.sync('npm-shrinkwrap.json');
|
47 | }
|
48 | Log_1.Log.success('Removed lockfile');
|
49 | }
|
50 | else {
|
51 | Log_1.Log.info('Keeping lockfile');
|
52 | }
|
53 | Log_1.Log.info(`Running ${pkgMgr} install`);
|
54 | const args = ['install'];
|
55 | if (pkgMgr === PackageManager_1.PackageManager.YARN) {
|
56 | args.push('--check-files');
|
57 | }
|
58 | const sp = xSpawn_1.xSpawnSync(pkgMgr, args, {
|
59 | stdio: process.env.RUNNING_PERSONAL_BUILD_TOOLS_TESTS ? 'ignore' : 'inherit'
|
60 | });
|
61 | if (sp.status !== 0) {
|
62 | throw new Error(`Exited with code ${sp.status}`);
|
63 | }
|
64 | Log_1.Log.success(`${pkgMgr} install finished successfully.`);
|
65 | }
|
66 | finally {
|
67 | process.chdir(initialCwd);
|
68 | }
|
69 | }
|
70 | };
|
71 | module.exports = cmd;
|