Skip to content

Commit

Permalink
feat(javascript): provide lite package (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Apr 14, 2022
1 parent 3bdcbcc commit fbc6674
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.2.1.0.1
8.0.2
29 changes: 29 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ runs:
'specs/common/**'
)}}
- name: Restore built algoliasearch-lite spec
if: ${{ inputs.job == 'cts' || inputs.job == 'client' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: specs/bundled/algoliasearch-lite.yml
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'specs/search/**',
'specs/common/**'
)}}
- name: Restore built sources spec
if: ${{ inputs.job == 'cts' || inputs.job == 'client' || inputs.job == 'codegen' }}
uses: actions/cache@v2
Expand Down Expand Up @@ -193,6 +205,23 @@ runs:
'clients/algoliasearch-client-javascript/packages/client-personalization/**'
)}}
- name: Restore built JavaScript algoliasearch-lite client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-javascript/packages/algoliasearch-lite
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/src/**',
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/model/**',
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/builds/**',
'clients/algoliasearch-client-javascript/packages/algoliasearch-lite/package.json',
'specs/bundled/algoliasearch-lite.yml',
'templates/javascript/**',
'generators/src/**'
)}}
- name: Restore built JavaScript search client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
Expand Down
26 changes: 13 additions & 13 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ jobs:
- name: Restore cache
uses: ./.github/actions/cache

- name: Cache '${{ matrix.client }}' bundled specs
- name: Cache '${{ matrix.client.name }}' bundled specs
id: cache
uses: actions/cache@v2
with:
path: specs/bundled/${{ matrix.client }}.yml
path: ${{ matrix.client.bundledPath }}
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
format('specs/{0}/**', matrix.client),
format('{0}/**', matrix.client.path),
'specs/common/**'
)}}
- name: Building '${{ matrix.client }}' specs
- name: Building '${{ matrix.client.name }}' specs
if: steps.cache.outputs.cache-hit != 'true'
run: yarn cli build specs ${{ matrix.client }}
run: yarn cli build specs ${{ matrix.client.name }}

client_javascript_common:
timeout-minutes: 10
Expand Down Expand Up @@ -169,14 +169,14 @@ jobs:
id: cache
uses: actions/cache@v2
with:
path: ${{ matrix.client.folder }}
path: ${{ matrix.client.path }}
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
format('{0}/src/**', matrix.client.folder),
format('{0}/model/**', matrix.client.folder),
format('{0}/builds/**', matrix.client.folder),
format('{0}/package.json', matrix.client.folder),
format('{0}/src/**', matrix.client.path),
format('{0}/model/**', matrix.client.path),
format('{0}/builds/**', matrix.client.path),
format('{0}/package.json', matrix.client.path),
format('specs/bundled/{0}.yml', matrix.client.name),
'templates/javascript/**',
'generators/src/**'
Expand Down Expand Up @@ -220,12 +220,12 @@ jobs:
id: cache
uses: actions/cache@v2
with:
path: ${{ matrix.client.folder }}
path: ${{ matrix.client.path }}
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
format('{0}/{1}/**', matrix.client.folder, matrix.client.name),
format('{0}/model/{1}/**', matrix.client.folder, matrix.client.name),
format('{0}/{1}/**', matrix.client.path, matrix.client.name),
format('{0}/model/{1}/**', matrix.client.path, matrix.client.name),
format('specs/bundled/{0}.yml', matrix.client.name),
'templates/java/**',
'generators/src/**'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
"maxSize": "7.05KB"
},
{
"path": "packages/algoliasearch-lite/dist/algoliasearch-lite.umd.browser.js",
"maxSize": "3.80KB"
},
{
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
"maxSize": "3.85KB"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

git_push.sh
.gitignore
2 changes: 1 addition & 1 deletion clients/algoliasearch-client-javascript/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function initPackagesConfig() {
}

return availableClients.flatMap((packageName) => {
const isAlgoliasearchClient = packageName.startsWith('algoliasearch');
const isAlgoliasearchClient = packageName === 'algoliasearch';
const commonConfig = {
package: packageName,
name: isAlgoliasearchClient
Expand Down
23 changes: 23 additions & 0 deletions openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@
"utilsPackageVersion": "0.0.5"
}
},
"javascript-algoliasearch-lite": {
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
"output": "#{cwd}/clients/algoliasearch-client-javascript/packages/algoliasearch-lite",
"glob": "specs/bundled/algoliasearch-lite.yml",
"gitHost": "algolia",
"gitUserId": "algolia",
"gitRepoId": "algoliasearch-client-javascript",
"reservedWordsMappings": "queryParameters=queryParameters,requestOptions=requestOptions",
"additionalProperties": {
"modelPropertyNaming": "original",
"supportsES6": true,
"npmName": "@experimental-api-clients-automation/algoliasearch-lite",
"buildFile": "algoliasearch-lite",
"apiName": "algoliasearchLite",
"capitalizedApiName": "AlgoliasearchLite",
"packageVersion": "0.0.5",
"packageName": "@experimental-api-clients-automation/algoliasearch-lite",
"utilsPackageVersion": "0.0.5"
}
},
"javascript-recommend": {
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
Expand Down
90 changes: 74 additions & 16 deletions scripts/buildSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ import { checkForCache, exists, run, toAbsolutePath } from './common';
import { createSpinner } from './oraLog';
import type { Spec } from './pre-gen/setHostsOptions';

const ALGOLIASEARCH_LITE_OPERATIONS = [
'search',
'multipleQueries',
'searchForFacetValues',
'post',
];

async function propagateTagsToOperations(
bundledPath: string
bundledPath: string,
client: string
): Promise<boolean> {
if (!(await exists(bundledPath))) {
throw new Error(`Bundled file not found ${bundledPath}.`);
Expand All @@ -17,17 +25,9 @@ async function propagateTagsToOperations(
await fsp.readFile(bundledPath, 'utf8')
) as Spec;

if (bundledSpec.tags.length === 0) {
throw new Error(
`No tags defined for ${bundledPath}, tags are required to properly generate a client.`
);
}

const tagsName = bundledSpec.tags.map((tag) => tag.name);

for (const pathMethods of Object.values(bundledSpec.paths)) {
for (const specMethod of Object.values(pathMethods)) {
specMethod.tags = tagsName;
specMethod.tags = [client];
}
}

Expand Down Expand Up @@ -74,22 +74,77 @@ async function lintCommon(verbose: boolean, useCache: boolean): Promise<void> {
spinner.succeed();
}

/**
* Creates a lite search spec with the `ALGOLIASEARCH_LITE_OPERATIONS` methods
* from the `search` spec.
*/
async function buildLiteSpec(
spec: string,
bundledPath: string,
outputFormat: string,
verbose: boolean
): Promise<void> {
const searchSpec = yaml.load(
await fsp.readFile(toAbsolutePath(bundledPath), 'utf8')
) as Spec;

searchSpec.paths = Object.entries(searchSpec.paths).reduce(
(acc, [path, operations]) => {
for (const [method, operation] of Object.entries(operations)) {
if (
method === 'post' &&
ALGOLIASEARCH_LITE_OPERATIONS.includes(operation.operationId)
) {
return { ...acc, [path]: { post: operation } };
}
}

return acc;
},
{} as Spec['paths']
);

const liteBundledPath = `specs/bundled/${spec}.${outputFormat}`;
await fsp.writeFile(toAbsolutePath(liteBundledPath), yaml.dump(searchSpec));

if (
!(await propagateTagsToOperations(toAbsolutePath(liteBundledPath), spec))
) {
throw new Error(
`Unable to propage tags to operations for \`${spec}\` spec.`
);
}

await run(`yarn specs:fix bundled/${spec}.${outputFormat}`, {
verbose,
});
}

