Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1export const scriptTypes = ['seed', 'migration'] as const;
2
3type ScriptType = (typeof scriptTypes)[number];
4
5/**
6 * Generates the name of a script file, excluding the file extension and the
7 * ordering-related prefixes added by umzug. This function enforces a convention
8 * that allows us to identify whether a script is a seed and, if so, which
9 * environment it should run in.
10 *
11 * @param type - Whether this is a migration or seed
12 * @param env - If a seed, which environment the seed applies to.
13 * @param userNamePortion - The portion of the name provided by the user to
14 * actually describe what the script does.
15 */
16export function nameScript(
17 type: ScriptType,
18 env: string | undefined,
19 userNamePortion: string,
20) {
21 return `${userNamePortion}${type === 'seed' ? `.seed.${env}` : ''}`;
22}
23
24/**
25 * Returns whether the given script, based on its name as generated by
26 * {@see nameScript}, should run in the given environment.
27 */
28export function shouldRun(
29 env: string,
30 legalScriptFormats: readonly string[],
31 scriptName: string,
32) {
33 const anyEnvSeedFileRegex = new RegExp(
34 `\\.seed\\.[^\\.]+\\.(${legalScriptFormats.join('|')})$`,
35 );
36 const thisEnvSeedFileRegex = new RegExp(
37 `\\.seed\\.${env}\\.(${legalScriptFormats.join('|')})$`,
38 );
39
40 const isSeed = anyEnvSeedFileRegex.test(scriptName);
41 return !isSeed || thisEnvSeedFileRegex.test(scriptName);
42}