libs/nx/src/generators/journey/generator.ts
Properties |
projectName | |
Type |
string
|
projectRoot | |
Type |
string
|
import { assertValidName } from '@allianz/taly-sdk';
import {
addDependenciesToPackageJson,
addProjectConfiguration,
generateFiles,
joinPathFragments,
Tree,
workspaceLayout
} from '@nx/devkit';
import { dasherize } from '@nx/devkit/src/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: '@angular-devkit/build-angular:dev-server',
options: {
buildTarget: `${options.projectName}:build-generated-app:development`
},
configurations: {
production: {
buildTarget: `${options.projectName}:build-generated-app:production`
}
}
},
'build-generated-app': {
executor: '@angular-devkit/build-angular:browser',
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-common/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/@aposin/ng-aquila/css/compatibility.css'
],
scripts: [],
crossOrigin: 'use-credentials'
},
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
}
}
}
});
}
export default async function generateJourney(tree: Tree, options: JourneyGeneratorSchema) {
const expandedOptions = expandOptions(options);
addProject(tree, expandedOptions);
addFiles(tree, expandedOptions);
// 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
return addDependenciesToPackageJson(tree, generatorDependencies.dependencies, {});
}