Skip to content

chore(scripts): use spinnies for multi spinner #1365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions scripts/buildClients.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/* eslint-disable no-case-declarations */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that we should always go verbose mode now that we are "out" of the hard testing phase, wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean on the CI ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope everywhere, I feel like people doesn't know it can be verbose and actually most of the time you want to see what's going on

import { run } from './common';
import { getClientsConfigField, getLanguageFolder } from './config';
import { createSpinner } from './oraLog';
import { createSpinner } from './spinners';
import type { Generator, Language } from './types';

/**
* Build client for a language at the same time, for those who live in the same folder.
*/
async function buildClient(
language: Language,
gens: Generator[],
{ verbose }: { verbose: boolean; skipUtils: boolean }
gens: Generator[]
): Promise<void> {
const cwd = getLanguageFolder(language);
const spinner = createSpinner(`building '${language}'`, verbose).start();
const spinner = createSpinner(`building '${language}'`);
switch (language) {
case 'java':
await run(`./gradle/gradlew --no-daemon -p ${cwd} assemble`, {
verbose,
});
await run(`./gradle/gradlew --no-daemon -p ${cwd} assemble`);
break;
case 'php':
break;
Expand All @@ -32,7 +29,6 @@ async function buildClient(
);

await run(`yarn build:many '{${packageNames.join(',')},}'`, {
verbose: true,
cwd,
});

Expand All @@ -42,10 +38,7 @@ async function buildClient(
spinner.succeed();
}

export async function buildClients(
generators: Generator[],
options: { verbose: boolean; skipUtils: boolean }
): Promise<void> {
export async function buildClients(generators: Generator[]): Promise<void> {
const langs = [...new Set(generators.map((gen) => gen.language))];
const generatorsMap = generators.reduce((map, gen) => {
if (!(gen.language in map)) {
Expand All @@ -59,6 +52,6 @@ export async function buildClients(
}, {} as Record<Language, Generator[]>);

await Promise.all(
langs.map((lang) => buildClient(lang, generatorsMap[lang], options))
langs.map((lang) => buildClient(lang, generatorsMap[lang]))
);
}
29 changes: 12 additions & 17 deletions scripts/buildSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
run,
toAbsolutePath,
} from './common';
import { createSpinner } from './oraLog';
import { createSpinner } from './spinners';
import type { Spec } from './types';

const ALGOLIASEARCH_LITE_OPERATIONS = ['search', 'post'];
Expand Down Expand Up @@ -109,8 +109,8 @@ async function transformBundle({
}
}

async function lintCommon(verbose: boolean, useCache: boolean): Promise<void> {
const spinner = createSpinner('linting common spec', verbose).start();
async function lintCommon(useCache: boolean): Promise<void> {
const spinner = createSpinner('linting common spec');

let hash = '';
const cacheFile = toAbsolutePath(`specs/dist/common.cache`);
Expand All @@ -130,10 +130,10 @@ async function lintCommon(verbose: boolean, useCache: boolean): Promise<void> {
hash = newCache;
}

await run(`yarn specs:lint common`, { verbose });
await run(`yarn specs:lint common`);

if (hash) {
spinner.text = `storing common spec cache`;
spinner.text = 'storing common spec cache';
await fsp.writeFile(cacheFile, hash);
}

Expand Down Expand Up @@ -191,7 +191,6 @@ async function buildLiteSpec({
async function buildSpec(
spec: string,
outputFormat: string,
verbose: boolean,
useCache: boolean
): Promise<void> {
const isAlgoliasearch = spec === 'algoliasearch';
Expand All @@ -200,7 +199,7 @@ async function buildSpec(
const cacheFile = toAbsolutePath(`specs/dist/${spec}.cache`);
let hash = '';

const spinner = createSpinner(`starting '${spec}' spec`, verbose).start();
const spinner = createSpinner(`starting '${spec}' spec`);

if (useCache) {
spinner.text = `checking cache for '${specBase}'`;
Expand All @@ -223,13 +222,12 @@ async function buildSpec(

// First linting the base
spinner.text = `linting '${spec}' spec`;
await run(`yarn specs:fix ${specBase}`, { verbose });
await run(`yarn specs:fix ${specBase}`);

// Then bundle the file
const bundledPath = `specs/bundled/${spec}.${outputFormat}`;
await run(
`yarn openapi bundle specs/${specBase}/spec.yml -o ${bundledPath} --ext ${outputFormat}`,
{ verbose }
`yarn openapi bundle specs/${specBase}/spec.yml -o ${bundledPath} --ext ${outputFormat}`
);

// Add the correct tags to be able to generate the proper client
Expand All @@ -249,12 +247,10 @@ async function buildSpec(

// Validate and lint the final bundle
spinner.text = `validating '${spec}' bundled spec`;
await run(`yarn openapi lint specs/bundled/${spec}.${outputFormat}`, {
verbose,
});
await run(`yarn openapi lint specs/bundled/${spec}.${outputFormat}`);

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

if (hash) {
spinner.text = `storing '${spec}' spec cache`;
Expand All @@ -267,14 +263,13 @@ async function buildSpec(
export async function buildSpecs(
clients: string[],
outputFormat: 'json' | 'yml',
verbose: boolean,
useCache: boolean
): Promise<void> {
await fsp.mkdir(toAbsolutePath('specs/dist'), { recursive: true });

await lintCommon(verbose, useCache);
await lintCommon(useCache);

await Promise.all(
clients.map((client) => buildSpec(client, outputFormat, verbose, useCache))
clients.map((client) => buildSpec(client, outputFormat, useCache))
);
}
47 changes: 21 additions & 26 deletions scripts/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Argument, program } from 'commander';

import { buildClients } from '../buildClients';
import { buildSpecs } from '../buildSpecs';
import { CI, DOCKER, LANGUAGES } from '../common';
import { CI, DOCKER, LANGUAGES, setVerbose } from '../common';
import { ctsGenerateMany } from '../cts/generate';
import { runCts } from '../cts/runCts';
import { formatter } from '../formatter';
Expand Down Expand Up @@ -49,10 +49,6 @@ const flags = {
flag: '-s, --skip-cache',
description: 'skip cache checking to force building specs',
},
skipUtils: {
flag: '-su, --skip-utils',
description: 'skip utils build when building a JavaScript client',
},
outputType: {
flag: '-json, --output-json',
description: 'outputs the spec in JSON instead of yml',
Expand All @@ -76,10 +72,9 @@ program
interactive,
});

await generate(
generatorList({ language, client, clientList }),
Boolean(verbose)
);
setVerbose(Boolean(verbose));

await generate(generatorList({ language, client, clientList }));
}
);

Expand All @@ -92,23 +87,17 @@ buildCommand
.addArgument(args.clients)
.option(flags.verbose.flag, flags.verbose.description)
.option(flags.interactive.flag, flags.interactive.description)
.option(flags.skipUtils.flag, flags.skipUtils.description)
.action(
async (
langArg: LangArg,
clientArg: string[],
{ verbose, interactive, skipUtils }
) => {
async (langArg: LangArg, clientArg: string[], { verbose, interactive }) => {
const { language, client, clientList } = await prompt({
langArg,
clientArg,
interactive,
});

await buildClients(generatorList({ language, client, clientList }), {
verbose: Boolean(verbose),
skipUtils: Boolean(skipUtils),
});
setVerbose(Boolean(verbose));

await buildClients(generatorList({ language, client, clientList }));
}
);

Expand All @@ -131,13 +120,14 @@ buildCommand
interactive,
});

setVerbose(Boolean(verbose));

const outputFormat = outputJson ? 'json' : 'yml';

// ignore cache when building from cli
await buildSpecs(
client[0] === ALL ? clientList : client,
outputFormat,
Boolean(verbose),
!skipCache
);
}
Expand All @@ -160,10 +150,9 @@ ctsCommand
interactive,
});

await ctsGenerateMany(
generatorList({ language, client, clientList }),
Boolean(verbose)
);
setVerbose(Boolean(verbose));

await ctsGenerateMany(generatorList({ language, client, clientList }));
}
);

Expand All @@ -180,7 +169,9 @@ ctsCommand
interactive,
});

await runCts(language === ALL ? LANGUAGES : [language], Boolean(verbose));
setVerbose(Boolean(verbose));

await runCts(language === ALL ? LANGUAGES : [language]);
});

program
Expand All @@ -196,6 +187,8 @@ program
interactive,
});

