From 23d4d2ccef031f65c7cfb3f12c4bdf6da5b39eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Tue, 14 Jun 2022 14:51:07 +0200 Subject: [PATCH 1/4] fix(scripts): bump playground in release process --- config/clients.config.json | 1 + .../codegen/AlgoliaJavaScriptGenerator.java | 1 + .../codegen/cts/AlgoliaCTSGenerator.java | 5 + scripts/release/common.ts | 29 +++++ scripts/release/updateAPIVersions.ts | 102 ++++++++++++++---- .../javascript/api/builds/imports.mustache | 4 +- templates/javascript/api/imports.mustache | 4 +- templates/javascript/browser.mustache | 4 +- templates/javascript/node.mustache | 4 +- templates/javascript/package.mustache | 6 +- .../javascript/tests/client/suite.mustache | 4 +- templates/javascript/tests/package.mustache | 4 +- .../tests/requests/requests.mustache | 6 +- 13 files changed, 135 insertions(+), 39 deletions(-) diff --git a/config/clients.config.json b/config/clients.config.json index 66bf0d5376..2a6aa6fe8b 100644 --- a/config/clients.config.json +++ b/config/clients.config.json @@ -13,6 +13,7 @@ }, "javascript": { "folder": "clients/algoliasearch-client-javascript", + "npmNamespace": "@experimental-api-clients-automation", "gitRepoId": "algoliasearch-client-javascript", "utilsPackageVersion": "0.5.0", "modelFolder": "model", diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaJavaScriptGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaJavaScriptGenerator.java index bfbc36d20f..7cc70b2392 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaJavaScriptGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaJavaScriptGenerator.java @@ -71,6 +71,7 @@ public Map postProcessOperationsWithModels(Map o try { Utils.generateServer((String) additionalProperties.get("client"), additionalProperties); additionalProperties.put("utilsPackageVersion", Utils.getClientConfigField("javascript", "utilsPackageVersion")); + additionalProperties.put("npmNamespace", Utils.getClientConfigField("javascript", "npmNamespace")); } catch (GeneratorException e) { e.printStackTrace(); System.exit(1); diff --git a/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCTSGenerator.java b/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCTSGenerator.java index 7972612653..992e8c30b1 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCTSGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCTSGenerator.java @@ -96,6 +96,11 @@ public Map postProcessSupportingFileData(Map obj bundle.put("hasRegionalHost", hasRegionalHost); bundle.put("defaultRegion", client.equals("predict") ? "ew" : "us"); bundle.put("lambda", lambda); + + if (language.equals("javascript")) { + bundle.put("npmNamespace", Utils.getClientConfigField(language, "npmNamespace")); + } + ctsManager.addDataToBundle(bundle); for (TestsGenerator testGen : testsGenerators) { diff --git a/scripts/release/common.ts b/scripts/release/common.ts index f8beb194f2..0bcd4214eb 100644 --- a/scripts/release/common.ts +++ b/scripts/release/common.ts @@ -1,3 +1,4 @@ +import fsp from 'fs/promises'; import path from 'path'; import config from '../../config/release.config.json'; @@ -45,3 +46,31 @@ export async function cloneRepository({ tempGitDir, }; } + +/** + * Reads a JSON file and returns its parsed data. + * + * @param ppath - The absolute path to the file. + */ +export async function readJsonFile( + ppath: string +): Promise> { + return JSON.parse( + await fsp.readFile(ppath, { + encoding: 'utf-8', + }) + ); +} + +/** + * Writes `data` in a file at the given `ppath`, appends a newline at the end of the file. + * + * @param ppath - The absolute path to the file. + * @param data - The data to store. + */ +export async function writeJsonFile( + ppath: string, + data: Record +): Promise { + await fsp.writeFile(ppath, JSON.stringify(data, null, 2).concat('\n')); +} diff --git a/scripts/release/updateAPIVersions.ts b/scripts/release/updateAPIVersions.ts index 210457486b..136136b7ca 100755 --- a/scripts/release/updateAPIVersions.ts +++ b/scripts/release/updateAPIVersions.ts @@ -16,6 +16,7 @@ import { LANGUAGES, MAIN_BRANCH, gitBranchExists, + CLIENTS_JS_UTILS, } from '../common'; import { getClientsConfigField, @@ -24,18 +25,49 @@ import { } from '../config'; import type { Language } from '../types'; +import { readJsonFile, writeJsonFile } from './common'; import type { Changelog, Versions, VersionsToRelease } from './types'; dotenv.config({ path: ROOT_ENV_PATH }); /** - * Bump each client version of the JavaScript client in openapitools.json. + * Bump each client version of the JavaScript client in workspace places and config files. * * We don't use the pre-computed `next` version for JavaScript, because the packages have independent versioning. */ async function updateVersionForJavascript( jsVersion: NonNullable ): Promise { + // Sets the new version of the utils package + const utilsPackageVersion = getClientsConfigField( + 'javascript', + 'utilsPackageVersion' + ); + const nextUtilsPackageVersion = semver.inc( + utilsPackageVersion, + jsVersion.releaseType + ); + + if (!nextUtilsPackageVersion) { + throw new Error( + `Failed to bump version ${utilsPackageVersion} by ${jsVersion.releaseType}.` + ); + } + + clientsConfig.javascript.utilsPackageVersion = nextUtilsPackageVersion; + + // update local playground deps + const nodePgPackageFile = await readJsonFile( + toAbsolutePath('playground/javascript/node/package.json') + ); + const browserPgPackageFile = await readJsonFile( + toAbsolutePath('playground/javascript/browser/package.json') + ); + + if (!nodePgPackageFile || !browserPgPackageFile) { + throw new Error('Failed to bump playground package files'); + } + // Sets the new version of the JavaScript client Object.values(GENERATORS) .filter((gen) => gen.language === 'javascript') @@ -45,39 +77,67 @@ async function updateVersionForJavascript( additionalProperties.packageVersion, jsVersion.releaseType ); + if (!newVersion) { throw new Error( `Failed to bump version ${additionalProperties.packageVersion} by ${jsVersion.releaseType}.` ); } + additionalProperties.packageVersion = newVersion; + + if (!additionalProperties.packageName) { + throw new Error( + `Package name is missing for JavaScript - ${gen.client}.` + ); + } + + if (nodePgPackageFile.dependencies[additionalProperties.packageName]) { + nodePgPackageFile.dependencies[additionalProperties.packageName] = + newVersion; + } + + if (browserPgPackageFile.dependencies[additionalProperties.packageName]) { + browserPgPackageFile.dependencies[additionalProperties.packageName] = + newVersion; + } }); - await fsp.writeFile( + CLIENTS_JS_UTILS.forEach((util) => { + const utilPackageName = `${clientsConfig.javascript.npmNamespace}/${util}`; + + if (nodePgPackageFile.dependencies[utilPackageName]) { + nodePgPackageFile.dependencies[utilPackageName] = nextUtilsPackageVersion; + } + + if (browserPgPackageFile.dependencies[utilPackageName]) { + browserPgPackageFile.dependencies[utilPackageName] = + nextUtilsPackageVersion; + } + }); + + // update `openapitools.json` config file + await writeJsonFile( toAbsolutePath('config/openapitools.json'), - JSON.stringify(openapiConfig, null, 2).concat('\n') + openapiConfig ); - // Sets the new version of the utils package - const utilsPackageVersion = getClientsConfigField( - 'javascript', - 'utilsPackageVersion' - ); - const nextUtilsPackageVersion = semver.inc( - utilsPackageVersion, - jsVersion.releaseType + // update `package.json` node playground file + await writeJsonFile( + toAbsolutePath('playground/javascript/node/package.json'), + nodePgPackageFile ); - if (!nextUtilsPackageVersion) { - throw new Error( - `Failed to bump version ${utilsPackageVersion} by ${jsVersion.releaseType}.` - ); - } + // update `package.json` browser playground file + await writeJsonFile( + toAbsolutePath('playground/javascript/browser/package.json'), + browserPgPackageFile + ); - clientsConfig.javascript.utilsPackageVersion = nextUtilsPackageVersion; - await fsp.writeFile( + // update `clients.config.json` file for the utils version + await writeJsonFile( toAbsolutePath('config/clients.config.json'), - JSON.stringify(clientsConfig, null, 2).concat('\n') + clientsConfig ); } @@ -97,9 +157,9 @@ async function updateConfigFiles( clientsConfig[lang].packageVersion = versionsToRelease[lang]!.next; }); - await fsp.writeFile( + await writeJsonFile( toAbsolutePath('config/clients.config.json'), - JSON.stringify(clientsConfig, null, 2).concat('\n') + clientsConfig ); } diff --git a/templates/javascript/api/builds/imports.mustache b/templates/javascript/api/builds/imports.mustache index d8f7499576..dc858669f9 100644 --- a/templates/javascript/api/builds/imports.mustache +++ b/templates/javascript/api/builds/imports.mustache @@ -1,5 +1,5 @@ -import type { InitClientOptions } from '@experimental-api-clients-automation/client-common'; -import { createMemoryCache, createFallbackableCache, createBrowserLocalStorageCache, createNullCache } from '@experimental-api-clients-automation/client-common'; +import type { InitClientOptions } from '{{{npmNamespace}}}/client-common'; +import { createMemoryCache, createFallbackableCache, createBrowserLocalStorageCache, createNullCache } from '{{{npmNamespace}}}/client-common'; import { create{{capitalizedApiName}}, apiClientVersion } from '../src/{{apiName}}'; import type { {{capitalizedApiName}} } from '../src/{{apiName}}'; diff --git a/templates/javascript/api/imports.mustache b/templates/javascript/api/imports.mustache index 231ba5ea36..9cd54b1a84 100644 --- a/templates/javascript/api/imports.mustache +++ b/templates/javascript/api/imports.mustache @@ -6,7 +6,7 @@ import { {{#isSearchClient}} createRetryablePromise, {{/isSearchClient}} -} from '@experimental-api-clients-automation/client-common'; +} from '{{{npmNamespace}}}/client-common'; import type { CreateClientOptions, Headers, @@ -17,7 +17,7 @@ import type { {{#isSearchClient}} CreateRetryablePromiseOptions, {{/isSearchClient}} -} from '@experimental-api-clients-automation/client-common'; +} from '{{{npmNamespace}}}/client-common'; {{#imports}} import { {{classname}} } from '{{filename}}'; diff --git a/templates/javascript/browser.mustache b/templates/javascript/browser.mustache index 680914cdc5..d9fb408163 100644 --- a/templates/javascript/browser.mustache +++ b/templates/javascript/browser.mustache @@ -1,5 +1,5 @@ -import { createXhrRequester } from '@experimental-api-clients-automation/requester-browser-xhr'; -import { DEFAULT_CONNECT_TIMEOUT_BROWSER, DEFAULT_READ_TIMEOUT_BROWSER, DEFAULT_WRITE_TIMEOUT_BROWSER } from '@experimental-api-clients-automation/client-common'; +import { createXhrRequester } from '{{{npmNamespace}}}/requester-browser-xhr'; +import { DEFAULT_CONNECT_TIMEOUT_BROWSER, DEFAULT_READ_TIMEOUT_BROWSER, DEFAULT_WRITE_TIMEOUT_BROWSER } from '{{{npmNamespace}}}/client-common'; {{> api/builds/imports}} export function {{apiName}}( diff --git a/templates/javascript/node.mustache b/templates/javascript/node.mustache index 0c00fe8699..98e7d4021f 100644 --- a/templates/javascript/node.mustache +++ b/templates/javascript/node.mustache @@ -1,5 +1,5 @@ -import { createHttpRequester } from '@experimental-api-clients-automation/requester-node-http'; -import { DEFAULT_CONNECT_TIMEOUT_NODE, DEFAULT_READ_TIMEOUT_NODE, DEFAULT_WRITE_TIMEOUT_NODE } from '@experimental-api-clients-automation/client-common'; +import { createHttpRequester } from '{{{npmNamespace}}}/requester-node-http'; +import { DEFAULT_CONNECT_TIMEOUT_NODE, DEFAULT_READ_TIMEOUT_NODE, DEFAULT_WRITE_TIMEOUT_NODE } from '{{{npmNamespace}}}/client-common'; {{> api/builds/imports}} export function {{apiName}}( diff --git a/templates/javascript/package.mustache b/templates/javascript/package.mustache index a9a43daa09..8980928634 100644 --- a/templates/javascript/package.mustache +++ b/templates/javascript/package.mustache @@ -21,9 +21,9 @@ "clean": "rm -rf ./dist" }, "dependencies": { - "@experimental-api-clients-automation/client-common": "{{utilsPackageVersion}}", - "@experimental-api-clients-automation/requester-browser-xhr": "{{utilsPackageVersion}}", - "@experimental-api-clients-automation/requester-node-http": "{{utilsPackageVersion}}" + "{{{npmNamespace}}}/client-common": "{{utilsPackageVersion}}", + "{{{npmNamespace}}}/requester-browser-xhr": "{{utilsPackageVersion}}", + "{{{npmNamespace}}}/requester-node-http": "{{utilsPackageVersion}}" }, "devDependencies": { "@types/node": "16.11.39", diff --git a/templates/javascript/tests/client/suite.mustache b/templates/javascript/tests/client/suite.mustache index 9bcf44b014..29bd98da11 100644 --- a/templates/javascript/tests/client/suite.mustache +++ b/templates/javascript/tests/client/suite.mustache @@ -1,8 +1,8 @@ /* eslint-disable @typescript-eslint/no-unused-vars, require-await */ // @ts-nocheck Failing tests will have type errors, but we cannot suppress them even with @ts-expect-error because it doesn't work for a block of lines. import { {{client}}, {{#lambda.titlecase}}{{client}}{{/lambda.titlecase}} } from '{{{import}}}'; -import { echoRequester } from '@experimental-api-clients-automation/requester-node-http'; -import type { EchoResponse } from '@experimental-api-clients-automation/requester-node-http'; +import { echoRequester } from '{{{npmNamespace}}}/requester-node-http'; +import type { EchoResponse } from '{{{npmNamespace}}}/requester-node-http'; const appId = 'test-app-id'; const apiKey = 'test-api-key'; diff --git a/templates/javascript/tests/package.mustache b/templates/javascript/tests/package.mustache index f4adb0638f..7d2ea7a07a 100644 --- a/templates/javascript/tests/package.mustache +++ b/templates/javascript/tests/package.mustache @@ -8,8 +8,8 @@ {{#packageDependencies}} "{{{packageName}}}": "{{packageVersion}}", {{/packageDependencies}} - "@experimental-api-clients-automation/client-common": "{{utilsPackageVersion}}", - "@experimental-api-clients-automation/requester-node-http": "{{utilsPackageVersion}}" + "{{{npmNamespace}}}/client-common": "{{utilsPackageVersion}}", + "{{{npmNamespace}}}/requester-node-http": "{{utilsPackageVersion}}" }, "devDependencies": { "@types/jest": "28.1.1", diff --git a/templates/javascript/tests/requests/requests.mustache b/templates/javascript/tests/requests/requests.mustache index 9a8253ccdf..338698a4fd 100644 --- a/templates/javascript/tests/requests/requests.mustache +++ b/templates/javascript/tests/requests/requests.mustache @@ -1,7 +1,7 @@ import { {{client}} } from '{{{import}}}'; -import { echoRequester } from '@experimental-api-clients-automation/requester-node-http'; -import type { EchoResponse } from '@experimental-api-clients-automation/client-common'; -import type { RequestOptions } from '@experimental-api-clients-automation/client-common'; +import { echoRequester } from '{{{npmNamespace}}}/requester-node-http'; +import type { EchoResponse } from '{{{npmNamespace}}}/client-common'; +import type { RequestOptions } from '{{{npmNamespace}}}/client-common'; const appId = process.env.ALGOLIA_APPLICATION_ID || 'test_app_id'; const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'test_api_key'; From 9fb79279604498638954e2faae2b76b9d8ac89cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Tue, 14 Jun 2022 15:52:43 +0200 Subject: [PATCH 2/4] update packageName logic --- config/openapitools.json | 30 +++++++-------------- scripts/common.ts | 3 +-- scripts/release/updateAPIVersions.ts | 2 +- scripts/types.ts | 1 - templates/javascript/index.mustache | 2 +- templates/javascript/package.mustache | 10 +++---- templates/javascript/tests/package.mustache | 2 +- 7 files changed, 19 insertions(+), 31 deletions(-) diff --git a/config/openapitools.json b/config/openapitools.json index 34a3893e96..4d13ff977b 100644 --- a/config/openapitools.json +++ b/config/openapitools.json @@ -6,8 +6,7 @@ "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-search", "reservedWordsMappings": "queryParameters=queryParameters,requestOptions=requestOptions", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-search", - "buildFile": "client-search", + "packageName": "client-search", "packageVersion": "0.5.0" } }, @@ -15,8 +14,7 @@ "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/algoliasearch-lite", "reservedWordsMappings": "queryParameters=queryParameters,requestOptions=requestOptions", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/algoliasearch-lite", - "buildFile": "algoliasearch-lite", + "packageName": "algoliasearch-lite", "packageVersion": "0.5.0" } }, @@ -24,64 +22,56 @@ "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/recommend", "reservedWordsMappings": "queryParameters=queryParameters", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/recommend", - "buildFile": "recommend", + "packageName": "recommend", "packageVersion": "0.5.0" } }, "javascript-personalization": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-personalization", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-personalization", - "buildFile": "client-personalization", + "packageName": "client-personalization", "packageVersion": "0.5.0" } }, "javascript-analytics": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-analytics", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-analytics", - "buildFile": "client-analytics", + "packageName": "client-analytics", "packageVersion": "0.5.0" } }, "javascript-insights": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-insights", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-insights", - "buildFile": "client-insights", + "packageName": "client-insights", "packageVersion": "0.5.0" } }, "javascript-abtesting": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-abtesting", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-abtesting", - "buildFile": "client-abtesting", + "packageName": "client-abtesting", "packageVersion": "0.5.0" } }, "javascript-query-suggestions": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-query-suggestions", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-query-suggestions", - "buildFile": "client-query-suggestions", + "packageName": "client-query-suggestions", "packageVersion": "0.5.0" } }, "javascript-sources": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-sources", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-sources", - "buildFile": "client-sources", + "packageName": "client-sources", "packageVersion": "0.5.0" } }, "javascript-predict": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-predict", "additionalProperties": { - "packageName": "@experimental-api-clients-automation/client-predict", - "buildFile": "client-predict", + "packageName": "client-predict", "packageVersion": "0.5.0" } }, diff --git a/scripts/common.ts b/scripts/common.ts index 86da81907e..62e6ac922c 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -41,8 +41,7 @@ export const GENERATORS: Record = { client: 'algoliasearch', key: 'javascript-algoliasearch', additionalProperties: { - buildFile: 'algoliasearch', - packageName: '@experimental-api-clients-automation/algoliasearch', + packageName: 'algoliasearch', packageVersion: getPackageVersionDefault('javascript'), }, }, diff --git a/scripts/release/updateAPIVersions.ts b/scripts/release/updateAPIVersions.ts index 136136b7ca..f11d61cb1d 100755 --- a/scripts/release/updateAPIVersions.ts +++ b/scripts/release/updateAPIVersions.ts @@ -65,7 +65,7 @@ async function updateVersionForJavascript( ); if (!nodePgPackageFile || !browserPgPackageFile) { - throw new Error('Failed to bump playground package files'); + throw new Error('Failed to read playground package files'); } // Sets the new version of the JavaScript client diff --git a/scripts/types.ts b/scripts/types.ts index 88e0e5c819..4dd4912a21 100644 --- a/scripts/types.ts +++ b/scripts/types.ts @@ -28,7 +28,6 @@ export type Generator = Record & { export type AdditionalProperties = Partial<{ packageName: string; - hasRegionalHost: boolean; }> & Record; diff --git a/templates/javascript/index.mustache b/templates/javascript/index.mustache index beb3e4bed0..443bbdb034 100644 --- a/templates/javascript/index.mustache +++ b/templates/javascript/index.mustache @@ -1,2 +1,2 @@ // eslint-disable-next-line import/no-commonjs,import/extensions -module.exports = require('./dist/{{buildFile}}.cjs.node.js'); +module.exports = require('./dist/{{packageName}}.cjs.node.js'); diff --git a/templates/javascript/package.mustache b/templates/javascript/package.mustache index 8980928634..1b2011d6d9 100644 --- a/templates/javascript/package.mustache +++ b/templates/javascript/package.mustache @@ -1,15 +1,15 @@ { - "name": "{{packageName}}", + "name": "{{{npmNamespace}}}/{{packageName}}", "version": "{{packageVersion}}", "description": "JavaScript client for {{packageName}}", "repository": "{{gitUserId}}/{{gitRepoId}}", "license": "MIT", "author": "Algolia", "main": "index.js", - "jsdelivr": "dist/{{buildFile}}.umd.browser.js", - "unpkg": "dist/{{buildFile}}.umd.browser.js", - "module": "dist/{{buildFile}}.esm.node.js", - "browser": "dist/{{buildFile}}.cjs.browser.js", + "jsdelivr": "dist/{{packageName}}.umd.browser.js", + "unpkg": "dist/{{packageName}}.umd.browser.js", + "module": "dist/{{packageName}}.esm.node.js", + "browser": "dist/{{packageName}}.cjs.browser.js", "types": "index.d.ts", "files": [ "dist", diff --git a/templates/javascript/tests/package.mustache b/templates/javascript/tests/package.mustache index 7d2ea7a07a..1d77d0be18 100644 --- a/templates/javascript/tests/package.mustache +++ b/templates/javascript/tests/package.mustache @@ -6,7 +6,7 @@ }, "dependencies": { {{#packageDependencies}} - "{{{packageName}}}": "{{packageVersion}}", + "{{{npmNamespace}}}/{{{packageName}}}": "{{packageVersion}}", {{/packageDependencies}} "{{{npmNamespace}}}/client-common": "{{utilsPackageVersion}}", "{{{npmNamespace}}}/requester-node-http": "{{utilsPackageVersion}}" From 3b601bec7b16de5cd8c354930839c448088d4e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Tue, 14 Jun 2022 17:35:18 +0200 Subject: [PATCH 3/4] remove packageName --- config/openapitools.json | 10 ------- .../cts/manager/JavaScriptCTSManager.java | 9 ++++-- scripts/buildClients.ts | 15 ++++++---- scripts/common.ts | 30 +++++++++---------- scripts/types.ts | 2 +- .../docs/contributing/add-new-api-client.md | 1 - 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/config/openapitools.json b/config/openapitools.json index 4d13ff977b..0b637f5cd6 100644 --- a/config/openapitools.json +++ b/config/openapitools.json @@ -6,7 +6,6 @@ "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-search", "reservedWordsMappings": "queryParameters=queryParameters,requestOptions=requestOptions", "additionalProperties": { - "packageName": "client-search", "packageVersion": "0.5.0" } }, @@ -14,7 +13,6 @@ "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/algoliasearch-lite", "reservedWordsMappings": "queryParameters=queryParameters,requestOptions=requestOptions", "additionalProperties": { - "packageName": "algoliasearch-lite", "packageVersion": "0.5.0" } }, @@ -22,56 +20,48 @@ "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/recommend", "reservedWordsMappings": "queryParameters=queryParameters", "additionalProperties": { - "packageName": "recommend", "packageVersion": "0.5.0" } }, "javascript-personalization": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-personalization", "additionalProperties": { - "packageName": "client-personalization", "packageVersion": "0.5.0" } }, "javascript-analytics": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-analytics", "additionalProperties": { - "packageName": "client-analytics", "packageVersion": "0.5.0" } }, "javascript-insights": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-insights", "additionalProperties": { - "packageName": "client-insights", "packageVersion": "0.5.0" } }, "javascript-abtesting": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-abtesting", "additionalProperties": { - "packageName": "client-abtesting", "packageVersion": "0.5.0" } }, "javascript-query-suggestions": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-query-suggestions", "additionalProperties": { - "packageName": "client-query-suggestions", "packageVersion": "0.5.0" } }, "javascript-sources": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-sources", "additionalProperties": { - "packageName": "client-sources", "packageVersion": "0.5.0" } }, "javascript-predict": { "output": "#{cwd}/clients/algoliasearch-client-javascript/packages/client-predict", "additionalProperties": { - "packageName": "client-predict", "packageVersion": "0.5.0" } }, diff --git a/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java index a634626215..5a0e3457ac 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java +++ b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java @@ -11,10 +11,14 @@ public class JavaScriptCTSManager implements CTSManager { private final String client; private final JsonNode openApiToolsConfig; + private final String packageName; public JavaScriptCTSManager(String client) { this.client = client; this.openApiToolsConfig = Utils.readJsonFile("config/openapitools.json").get("generator-cli").get("generators"); + + String output = this.openApiToolsConfig.get("javascript-" + client).get("output").asText(); + this.packageName = Utils.getClientConfigField("javascript", "npmNamespace") + "/" + output.substring(output.lastIndexOf('/') + 1); } @Override @@ -26,7 +30,7 @@ public void addSupportingFiles(List supportingFiles) { public void addDataToBundle(Map bundle) throws GeneratorException { bundle.put("packageDependencies", this.getPackageDependencies()); bundle.put("utilsPackageVersion", Utils.getClientConfigField("javascript", "utilsPackageVersion")); - bundle.put("import", openApiToolsConfig.get("javascript-" + client).get("additionalProperties").get("packageName").asText()); + bundle.put("import", this.packageName); } private List> getPackageDependencies() { @@ -38,8 +42,9 @@ private List> getPackageDependencies() { } JsonNode generator = field.getValue(); JsonNode additionalProperties = generator.get("additionalProperties"); - String packageName = additionalProperties.get("packageName").asText(); + String output = generator.get("output").asText(); String packageVersion = additionalProperties.get("packageVersion").asText(); + String packageName = output.substring(output.lastIndexOf('/') + 1); Map newEntry = new HashMap<>(); newEntry.put("packageName", packageName); diff --git a/scripts/buildClients.ts b/scripts/buildClients.ts index 1b7fe6f0d1..d9b7968382 100644 --- a/scripts/buildClients.ts +++ b/scripts/buildClients.ts @@ -1,5 +1,5 @@ import { CI, run } from './common'; -import { getLanguageFolder } from './config'; +import { getClientsConfigField, getLanguageFolder } from './config'; import { createSpinner } from './oraLog'; import type { Generator } from './types'; @@ -13,13 +13,18 @@ async function buildPerClient( verbose: boolean ): Promise { const spinner = createSpinner(`building ${key}`, verbose).start(); + const npmNamespace = getClientsConfigField(language, 'npmNamespace'); + switch (language) { case 'javascript': - await run(`yarn workspace ${additionalProperties?.packageName} clean`, { - verbose, - }); await run( - `SKIP_UTILS=true yarn workspace algoliasearch-client-javascript build ${additionalProperties?.packageName}`, + `yarn workspace ${npmNamespace}/${additionalProperties.packageName} clean`, + { + verbose, + } + ); + await run( + `SKIP_UTILS=true yarn workspace algoliasearch-client-javascript build ${npmNamespace}/${additionalProperties.packageName}`, { verbose } ); break; diff --git a/scripts/common.ts b/scripts/common.ts index 62e6ac922c..57d4439845 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -35,7 +35,7 @@ export const ROOT_DIR = path.resolve(process.cwd(), '..'); export const ROOT_ENV_PATH = path.resolve(ROOT_DIR, '.env'); export const GENERATORS: Record = { - // Default `algoliasearch` package as it's built similarly to generated clients + // Default `algoliasearch` package as it's an aggregation of generated clients 'javascript-algoliasearch': { language: 'javascript', client: 'algoliasearch', @@ -49,12 +49,23 @@ export const GENERATORS: Record = { // Build `GENERATORS` from the openapitools file Object.entries(openapiConfig['generator-cli'].generators).forEach( - ([key, gen]) => { + ([key, { output, ...gen }]) => { + const language = key.slice(0, key.indexOf('-')) as Language; + GENERATORS[key] = { + additionalProperties: {}, ...gen, - output: gen.output.replace('#{cwd}/', ''), - ...splitGeneratorKey(key), + output: output.replace('#{cwd}/', ''), + client: key.slice(key.indexOf('-') + 1), + language, + key, }; + + if (language === 'javascript') { + GENERATORS[key].additionalProperties.packageName = output.substring( + output.lastIndexOf('/') + 1 + ); + } } ); @@ -76,17 +87,6 @@ export const CLIENTS = CLIENTS_JS.filter( (client) => client !== 'algoliasearch' ); -/** - * Takes a generator key in the form 'language-client' and returns the Generator object. - */ -export function splitGeneratorKey( - generatorKey: string -): Pick { - const language = generatorKey.slice(0, generatorKey.indexOf('-')) as Language; - const client = generatorKey.slice(generatorKey.indexOf('-') + 1); - return { language, client, key: generatorKey }; -} - export function createGeneratorKey({ language, client, diff --git a/scripts/types.ts b/scripts/types.ts index 4dd4912a21..1c974c551c 100644 --- a/scripts/types.ts +++ b/scripts/types.ts @@ -23,7 +23,7 @@ export type Generator = Record & { language: Language; client: string; key: string; - additionalProperties?: AdditionalProperties; + additionalProperties: AdditionalProperties; }; export type AdditionalProperties = Partial<{ diff --git a/website/docs/contributing/add-new-api-client.md b/website/docs/contributing/add-new-api-client.md index 6f0b96acb4..698426fc1c 100644 --- a/website/docs/contributing/add-new-api-client.md +++ b/website/docs/contributing/add-new-api-client.md @@ -66,7 +66,6 @@ Below are the options you need to **make sure to define for your client**, other | Option | File | Language | Description | | --------------------- | :-------------------: | :---------------------: | -------------------------------------------------------------------------------------------------------------------- | | `output` | `openapitools.json` | All | The output path of the client. | -| `packageName` | `openapitools.json` | JavaScript | Name of the API package, used in [CTS](/docs/contributing/testing/common-test-suite). | | `packageVersion` | `openapitools.json` | JavaScript | The version you'd like to publish the first iteration of the generated client. It will be automatically incremented. | | `utilsPackageVersion` | `clients.config.json` | JavaScript | The version of the utils package. Every utils package should have synchronized version. | | `packageVersion` | `clients.config.json` | All (except JavaScript) | The version you'd like to publish the first iteration of the generated client. It will be automatically incremented. | From 41ef8064016664754240cbd076bc5b135540bc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Tue, 14 Jun 2022 17:36:31 +0200 Subject: [PATCH 4/4] remove duplicate --- .../codegen/cts/manager/JavaScriptCTSManager.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java index 5a0e3457ac..a7e0e5cdce 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java +++ b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCTSManager.java @@ -11,14 +11,10 @@ public class JavaScriptCTSManager implements CTSManager { private final String client; private final JsonNode openApiToolsConfig; - private final String packageName; public JavaScriptCTSManager(String client) { this.client = client; this.openApiToolsConfig = Utils.readJsonFile("config/openapitools.json").get("generator-cli").get("generators"); - - String output = this.openApiToolsConfig.get("javascript-" + client).get("output").asText(); - this.packageName = Utils.getClientConfigField("javascript", "npmNamespace") + "/" + output.substring(output.lastIndexOf('/') + 1); } @Override @@ -30,7 +26,10 @@ public void addSupportingFiles(List supportingFiles) { public void addDataToBundle(Map bundle) throws GeneratorException { bundle.put("packageDependencies", this.getPackageDependencies()); bundle.put("utilsPackageVersion", Utils.getClientConfigField("javascript", "utilsPackageVersion")); - bundle.put("import", this.packageName); + + String output = this.openApiToolsConfig.get("javascript-" + client).get("output").asText(); + String packageName = Utils.getClientConfigField("javascript", "npmNamespace") + "/" + output.substring(output.lastIndexOf('/') + 1); + bundle.put("import", packageName); } private List> getPackageDependencies() {