Skip to content

Commit

Permalink
Prettier code for test lint check
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmevichu committed Mar 31, 2024
1 parent fb857e9 commit 2154f0f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 100 deletions.
81 changes: 43 additions & 38 deletions buildutils/src/get-latest-lab-version.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
function extractVersionFromReleases(releases: any, versionTag: string): string | null {
for (const release of releases) {
const tagName: string = release['tag_name'];
if (versionTag === "latest") {
if (!release['prerelease'] && !release['draft']) {
return tagName;
}
} else if (versionTag === tagName) {
return tagName;
}
function extractVersionFromReleases(
releases: any,
versionTag: string
): string | null {
for (const release of releases) {
const tagName: string = release['tag_name'];
if (versionTag === 'latest') {
if (!release['prerelease'] && !release['draft']) {
return tagName;
}
} else if (versionTag === tagName) {
return tagName;
}
return null;
}
return null;
}

async function findVersion(versionTag: string): Promise<string> {
const url: string = `https://api.github.com/repos/jupyterlab/jupyterlab/releases`;
const response = await fetch(url);
if (!response.ok) {
const error_message: string = `Failed to fetch package.json from ${url}. HTTP status code: ${response.status}`;
throw new Error(error_message);
}
const releases: any = await response.json();
const version: string | null = extractVersionFromReleases(releases, versionTag);
if (version === null) {
const error_message: string = "Invalid release tag";
throw new Error(error_message);
}
return version.substring(1);
const url = 'https://api.github.com/repos/jupyterlab/jupyterlab/releases';
const response = await fetch(url);
if (!response.ok) {
const error_message = `Failed to fetch package.json from ${url}. HTTP status code: ${response.status}`;
throw new Error(error_message);
}
const releases: any = await response.json();
const version: string | null = extractVersionFromReleases(
releases,
versionTag
);
if (version === null) {
const error_message = 'Invalid release tag';
throw new Error(error_message);
}
return version.substring(1);
}


async function getLatestLabVersion(): Promise<void> {
const args: string[] = process.argv.slice(2);
if (args.length !== 2 || args[0] !== '--set-version') {
console.error('Usage: node script.js --set-version <version>');
process.exit(1);
}
const version_tag: string = args[1];
const args: string[] = process.argv.slice(2);
if (args.length !== 2 || args[0] !== '--set-version') {
console.error('Usage: node script.js --set-version <version>');
process.exit(1);
}
const version_tag: string = args[1];

try {
const result: string = await findVersion(version_tag);
console.log(result);
} catch (error:any) {
console.error('Error:', error.message);
process.exit(1);
}
try {
const result: string = await findVersion(version_tag);
console.log(result);
} catch (error: any) {
console.error('Error:', error.message);
process.exit(1);
}
}

getLatestLabVersion();
130 changes: 68 additions & 62 deletions buildutils/src/upgrade-lab-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,100 @@ import fs from 'fs';
import path from 'path';

const PACKAGE_JSON_PATHS: string[] = [
"app/package.json",
"buildutils/package.json",
"package.json",
"packages/application-extension/package.json",
"packages/application/package.json",
"packages/console-extension/package.json",
"packages/docmanager-extension/package.json",
"packages/documentsearch-extension/package.json",
"packages/help-extension/package.json",
"packages/lab-extension/package.json",
"packages/notebook-extension/package.json",
"packages/terminal-extension/package.json",
"packages/tree-extension/package.json",
"packages/tree/package.json",
"packages/ui-components/package.json",
'app/package.json',
'buildutils/package.json',
'package.json',
'packages/application-extension/package.json',
'packages/application/package.json',
'packages/console-extension/package.json',
'packages/docmanager-extension/package.json',
'packages/documentsearch-extension/package.json',
'packages/help-extension/package.json',
'packages/lab-extension/package.json',
'packages/notebook-extension/package.json',
'packages/terminal-extension/package.json',
'packages/tree-extension/package.json',
'packages/tree/package.json',
'packages/ui-components/package.json',
];

const DEPENDENCY_GROUP: string = "@jupyterlab";
const DEPENDENCY_GROUP = '@jupyterlab';

async function updatePackageJson(newVersion: string): Promise<void> {
const url: string = `https://raw.githubusercontent.com/jupyterlab/jupyterlab/v${newVersion}/jupyterlab/staging/package.json`;
const response = await fetch(url);
const url = `https://raw.githubusercontent.com/jupyterlab/jupyterlab/v${newVersion}/jupyterlab/staging/package.json`;
const response = await fetch(url);

if (!response.ok) {
const errorMessage: string = `Failed to fetch package.json from ${url}. HTTP status code: ${response.status}`;
throw new Error(errorMessage);
}
if (!response.ok) {
const errorMessage = `Failed to fetch package.json from ${url}. HTTP status code: ${response.status}`;
throw new Error(errorMessage);
}

const newPackageJson = await response.json();
const newPackageJson = await response.json();

for (const packageJsonPath of PACKAGE_JSON_PATHS) {
const filePath: string = path.resolve(packageJsonPath);
const existingPackageJson = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
for (const packageJsonPath of PACKAGE_JSON_PATHS) {
const filePath: string = path.resolve(packageJsonPath);
const existingPackageJson = JSON.parse(fs.readFileSync(filePath, 'utf-8'));

const newDependencies = {
...newPackageJson.devDependencies,
...newPackageJson.resolutions
};
const newDependencies = {
...newPackageJson.devDependencies,
...newPackageJson.resolutions,
};

updateDependencyVersion(existingPackageJson, newDependencies);
updateDependencyVersion(existingPackageJson, newDependencies);

fs.writeFileSync(filePath, JSON.stringify(existingPackageJson, null, 2));
}
fs.writeFileSync(filePath, JSON.stringify(existingPackageJson, null, 2));
}
}

function updateDependencyVersion(existingJson: any, newJson: any): void {
if (!existingJson) {
return;
if (!existingJson) {
return;
}

const sectionPaths: string[] = [
'resolutions',
'dependencies',
'devDependencies',
];

for (const section of sectionPaths) {
if (!existingJson[section]) {
continue;
}

const sectionPaths: string[] = ["resolutions", "dependencies", "devDependencies"];
const updated = existingJson[section];

for (const section of sectionPaths) {
if (!existingJson[section]) {
continue;
}

const updated = existingJson[section];

for (const [pkg, version] of Object.entries<string>(existingJson[section])) {
if (pkg.startsWith(DEPENDENCY_GROUP) && pkg in newJson) {
if (version[0] === '^' || version[0] === '~') {
updated[pkg] = version[0] + absoluteVersion(newJson[pkg]);
} else {
updated[pkg] = absoluteVersion(newJson[pkg]);
}
}
for (const [pkg, version] of Object.entries<string>(
existingJson[section]
)) {
if (pkg.startsWith(DEPENDENCY_GROUP) && pkg in newJson) {
if (version[0] === '^' || version[0] === '~') {
updated[pkg] = version[0] + absoluteVersion(newJson[pkg]);
} else {
updated[pkg] = absoluteVersion(newJson[pkg]);
}
}
}
}
}

function absoluteVersion(version: string): string {
if (version.length > 0 && (version[0] === '^' || version[0] === '~')) {
return version.substring(1);
}
return version;
if (version.length > 0 && (version[0] === '^' || version[0] === '~')) {
return version.substring(1);
}
return version;
}

async function upgradeLabDependencies(): Promise<void> {
const args: string[] = process.argv.slice(2);
const args: string[] = process.argv.slice(2);

if (args.length !== 2 || args[0] !== '--set-version') {
console.error('Usage: node script.js --set-version <version>');
process.exit(1);
}
if (args.length !== 2 || args[0] !== '--set-version') {
console.error('Usage: node script.js --set-version <version>');
process.exit(1);
}

const newVersion: string = args[1];
await updatePackageJson(newVersion);
const newVersion: string = args[1];
await updatePackageJson(newVersion);
}

upgradeLabDependencies();

0 comments on commit 2154f0f

Please sign in to comment.