npm-update-package 0.21.0 → 0.22.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # npm-update-package
5
5
 
6
- CLI tool for creating pull request to update npm packages
6
+ CLI tool for creating pull requests to update npm packages
7
7
 
8
8
  ## Usage
9
9
 
@@ -14,37 +14,25 @@ npx npm-update-package --github-token $GITHUB_TOKEN
14
14
  ## Options
15
15
 
16
16
  You can customize behavior via command-line options.
17
- Template strings such as `--branch-name` can embed variables like `{{packageName}}`(HTML-escaped) or `{{{packageName}}}`(not HTML-escaped).
18
-
19
- ### `--branch-name`
20
-
21
- Branch name template
22
-
23
- - type: string
24
- - variables:
25
- - `currentVersion`
26
- - `newVersion`
27
- - `packageName`
28
- - `updateType`
29
- - required: false
30
- - default: `npm-update-package/{{{packageName}}}/v{{newVersion}}`
17
+ Template strings such as `--commit-message` can embed variables like `{{packageName}}`(HTML-escaped) or `{{{packageName}}}`(not HTML-escaped).
31
18
 
32
19
  ### `--commit-message`
33
20
 
34
21
  Commit message template
35
22
 
36
23
  - type: string
24
+ - required: false
37
25
  - variables:
38
26
  - `currentVersion`
39
27
  - `newVersion`
40
28
  - `packageName`
41
29
  - `updateType`
42
- - required: false
43
30
  - default: `chore(deps): {{updateType}} update {{{packageName}}} to v{{newVersion}}`
44
31
 
45
32
  ### `--github-token`
46
33
 
