Skip to content

Commit

Permalink
Merge pull request #1956 from timdeschryver/fix-ng-add
Browse files Browse the repository at this point in the history
feat(core): adds angular 18 support
  • Loading branch information
damienbod authored Jun 6, 2024
2 parents 11b97a4 + 5c4970b commit 981375f
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 83 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@
"ts-node": "^10.9.1",
"typescript": "~5.4.5"
}
}
}
8 changes: 4 additions & 4 deletions projects/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"license": "MIT",
"schematics": "./src/collection.json",
"dependencies": {
"@schematics/angular": "^16.0.0",
"@angular-devkit/core": "^16.0.0",
"@angular-devkit/schematics": "^16.0.0",
"typescript": "5.0.4"
"@schematics/angular": "^18.0.0",
"@angular-devkit/core": "^18.0.0",
"@angular-devkit/schematics": "^18.0.0",
"typescript": "~5.4.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NgAddOptions } from '../models/ng-add-options';

export function addModuleToImports(options: NgAddOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const project = getProject(host);
const [,project] = getProject(host);

const { moduleFileName, moduleName } = options.moduleInfo!;

Expand Down
82 changes: 12 additions & 70 deletions projects/schematics/src/ng-add/actions/add-standalone-import.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,20 @@
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import {
addFunctionalProvidersToStandaloneBootstrap,
callsProvidersFunction,
} from '@schematics/angular/private/standalone';
import { insertImport } from '@schematics/angular/utility/ast-utils';
import { InsertChange } from '@schematics/angular/utility/change';
import * as ts from 'typescript';
import { getProject, readIntoSourceFile } from '../../utils/angular-utils';
import { Rule, SchematicContext, Tree, chain } from '@angular-devkit/schematics';
import { addRootProvider } from '@schematics/angular/utility';
import { getProject } from '../../utils/angular-utils';
import { NgAddOptions } from '../models/ng-add-options';

export function addStandaloneConfigsToProviders(options: NgAddOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const project = getProject(host);

const { fileName, configName } = options.standaloneInfo!;

const standaloneConfigs = [
{
target: `${project.sourceRoot}/main.ts`,
configName,
configPath: `./auth/${fileName}`,
},
];

standaloneConfigs.forEach(({ target, configName, configPath }) => {
addProvider(host, context, configName, configPath, target);
});

const [projectName] = getProject(host);
const { fileName } = options.standaloneInfo!;

context.logger.info(`✅️ All imports done, please add the 'provideRouter()' as well if you don't have it provided yet.`);

return host;
return chain([
addRootProvider(projectName, ({code, external}) => {
external('authConfig', `./auth/${fileName}`);
return code`${external('provideAuth', 'angular-auth-oidc-client')}(authConfig)`;
}),
]);
};
}

function addProvider(host: Tree, context: SchematicContext, configName: string, configPath: string, target: string) {
const sourcefile = readIntoSourceFile(host, target);
const providerFn = 'provideAuth';
if (callsProvidersFunction(host, sourcefile.fileName, providerFn)) {
// exit because the store config is already provided
return host;
}

const patchedConfigFile = addFunctionalProvidersToStandaloneBootstrap(
host,
sourcefile.fileName,
providerFn,
'angular-auth-oidc-client',
[ts.factory.createIdentifier('authConfig')]
);

const configFileContent = host.read(patchedConfigFile);
const source = ts.createSourceFile(
patchedConfigFile,
configFileContent?.toString('utf-8') || '',
ts.ScriptTarget.Latest,
true
);


const change = insertImport(
source as any, // Angular uses the TS 5.1 compiler internally for schematics?
patchedConfigFile,
configName,
configPath
);

const recorder = host.beginUpdate(patchedConfigFile);

if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}

host.commitUpdate(recorder);

return host;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function addSilentRenewHtmlToAssetsArrayInAngularJson(ngAddOptions: NgAdd
return host;
}

const project = getProject(host);
const [,project] = getProject(host);

const options = project.architect?.build?.options;
const srcRoot = project.sourceRoot;
Expand Down
2 changes: 1 addition & 1 deletion projects/schematics/src/ng-add/actions/copy-module-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { AUTH_0, AZURE_AD_REFRESH_TOKENS, AZURE_AD_SILENT_RENEW, DEFAULT_CONFIG,

export function copyModuleFile(options: NgAddOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const project = getProject(host);
const [,project] = getProject(host);

const { moduleFileName, filesFolder } = options.moduleInfo!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function copySilentRenewHtmlToRoot(options: NgAddOptions): Rule {
return host;
}

const project = getProject(host);
const [,project] = getProject(host);
const templateSource = apply(url(`./files/silent-renew`), [move(normalize(`${project.sourceRoot}`))]);

return chain([mergeWith(templateSource)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { AUTH_0, AZURE_AD_REFRESH_TOKENS, AZURE_AD_SILENT_RENEW, DEFAULT_CONFIG,

export function copyStandaloneFile(options: NgAddOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const project = getProject(host);
const [,project] = getProject(host);

const { fileName, filesFolder } = options.standaloneInfo!;

Expand Down
6 changes: 3 additions & 3 deletions projects/schematics/src/utils/angular-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export function updateProjectInAngularJson(tree: Tree, content: WorkspaceProject
tree.overwrite(ANGULAR_JSON_FILENAME, JSON.stringify(workspace, null, 2));
}

export function getProject(tree: Tree, projectName?: string): WorkspaceProject {
export function getProject(tree: Tree, projectName?: string): [string, WorkspaceProject] {
const workspace = getAngularWorkspace(tree);
const defaultProject = getDefaultProjectName(tree);

if (!!projectName) {
return workspace.projects[projectName as string] || null;
return [defaultProject, workspace.projects[projectName as string] || null];
} else if (!!defaultProject) {
return workspace.projects[defaultProject as string];
return [defaultProject, workspace.projects[defaultProject as string]];
}

throw new SchematicsException(`Could not get project. Searched for '${projectName}',
Expand Down

0 comments on commit 981375f

Please sign in to comment.