-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpublish.mjs
105 lines (88 loc) · 3.09 KB
/
publish.mjs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* This is a minimal script to publish your package to "npm".
* This is meant to be used as-is or customize as you see fit.
*
* This script is executed on "dist/path/to/library" as "cwd" by default.
*
* You might need to authenticate with NPM before running this script.
*/
import { execSync } from 'child_process';
import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'fs';
import { join } from 'path';
import devkit from '@nx/devkit';
const { readCachedProjectGraph, workspaceRoot } = devkit;
function invariant(condition, message) {
if (!condition) {
console.error(message);
process.exit(1);
}
}
// Executing publish script: node path/to/publish.mjs {name} --version {version} --tag {tag}
// Default "tag" to "next" so we won't publish the "latest" tag by accident.
const [, , name, version, tag = 'next'] = process.argv;
// A simple SemVer validation to validate the version
const validVersion = /^\d+\.\d+\.\d+(-\w+\.\d+)?/;
invariant(
version && validVersion.test(version),
`No version provided or version did not match Semantic Versioning, expected: #.#.#-tag.# or #.#.#, got ${version}.`
);
const graph = readCachedProjectGraph();
const project = graph.nodes[name];
invariant(
project,
`Could not find project "${name}" in the workspace. Is the project.json configured correctly?`
);
const outputPath = project.data?.targets?.build?.options?.outputPath;
invariant(
outputPath,
`Could not find "build.options.outputPath" of project "${name}". Is project.json configured correctly?`
);
process.chdir(outputPath);
const sharedProperty = [
'license',
'contributors',
'repository',
'engines',
'private',
'files',
];
// Updating the version in "package.json" before publishing
try {
const mainJson = JSON.parse(
readFileSync(join(workspaceRoot, 'package.json')).toString()
);
const json = JSON.parse(readFileSync(`package.json`).toString());
json.version = version;
for (const props of sharedProperty) {
if (!mainJson[props] || json[props]) continue;
json[props] = mainJson[props];
}
removeDepFromOtherLib(graph, name, json);
writeFileSync(`package.json`, JSON.stringify(json, null, 2));
} catch (e) {
console.log(e);
console.error(`Error reading package.json file from library build output.`);
}
if (existsSync(join(workspaceRoot, outputPath, 'cjs', 'package.json'))) {
unlinkSync(join(workspaceRoot, outputPath, 'cjs', 'package.json'));
}
if (existsSync(join(workspaceRoot, outputPath, 'mjs', 'package.json'))) {
unlinkSync(join(workspaceRoot, outputPath, 'mjs', 'package.json'));
}
// Execute "npm publish" to publish
execSync(`npm publish --access public --tag ${tag}`);
function removeDepFromOtherLib(graph, name, json) {
const libsName = Object.values(graph.nodes)
.filter((i) => i.data.tags.includes('type:publish'))
.map((i) => i.data.metadata.js.packageName);
if (!('peerDependencies' in json)) return;
json['peerDependencies'] = Object.entries(json['peerDependencies']).reduce(
(acum, [name, value]) => {
if (libsName.includes(name)) {
acum[name] = `^${value}`;
}
return acum;
},
{}
);
}