setVerbose(false);

await playground({
language,
client: client[0],
Expand All @@ -209,7 +202,9 @@ program
.argument('folder', 'The folder to format')
.option(flags.verbose.flag, flags.verbose.description)
.action(async (language: string, folder: string, { verbose }) => {
await formatter(language, folder, verbose);
setVerbose(Boolean(verbose));

await formatter(language, folder);
});

program.parse();
30 changes: 17 additions & 13 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { remove } from 'fs-extra';
import openapiConfig from '../config/openapitools.json';
import releaseConfig from '../config/release.config.json';

import { createSpinner } from './oraLog';
import { createSpinner } from './spinners';
import type {
CheckForCache,
CheckForCacheOptions,
Expand Down Expand Up @@ -76,11 +76,11 @@ export const CLIENTS_JS_UTILS = [

export async function run(
command: string,
{ errorMessage, verbose, cwd }: RunOptions = {}
{ errorMessage, cwd }: RunOptions = {}
): Promise<string> {
const realCwd = path.resolve(ROOT_DIR, cwd ?? '.');
try {
if (verbose) {
if (isVerbose()) {
return (
(
await execa.command(command, {
Expand Down Expand Up @@ -187,8 +187,8 @@ export async function checkForCache({
return cache;
}

export async function buildCustomGenerators(verbose: boolean): Promise<void> {
const spinner = createSpinner('building custom generators', verbose).start();
export async function buildCustomGenerators(): Promise<void> {
const spinner = createSpinner('building custom generators');

const cacheFile = toAbsolutePath('generators/.cache');
const { cacheExists, hash } = await checkForCache({
Expand All @@ -203,9 +203,7 @@ export async function buildCustomGenerators(verbose: boolean): Promise<void> {
return;
}

await run('./gradle/gradlew --no-daemon -p generators assemble', {
verbose,
});
await run('./gradle/gradlew --no-daemon -p generators assemble');

if (hash) {
spinner.text = 'storing custom generators cache';
Expand All @@ -227,13 +225,10 @@ export async function emptyDirExceptForDotGit(dir: string): Promise<void> {
}
}

export async function runComposerUpdate(verbose: boolean): Promise<void> {
export async function runComposerUpdate(): Promise<void> {
if (!CI) {
await run(
'composer update --working-dir=clients/algoliasearch-client-php && composer dump-autoload --working-dir=clients/algoliasearch-client-php',
{
verbose,
}
'composer update --working-dir=clients/algoliasearch-client-php && composer dump-autoload --working-dir=clients/algoliasearch-client-php'
);
}
}
Expand Down Expand Up @@ -289,3 +284,12 @@ export function camelize(str: string, delimiter: string = '-'): string {
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}

let verbose = false;
export function setVerbose(v: boolean): void {
verbose = v;
}

export function isVerbose(): boolean {
return verbose;
}
Loading