async function buildSpec(
client: string,
spec: string,
outputFormat: string,
verbose: boolean,
useCache: boolean
): Promise<void> {
createSpinner(`'${client}' spec`, verbose).start().info();
const shouldBundleLiteSpec = spec === 'algoliasearch-lite';
const client = shouldBundleLiteSpec ? 'search' : spec;
const cacheFile = toAbsolutePath(`specs/dist/${client}.cache`);
let hash = '';

createSpinner(`'${client}' spec`, verbose).start().info();

if (useCache) {
const generatedFiles = [`bundled/${client}.yml`];

if (shouldBundleLiteSpec) {
generatedFiles.push(`bundled/${spec}.yml`);
}

const { cacheExists, hash: newCache } = await checkForCache(
{
job: `'${client}' specs`,
folder: toAbsolutePath('specs/'),
generatedFiles: [`bundled/${client}.yml`],
generatedFiles,
filesToCache: [client, 'common'],
cacheFile,
},
Expand All @@ -110,9 +165,7 @@ async function buildSpec(
{ verbose }
);

if (
(await propagateTagsToOperations(toAbsolutePath(bundledPath))) === false
) {
if (!(await propagateTagsToOperations(toAbsolutePath(bundledPath), client))) {
spinner.fail();
throw new Error(
`Unable to propage tags to operations for \`${client}\` spec.`
Expand All @@ -130,6 +183,11 @@ async function buildSpec(
spinner.text = `linting '${client}' bundled spec`;
await run(`yarn specs:fix bundled/${client}.${outputFormat}`, { verbose });

if (shouldBundleLiteSpec) {
spinner.text = `Building and linting '${spec}' spec`;
await buildLiteSpec(spec, bundledPath, outputFormat, verbose);
}

if (hash) {
spinner.text = `storing ${client} spec cache`;
await fsp.writeFile(cacheFile, hash);
Expand Down
35 changes: 29 additions & 6 deletions scripts/ci/createMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ type CreateMatrix = {
language?: Language;
};

type ClientMatrix = {
type BaseMatrix = {
name: string;
folder: string;
path: string;
};

type ClientMatrix = BaseMatrix & {
config?: string;
api?: string;
capitalizedName?: string;
};

type SpecMatrix = BaseMatrix & {
bundledPath: string;
};

type Matrix<TMatrix> = {
client: TMatrix[];
};
Expand Down Expand Up @@ -61,7 +68,7 @@ async function getClientMatrix({

const matchedGenerator: ClientMatrix = {
name: client,
folder: output,
path: output,
};

// Extra informations for the PHP matrix in order to properly scope the
Expand All @@ -84,8 +91,8 @@ async function getClientMatrix({
async function getSpecMatrix({
baseBranch,
baseChanged,
}: CreateMatrix): Promise<Matrix<string>> {
const matrix: Matrix<string> = { client: [] };
}: CreateMatrix): Promise<Matrix<SpecMatrix>> {
const matrix: Matrix<SpecMatrix> = { client: [] };

for (const client of CLIENTS) {
const specChanges = await getNbGitDiff({
Expand All @@ -97,7 +104,23 @@ async function getSpecMatrix({
continue;
}

matrix.client.push(client);
const spec = {
name: client,
path: `specs/${client}`,
bundledPath: `specs/bundled/${client}.yml`,
};

// The `algoliasearch-lite` spec is created by the `search` spec
if (client === 'algoliasearch-lite') {
matrix.client.push({
...spec,
path: 'specs/search',
});

continue;
}

matrix.client.push(spec);
}

return matrix;
Expand Down
Loading

0 comments on commit fbc6674

Please sign in to comment.