Skip to content

Commit 58ebbf7

Browse files
authored
Support npm trusted publishing (#188)
1 parent 3b75100 commit 58ebbf7

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.15.0
2+
3+
* Allow `pkg.npmToken` to be null. This causes npm deployment to use the system
4+
token if one is available, which is necessary for using [trusted publishing].
5+
6+
[trusted publishing]: https://docs.npmjs.com/trusted-publishers
7+
18
## 2.14.0
29

310
* Add a `pkg.useExe` config variable to allow users to customize exactly when

lib/src/npm.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,13 @@ final npmReadme = InternalConfigVariable.fn<String?>(
224224
/// **Do not check this in directly.** This should only come from secure
225225
/// sources.
226226
///
227-
/// By default this comes from the `NPM_TOKEN` environment variable.
228-
final npmToken = InternalConfigVariable.fn<String>(
229-
() =>
230-
Platform.environment["NPM_TOKEN"] ??
231-
fail("pkg.npmToken must be set to deploy to npm."),
227+
/// By default this comes from the `NPM_TOKEN` environment variable. If it's not
228+
/// set, npm will use whatever tokens the system has available; this is
229+
/// necessary for using [trusted publishing].
230+
///
231+
/// [trusted publishing]: https://docs.npmjs.com/trusted-publishers
232+
final npmToken = InternalConfigVariable.fn<String?>(
233+
() => Platform.environment["NPM_TOKEN"],
232234
);
233235

234236
/// The [distribution tag][] to use when publishing the current `npm` package.
@@ -807,9 +809,13 @@ const _cliPkgExports = {};
807809

808810
/// Publishes the contents of `build/npm` to npm.
809811
Future<void> _deploy() async {
810-
var file = File(".npmrc").openSync(mode: FileMode.writeOnlyAppend);
811-
file.writeStringSync("\n//registry.npmjs.org/:_authToken=$npmToken");
812-
file.closeSync();
812+
if (npmToken.value case var token?) {
813+
var file = File(".npmrc").openSync(mode: FileMode.writeOnlyAppend);
814+
file.writeStringSync("\n//registry.npmjs.org/:_authToken=$token");
815+
file.closeSync();
816+
} else {
817+
log("npmToken not set, using system credentials");
818+
}
813819

814820
// The trailing slash in "build/npm/" is necessary to avoid NPM trying to
815821
// treat the path name as a GitHub repository slug.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cli_pkg
2-
version: 2.14.0
2+
version: 2.15.0
33
description: Grinder tasks for releasing Dart CLI packages.
44
homepage: https://github.com/google/dart_cli_pkg
55

0 commit comments

Comments
 (0)