Skip to content

Commit

Permalink
GLSP-1298: Add updateNext command (#1299)
Browse files Browse the repository at this point in the history
* GLSP-1298: Add `updateNext` command

Add `updateNext` command that serves as a replacement for the (currently broken) update:next scripts in GLSP repos.

It automatically scanns all workspace pacakges for `next` dependencies, resolves the latest `next` version for these dependencies and then updates the `yarn.lock` by temorary adding a `resolutions` block to the root package.json to enforece an update.

Fixes #1298

* Address review feedback
  • Loading branch information
tortmayr committed Apr 12, 2024
1 parent 886d2d0 commit b5aff4f
Show file tree
Hide file tree
Showing 6 changed files with 819 additions and 24 deletions.
16 changes: 16 additions & 0 deletions dev-packages/cli/README.md
Expand Up @@ -83,6 +83,22 @@ Options:
-h, --help display help for command
```

## updateNext

```console
$ glsp updateNext -h
Usage: glsp updateNext|u [options] [rootDir]

Updates all `next` dependencies in GLSP project to the latest version

Arguments:
rootDir The repository root (default: "<cwd>")

Options:
-v, --verbose Enable verbose (debug) log output (default: false)
-h, --help display help for command
```

## More information

For more information, please visit the [Eclipse GLSP Umbrella repository](https://github.com/eclipse-glsp/glsp) and the [Eclipse GLSP Website](https://www.eclipse.org/glsp/).
Expand Down
1 change: 1 addition & 0 deletions dev-packages/cli/package.json
Expand Up @@ -44,6 +44,7 @@
"commander": "^10.0.1",
"glob": "^10.3.10",
"node-fetch": "^2.6.11",
"node-jq": "^4.3.1",
"readline-sync": "^1.4.10",
"semver": "^7.5.1",
"shelljs": "^0.8.5"
Expand Down
4 changes: 3 additions & 1 deletion dev-packages/cli/src/app.ts
Expand Up @@ -17,6 +17,7 @@
import { CheckHeaderCommand } from './commands/check-header';
import { CoverageReportCommand } from './commands/coverage-report';
import { ReleaseCommand } from './commands/release/release';
import { UpdateNextCommand } from './commands/update-next';
import { baseCommand } from './util/command-util';

export const COMMAND_VERSION = '1.1.0-next';
Expand All @@ -26,6 +27,7 @@ const app = baseCommand() //
.name('glsp')
.addCommand(CoverageReportCommand)
.addCommand(ReleaseCommand)
.addCommand(CheckHeaderCommand);
.addCommand(CheckHeaderCommand)
.addCommand(UpdateNextCommand);

app.parse(process.argv);
2 changes: 1 addition & 1 deletion dev-packages/cli/src/commands/release/release.ts
Expand Up @@ -77,9 +77,9 @@ export async function release(
cliOptions: ReleaseCmdOptions
): Promise<void> {
try {
configureLogger(cliOptions.verbose);
LOGGER.debug('Cli options:', cliOptions);
configureShell({ silent: !cliOptions.verbose });
configureLogger(cliOptions.verbose);
checkGHCli();
const version = deriveVersion(releaseType, customVersion);
const options: ReleaseOptions = { ...cliOptions, component, releaseType, version };
Expand Down
128 changes: 128 additions & 0 deletions dev-packages/cli/src/commands/update-next.ts
@@ -0,0 +1,128 @@
/********************************************************************************
* Copyright (c) 2024 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as fs from 'fs';
import { glob } from 'glob';
import * as jq from 'node-jq';
import * as path from 'path';
import * as sh from 'shelljs';
import { baseCommand, configureShell } from '../util/command-util';
import { getUncommittedChanges } from '../util/git-util';
import { LOGGER, configureLogger } from '../util/logger';
import { validateGitDirectory } from '../util/validation-util';
export const UpdateNextCommand = baseCommand() //
.name('updateNext')
.alias('u')
.description('Updates all `next` dependencies in GLSP project to the latest version')
.argument('[rootDir]', 'The repository root', validateGitDirectory, process.cwd())
.option('-v, --verbose', 'Enable verbose (debug) log output', false)
.action(updateNext);

export async function updateNext(rootDir: string, options: { verbose: boolean }): Promise<void> {
configureLogger(options.verbose);
configureShell({ silent: true, fatal: true });

const rootPackage = path.join(rootDir, 'package.json');
if (getUncommittedChanges(rootDir).includes(rootPackage)) {
LOGGER.warn('Uncommitted changes in root `package.json`. Please commit or stash them before running this command.');
return;
}

configureShell({ silent: false, fatal: true });

LOGGER.info('Updating next dependencies ...');
rootDir = path.resolve(rootDir);
const packages = await getWorkspacePackages(rootDir);
LOGGER.debug(`Scanning ${packages.length} packages to derive resolutions`, packages);
const resolutions = await getResolutions(packages);
if (Object.keys(resolutions).length === 0) {
LOGGER.info('No next dependencies found');
return;
}
LOGGER.info('Upgrade and rebuild packages ...');
const packageJson = fs.readFileSync(path.join(rootDir, 'package.json'), 'utf-8');
LOGGER.debug('Updating package.json with resolutions', resolutions);
fs.writeFileSync(path.join(rootDir, 'package.json'), JSON.stringify({ ...JSON.parse(packageJson), resolutions }, undefined, 2));
LOGGER.debug('Running yarn install');
sh.exec('yarn install --ignore-scripts');
LOGGER.debug('Reverting package.json');
sh.exec('git checkout HEAD -- package.json');
LOGGER.debug('Rebuild to update yarn.lock');
sh.exec('yarn');
LOGGER.info('Upgrade successfully completed');
}

async function getWorkspacePackages(rootDir: string): Promise<string[]> {
const rootPackage = path.join(rootDir, 'package.json');
const packages = [rootPackage];
if (!fs.existsSync(rootPackage)) {
LOGGER.error('No package.json found in root directory');
process.exit(1);
}
const workspaces = await getWorkspaceConfig(rootPackage);
if (workspaces) {
workspaces
.map(workspace => `${workspace}/**/package.json`)
.forEach(pattern => {
glob.sync(pattern, {
cwd: rootDir,
ignore: ['**/node_modules/**']
}).forEach(packageJson => packages.push(path.join(rootDir, packageJson)));
});
}

return [...new Set(packages)];
}

async function getResolutions(packages: string[]): Promise<Record<string, string>> {
let dependencies: string[] = [];
for (const pkg of packages) {
const deps = await jq.run(
'.dependencies //{} + .devDependencies + .peerDependencies | with_entries(select(.value == "next")) | keys',
pkg,
{
output: 'json'
}
);
if (Array.isArray(deps)) {
dependencies.push(...deps);
}
}
dependencies = [...new Set(dependencies)];
LOGGER.debug(`Found ${dependencies.length} 'next' dependencies`, dependencies);
LOGGER.info('Retrieve next versions ... ');
const resolutions: Record<string, string> = {};
[...new Set(dependencies)].forEach(dep => {
LOGGER.info(`Retrieving next version for ${dep}`);
const version = sh.exec(`npm view ${dep}@next version`, { silent: true }).stdout.trim();
resolutions[`**/${dep}`] = version;
});
return resolutions;
}
async function getWorkspaceConfig(rootPackage: string): Promise<string[] | undefined> {
const result = await jq.run('.workspaces', rootPackage, { output: 'json' });
if (!result) {
return undefined;
}
if (Array.isArray(result)) {
return result;
}
if (typeof result === 'object' && 'packages' in result && Array.isArray(result.packages)) {
return result.packages;
}

return undefined;
}

0 comments on commit b5aff4f

Please sign in to comment.