Skip to content

Commit

Permalink
Merge pull request #5143 from Tyriar/5135
Browse files Browse the repository at this point in the history
Publish commit and set peerDependencies to @xterm/xterm beta when publishing
  • Loading branch information
Tyriar authored Aug 31, 2024
2 parents 1b48cd0 + fd0bb58 commit 44f9fa3
Showing 1 changed file with 82 additions and 21 deletions.
103 changes: 82 additions & 21 deletions bin/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,35 @@ const os = require('os');
const path = require('path');

// Setup auth
fs.writeFileSync(`${process.env['HOME']}/.npmrc`, `//registry.npmjs.org/:_authToken=${process.env['NPM_AUTH_TOKEN']}`);
if (process.env['NPM_AUTH_TOKEN']) {
fs.writeFileSync(`${process.env['HOME']}/.npmrc`, `//registry.npmjs.org/:_authToken=${process.env['NPM_AUTH_TOKEN']}`);
}

log('Configuration:', 'green')
const isDryRun = process.argv.includes('--dry');
if (isDryRun) {
console.log('Publish dry run');
log(' Publish dry run');
}

const allAddons = process.argv.includes('--all-addons');
if (allAddons) {
log(' Publish all addons');
}

const repoCommit = getRepoCommit();
const changedFiles = getChangedFilesInCommit('HEAD');

// Publish xterm if any files were changed outside of the addons directory
let isStableRelease = false;
if (changedFiles.some(e => e.search(/^addons\//) === -1)) {
isStableRelease = checkAndPublishPackage(path.resolve(__dirname, '..'));
checkAndPublishPackage(path.resolve(__dirname, '../headless'));
}
// Always publish xterm, technically this isn't needed if
// `changedFiles.some(e => e.search(/^addons\//)`, but it's here for convenience to get the right
// peer dependencies for addons.
const result = checkAndPublishPackage(path.resolve(__dirname, '..'), repoCommit);
const isStableRelease = result.isStableRelease;
const peerDependencies = result.nextVersion.includes('beta') ? {
'@xterm/xterm': `^${result.nextVersion}`,
} : undefined;
checkAndPublishPackage(path.resolve(__dirname, '../headless'), repoCommit);

// Addon peer dependencies

// Publish addons if any files were changed inside of the addon
const addonPackageDirs = [
Expand All @@ -39,12 +53,11 @@ const addonPackageDirs = [
path.resolve(__dirname, '../addons/addon-web-links'),
path.resolve(__dirname, '../addons/addon-webgl')
];
console.log(`Checking if addons need to be published`);
log(`Checking if addons need to be published`, 'green');
for (const p of addonPackageDirs) {
const addon = path.basename(p);
if (changedFiles.some(e => e.includes(addon))) {
console.log(`Try publish ${addon}`);
checkAndPublishPackage(p);
if (allAddons || changedFiles.some(e => e.includes(addon))) {
checkAndPublishPackage(p, repoCommit, peerDependencies);
}
}

Expand All @@ -53,7 +66,7 @@ if (isStableRelease) {
updateWebsite();
}

function checkAndPublishPackage(packageDir) {
function checkAndPublishPackage(packageDir, repoCommit, peerDependencies) {
const packageJson = require(path.join(packageDir, 'package.json'));

// Determine if this is a stable or beta release
Expand All @@ -62,12 +75,29 @@ function checkAndPublishPackage(packageDir) {

// Get the next version
let nextVersion = isStableRelease ? packageJson.version : getNextBetaVersion(packageJson);
console.log(`Publishing version: ${nextVersion}`);
log(`Publishing version: ${nextVersion}`, 'green');

// Set the version in package.json
const packageJsonFile = path.join(packageDir, 'package.json');
packageJson.version = nextVersion;
console.log(`Set version of ${packageJsonFile} to ${nextVersion}`);

// Set the commit in package.json
if (repoCommit) {
packageJson.commit = repoCommit;
log(`Set commit of ${packageJsonFile} to ${repoCommit}`);
} else {
throw new Error(`No commit set`);
}

// Set peer dependencies
if (peerDependencies) {
packageJson.peerDependencies = peerDependencies;
log(`Set peerDependencies of ${packageJsonFile} to ${JSON.stringify(peerDependencies)}`);
} else {
log(`Skipping peerDependencies`);
}

// Write new package.json
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, null, 2));

// Publish
Expand All @@ -78,19 +108,27 @@ function checkAndPublishPackage(packageDir) {
if (isDryRun) {
args.push('--dry-run');
}
console.log(`Spawn: npm ${args.join(' ')}`);
log(`Spawn: npm ${args.join(' ')}`);
const result = cp.spawnSync('npm', args, {
cwd: packageDir,
stdio: 'inherit'
});
if (result.status) {
console.error(`Spawn exited with code ${result.status}`);
error(`Spawn exited with code ${result.status}`);
process.exit(result.status);
}

console.groupEnd();
return { isStableRelease, nextVersion };
}

return isStableRelease;
function getRepoCommit() {
const commitProcess = cp.spawnSync('git', ['log', '-1', '--format="%H"']);
if (commitProcess.stdout.length === 0 && commitProcess.stderr) {
const err = versionsProcess.stderr.toString();
throw new Error('Could not get repo commit\n' + err);
}
const output = commitProcess.stdout.toString().trim();
return output.replace(/^"/, '').replace(/"$/, '');
}

function getNextBetaVersion(packageJson) {
Expand Down Expand Up @@ -118,7 +156,11 @@ function asArray(value) {
}

function getPublishedVersions(packageJson, version, tag) {
const versionsProcess = cp.spawnSync(os.platform === 'win32' ? 'npm.cmd' : 'npm', ['view', packageJson.name, 'versions', '--json']);
const versionsProcess = cp.spawnSync(
os.platform === 'win32' ? 'npm.cmd' : 'npm',
['view', packageJson.name, 'versions', '--json'],
{ shell: true }
);
if (versionsProcess.stdout.length === 0 && versionsProcess.stderr) {
const err = versionsProcess.stderr.toString();
if (err.indexOf('404 Not Found - GET https://registry.npmjs.org/@xterm') > 0) {
Expand Down Expand Up @@ -152,10 +194,29 @@ function getChangedFilesInCommit(commit) {
}

function updateWebsite() {
console.log('Updating website');
log('Updating website', 'green');
const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'website-'));
const packageJson = require(path.join(path.resolve(__dirname, '..'), 'package.json'));
if (!isDryRun) {
cp.spawnSync('sh', [path.join(__dirname, 'update-website.sh'), packageJson.version], { cwd, stdio: [process.stdin, process.stdout, process.stderr] });
}
}

/**
* @param {string} message
*/
function log(message, color) {
let colorSequence = '';
switch (color) {
case 'green': {
colorSequence = '\x1b[32m';
break;
}
}
console.info([
`[\x1b[2m${new Date().toLocaleTimeString('en-GB')}\x1b[0m] `,
colorSequence,
message,
'\x1b[0m',
].join(''));
}

0 comments on commit 44f9fa3

Please sign in to comment.