发布教程.md 2.1 KB

发布教程

Step 1

npm init  # 或者使用, npm init -y 跳过所有提问

Step 2 根据提示,填写相应信息,得到 package.json文件

# package.json

{
  "name": "ts-hi",
  "version": "0.0.1",
  "description": "create npm package with typescript",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/youthcity/ts-hi.git"
  },
  "keywords": [
    "typescript"
  ],
  "author": "youthcity",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/youthcity/ts-hi/issues"
  },
  "homepage": "https://github.com/youthcity/ts-hi#readme"
}

Step 3 安装依赖 安装 Typescript

# 使用 npm 安装
npm i typescript -D

# 或使用 yarn 进行安装
yarn add typescript -D

配置 tsconfig.json 文件 手动创建配置文件,文件如下

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true
  }
}

使用命令行创建。

# 需要全局安装 typescript包
yarn add i typescript -D
npm i typescript -D
tsc --init 

# 使用当前项目中的 typescript
./node_modules/.bin/tsc --init

Step 6 编写测试 1)安装测试框架和断言库

npm i mocha -D
npm i chai -D
yarn add  mocha -D
yarn add chai -D

2)创建测试文件目录和文件

根目录下

mkdir test && touch test/test.js
test.js
'use strict';
const expect = require('chai').expect;
const add = require('../dist/index').add;

describe('ts-hi function test', () => {
  it('should return 2', () => {
    const result = add(1, 1);
    expect(result).to.equal(2);
  });
});

Step 7 运行测试 添加测试脚本

  "scripts": {
    "build": "tsc",
    "test": "mocha --reporter spec"
  },

运行测试脚本

npm run test

版本管理

版本升级

# From 1.0.0 -> 1.0.1
npm version patch 

# From 1.0.1 -> 1.1.0
npm version minor

# From 1.1.0 -> 2.0.0
npm version major

版本发布

npm publish --access public 

版本删除

npm unpublish @ycxxkj/uniapp@1.0.0 --force