forked from lidofinance/lido-nestjs-modules
-
Notifications
You must be signed in to change notification settings - Fork 2
/
npmPublish.js
68 lines (55 loc) · 2.21 KB
/
npmPublish.js
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
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const packagesDir = './packages';
const packageFolders = fs.readdirSync(packagesDir);
const includePackage = ['constants'];
packageFolders.forEach((packageFolder) => {
const packagePath = path.join(packagesDir, packageFolder);
if (!includePackage.includes(packagePath.split('/')[1])) return;
const packageJsonPath = path.join(packagePath, 'package.json');
if (fs.existsSync(packageJsonPath)) {
const packageJson = require('./' + packageJsonPath);
const packageName = packageJson.name;
let currentVersion = packageJson.version;
// Get the current version from npm
const currentPublishedVersion = execSync(`npm show ${packageName} version`)
.toString()
.trim();
const currentPublishedVersionParts = currentPublishedVersion.split('.');
const versionParts = currentVersion.split('.');
let newVersion = '';
currentVersion = currentPublishedVersion;
if (
+currentPublishedVersionParts[0] < +versionParts[0] ||
+currentPublishedVersionParts[1] < +versionParts[1] ||
+currentPublishedVersionParts[2] < +versionParts[1]
) {
newVersion = `${versionParts[0]}.${versionParts[1]}.${+versionParts[2]}`;
} else {
console.log(
`${packageName} is already at version ${currentPublishedVersion}.`,
);
newVersion = `${currentPublishedVersionParts[0]}.${
currentPublishedVersionParts[1]
}.${parseInt(currentPublishedVersionParts[2]) + 1}`;
}
packageJson.version = newVersion;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
try {
const publishCommand = `npm publish`;
// Execute the npm publish command
execSync(publishCommand, { cwd: './' + packagePath, stdio: 'inherit' });
console.log(
`Publishing ${packageName}@${packageJson.version} in ${packagePath}`,
);
} catch (error) {
console.log(error);
console.error(
`Failed to publish ${packageName}@${packageJson.version} in ${packagePath}`,
);
packageJson.version = currentVersion;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
}
});