libs/sdk/src/node/workspace/generate/generate.ts
Properties |
name (Optional) | |
Type |
string
|
Description
|
Path where the new workspace will be generated |
preset (Optional) | |
Type |
GenerateWorkspacePreset
|
Description
|
Preset for the generated workspace |
import inquirer from 'inquirer';
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
import { dasherize } from '../../../lib/journey/utils/string-utils';
import { assertValidName } from '../../../lib/journey/utils/validate-parameters';
import { execAsync } from '../../journey/generate/utils/exec-async';
import { createWorkspaceEmpty } from './lib/create-workspace-empty';
import { createWorkspaceWithJourney } from './lib/create-workspace-with-journey';
import { createWorkspaceWithLibrary } from './lib/create-workspace-with-library';
export interface GenerateWorkspaceOptions {
/**
* Path where the new workspace will be generated
*/
name?: string;
/**
* Preset for the generated workspace
*/
preset?: GenerateWorkspacePreset;
}
export type GenerateWorkspacePreset = 'empty' | 'journey' | 'library';
export interface ProgressReport {
type: 'start' | 'success' | 'info' | 'warning';
text: string;
}
/**
* This function generates a new TALY Nx workspace.
*
* It returns an AsyncIterator that yields the generation progress.
*
*/
export async function* generateWorkspace(
options: GenerateWorkspaceOptions
): AsyncGenerator<ProgressReport, void> {
if (options.name) {
assertValidName(options.name);
} else {
const workspace = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Please type the name of the workspace:',
validate: (input) => {
if (input === '') return 'Please enter a workspace name';
try {
assertValidName(input);
} catch (e) {
return e;
}
return true;
}
}
]);
options.name = workspace.name as string;
}
// Placed right after receiving the value to fail fast if invalid
const normalizedOptions = {
...options,
name: dasherize(options.name)
};
const destinationDirectory = resolve(normalizedOptions.name);
if (existsSync(destinationDirectory)) {
throw Error(
`The directory ${destinationDirectory} already exists. Please create a workspace with a different name or remove the existing directory with the same name.`
);
}
if (!options.preset) {
const { preset } = await inquirer.prompt([
{
type: 'list',
name: 'preset',
message: 'How would you like your workspace to be generated?',
default: 'empty',
choices: [
{ name: 'Empty', value: 'empty' },
{ name: 'With a Journey', value: 'journey' },
{ name: 'With a Building Block Library', value: 'library' }
]
}
]);
options.preset = preset as GenerateWorkspacePreset;
}
switch (options.preset) {
case 'empty':
yield* createWorkspaceEmpty(destinationDirectory, normalizedOptions);
break;
case 'journey':
yield* createWorkspaceWithJourney(destinationDirectory, normalizedOptions);
break;
case 'library':
yield* createWorkspaceWithLibrary(destinationDirectory, normalizedOptions);
break;
}
// Commiting produces an error if done in Jenkins (nightly pipeline). Therefore skipping.
if (process.env['CI']) return;
await execAsync(`git add -A && git commit --amend --no-edit`, {
cwd: destinationDirectory
});
}