47
- GitHub token
34
+ GitHub token.
35
+ See more in [GitHub token](#github-token) section.
48
36
 
49
37
  - type: string
50
38
  - required: true
@@ -54,12 +42,12 @@ GitHub token
54
42
  Log level to show
55
43
 
56
44
  - type: string
45
+ - required: false
57
46
  - allowed values:
58
47
  - `off`: Do not output any logs.
59
48
  - `error`: Output error logs.
60
49
  - `info`: Output error/info logs.
61
50
  - `debug`: Output error/info/debug logs.
62
- - required: false
63
51
  - default: `info`
64
52
 
65
53
  ### `--package-manager`
@@ -67,35 +55,60 @@ Log level to show
67
55
  Package manager of your project
68
56
 
69
57
  - type: string
70
- - allowed values:
71
- - `npm`
72
- - `yarn`
73
58
  - required: false
59
+ - allowed values:
60
+ - `npm`: npm
61
+ - `yarn`: Yarn
74
62
  - default: `npm`
75
63
 
76
- ### `--pull-request-labels`
77
-
78
- Pull request labels
79
-
80
- - type: string array
81
- - required: false
82
-
83
64
  ### `--pull-request-title`
84
65
 
85
66
  Pull request title template
86
67
 
87
68
  - type: string
69
+ - required: false
88
70
  - variables:
89
71
  - `currentVersion`
90
72
  - `newVersion`
91
73
  - `packageName`
92
74
  - `updateType`
93
- - required: false
94
75
  - default: `chore(deps): {{updateType}} update {{{packageName}}} to v{{newVersion}}`
95
76
 
77
+ ## GitHub token
78
+
79
+ GitHub token is required to run npm-update-package.
80
+ For example, the following tokens can be used.
81
+
82
+ |GitHub token|Author of pull requests|Trigger other actions|
83
+ |---|---|---|
84
+ |[GitHub Actions](https://docs.github.com/en/actions/security-guides/automatic-token-authentication)|`github-actions`||
85
+ |[GitHub App](https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps)|GitHub App which issued the token|✓|
86
+ |[Personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)|user who issued the token|✓|
87
+
88
+ We recommend using GitHub App for the following reasons.
89
+
90
+ - When you use the token of GitHub Actions, the job will not trigger other actions.
91
+ - When you use the Personal access token, the author of pull requests will be the user who issued the token.
92
+
93
+ Creating a GitHub App may be tedious, but you only have to do it once the first time.
94
+
95
+ ### Token of GitHub App
96
+
97
+ These permissions are required.
98
+
99
+ - Contents: Read-only
100
+ - Metadata: Read-only
101
+ - Pull requests: Read & write
102
+
103
+ ### Personal access token
104
+
105
+ These permissions are required.
106
+
107
+ - repo
108
+
96
109
  ## Examples
97
110
 
98
- Example of running npm-update-package on GitHub Actions at 0:00 (UTC) every day:
111
+ - Use token of GitHub Actions
99
112
 
100
113
  ```yaml
101
114
  name: npm-update-package
@@ -118,13 +131,48 @@ jobs:
118
131
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
119
132
  ```
120
133
 
134
+
135
+ - Use token of GitHub App
136
+
137
+ ```yaml
138
+ name: npm-update-package
139
+ on:
140
+ schedule:
141
+ - cron: '0 0 * * *'
142
+ jobs:
143
+ npm-update-package:
144
+ runs-on: ubuntu-latest
145
+ steps:
146
+ - uses: actions/checkout@v2
147
+ - uses: actions/setup-node@v2
148
+ - name: Generate token
149
+ id: generate_token
150
+ uses: tibdex/github-app-token@v1
151
+ with:
152
+ app_id: ${{ secrets.APP_ID }}
153
+ private_key: ${{ secrets.PRIVATE_KEY }}
154
+ - run: |
155
+ git config user.name $GIT_USER_NAME
156
+ git config user.email $GIT_USER_EMAIL
157
+ npx npm-update-package --github-token $GITHUB_TOKEN
158
+ env:
159
+ # TODO: Replace with your GitHub App's email
160
+ GIT_USER_EMAIL: 97396142+npm-update-package-bot[bot]@users.noreply.github.com
161
+ # TODO: Replace with your GitHub App's user name
162
+ GIT_USER_NAME: npm-update-package-bot[bot]
163
+ GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
164
+ ```
165
+
121
166
  Actual working examples can be seen in these repositories.
122
167
 
123
- - [npm-update-package/example-npm](https://github.com/npm-update-package/example-npm)
168
+ |Repository|Package manager|GitHub token|
169
+ |---|---|---|
170
+ |[example-github-actions](https://github.com/npm-update-package/example-github-actions)|npm|GitHub Actions|
171
+ |[example-github-app](https://github.com/npm-update-package/example-github-app)|npm|GitHub App|
124
172
 
125
173
  ## Flow
126
174
 
127
- The following shows the process flow of npm-update-package:
175
+ The following shows the process flow of npm-update-package.
128
176
 
129
177
  <!--
130
178
  ```plantuml
@@ -138,18 +186,23 @@ if (All packages are up-to-date) then (yes)
138
186
  else (no)
139
187
  endif
140
188
 
189
+ :Get remote branches;
190
+ :Get pull requests;
191
+
141
192
  group OutdatedPackagesProcessor
142
- repeat
193
+ while (Package exists) is (yes)
143
194
  group OutdatedPackageProcessor
144
195
  if (Remote branch exists) then (yes)
145
196
  else (no)
146
197
  :Create branch;
147
198
  :Update package;
148
199
  :Create pull request;
200
+ :Close old pull requests;
149
201
  :Remove branch;
150
202
  endif
151
203
  end group
152
- repeat while (Next package exists)
204
+ endwhile (no)
205
+
153
206
  end group
154
207
 
155
208
  end group
@@ -158,7 +211,7 @@ end
158
211
  ```
159
212
  -->
160
213
 
161
- [![](http://www.plantuml.com/plantuml/svg/RKyxKiCm5EpVARPqYbmWRi9Gq4666GuWx4tiGPQ4z0JXzaIn70D38wsUzkrddzH4pPELAv1Jz3bWCgEJtGCLFcjlb3s2wTxCYQaL6OzexgozZM0YaSDM_RPmDz213iqNqqO0kbve4z4ulv7BQpnAjNcgueSgT8Y-Oqe-IcIWKG7-9Quye8HvvkILU8t6TGDu7fEcFodM102mkuyqjursdhs4uh6qQdyHGxOMaU-PIUjFCVtuAN6jDjzpxDe2dyDeYUQHPrtKbv2oabTOaEmlHvxiDm00)](http://www.plantuml.com/plantuml/uml/RKyxKiCm5EpVARPqYbmWRi9Gq4666GuWx4tiGPQ4z0JXzaIn70D38wsUzkrddzH4pPELAv1Jz3bWCgEJtGCLFcjlb3s2wTxCYQaL6OzexgozZM0YaSDM_RPmDz213iqNqqO0kbve4z4ulv7BQpnAjNcgueSgT8Y-Oqe-IcIWKG7-9Quye8HvvkILU8t6TGDu7fEcFodM102mkuyqjursdhs4uh6qQdyHGxOMaU-PIUjFCVtuAN6jDjzpxDe2dyDeYUQHPrtKbv2oabTOaEmlHvxiDm00)
214
+ [![](http://www.plantuml.com/plantuml/svg/RL0vSiCm3EpnYYsbmX-m6cTIf8mdCta08y4M9n39yCZn-uYYJ4kPD3omm1uuXQXyJ9FGyg2hjybXKjhGyOaZR8g3YZp0gVvTNJbqHFg2va6abg0y8xb3j8VSsoAERD3yS6W9O3CGIs0qnhOq__I52hVdoKR6crUc7sVcKdL919u_4eUOrOgZvzN7UTKyUzjp2DRJrwY5qQm0-5lFSorqg0x-PRWJ03dGozPCPTadgIa0uFZeMTM9hjHUNTQuRQRRDkw2tH2nWM5b-9EweDdIvrPWMLwvIwYypf9_iKLtW7OzT9gFDCal)](http://www.plantuml.com/plantuml/uml/RL0vSiCm3EpnYYsbmX-m6cTIf8mdCta08y4M9n39yCZn-uYYJ4kPD3omm1uuXQXyJ9FGyg2hjybXKjhGyOaZR8g3YZp0gVvTNJbqHFg2va6abg0y8xb3j8VSsoAERD3yS6W9O3CGIs0qnhOq__I52hVdoKR6crUc7sVcKdL919u_4eUOrOgZvzN7UTKyUzjp2DRJrwY5qQm0-5lFSorqg0x-PRWJ03dGozPCPTadgIa0uFZeMTM9hjHUNTQuRQRRDkw2tH2nWM5b-9EweDdIvrPWMLwvIwYypf9_iKLtW7OzT9gFDCal)
162
215
 
163
216
  ## FAQ
164
217
 
@@ -166,6 +219,6 @@ end
166
219
 
167
220
  npm-update-package can be used in environments where Renovate cannot be used for some reason.
168
221
 
169
- ### Conflicts have occurred in PR. What should I do?
222
+ ### Conflicts have occurred in the pull request. What should I do?
170
223
 
171
- If conflicts have occurred in PR, close it and run npm-update-package again.
224
+ If conflicts have occurred in the pull request, close it and run npm-update-package again.
package/dist/app.js CHANGED
@@ -3,6 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.app = void 0;
4
4
  exports.app = {
5
5
  name: 'npm-update-package',
6
- version: '0.21.0',
6
+ version: '0.22.3',
7
7
  web: 'https://github.com/npm-update-package/npm-update-package'
8
8
  };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createBranchName = void 0;
4
+ const mustache_1 = require("mustache");
5
+ const TEMPLATE = 'npm-update-package/{{{packageName}}}/v{{newVersion}}';
6
+ const createBranchName = (outdatedPackage) => {
7
+ const packageName = outdatedPackage.name;
8
+ const currentVersion = outdatedPackage.currentVersion.version;
9
+ const newVersion = outdatedPackage.newVersion.version;
10
+ const updateType = outdatedPackage.type;
11
+ return (0, mustache_1.render)(TEMPLATE, {
12
+ packageName,
13
+ currentVersion,
14
+ newVersion,
15
+ updateType
16
+ });
17
+ };
18
+ exports.createBranchName = createBranchName;
package/dist/git/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GitRepository = exports.Git = exports.CommitMessageCreator = exports.BranchNameCreator = void 0;
4
- var BranchNameCreator_1 = require("./BranchNameCreator");
5
- Object.defineProperty(exports, "BranchNameCreator", { enumerable: true, get: function () { return BranchNameCreator_1.BranchNameCreator; } });
3
+ exports.GitRepository = exports.Git = exports.CommitMessageCreator = exports.createBranchName = void 0;
4
+ var createBranchName_1 = require("./createBranchName");
5
+ Object.defineProperty(exports, "createBranchName", { enumerable: true, get: function () { return createBranchName_1.createBranchName; } });
6
6
  var CommitMessageCreator_1 = require("./CommitMessageCreator");
7
7
  Object.defineProperty(exports, "CommitMessageCreator", { enumerable: true, get: function () { return CommitMessageCreator_1.CommitMessageCreator; } });
8
8
  var Git_1 = require("./Git");
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -10,12 +10,30 @@ class GitHub {
10
10
  const { data } = await this.octokit.issues.addLabels(params);
11
11
  return data;
12
12
  }
13
+ async closePullRequest(params) {
14
+ await this.octokit.pulls.update({
15
+ ...params,
16
+ state: 'closed'
17
+ });
18
+ }
13
19
  async createPullRequest(params) {
14
20
  const { data } = await this.octokit.pulls.create(params);
15
21
  return data;
16
22
  }
23
+ // TODO: fetch all branches with page option
17
24
  async fetchBranches(params) {
18
- const { data } = await this.octokit.repos.listBranches(params);
25
+ const { data } = await this.octokit.repos.listBranches({
26
+ ...params,
27
+ per_page: 100
28
+ });
29
+ return data;
30
+ }
31
+ // TODO: fetch all pull requests with page option
32
+ async fetchPullRequests(params) {
33
+ const { data } = await this.octokit.pulls.list({
34
+ ...params,
35
+ per_page: 100
36
+ });
19
37
  return data;
20
38
  }
21
39
  async fetchRepository(params) {
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PullRequestCloser = void 0;
4
+ // TODO: Add test
5
+ class PullRequestCloser {
6
+ constructor(github) {
7
+ this.github = github;
8
+ }
9
+ async close(pullRequest) {
10
+ await this.github.closePullRequest({
11
+ owner: pullRequest.base.repo.owner.login,
12
+ repo: pullRequest.base.repo.name,
13
+ pull_number: pullRequest.number
14
+ });
15
+ }
16
+ }
17
+ exports.PullRequestCloser = PullRequestCloser;
@@ -1,20 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PullRequestCreator = void 0;
4
+ const createPullRequestBody_1 = require("./createPullRequestBody");
4
5
  class PullRequestCreator {
5
- constructor({ github, gitRepo, githubRepo, pullRequestTitleCreator, pullRequestBodyCreator, logger, labels }) {
6
+ constructor({ github, gitRepo, githubRepo, pullRequestTitleCreator, logger }) {
6
7
  this.github = github;
7
8
  this.gitRepo = gitRepo;
8
9
  this.githubRepo = githubRepo;
9
10
  this.pullRequestTitleCreator = pullRequestTitleCreator;
10
- this.pullRequestBodyCreator = pullRequestBodyCreator;
11
11
  this.logger = logger;
12
- this.labels = labels;
13
12
  }
14
13
  async create({ outdatedPackage, branchName }) {
15
14
  const title = this.pullRequestTitleCreator.create(outdatedPackage);
16
15
  this.logger.debug(`title=${title}`);
17
- const body = this.pullRequestBodyCreator.create(outdatedPackage);
16
+ const body = (0, createPullRequestBody_1.createPullRequestBody)(outdatedPackage);
18
17
  this.logger.debug(`body=${body}`);
19
18
  const pullRequest = await this.github.createPullRequest({
20
19
  owner: this.gitRepo.owner,
@@ -25,16 +24,13 @@ class PullRequestCreator {
25
24
  body
26
25
  });
27
26
  this.logger.debug(`pullRequest=${JSON.stringify(pullRequest)}`);
28
- if (this.labels !== undefined) {
29
- const labels = await this.github.addLabels({
30
- owner: this.gitRepo.owner,
31
- repo: this.gitRepo.name,
32
- issue_number: pullRequest.number,
33
- labels: this.labels
34
- });
35
- this.logger.debug(`labels=${JSON.stringify(labels)}`);
36
- }
37
- this.logger.info(`Pull request for ${outdatedPackage.name} has created. ${pullRequest.html_url}`);
27
+ await this.github.addLabels({
28
+ owner: this.gitRepo.owner,
29
+ repo: this.gitRepo.name,
30
+ issue_number: pullRequest.number,
31
+ labels: ['npm-update-package']
32
+ });
33
+ return pullRequest;
38
34
  }
39
35
  }
40
36
  exports.PullRequestCreator = PullRequestCreator;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PullRequestFinder = void 0;
4
+ const extractPullRequestMetadata_1 = require("./extractPullRequestMetadata");
5
+ const isPullRequestByNpmUpdatePackage_1 = require("./isPullRequestByNpmUpdatePackage");
6
+ // TODO: Add test
7
+ class PullRequestFinder {
8
+ constructor(pullRequests) {
9
+ this.pullRequests = pullRequests;
10
+ }
11
+ findByPackageName(packageName) {
12
+ return this.pullRequests
13
+ .filter(isPullRequestByNpmUpdatePackage_1.isPullRequestByNpmUpdatePackage)
14
+ .filter(({ body }) => {
15
+ if (body === null) {
16
+ return false;
17
+ }
18
+ const metadata = (0, extractPullRequestMetadata_1.extractPullRequestMetadata)(body);
19
+ if (metadata === undefined) {
20
+ return false;
21
+ }
22
+ return metadata.packages.some(({ name }) => name === packageName);
23
+ });
24
+ }
25
+ }
26
+ exports.PullRequestFinder = PullRequestFinder;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPullRequestMetadata = void 0;
4
+ const io_ts_1 = require("io-ts");
5
+ const ncu_1 = require("../ncu");
6
+ const PullRequestMetadata = (0, io_ts_1.type)({
7
+ version: io_ts_1.string,
8
+ packages: (0, io_ts_1.array)((0, io_ts_1.type)({
9
+ name: io_ts_1.string,
10
+ currentVersion: io_ts_1.string,
11
+ newVersion: io_ts_1.string,
12
+ type: (0, io_ts_1.union)([(0, io_ts_1.literal)(ncu_1.UpdateType.Major), (0, io_ts_1.literal)(ncu_1.UpdateType.Minor), (0, io_ts_1.literal)(ncu_1.UpdateType.Patch)])
13
+ }))
14
+ });
15
+ exports.isPullRequestMetadata = PullRequestMetadata.is;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPullRequestBody = void 0;
4
+ const mustache_1 = require("mustache");
5
+ const app_1 = require("../app");
6
+ const TEMPLATE = `This PR updates these packages:
7
+
8
+ |package|type|current version|new version|
9
+ |---|---|---|---|
10
+ |[{{{packageName}}}](https://www.npmjs.com/package/{{{packageName}}})|{{updateType}}|\`{{currentVersion}}\`|\`{{newVersion}}\`|
11
+
12
+ <details>
13
+ <summary>Metadata</summary>
14
+
15
+ **Don't remove or edit this section because it will be used by npm-update-package.**
16
+
17
+ <div id="npm-update-package-metadata">
18
+
19
+ \`\`\`json
20
+ {
21
+ "version": "{{appVersion}}",
22
+ "packages": [
23
+ {
24
+ "name": "{{{packageName}}}",
25
+ "type": "{{updateType}}",
26
+ "currentVersion": "{{currentVersion}}",
27
+ "newVersion": "{{newVersion}}"
28
+ }
29
+ ]
30
+ }
31
+ \`\`\`
32
+
33
+ </div>
34
+ </details>
35
+
36
+ ---
37
+ This PR has been generated by [{{{appName}}}]({{{appWeb}}}) v{{appVersion}}`;
38
+ const createPullRequestBody = (outdatedPackage) => {
39
+ const appName = app_1.app.name;
40
+ const appVersion = app_1.app.version;
41
+ const appWeb = app_1.app.web;
42
+ const currentVersion = outdatedPackage.currentVersion.version;
43
+ const newVersion = outdatedPackage.newVersion.version;
44
+ const packageName = outdatedPackage.name;
45
+ const updateType = outdatedPackage.type;
46
+ return (0, mustache_1.render)(TEMPLATE, {
47
+ appName,
48
+ appVersion,
49
+ appWeb,
50
+ currentVersion,
51
+ newVersion,
52
+ packageName,
53
+ updateType
54
+ });
55
+ };
56
+ exports.createPullRequestBody = createPullRequestBody;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractPullRequestMetadata = void 0;
4
+ const PullRequestMetadata_1 = require("./PullRequestMetadata");
5
+ const extractPullRequestMetadata = (pullRequestBody) => {
6
+ const matched = pullRequestBody.match(/<div id="npm-update-package-metadata">\s*```json\s*([\s\S]+?)\s*```\s*<\/div>/);
7
+ const json = matched === null || matched === void 0 ? void 0 : matched[1];
8
+ if (json === undefined) {
9
+ return undefined;
10
+ }
11
+ const metadata = JSON.parse(json);
12
+ if (!(0, PullRequestMetadata_1.isPullRequestMetadata)(metadata)) {
13
+ return undefined;
14
+ }
15
+ return metadata;
16
+ };
17
+ exports.extractPullRequestMetadata = extractPullRequestMetadata;
@@ -1,14 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RemoteBranchExistenceChecker = exports.PullRequestTitleCreator = exports.PullRequestCreator = exports.PullRequestBodyCreator = exports.GitHub = exports.createGitHub = void 0;
3
+ exports.RemoteBranchExistenceChecker = exports.PullRequestTitleCreator = exports.PullRequestFinder = exports.PullRequestCreator = exports.PullRequestCloser = exports.GitHub = exports.createGitHub = void 0;
4
4
  var createGitHub_1 = require("./createGitHub");
5
5
  Object.defineProperty(exports, "createGitHub", { enumerable: true, get: function () { return createGitHub_1.createGitHub; } });
6
6
  var GitHub_1 = require("./GitHub");
7
7
  Object.defineProperty(exports, "GitHub", { enumerable: true, get: function () { return GitHub_1.GitHub; } });
8
- var PullRequestBodyCreator_1 = require("./PullRequestBodyCreator");
9
- Object.defineProperty(exports, "PullRequestBodyCreator", { enumerable: true, get: function () { return PullRequestBodyCreator_1.PullRequestBodyCreator; } });
8
+ var PullRequestCloser_1 = require("./PullRequestCloser");
9
+ Object.defineProperty(exports, "PullRequestCloser", { enumerable: true, get: function () { return PullRequestCloser_1.PullRequestCloser; } });
10
10
  var PullRequestCreator_1 = require("./PullRequestCreator");
11
11
  Object.defineProperty(exports, "PullRequestCreator", { enumerable: true, get: function () { return PullRequestCreator_1.PullRequestCreator; } });
12
+ var PullRequestFinder_1 = require("./PullRequestFinder");
13
+ Object.defineProperty(exports, "PullRequestFinder", { enumerable: true, get: function () { return PullRequestFinder_1.PullRequestFinder; } });
12
14
  var PullRequestTitleCreator_1 = require("./PullRequestTitleCreator");
13
15
  Object.defineProperty(exports, "PullRequestTitleCreator", { enumerable: true, get: function () { return PullRequestTitleCreator_1.PullRequestTitleCreator; } });
14
16
  var RemoteBranchExistenceChecker_1 = require("./RemoteBranchExistenceChecker");
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPullRequestByNpmUpdatePackage = void 0;
4
+ const isPullRequestByNpmUpdatePackage = (pullRequest) => {
5
+ return pullRequest.labels.some(({ name }) => name === 'npm-update-package');
6
+ };
7
+ exports.isPullRequestByNpmUpdatePackage = isPullRequestByNpmUpdatePackage;
package/dist/main.js CHANGED
@@ -50,24 +50,27 @@ const main = async ({ options, logger }) => {
50
50
  repo: gitRepo.name
51
51
  });
52
52
  logger.debug(`remoteBranches=${JSON.stringify(remoteBranches)}`);
53
+ const pullRequests = await github.fetchPullRequests({
54
+ owner: gitRepo.owner,
55
+ repo: gitRepo.name
56
+ });
57
+ logger.debug(`pullRequests=${JSON.stringify(pullRequests)}`);
53
58
  const remoteBranchExistenceChecker = github_1.RemoteBranchExistenceChecker.of(remoteBranches);
54
59
  const packageManager = (0, package_manager_1.createPackageManager)({
55
60
  terminal,
56
61
  packageManager: options.packageManager
57
62
  });
58
63
  const pullRequestTitleCreator = new github_1.PullRequestTitleCreator(options.pullRequestTitle);
59
- const pullRequestBodyCreator = new github_1.PullRequestBodyCreator();
60
64
  const pullRequestCreator = new github_1.PullRequestCreator({
61
65
  github,
62
66
  gitRepo,
63
67
  githubRepo,
64
68
  pullRequestTitleCreator,
65
- pullRequestBodyCreator,
66
- logger,
67
- labels: options.pullRequestLabels
69
+ logger
68
70
  });
69
- const branchNameCreator = new git_1.BranchNameCreator(options.branchName);
70
71
  const commitMessageCreator = new git_1.CommitMessageCreator(options.commitMessage);
72
+ const pullRequestFinder = new github_1.PullRequestFinder(pullRequests);
73
+ const pullRequestCloser = new github_1.PullRequestCloser(github);
71
74
  const outdatedPackageProcessor = new processors_1.OutdatedPackageProcessor({
72
75
  git,
73
76
  ncu,
@@ -75,8 +78,9 @@ const main = async ({ options, logger }) => {
75
78
  pullRequestCreator,
76
79
  remoteBranchExistenceChecker,
77
80
  logger,
78
- branchNameCreator,
79
- commitMessageCreator
81
+ commitMessageCreator,
82
+ pullRequestFinder,
83
+ pullRequestCloser
80
84
  });
81
85
  const outdatedPackagesProcessor = new processors_1.OutdatedPackagesProcessor({
82
86
  outdatedPackageProcessor,
@@ -86,10 +90,10 @@ const main = async ({ options, logger }) => {
86
90
  logger.debug(`results=${JSON.stringify(results)}`);
87
91
  const succeededResults = results.filter(Either_1.isRight).map(({ right }) => right);
88
92
  logger.debug(`succeededResults=${JSON.stringify(succeededResults)}`);
89
- const updatedPackages = succeededResults
90
- .filter(({ updated }) => updated)
93
+ const createdPackages = succeededResults
94
+ .filter(({ created }) => created)
91
95
  .map(({ outdatedPackage }) => outdatedPackage);
92
- logger.debug(`updatedPackages=${JSON.stringify(updatedPackages)}`);
96
+ logger.debug(`createdPackages=${JSON.stringify(createdPackages)}`);
93
97
  const skippedPackages = succeededResults
94
98
  .filter(({ skipped }) => skipped)
95
99
  .map(({ outdatedPackage }) => outdatedPackage);
@@ -100,8 +104,8 @@ const main = async ({ options, logger }) => {
100
104
  logger.debug(`failedPackages=${JSON.stringify(failedPackages)}`);
101
105
  // TODO: show as table
102
106
  logger.info(`Processed ${succeededResults.length + failedPackages.length} packages:
103
- - ${updatedPackages.length} packages has updated: ${updatedPackages.map(({ name }) => name).join(',')}
104
- - ${skippedPackages.length} packages has skipped: ${skippedPackages.map(({ name }) => name).join(',')}
105
- - ${failedPackages.length} packages has failed: ${failedPackages.map(({ name }) => name).join(',')}`);
107
+ - ${createdPackages.length} packages: created (${createdPackages.map(({ name }) => name).join(',')})
108
+ - ${skippedPackages.length} packages: skipped: (${skippedPackages.map(({ name }) => name).join(',')})
109
+ - ${failedPackages.length} packages: failed: (${failedPackages.map(({ name }) => name).join(',')})`);
106
110
  };
107
111
  exports.main = main;
@@ -4,17 +4,11 @@ exports.isOptions = void 0;
4
4
  const io_ts_1 = require("io-ts");
5
5
  const logger_1 = require("../logger");
6
6
  const package_manager_1 = require("../package-manager");
7
- const Options = (0, io_ts_1.intersection)([
8
- (0, io_ts_1.type)({
9
- branchName: io_ts_1.string,
10
- commitMessage: io_ts_1.string,
11
- githubToken: io_ts_1.string,
12
- logLevel: (0, io_ts_1.union)([(0, io_ts_1.literal)(logger_1.LogLevel.Off), (0, io_ts_1.literal)(logger_1.LogLevel.Error), (0, io_ts_1.literal)(logger_1.LogLevel.Info), (0, io_ts_1.literal)(logger_1.LogLevel.Debug)]),
13
- packageManager: (0, io_ts_1.union)([(0, io_ts_1.literal)(package_manager_1.PackageManagerName.Npm), (0, io_ts_1.literal)(package_manager_1.PackageManagerName.Yarn)]),
14
- pullRequestTitle: io_ts_1.string
15
- }),
16
- (0, io_ts_1.partial)({
17
- pullRequestLabels: (0, io_ts_1.array)(io_ts_1.string)
18
- })
19
- ]);
7
+ const Options = (0, io_ts_1.type)({
8
+ commitMessage: io_ts_1.string,
9
+ githubToken: io_ts_1.string,
10
+ logLevel: (0, io_ts_1.union)([(0, io_ts_1.literal)(logger_1.LogLevel.Off), (0, io_ts_1.literal)(logger_1.LogLevel.Error), (0, io_ts_1.literal)(logger_1.LogLevel.Info), (0, io_ts_1.literal)(logger_1.LogLevel.Debug)]),
11
+ packageManager: (0, io_ts_1.union)([(0, io_ts_1.literal)(package_manager_1.PackageManagerName.Npm), (0, io_ts_1.literal)(package_manager_1.PackageManagerName.Yarn)]),
12
+ pullRequestTitle: io_ts_1.string
13
+ });
20
14
  exports.isOptions = Options.is;
@@ -5,13 +5,6 @@ const logger_1 = require("../logger");
5
5
  const package_manager_1 = require("../package-manager");
6
6
  const OptionType_1 = require("./OptionType");
7
7
  exports.cliOptions = [
8
- {
9
- name: 'branch-name',
10
- description: 'Branch name template',
11
- type: OptionType_1.OptionType.String,
12
- required: false,
13
- default: 'npm-update-package/{{{packageName}}}/v{{newVersion}}'
14
- },
15
8
  {
16
9
  name: 'commit-message',
17
10
  description: 'Commit message template',
@@ -49,12 +42,6 @@ exports.cliOptions = [
49
42
  ],
50
43
  default: package_manager_1.PackageManagerName.Npm
51
44
  },
52
- {
53
- name: 'pull-request-labels',
54
- description: 'Pull request labels',
55
- type: OptionType_1.OptionType.StringArray,
56
- required: false
57
- },
58
45
  {
59
46
  name: 'pull-request-title',
60
47
  description: 'Pull request title template',
@@ -2,23 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OutdatedPackageProcessor = void 0;
4
4
  const Either_1 = require("fp-ts/lib/Either");
5
+ const git_1 = require("../git");
5
6
  // TODO: add test
6
7
  class OutdatedPackageProcessor {
7
- constructor({ git, ncu, packageManager, pullRequestCreator, remoteBranchExistenceChecker, logger, branchNameCreator, commitMessageCreator }) {
8
+ constructor({ git, ncu, packageManager, pullRequestCreator, remoteBranchExistenceChecker, logger, commitMessageCreator, pullRequestFinder, pullRequestCloser }) {
8
9
  this.git = git;
9
10
  this.ncu = ncu;
10
11
  this.packageManager = packageManager;
11
12
  this.pullRequestCreator = pullRequestCreator;
12
13
  this.remoteBranchExistenceChecker = remoteBranchExistenceChecker;
13
14
  this.logger = logger;
14
- this.branchNameCreator = branchNameCreator;
15
15
  this.commitMessageCreator = commitMessageCreator;
16
+ this.pullRequestFinder = pullRequestFinder;
17
+ this.pullRequestCloser = pullRequestCloser;
16
18
  }
17
19
  /**
18
20
  * Don't run in parallel because it includes file operations.
19
21
  */
20
22
  async process(outdatedPackage) {
21
- const branchName = this.branchNameCreator.create(outdatedPackage);
23
+ const branchName = (0, git_1.createBranchName)(outdatedPackage);
22
24
  this.logger.debug(`branchName=${branchName}`);
23
25
  if (this.remoteBranchExistenceChecker.check(branchName)) {
24
26
  this.logger.info(`Skip ${outdatedPackage.name} because ${branchName} branch already exists on remote.`);
@@ -50,13 +52,20 @@ class OutdatedPackageProcessor {
50
52
  this.logger.debug(`message=${message}`);
51
53
  await this.git.commit(message);
52
54
  await this.git.push(branchName);
53
- await this.pullRequestCreator.create({
55
+ const pullRequest = await this.pullRequestCreator.create({
54
56
  outdatedPackage,
55
57
  branchName
56
58
  });
59
+ this.logger.info(`Pull request for ${outdatedPackage.name} has created. ${pullRequest.html_url}`);
60
+ const pullRequests = this.pullRequestFinder.findByPackageName(outdatedPackage.name);
61
+ this.logger.debug(`pullRequests=${JSON.stringify(pullRequests)}`);
62
+ await Promise.all(pullRequests.map(async (pullRequest) => {
63
+ await this.pullRequestCloser.close(pullRequest);
64
+ this.logger.info(`Pull request for ${outdatedPackage.name} has closed. ${pullRequest.html_url}`);
65
+ }));
57
66
  return (0, Either_1.right)({
58
67
  outdatedPackage,
59
- updated: true
68
+ created: true
60
69
  });
61
70
  }
62
71
  finally {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "npm-update-package",
3
- "version": "0.21.0",
4
- "description": "CLI tool for creating pull request to update npm packages",
3
+ "version": "0.22.3",
4
+ "description": "CLI tool for creating pull requests to update npm packages",
5
5
  "scripts": {
6
6
  "build": "tsc --project tsconfig.build.json",
7
7
  "clean": "rimraf dist",
@@ -20,7 +20,7 @@
20
20
  "@octokit/rest": "18.12.0",
21
21
  "commander": "8.3.0",
22
22
  "execa": "5.1.1",
23
- "fp-ts": "2.11.6",
23
+ "fp-ts": "2.11.7",
24
24
  "io-ts": "2.2.16",
25
25
  "log4js": "6.3.0",
26
26
  "mustache": "4.1.0",
@@ -37,7 +37,7 @@
37
37
  "@types/node": "12.20.40",
38
38
  "@types/parse-github-url": "1.0.0",
39
39
  "@types/semver": "7.3.9",
40
- "@typescript-eslint/eslint-plugin": "5.9.0",
40
+ "@typescript-eslint/eslint-plugin": "5.9.1",
41
41
  "eslint": "8.6.0",
42
42
  "eslint-config-standard-with-typescript": "21.0.1",
43
43
  "eslint-plugin-import": "2.25.4",
@@ -46,8 +46,8 @@
46
46
  "eslint-plugin-promise": "6.0.0",
47
47
  "eslint-plugin-tsdoc": "0.2.14",
48
48
  "husky": "7.0.4",
49
- "jest": "27.4.6",
50
- "lint-staged": "12.1.5",
49
+ "jest": "27.4.7",
50
+ "lint-staged": "12.1.7",
51
51
  "npm-run-all": "4.1.5",
52
52
  "rimraf": "3.0.2",
53
53
  "ts-jest": "27.1.2",
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BranchNameCreator = void 0;
4
- const mustache_1 = require("mustache");
5
- class BranchNameCreator {
6
- constructor(template) {
7
- this.template = template;
8
- }
9
- create(outdatedPackage) {
10
- const packageName = outdatedPackage.name;
11
- const currentVersion = outdatedPackage.currentVersion.version;
12
- const newVersion = outdatedPackage.newVersion.version;
13
- const updateType = outdatedPackage.type;
14
- return (0, mustache_1.render)(this.template, {
15
- packageName,
16
- currentVersion,
17
- newVersion,
18
- updateType
19
- });
20
- }
21
- }
22
- exports.BranchNameCreator = BranchNameCreator;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PullRequestBodyCreator = void 0;
4
- const mustache_1 = require("mustache");
5
- const app_1 = require("../app");
6
- class PullRequestBodyCreator {
7
- constructor() {
8
- this.template = `This PR updates these packages:
9
-
10
- |package|type|current version|new version|
11
- |---|---|---|---|
12
- |[{{{packageName}}}](https://www.npmjs.com/package/{{{packageName}}})|{{updateType}}|\`{{currentVersion}}\`|\`{{newVersion}}\`|
13
-
14
- ---
15
- This PR has been generated by [{{{appName}}}]({{{appWeb}}}) v{{appVersion}}`;
16
- }
17
- create(outdatedPackage) {
18
- const appName = app_1.app.name;
19
- const appVersion = app_1.app.version;
20
- const appWeb = app_1.app.web;
21
- const currentVersion = outdatedPackage.currentVersion.version;
22
- const newVersion = outdatedPackage.newVersion.version;
23
- const packageName = outdatedPackage.name;
24
- const updateType = outdatedPackage.type;
25
- return (0, mustache_1.render)(this.template, {
26
- appName,
27
- appVersion,
28
- appWeb,
29
- currentVersion,
30
- newVersion,
31
- packageName,
32
- updateType
33
- });
34
- }
35
- }
36
- exports.PullRequestBodyCreator = PullRequestBodyCreator;