libs/nx/src/generators/journey/generator.ts
Properties |
|
| projectName | |
| Type |
string
|
| projectRoot | |
| Type |
string
|
| directory (Optional) | |
| Type |
string
|
| CLI Usage |
--directory="apps/oe/acm/my-journey"
|
|
Description
|
The root directory for the journey project. --directory="apps/oe/acm/my-journey" |
| name | |
| Type |
string
|
| CLI Usage |
--name="My Journey"
|
|
Description
|
The name of the journey to generate. It will be dasherized and used as project name. If the "directory" option is not specified, it is also used to construct the project's root path by joining the apps directory of your Nx workspace and the project name. Please type the name of the journey --name="My Journey" |
| splitJourney (Optional) | |
| Type |
boolean
|
| CLI Usage |
--splitJourney=true
|
| Default value |
false
|
|
Description
|
Whether the split journey config format should be used. More information on different config formats can be found here: https://taly.frameworks.allianz.io/additional-documentation/app-configuration.html#journey-config-files --splitJourney=true false |
import { assertValidName, splitJourney } from '@allianz/taly-sdk';
import {
addDependenciesToPackageJson,
addProjectConfiguration,
generateFiles,
GeneratorCallback,
joinPathFragments,
runTasksInSerial,
Tree,
workspaceLayout
} from '@nx/devkit';
import { dasherize } from '../../utils/string-utils';
import { join, posix } from 'path';
import generatorDependencies from './package.json';
import { JourneyGeneratorSchema } from './schema';
interface NormalizedSchema extends JourneyGeneratorSchema {
projectName: string;
projectRoot: string;
}
function expandOptions(options: JourneyGeneratorSchema): NormalizedSchema {
if (!(process.env['TALY_SKIP_CONFIG_VALIDATION'] === 'true')) {
assertValidName(options.name);
}
const projectName = dasherize(options.name);
const projectRoot =
options.directory || joinPathFragments(workspaceLayout().appsDir, projectName);
return {
...options,
projectName,
projectRoot
};
}
function addFiles(tree: Tree, options: NormalizedSchema) {
const pathToPagesSchema = posix.relative(
`${options.projectRoot}/config`,
'node_modules/@allianz/taly-sdk/schemas/pages-json.schema.json'
);
const pathToPfeSchema = posix.relative(
`${options.projectRoot}/config`,
'node_modules/@allianz/taly-sdk/schemas/pfe-json.schema.json'
);
const templateOptions = {
title: options.name,
pathToPagesSchema,
pathToPfeSchema
};
generateFiles(tree, join(__dirname, 'files'), options.projectRoot, templateOptions);
}
function addProject(tree: Tree, options: NormalizedSchema) {
const generatedRoot = `${options.projectRoot}/generated`;
const sourceRoot = `${generatedRoot}/src`;
addProjectConfiguration(tree, options.projectName, {
name: options.projectName,
root: options.projectRoot,
projectType: 'application',
sourceRoot: sourceRoot,
targets: {
develop: {
executor: '@allianz/taly-nx:generate-and-serve',
options: {
serveTarget: `${options.projectName}:serve-generated-app`
},
configurations: {
production: {
serveTarget: `${options.projectName}:serve-generated-app:production`
}
}
},
'generate-only': {
executor: '@allianz/taly-nx:generate'
},
'serve-generated-app': {
executor: '@nx/angular:dev-server',
options: {
buildTarget: `${options.projectName}:build-generated-app:development`
},
configurations: {
production: {
buildTarget: `${options.projectName}:build-generated-app:production`
}
}
},
'build-generated-app': {
executor: '@nx/angular:browser-esbuild',
outputs: ['{options.outputPath}'],
options: {
outputPath: `dist/${options.projectName}`,
index: `${sourceRoot}/index.html`,
main: `${sourceRoot}/main.ts`,
polyfills: [`${sourceRoot}/polyfills.ts`],
tsConfig: `${generatedRoot}/tsconfig.app.json`,
assets: [
`${sourceRoot}/favicon.ico`,
`${sourceRoot}/assets`,
`${sourceRoot}/journey.model.json`,
{
glob: '**/*',
input: 'node_modules/@allianz/taly-core/ui/assets/',
output: '/assets/'
}
],
styles: [
`${sourceRoot}/styles.scss`,
// TODO: fix for https://github.developer.allianz.io/ilt/taly-workspace/issues/295
// To be removed when building blocks are properly migrated.
// Related: https://github.developer.allianz.io/it-master-platform/itmp-frontend-workspace/issues/1917
'node_modules/@allianz/ng-aquila/css/compatibility.css'
],
crossOrigin: 'use-credentials',
allowedCommonJsDependencies: [
'@angular-devkit/core',
'@allianz/tracking',
'dayjs',
'deepmerge',
'fast-deep-equal/es6',
'jsonpath',
'static-eval',
'iban',
'debug',
'extend'
]
},
configurations: {
production: {
budgets: [
{
type: 'initial',
maximumWarning: '2mb',
maximumError: '5mb'
},
{
type: 'anyComponentStyle',
maximumWarning: '6kb',
maximumError: '10kb'
}
],
fileReplacements: [
{
replace: `${sourceRoot}/environments/environment.ts`,
with: `${sourceRoot}/environments/environment.prod.ts`
}
],
outputHashing: 'all'
},
development: {
buildOptimizer: false,
optimization: false,
vendorChunk: true,
extractLicenses: false,
sourceMap: true,
namedChunks: true
}
}
},
'extract-i18n': {
executor: '@allianz/taly-nx:extract-i18n',
options: {
buildTarget: `${options.projectName}:build-generated-app`,
outputPath: options.projectRoot
}
}
}
});
}
function updateGitignore(tree: Tree) {
const gitignorePath = '.gitignore';
const ignoreEntry = `generated/`;
const comment = '# Generator output';
if (tree.exists(gitignorePath)) {
const gitignoreContent = tree.read(gitignorePath, 'utf-8') || '';
const gitignoreContentLines = gitignoreContent.split('\n');
if (!gitignoreContentLines.includes(ignoreEntry)) {
const updatedContent =
gitignoreContent.length > 0
? [gitignoreContent.trim(), '', comment, ignoreEntry, ''].join('\n')
: [comment, ignoreEntry, ''].join('\n');
tree.write(gitignorePath, updatedContent);
}
} else {
tree.write(gitignorePath, [comment, ignoreEntry, ''].join('\n'));
}
}
export default async function generateJourney(tree: Tree, options: JourneyGeneratorSchema) {
const expandedOptions = expandOptions(options);
addProject(tree, expandedOptions);
addFiles(tree, expandedOptions);
updateGitignore(tree);
const generatorCallbacks: GeneratorCallback[] = [];
if (options?.splitJourney) {
const configDirectory = joinPathFragments(expandedOptions.projectRoot, 'config');
generatorCallbacks.push(() => splitJourney(configDirectory, true));
}
// Nx sets npm_config_legacy_peer_deps = true by default inside the `installPackagesTask` (https://github.com/nrwl/nx/blob/5ded713c3c487bd28874e75b1623ece07a79d91d/packages/nx/src/utils/package-manager.ts#L130)
// We have to explicitly set it to prevent the default behavior
process.env['npm_config_legacy_peer_deps'] ??= 'false';
// the GeneratorCallback that is returned by `addDependenciesToPackageJson()`
// lets Nx know that we (maybe) need to install new packages
generatorCallbacks.push(
addDependenciesToPackageJson(tree, generatorDependencies.dependencies, {})
);
return runTasksInSerial(...generatorCallbacks);
}