安装-nativescript
帮助您安装 Node.js、npm 和 nativescript-cli 的软件包。仅在 Windows 和 Mac 上运行。
npm i --save install-nativescript

安装-nativescript

帮助您在 Electron 应用程序内部安装 Node.js、npm 和 nativescript-cli 的软件包。仅在 Windows 和 Mac 上运行。

用法

  1. 首先安装软件包
npm install --save install-nativescript
  1. 现在在您的代码中使用它
const installNativeScript = require("install-nativescript");

installNativeScript.ensureNode()
.then(() => {
return installNativeScript.ensureCLI();
})

公共 API

ensureNode

ensureNode 方法检查机器上是否安装了 node。如果无法检测到,该方法将下载 node 安装程序并运行。您可以安装特定的 node 版本。如果没有提供,该方法将安装版本 10.15.3。

注意:此方法更改 process.env,以便成功后,您可以安装和检测全局安装的 npm 模块。

用法

const installNativeScript = require("install-nativescript");

installNativeScript.ensureNode();

ensureCLI

ensureCLI 方法检查您是否在机器上全局安装了 NativeScript CLI。

  • 如果无法检测到,该方法将使用 npm 全局安装它。您可以安装特定的 NativeScript CLI 版本。如果没有提供,该方法将安装最新官方版本。
  • 如果检测到版本,并且您传递了特定的版本范围而当前版本不满足要求,则方法将更新它。再次,如果没有提供特定版本,方法将安装最新官方版本。

用法

const installNativeScript = require("install-nativescript");

installNativeScript.ensureCLI();

getNPMFoldersWithMissingPermissions

ensureCLI 方法可能在 Mac 机器上失败的一个原因是某些 npm 文件夹需要 root 权限。此方法可以帮助您检测这种情况。只需传递来自 ensureCLI 的错误,它将返回可能需要 root 权限的文件夹数组。如果数组为空,则错误与缺失权限无关。

用法

const installNativeScript = require("install-nativescript");

installNativeScript.ensureCLI()
.catch((error) => {
const permissionsFolders = installNativeScript.getNPMFoldersWithMissingPermissions(error);
});

fixMissingNPMPermissions

fixMissingNPMPermissions 方法接受文件夹数组并尝试更改它们的权限。

用法

const installNativeScript = require("install-nativescript");

installNativeScript.ensureCLI()
.catch((error) => {
const permissionsFolders = installNativeScript.getNPMFoldersWithMissingPermissions(error);

if (permissionsFolders.length === 0) {
return Promise.reject(error);
}

// It is a good idea to ask the user at this point if they approve the change and then change the permissions.

return installNativeScript.fixMissingNPMPermissions(permissionsFolders)
.then(() => {
return installNativeScript.ensureCLI();
});
});