-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,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(); |
This file contains hidden or 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,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({ | ||
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: