yarn publish & yarn link

# yarn publish & yarn link

リポジトリサーバ(nexus)にnpmモジュールをpublishしてそれをpullする。

ソースは [github](https://github.com/va034600/sample-html/blob/master/sample-yarn-link) にあげました。

## リポジトリサーバをローカルに立てる

- 参考サイト

https://kazuhira-r.hatenablog.com/entry/20170109/1483972099

## 実装

### library(yarn-fetch-library)

```json:yarn-fetch-library/package.json
{
  "name": "@va034600/yarn-fetch-library",
  "version": "1.0.5",
  "description": "",
  "main": "./src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "underscore": "^1.9.1"
  },
  "files": [
    "src"
  ],
  "publishConfig": {
    "registry": "http://localhost:9998/repository/my-npm-hosted-repo/"
  }
}
```

```yaml:yarn-fetch-library/yarn.lock
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


underscore@^1.9.1:
  version "1.9.1"
  resolved "http://localhost:8081/repository/my-npm-group-repo/underscore/-/underscore-1.9.1.tgz#06dce34a0e68a7babc29b365b8e74b8925203961"
  integrity sha512-5/4etnCkd9c8gwgowi5/om/mYO5ajCaOgdzj/oW+0eQV9WxKBDZw5+ycmKmeaTXjInS/W0BzpGLo2xR2aBwZdg==
```

```javascript:yarn-fetch-library/src/index.js
var _ = require('underscore');
var https = require('https');

function fetchSample(path) {
    var req = https.request(path, (res) => {
        res.on('data', (chunk) => {
            console.log(`BODY: ${chunk}`);
        });
    })
    req.end();
    _.each([1, 2, 3], console.log);
};

module.exports = fetchSample;
```

### main(yarn-fetch-main)

```json:yarn-fetch-main/package.json
{
  "name": "yarn-fetch-main",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "private": false,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@va034600/yarn-fetch-library": "^1.0.5"
  }
}
```

```yaml:yarn-fetch-main/yarn.lock
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@va034600/yarn-fetch-library@^1.0.5":
  version "1.0.5"
  resolved "http://localhost:8081/repository/my-npm-group-repo/@va034600/yarn-fetch-library/-/yarn-fetch-library-1.0.5.tgz#354fb62ff73126f480152421d901ba654e84914b"
  integrity sha512-ccvs3zrAY1Y5DW0S68ljJNyn1IoBFZUu/8Jqk6hDBxAqtmTlpzlsWIL0OVxxjYleX+SRIHqpE1/Ig3KtcWoSeg==
  dependencies:
    underscore "^1.9.1"

underscore@^1.9.1:
  version "1.9.1"
  resolved "http://localhost:8081/repository/my-npm-group-repo/underscore/-/underscore-1.9.1.tgz#06dce34a0e68a7babc29b365b8e74b8925203961"
  integrity sha512-5/4etnCkd9c8gwgowi5/om/mYO5ajCaOgdzj/oW+0eQV9WxKBDZw5+ycmKmeaTXjInS/W0BzpGLo2xR2aBwZdg==
```

```javascript:yarn-fetch-main/src/index.js
console.log("-----")

var fetchSample = require("@va034600/yarn-fetch-library")
fetchSample("https://google.com")

console.log("-----")
```

## 実行

### npm認証情報

~/.npmrc に以下を追加

```
email=va034600@gmail.com
always-auth=true
# $ echo -n 'admin:admin123' | openssl base64
_auth=YWRtaW46YWRtaW4xMjM=

registry=http://localhost:9998/repository/my-npm-group-repo/
```

### yarn publish

```
$ cd yarn-fetch-library
$ yarn publish
```

### yarn install

```
$ yarn install
```

### yarn-fetch-library/src/index.js 修正

```
# 4 を追加
_.each([1, 2, 3, 4], console.log);
```

### test1

```
$ cd yarn-fetch-main
$ node src/index.js
```

console.logから反映されないこと確認


### yarn link

```
$ cd yarn-fetch-library
$ yarn link
```

```
$ cd yarn-fetch-main
$ yarn link @va034600/yarn-fetch-library
```

### test2

```
$ cd yarn-fetch-main
$ node src/index.js
```

console.logから反映されていること確認

# 最後に

やっぱりrepository serverですよね