Skip to content

Commit 0849ba4

Browse files
committed
feat(publish): add version auto-increment and publish checks
- Implement version auto-increment when version already exists in NPM - Add checks for published versions before publishing - Improve logging with version information
1 parent d093664 commit 0849ba4

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

libs/rnc-theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rnc-theme",
3-
"version": "0.0.92",
3+
"version": "0.0.93",
44
"funding": {
55
"type": "github",
66
"url": "https://github.com/masumrpg/react-native-components"

tools/scripts/publish-all.js

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const path = require('path');
77

88
const libsDir = path.join(__dirname, '../..', 'libs');
99

10+
// Cek folder library yang punya package.json
1011
const getLibDirs = () => {
1112
return fs.readdirSync(libsDir).filter((dir) => {
1213
const stat = fs.statSync(path.join(libsDir, dir));
@@ -17,23 +18,76 @@ const getLibDirs = () => {
1718
});
1819
};
1920

21+
// Cek apakah versi sudah publish di NPM
22+
const isVersionPublished = (pkgName, version) => {
23+
try {
24+
const result = execSync(`npm view ${pkgName}@${version} version`, {
25+
stdio: 'pipe',
26+
})
27+
.toString()
28+
.trim();
29+
return result === version;
30+
} catch {
31+
return false;
32+
}
33+
};
34+
35+
// Naikkan patch version (0.0.1 -> 0.0.2)
36+
const bumpPatchVersion = (version) => {
37+
const parts = version.split('.').map(Number);
38+
parts[2]++;
39+
return parts.join('.');
40+
};
41+
42+
// Auto-increment versi jika sudah pernah dipublish
43+
const autoIncrementVersion = (pkgPath) => {
44+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
45+
let currentVersion = pkg.version;
46+
47+
while (isVersionPublished(pkg.name, currentVersion)) {
48+
currentVersion = bumpPatchVersion(currentVersion);
49+
}
50+
51+
if (currentVersion !== pkg.version) {
52+
console.log(
53+
`🔁 Auto bumped version ${pkg.name}: ${pkg.version}${currentVersion}`
54+
);
55+
pkg.version = currentVersion;
56+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
57+
}
58+
59+
return pkg.name + '@' + pkg.version;
60+
};
61+
62+
// Build dan publish satu lib
2063
const publishLib = (lib) => {
64+
const libRoot = path.join(libsDir, lib);
65+
const pkgPath = path.join(libRoot, 'package.json');
66+
67+
const bumped = autoIncrementVersion(pkgPath);
68+
2169
const distPath = path.join(__dirname, '../..', 'libs', lib);
2270

2371
console.log(`📦 Building ${lib}...`);
24-
execSync(`npx nx build ${lib}`, { stdio: 'inherit' });
72+
execSync(`npx nx build ${lib} --skip-nx-cache`, { stdio: 'inherit' });
73+
74+
const distPkg = path.join(distPath, 'package.json');
75+
if (!fs.existsSync(distPkg)) {
76+
console.warn(`❌ No package.json found in ${distPath}, skipping`);
77+
return;
78+
}
2579

26-
console.log(`🚀 Publishing ${lib}...`);
80+
console.log(`🚀 Publishing ${bumped}...`);
2781
try {
2882
execSync(`npm publish ${distPath} --access public`, { stdio: 'inherit' });
29-
console.log(`✅ Published ${lib}`);
83+
console.log(`✅ Published ${bumped}`);
3084
} catch (err) {
31-
console.warn(`⚠️ Skipped ${lib}: ${err.message}`);
85+
console.warn(`⚠️ Skipped ${bumped}: ${err.message}`);
3286
}
3387
};
3488

89+
// Loop semua libs
3590
const libs = getLibDirs();
36-
3791
for (const lib of libs) {
3892
publishLib(lib);
3993
}

0 commit comments

Comments
 (0)