1 | const { spawn } = require("child_process");
|
2 | const { resolve: pathResolve } = require("path");
|
3 |
|
4 | const {
|
5 | getPackageJson,
|
6 | getProjectDir,
|
7 | writePackageJson,
|
8 | } = require("../projectHelpers");
|
9 | const { forceUpdateProjectFiles } = require("../projectFilesManager");
|
10 | const { forceUpdateProjectDeps } = require("../dependencyManager");
|
11 |
|
12 | const PLUGIN_NAME = "nativescript-dev-webpack";
|
13 | const PROJECT_DIR = getProjectDir();
|
14 |
|
15 | function update({
|
16 | deps: shouldUpdateDeps,
|
17 | configs: shouldUpdateConfigs,
|
18 | projectDir = PROJECT_DIR
|
19 | } = {}) {
|
20 |
|
21 | const commands = [];
|
22 |
|
23 | if (shouldUpdateDeps) {
|
24 | commands.push(() => updateDeps(projectDir));
|
25 | }
|
26 |
|
27 | if (shouldUpdateConfigs) {
|
28 | commands.push(() => Promise.resolve(updateConfigs(projectDir)));
|
29 | }
|
30 |
|
31 | return commands.reduce((current, next) => current.then(next), Promise.resolve());
|
32 | }
|
33 |
|
34 | function updateDeps(projectDir) {
|
35 | console.info("Updating dev dependencies...");
|
36 |
|
37 | return new Promise((resolve, reject) => {
|
38 | const packageJson = getPackageJson(projectDir);
|
39 | const { deps } = forceUpdateProjectDeps(packageJson);
|
40 | packageJson.devDependencies = deps;
|
41 | writePackageJson(packageJson, projectDir);
|
42 |
|
43 | const command = `npm install --ignore-scripts`;
|
44 | execute(command).then(resolve).catch(reject);
|
45 | });
|
46 | }
|
47 |
|
48 | function updateConfigs(projectDir) {
|
49 | console.info("Updating configuration files...");
|
50 | forceUpdateProjectFiles(projectDir);
|
51 | }
|
52 |
|
53 | function execute(command) {
|
54 | return new Promise((resolve, reject) => {
|
55 | const args = command.split(" ");
|
56 | spawnChildProcess(...args)
|
57 | .then(resolve)
|
58 | .catch(throwError)
|
59 | });
|
60 | }
|
61 |
|
62 | function spawnChildProcess(command, ...args) {
|
63 | return new Promise((resolve, reject) => {
|
64 | const escapedArgs = args.map(a => `"${a}"`);
|
65 |
|
66 | const childProcess = spawn(command, escapedArgs, {
|
67 | stdio: "inherit",
|
68 | cwd: PROJECT_DIR,
|
69 | shell: true,
|
70 | });
|
71 |
|
72 | childProcess.on("close", code => {
|
73 | if (code === 0) {
|
74 | resolve();
|
75 | } else {
|
76 | reject({
|
77 | code,
|
78 | message: `child process exited with code ${code}`,
|
79 | });
|
80 | }
|
81 | });
|
82 | });
|
83 | }
|
84 |
|
85 | function throwError(error) {
|
86 | console.error(error.message);
|
87 | process.exit(error.code || 1);
|
88 | }
|
89 |
|
90 | module.exports = update;
|
91 |
|