generated from JS-DevTools/template-node-typescript
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathnpm-publish.ts
52 lines (44 loc) · 1.66 KB
/
npm-publish.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as semver from "semver";
import { normalizeOptions } from "./normalize-options";
import { npm } from "./npm";
import { Options } from "./options";
import { readManifest } from "./read-manifest";
import { Results } from "./results";
/**
* Publishes a package to NPM, if its version has changed
*/
export async function npmPublish(opts: Options = {}): Promise<Results> {
let options = normalizeOptions(opts);
// Get the old and new version numbers
let manifest = await readManifest(options.package, options.debug);
let publishedVersion = await npm.getLatestVersion(manifest.name, options);
// Determine if/how the version has changed
let diff = semver.diff(manifest.version, publishedVersion);
// Compare both versions to see if it's changed
let cmp = semver.compare(manifest.version, publishedVersion);
let shouldPublish =
!options.checkVersion ||
// compare returns 1 if manifest is higher than published
(options.greaterVersionOnly && cmp === 1) ||
// compare returns 0 if the manifest is the same as published
cmp !== 0;
if (shouldPublish) {
// Publish the new version to NPM
await npm.publish(manifest, options);
}
let results: Results = {
package: manifest.name,
// The version should be marked as lower if we disallow decrementing the version
type:
(options.greaterVersionOnly && cmp === -1 && "lower") || diff || "none",
version: manifest.version.raw,
oldVersion: publishedVersion.raw,
tag: options.tag,
access:
options.access ||
(manifest.name.startsWith("@") ? "restricted" : "public"),
dryRun: options.dryRun,
};
options.debug("OUTPUT:", results);
return results;
}