-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extends plugin-helpers to be used for automating version changes
Signed-off-by: Miki <[email protected]>
- Loading branch information
Showing
11 changed files
with
220 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// @ts-ignore | ||
import { readFile, writeFile } from 'fs/promises'; | ||
import path from 'path'; | ||
import { createFailError } from '@osd/dev-utils'; | ||
import { FileUpdateContext, ObjectUpdateContext, VersionContext } from '../contexts'; | ||
|
||
export async function updateVersions({ | ||
log, | ||
sourceDir, | ||
newVersion, | ||
}: VersionContext): Promise<boolean | undefined> { | ||
if (!newVersion) throw createFailError('Missing new version value'); | ||
if (!/^\d+\.\d+\.\d+/.test(newVersion)) | ||
throw createFailError('The new version is not a valid semantic version'); | ||
|
||
const cleanVersion = newVersion.replace(/^(\d+\.\d+\.\d+)(-.*)?$/, '$1'); | ||
|
||
const updateManifestFile = updateFile({ | ||
log, | ||
file: path.join(sourceDir, 'opensearch_dashboards.json'), | ||
updates: { | ||
version: `${cleanVersion}.0`, | ||
opensearchDashboardsVersion: cleanVersion, | ||
}, | ||
}); | ||
|
||
const updatePackageJson = updateFile({ | ||
log, | ||
file: path.join(sourceDir, 'package.json'), | ||
updates: { | ||
version: `${cleanVersion}.0`, | ||
opensearchDashboards: { | ||
version: cleanVersion, | ||
templateVersion: cleanVersion, | ||
}, | ||
}, | ||
}); | ||
|
||
await Promise.all([updateManifestFile, updatePackageJson]); | ||
|
||
return true; | ||
} | ||
|
||
async function updateFile({ log, file, updates }: FileUpdateContext) { | ||
log.info('Updating', file); | ||
|
||
let json; | ||
|
||
try { | ||
json = JSON.parse(await readFile(file, 'utf8')); | ||
} catch (ex) { | ||
log.error(ex); | ||
throw createFailError(`Failed to parse ${file}`); | ||
} | ||
|
||
const context: ObjectUpdateContext = { | ||
original: json, | ||
updates, | ||
}; | ||
updateObject(context); | ||
|
||
try { | ||
await writeFile(file, JSON.stringify(json, null, 2), 'utf8'); | ||
} catch (ex) { | ||
log.error(ex); | ||
throw createFailError(`Failed to update ${file}`); | ||
} | ||
|
||
log.success(`Updated`, file); | ||
} | ||
|
||
// Copies values in `updates` onto `obj` only if the keys exist | ||
function updateObject({ original, updates }: ObjectUpdateContext) { | ||
for (const key in updates) { | ||
if (!updates[key]) continue; | ||
|
||
// If `key` is not found in `original`, just skip it | ||
if (key in original) { | ||
// If both are objects, merge them | ||
if (updates[key] === 'object' && typeof original[key] === 'object') { | ||
updateObject({ | ||
original: original[key], | ||
updates: updates[key], | ||
} as ObjectUpdateContext); | ||
} else { | ||
original[key] = updates[key]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters