Skip to content

fix(scripts): prevent incorrect run conditions #731

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 3 commits into from
Jun 23, 2022
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"build:eslint": "yarn workspace eslint-plugin-automation-custom build && yarn install",
"clean": "rm -rf **/dist **/build **/node_modules **/.gradle **/vendor || true",
"cli": "yarn workspace scripts ts-node --transpile-only ./index.ts",
"cli": "yarn workspace scripts ts-node --transpile-only ./cli/index.ts",
"docker": "docker exec -it dev yarn cli $*",
"docker:build": "./scripts/docker/build.sh",
"docker:clean": "docker stop dev; docker rm -f dev; docker image rm -f api-clients-automation",
Expand Down
4 changes: 2 additions & 2 deletions scripts/buildClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ async function buildAllClients(

export async function buildClients(
generators: Generator[],
verbose: boolean
{ verbose, skipUtils }: { verbose: boolean; skipUtils: boolean }
): Promise<void> {
const langs = [...new Set(generators.map((gen) => gen.language))];

if (!CI && langs.includes('javascript')) {
if (!CI && !skipUtils && langs.includes('javascript')) {
const spinner = createSpinner(
"building 'JavaScript' utils",
verbose
Expand Down
220 changes: 220 additions & 0 deletions scripts/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import { Argument, program } from 'commander';

import { buildClients } from '../buildClients';
import { buildSpecs } from '../buildSpecs';
import { CI, DOCKER, LANGUAGES } from '../common';
import { ctsGenerateMany } from '../cts/generate';
import { runCts } from '../cts/runCts';
import { formatter } from '../formatter';
import { generate } from '../generate';
import { playground } from '../playground';

import type { Job, LangArg } from './utils';
import {
ALL,
getClientChoices,
generatorList,
prompt,
PROMPT_CLIENTS,
PROMPT_LANGUAGES,
} from './utils';

if (!CI && !DOCKER) {
// eslint-disable-next-line no-console
console.log('You should run scripts via the docker container, see README.md');
// eslint-disable-next-line no-process-exit
process.exit(1);
}

const args = {
language: new Argument('[language]', 'The language').choices(
PROMPT_LANGUAGES
),
clients: (job: Job): Argument =>
new Argument('[client...]', 'The client').choices(getClientChoices(job)),
client: new Argument('[client]', 'The client').choices(PROMPT_CLIENTS),
};

const flags = {
verbose: {
flag: '-v, --verbose',
description: 'make the generation verbose',
},
interactive: {
flag: '-i, --interactive',
description: 'open prompt to query parameters',
},
skipCache: {
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',
},
};

program.name('cli');

program
.command('generate')
.description('Generate a specified client')
.addArgument(args.language)
.addArgument(args.clients('generate'))
.option(flags.verbose.flag, flags.verbose.description)
.option(flags.interactive.flag, flags.interactive.description)
.action(
async (langArg: LangArg, clientArg: string[], { verbose, interactive }) => {
const { language, client, clientList } = await prompt({
langArg,
clientArg,
job: 'generate',
interactive,
});

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

const buildCommand = program.command('build');

buildCommand
.command('clients')
.description('Build a specified client')
.addArgument(args.language)
.addArgument(args.clients('build'))
.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 }
) => {
const { language, client, clientList } = await prompt({
langArg,
clientArg,
job: 'build',
interactive,
});

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

buildCommand
.command('specs')
.description('Build a specified spec')
.addArgument(args.clients('specs'))
.option(flags.verbose.flag, flags.verbose.description)
.option(flags.interactive.flag, flags.interactive.description)
.option(flags.skipCache.flag, flags.skipCache.description)
.option(flags.outputType.flag, flags.outputType.description)
.action(
async (
clientArg: string[],
{ verbose, interactive, skipCache, outputJson }
) => {
const { client, clientList } = await prompt({
langArg: ALL,
clientArg,
job: 'specs',
interactive,
});

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

// ignore cache when building from cli
await buildSpecs(
client[0] === ALL ? clientList : client,
outputFormat,
Boolean(verbose),
!skipCache
);
}
);

const ctsCommand = program.command('cts');

ctsCommand
.command('generate')
.description('Generate the CTS tests')
.addArgument(args.language)
.addArgument(args.clients('generate'))
.option(flags.verbose.flag, flags.verbose.description)
.option(flags.interactive.flag, flags.interactive.description)
.action(
async (langArg: LangArg, clientArg: string[], { verbose, interactive }) => {
const { language, client, clientList } = await prompt({
langArg,
clientArg,
job: 'generate',
interactive,
});

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

ctsCommand
.command('run')
.description('Run the tests for the CTS')
.addArgument(args.language)
.option(flags.verbose.flag, flags.verbose.description)
.option(flags.interactive.flag, flags.interactive.description)
.action(async (langArg: LangArg, { verbose, interactive }) => {
const { language } = await prompt({
langArg,
clientArg: [ALL],
job: 'generate',
interactive,
});

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

program
.command('playground')
.description('Run the playground')
.addArgument(args.language)
.addArgument(args.client)
.option(flags.interactive.flag, flags.interactive.description)
.action(async (langArg: LangArg, cliClient: string, { interactive }) => {
const { language, client } = await prompt({
langArg,
clientArg: [cliClient],
job: 'build',
interactive,
});

await playground({
language,
client: client[0],
});
});

program
.command('format')
.description('Format the specified folder for a specific language')
.addArgument(args.language)
.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);
});

program.parse();
141 changes: 141 additions & 0 deletions scripts/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import inquirer from 'inquirer';

import { CLIENTS, GENERATORS, LANGUAGES } from '../common';
import type { Generator, Language } from '../types';

export const ALL = 'all';
export const PROMPT_LANGUAGES = [ALL, ...LANGUAGES];
export const PROMPT_CLIENTS = [ALL, ...CLIENTS];

export type AllLanguage = Language | typeof ALL;
export type LangArg = AllLanguage | undefined;

export type PromptDecision = {
language: AllLanguage;
client: string[];
clientList: string[];
};

export type Job = 'build' | 'generate' | 'specs';

type Prompt = {
langArg: LangArg;
clientArg: string[];
job: Job;
interactive: boolean;
};

export function getClientChoices(job: Job, language?: LangArg): string[] {
const withoutAlgoliaSearch = PROMPT_CLIENTS.filter(
(client) => client !== 'algoliasearch'
);

if (!language) {
return job === 'specs' ? withoutAlgoliaSearch : PROMPT_CLIENTS;
}

const isJavaScript = language === ALL || language === 'javascript';

switch (job) {
// We don't need to build `lite` client as it's a subset of the `algoliasearch` one
case 'build':
// Only `JavaScript` provide a lite client, others can build anything but it.
if (isJavaScript) {
return PROMPT_CLIENTS.filter((client) => client !== 'lite');
}

return withoutAlgoliaSearch.filter((client) => client !== 'lite');
// `algoliasearch` is not built from specs, it's an aggregation of clients
case 'specs':
return withoutAlgoliaSearch;
case 'generate':
// Only `JavaScript` provide a lite client, others can build anything but it.
if (isJavaScript) {
return withoutAlgoliaSearch;
}

return withoutAlgoliaSearch.filter((client) => client !== 'lite');
default:
return PROMPT_CLIENTS;
}
}

export function generatorList({
language,
client,
clientList,
}: {
language: AllLanguage;
client: string[];
clientList: string[];
}): Generator[] {
const langsTodo = language === ALL ? LANGUAGES : [language];
const clientsTodo = client[0] === ALL ? clientList : client;

return langsTodo
.flatMap((lang) => clientsTodo.map((cli) => GENERATORS[`${lang}-${cli}`]))
.filter(Boolean);
}

export async function prompt({
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if anyone will ever use this feature ahah, I did it for fun but it should not become a pain to maintain

Copy link
Member Author

Choose a reason for hiding this comment

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

I sometimes do when I'm lazy to type things D:

langArg,
clientArg,
job,
interactive,
}: Prompt): Promise<PromptDecision> {
const decision: PromptDecision = {
client: [ALL],
language: ALL,
clientList: [],
};

if (!langArg) {
if (interactive) {
const { language } = await inquirer.prompt<PromptDecision>([
{
type: 'list',
name: 'language',
message: 'Select a language',
default: ALL,
choices: LANGUAGES,
},
]);

decision.language = language;
}
} else {
decision.language = langArg;
}

decision.clientList = getClientChoices(job, decision.language);

if (!clientArg || !clientArg.length) {
if (interactive) {
const { client } = await inquirer.prompt<{ client: string }>([
{
type: 'list',
name: 'client',
message: 'Select a client',
default: ALL,
choices: decision.clientList,
},
]);

decision.client = [client];
}
} else {
clientArg.forEach((client) => {
if (!decision.clientList.includes(client)) {
throw new Error(
`The '${clientArg}' client can't run with the given job: '${job}'.\n\nAllowed choices are: ${decision.clientList.join(
', '
)}`
);
}
});

decision.client = clientArg;
}

return decision;
}
Loading