Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 5x 5x 5x 5x 5x 5x 5x 1x 5x 5x 5x 5x 5x 5x 15x 10x 5x 5x 5x 5x 1x 5x 1x 5x 5x 5x 5x 5x 5x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 5x | import { join, normalize, Path, workspaces } from '@angular-devkit/core';
import { Rule, SchematicsException, Tree, chain } from '@angular-devkit/schematics';
import Project, { IndentationText, QuoteKind, SourceFile, SyntaxKind, TypeGuards } from 'ts-simple-ast';
import { getApps, getWorkspace } from '../utils/backbase/package-utils';
import { readJsonFile } from './utils/json';
import { tryRule } from './utils/rules';
import { updateDevDependency } from './utils/package-json';
const manualProcess = `
Update each application's "environments/type.ts" file to make the "mockProviders" key optional and
add an extra "shared" environment configuration.
## 1. Update the "Environment" type to make "mockProviders" optional (add the "?")
export interface Environment {
readonly production: boolean;
readonly mockProviders?: Array<Provider>;
readonly bootstrap?: {
readonly pageModel: Item;
readonly services: ExternalServices;
};
}
This will require an update to the apps "AppModule" to handle the case where "mockProviders" is
undefined. In the apps "app/app.module.ts" file, add a default value (an empty array) where "mockProviders" is
spread into the module providers.
@NgModule({
...
providers: [...environment.mockProviders || []],
...
})
## 2. Create a new "shared" environment configuration file ("environments/environment.shared.ts")
import { createMocksInterceptor } from '@backbase/foundation-ang/data-http';
import { Environment } from './type';
export const environment: Environment = {
production: false,
mockProviders: [createMocksInterceptor()],
};
Add the new configuration to the "angular.json"
{
...
"projects": {
"example-app": {
...
"architect": {
...
"build": {
...
"configurations": {
...
"shared": {
"fileReplacements": [
{
"replace": "apps/tutorial-app/src/environments/environment.ts",
"with": "apps/tutorial-app/src/environments/environment.shared.ts"
}
],
}
}
...
## 3. Upgrade the "bb-ang" dev dependency
Install it as a dev dependency using the following command:
npm i --save-dev @bb-cli/bb-ang@3.6.0`;
const updateEnvironmentInterface = (typeFile: SourceFile): void => {
const environmentInterfaceDeclaration = typeFile.getInterface('Environment');
Iif (!environmentInterfaceDeclaration) {
throw new SchematicsException(`Interface "Environment" is not defined in ${typeFile.getFilePath()}`);
}
const mockProvidersProperty = environmentInterfaceDeclaration.getProperty('mockProviders');
if (mockProvidersProperty) {
mockProvidersProperty.setHasQuestionToken(true);
}
};
const removeMockProvidersKeyFromProd = (prodEnvironmentFile: SourceFile): void => {
const environmentVariable = prodEnvironmentFile.getVariableDeclarationOrThrow('environment');
const environmentValue = environmentVariable.getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
const mockProvidersProperty = environmentValue.getProperty('mockProviders');
Iif (!mockProvidersProperty || !TypeGuards.isPropertyAssignment(mockProvidersProperty)) {
throw new SchematicsException('Could not find expected "mockProviders" key in environment.prod.ts');
}
Iif (mockProvidersProperty.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression).getElements().length > 0) {
process.stdout.write(`
## Migration Note
It is not recommended to use "mockProviders" in a production environment configuration as it may unintentionally cause
fake data to appear in a production deployment. The production environment configuration in this project
('environment.prod.ts") includes configuration for "mockProviders". It is recommended that the mock data providers
configuration be moved to a more appropriate environment configuration (probably the default development environment or
the new "shared" environment).
`);
return;
}
const commaToken = mockProvidersProperty.getNextSiblingIfKind(SyntaxKind.CommaToken);
prodEnvironmentFile.removeText(
mockProvidersProperty.getPos(),
commaToken ? commaToken.getEnd() : mockProvidersProperty.getEnd(),
);
};
const provideDefaultMockProviders = (moduleFile: SourceFile): void => {
const decoratorArg = moduleFile.getClassOrThrow('AppModule').getDecoratorOrThrow('NgModule').getArguments()[0];
Iif (!TypeGuards.isObjectLiteralExpression(decoratorArg)) {
throw new SchematicsException('Argument to NgModule Decorator on AppModule expected to be an object literal');
}
const providersProperty = decoratorArg.getProperty('providers');
Iif (!providersProperty || !TypeGuards.isPropertyAssignment(providersProperty)) {
return;
}
const providers = providersProperty.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression);
const mockProviders = providers.getFirstDescendant((el) => {
if (!TypeGuards.isSpreadElement(el)) {
return false;
}
const spreadVariable = el.getFirstChildByKind(SyntaxKind.PropertyAccessExpression);
return !!spreadVariable && spreadVariable.getText() === `environment.mockProviders`;
});
Iif (!mockProviders) {
return;
}
mockProviders.replaceWithText('...environment.mockProviders || []');
};
const createSharedEnvironmentFile = (tree: Tree, path: Path): void => {
tree.create(
path,
`import { createMocksInterceptor } from '@backbase/foundation-ang/data-http';
import { Environment } from './type';
export const environment: Environment = {
production: false,
mockProviders: [createMocksInterceptor()],
};
`,
);
};
const addSharedEnvironmentConfiguration = (tree: Tree, projectName: string) => {
const content = readJsonFile<any>(tree, 'angular.json');
let configurations;
try {
configurations = content.projects[projectName].architect.build.configurations;
} catch (_) {
throw new SchematicsException(`Could not find build configurations for project ${projectName}`);
}
configurations.shared = {
fileReplacements: [
{
replace: `apps/${projectName}/src/environments/environment.ts`,
with: `apps/${projectName}/src/environments/environment.shared.ts`,
},
],
};
tree.overwrite('angular.json', JSON.stringify(content, null, 2));
return tree;
};
const updateEnvironmentTypes = (tree: Tree, project: workspaces.ProjectDefinition) => {
const astProject = new Project({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
quoteKind: QuoteKind.Single,
},
useVirtualFileSystem: true,
});
Iif (!project.sourceRoot) {
throw new SchematicsException('Project missing required "sourceRoot" property in angular.json');
}
const typeFilePath = join(normalize(project.sourceRoot), 'environments', 'type.ts');
const existingTypeTs = tree.read(typeFilePath);
Iif (!existingTypeTs) {
throw new SchematicsException(`Project missing expected "type.ts" file at ${typeFilePath}`);
}
const typeFile = astProject.createSourceFile(typeFilePath, existingTypeTs.toString('utf-8'));
updateEnvironmentInterface(typeFile);
tree.overwrite(typeFilePath, typeFile.getFullText());
const prodEnvironmentFilePath = join(normalize(project.sourceRoot), 'environments', 'environment.prod.ts');
const existingProdEnvironmentFile = tree.read(prodEnvironmentFilePath);
Iif (!existingProdEnvironmentFile) {
throw new SchematicsException(`Project missing expected "environment.prod.ts" file at ${prodEnvironmentFilePath}`);
}
const prodEnvironmentFile = astProject.createSourceFile(
prodEnvironmentFilePath,
existingProdEnvironmentFile.toString('utf-8'),
);
removeMockProvidersKeyFromProd(prodEnvironmentFile);
tree.overwrite(prodEnvironmentFilePath, prodEnvironmentFile.getFullText());
const appModuleFilePath = join(normalize(project.sourceRoot), 'app', 'app.module.ts');
const existingAppModule = tree.read(appModuleFilePath);
Iif (!existingAppModule) {
throw new SchematicsException(`Project missing expected "app.module.ts" file at ${appModuleFilePath}`);
}
const appModuleFile = astProject.createSourceFile(appModuleFilePath, existingAppModule.toString('utf-8'));
provideDefaultMockProviders(appModuleFile);
tree.overwrite(appModuleFilePath, appModuleFile.getFullText());
};
const createNewEnvironment = (tree: Tree, project: workspaces.ProjectDefinition, projectName: string) => {
Iif (!project.sourceRoot) {
throw new SchematicsException('Project missing required "sourceRoot" property in angular.json');
}
const sharedEnvironmentPath = join(normalize(project.sourceRoot), 'environments', 'environment.shared.ts');
Iif (tree.exists(sharedEnvironmentPath)) {
throw new SchematicsException(`Project already contains ${sharedEnvironmentPath}`);
}
createSharedEnvironmentFile(tree, sharedEnvironmentPath);
addSharedEnvironmentConfiguration(tree, projectName);
};
const updateEnvironments = async (tree: Tree) => {
const workspace = await getWorkspace(tree);
const apps = getApps(workspace);
for (const name of apps) {
const project = workspace.projects.get(name);
Iif (!project) {
continue;
}
updateEnvironmentTypes(tree, project);
createNewEnvironment(tree, project, name);
}
};
export default function (): Rule {
return tryRule(chain([updateEnvironments, updateDevDependency('@bb-cli/bb-ang', '~3.6.0')]), manualProcess);
}
|