UNPKG

2.41 kBJavaScriptView Raw
1const { spawn } = require("child_process");
2const { resolve: pathResolve } = require("path");
3
4const {
5 getPackageJson,
6 getProjectDir,
7 writePackageJson,
8} = require("../projectHelpers");
9const { forceUpdateProjectFiles } = require("../projectFilesManager");
10const { forceUpdateProjectDeps } = require("../dependencyManager");
11
12const PLUGIN_NAME = "nativescript-dev-webpack";
13const PROJECT_DIR = getProjectDir();
14
15function 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
34function 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
48function updateConfigs(projectDir) {
49 console.info("Updating configuration files...");
50 forceUpdateProjectFiles(projectDir);
51}
52
53function 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
62function 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
85function throwError(error) {
86 console.error(error.message);
87 process.exit(error.code || 1);
88}
89
90module.exports = update;
91