关于javascript:编写自定义的CLI工具

22次阅读

共计 607 个字符,预计需要花费 2 分钟才能阅读完成。

繁难 demo
  1. 编码 package.json
{
  "name": "daytoy",
  "version": "0.0.1",
  "description": "nodejs cli program",
  "main": "index.js",
  "scripts": {"test": "echo \"Error: no test specified\"&& exit 1"},
  "author": "daytoy",
  "license": "ISC",
  "bin": {"toy": "index.js"}
}

bin 属性具备可执行文件,cli 命令要求运行 index.js 文件。

  1. 编写可执行文件
#! node
console.log('DAY DAY TOY !');
/**
在 Linux 和基于 Unix 的操作系统(例如 Mac)中,#!/ usr / bin / env node 不仅仅是一个正文。它应用在 /usr/bin/env 中注册的 node 命令来运行文件。然而,在 Windows 中,这只是一个正文。**/
  1. 运行 npm i -g 在 package.json 中装置配置

  1. 执行自定义的命令

问题: 零碎上禁止运行脚本?

解决:
1. 关上 PowerShell(以管理员身份运行)
2.set-ExecutionPolicy RemoteSigned // 设置为关上
3. 键入 Y 或者 A, 批准
4. 执行 get-executionpolicy 查看是否更改胜利,为 RemoteSigned 示意胜利

### 利用:创立工程脚手架模板交互(nodejs)

正文完
 0