-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpublish.js
93 lines (79 loc) · 2.98 KB
/
publish.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
import { readdirSync } from 'fs';
import { spawn } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
// This publish process automatically replaces workspace:* with the actual version number of other packages currently in the repo
// The packages to publish – this will run in order and await each package before moving on
const packages = ['shaders', 'shaders-react'];
const isDryRun = process.argv.includes('--dry-run');
// Extract the tag value from the command line arguments
const tagArg = process.argv.find((arg) => arg.startsWith('--tag='));
const tag = tagArg ? tagArg.split('=')[1] : null;
if (tag) {
console.log(`Publishing with tag: ${tag}`);
} else {
console.log('No tag specified, publishing without a tag');
}
/** Stores the newest version of each package so we can replace it in package.json before publishing */
const packageVersionMap = {};
// Loop through all the packages and get the current version of each
for (const pkg of packages) {
const packageJson = JSON.parse(readFileSync(`packages/${pkg}/package.json`, 'utf8'));
// Get the name of the package
const name = packageJson.name;
const currentVersion = packageJson.version;
packageVersionMap[name] = currentVersion;
}
async function publish(pkg) {
const packagePath = `packages/${pkg}`;
console.log(`Publishing ${pkg}...`);
// ----- Update any workspace dependencies with the current version ----- //
const originalPackageJson = readFileSync(`${packagePath}/package.json`, 'utf8');
const packageJson = JSON.parse(originalPackageJson);
// Search the package.json for any packages in our packageVersionMap and replace the version with the current version
for (const [key, value] of Object.entries(packageJson.dependencies)) {
if (packageVersionMap[key]) {
packageJson.dependencies[key] = packageVersionMap[key];
}
}
// Write the updated package.json
writeFileSync(`${packagePath}/package.json`, JSON.stringify(packageJson, null, 2));
// ----- Publish the package ----- //
const args = ['publish', '--access', 'public'];
if (isDryRun) {
args.push('--dry-run');
}
if (tag) {
args.push('--tag', tag);
}
return new Promise((resolve, reject) => {
const child = spawn('npm', args, {
cwd: packagePath,
stdio: 'inherit',
});
child.on('close', (code) => {
if (code !== 0) {
console.log(`Skipping ${pkg}: Publication failed or package is already up to date`);
} else {
console.log(`Published ${pkg}`);
}
// Restore the original package.json to put back workspace:* dependencies
writeFileSync(`${packagePath}/package.json`, originalPackageJson);
resolve();
});
child.on('error', (error) => {
reject(error);
});
});
}
async function publishAll() {
try {
for (const pkg of packages) {
await publish(pkg);
}
console.log('All packages processed!');
} catch (error) {
console.error('An unexpected error occurred:', error);
process.exit(1);
}
}
publishAll();