-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublish-package.js
144 lines (127 loc) · 3.89 KB
/
publish-package.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs');
const { execSync } = require('child_process');
require('dotenv').config();
const packagesDir = './packages';
const npmToken = process.env.NPM_TOKEN;
if (!npmToken) {
console.error('Error: Missing npm_token.');
process.exit(1);
}
function loginToNpm() {
try {
// Use the npm token to log in
execSync(`echo //registry.npmjs.org/:_authToken=${npmToken} > ~/.npmrc`);
console.log('Logged in to npm.');
} catch (error) {
console.error('Error logging in to npm:', error.message);
process.exit(1);
}
}
function getVersionFromPackageJson(packagePath) {
try {
const packageJsonContent = fs.readFileSync(
`${packagePath}/package.json`,
'utf8'
);
const packageJson = JSON.parse(packageJsonContent);
const version = packageJson.version;
return version;
} catch (error) {
console.error(
`Error reading package.json in ${packagePath}:`,
error.message
);
return null;
}
}
function isVersionPublished(packageName, version) {
try {
execSync(`npm view ${packageName}@${version}`, { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}
function publishWithAlphaTag(packagePath, version, packageName) {
try {
execSync('yarn && npm publish --tag alpha', {
stdio: 'inherit',
cwd: `${packagePath}`,
});
console.log('Package published successfully!');
console.log('::set-output name=published::true'); // Output
console.log(`::set-output name=packageName::${packageName}`); // Output
console.log(`::set-output name=packageVersion::${version}`); // Output
} catch (error) {
console.error(
`Error publishing with alpha tag in ${packagePath}:`,
error.message
);
console.log('::set-output name=published::false'); // Output
}
}
function publishWithLatestTag(packagePath, version, packageName) {
try {
execSync('npm publish --tag latest', {
stdio: 'inherit',
cwd: `${packagePath}`,
});
console.log('Package published successfully!');
console.log('::set-output name=published::true'); // Output
console.log(`::set-output name=packageName::${packageName}`); // Output
console.log(`::set-output name=packageVersion::${version}`); // Output
} catch (error) {
console.error(
`Error publishing with latest tag in ${packagePath}:`,
error.message
);
console.log('::set-output name=published::false'); // Output
}
}
function publishPackage(packagePath) {
const version = getVersionFromPackageJson(packagePath);
const packageName = require(`${packagePath}/package.json`).name;
if (!version) {
console.log(`Error: Version not found in package.json in ${packagePath}.`);
return;
}
if (isVersionPublished(packageName, version)) {
console.log(
`Version ${version} of ${packageName} is already published on npm. Skipping publish.`
);
return;
}
if (version.includes('alpha')) {
console.log(
`Alpha version detected in ${packageName}. Publishing with "alpha" tag.`
);
publishWithAlphaTag(packagePath, version, packageName);
} else {
console.log(
`Non-alpha version detected in ${packageName}. Publishing with "latest" tag.`
);
publishWithLatestTag(packagePath, version, packageName);
}
}
function publishAllPackages() {
try {
// const packageDirs = fs.readdirSync(packagesDir);
const packageDirs = [
'create-next-app-with-gluestack-ui',
'create-expo-app-with-gluestack-ui',
'create-react-native-app-with-gluestack-ui',
'utils',
'gluestack-ui',
];
for (const packageDir of packageDirs) {
const packagePath = `${packagesDir}/${packageDir}`;
publishPackage(packagePath);
}
} catch (error) {
console.error('Error reading packages directory:', error.message);
}
}
// Log in to npm with the provided token
loginToNpm();
// Call the function to publish all packages
publishAllPackages();