angular-auth-oidc-client
Version:
Angular Lib for OpenID Connect & OAuth2
159 lines • 7.34 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ANGULAR_JSON_FILENAME = void 0;
exports.getAngularWorkspace = getAngularWorkspace;
exports.updateProjectInAngularJson = updateProjectInAngularJson;
exports.getProject = getProject;
exports.readIntoSourceFile = readIntoSourceFile;
exports.getDefaultProjectName = getDefaultProjectName;
exports.getAngularJsonContent = getAngularJsonContent;
exports.isStandaloneSchematic = isStandaloneSchematic;
const schematics_1 = require("@angular-devkit/schematics");
const workspace_1 = require("@schematics/angular/utility/workspace");
const ts = require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript");
exports.ANGULAR_JSON_FILENAME = 'angular.json';
function getAngularWorkspace(tree) {
const workspaceContent = getAngularJsonContent(tree);
const workspace = JSON.parse(workspaceContent);
return workspace;
}
function updateProjectInAngularJson(tree, content, projectName) {
projectName = projectName || getDefaultProjectName(tree);
if (!projectName) {
throw new schematics_1.SchematicsException(`Could not Update Project in Angular.json because no project name was found.`);
}
const workspaceContent = getAngularJsonContent(tree);
const workspace = JSON.parse(workspaceContent);
workspace['projects'][projectName] = content;
tree.overwrite(exports.ANGULAR_JSON_FILENAME, JSON.stringify(workspace, null, 2));
}
function getProject(tree, projectName) {
const workspace = getAngularWorkspace(tree);
const defaultProject = getDefaultProjectName(tree);
if (!!projectName) {
return [defaultProject, workspace.projects[projectName] || null];
}
else if (!!defaultProject) {
return [defaultProject, workspace.projects[defaultProject]];
}
throw new schematics_1.SchematicsException(`Could not get project. Searched for '${projectName}',
but it could not be found and no default project is given in workspace - ${JSON.stringify(workspace.projects, null, 2)}`);
}
function readIntoSourceFile(host, fileName) {
const buffer = host.read(fileName);
if (buffer === null) {
throw new schematics_1.SchematicsException(`File ${fileName} does not exist.`);
}
return ts.createSourceFile(fileName, buffer.toString('utf-8'), ts.ScriptTarget.Latest, true);
}
function getDefaultProjectName(tree) {
const workspace = getAngularWorkspace(tree);
const allProjects = Object.keys(workspace.projects);
return allProjects[0];
}
function getAngularJsonContent(tree) {
const workspaceConfig = tree.read(exports.ANGULAR_JSON_FILENAME);
if (!workspaceConfig) {
throw new schematics_1.SchematicsException('Could not find Angular workspace configuration');
}
return workspaceConfig.toString();
}
// Taken from https://github.com/angular/components/blob/5f5c5160dc20331619fc6729aa2ad78ac84af1c3/src/cdk/schematics/utils/schematic-options.ts#L46
function isStandaloneSchematic(host, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const workspace = yield (0, workspace_1.getWorkspace)(host);
const project = getProjectFromWorkspace(workspace, getDefaultProjectName(host));
// not on an Angular version that supports standalone either.
if (!((_a = project.targets) === null || _a === void 0 ? void 0 : _a.has('build'))) {
return false;
}
return isStandaloneApp(host, getProjectMainFile(project));
});
}
// TODO: replace with the following when NG 15 supprt is dropped
// import { isStandaloneApp } from '@schematics/angular/utility/ng-ast-utils';
function isStandaloneApp(host, mainPath) {
const source = ts.createSourceFile(mainPath, host.readText(mainPath), ts.ScriptTarget.Latest, true);
const bootstrapCall = findBootstrapApplicationCall(source);
return bootstrapCall !== null;
}
function findBootstrapApplicationCall(sourceFile) {
const localName = findImportLocalName(sourceFile, 'bootstrapApplication', '@angular/platform-browser');
if (!localName) {
return null;
}
let result = null;
sourceFile.forEachChild(function walk(node) {
if (ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === localName) {
result = node;
}
if (!result) {
node.forEachChild(walk);
}
});
return result;
}
function findImportLocalName(sourceFile, name, moduleName) {
for (const node of sourceFile.statements) {
// Only look for top-level imports.
if (!ts.isImportDeclaration(node) ||
!ts.isStringLiteral(node.moduleSpecifier) ||
node.moduleSpecifier.text !== moduleName) {
continue;
}
// Filter out imports that don't have the right shape.
if (!node.importClause ||
!node.importClause.namedBindings ||
!ts.isNamedImports(node.importClause.namedBindings)) {
continue;
}
// Look through the elements of the declaration for the specific import.
for (const element of node.importClause.namedBindings.elements) {
if ((element.propertyName || element.name).text === name) {
// The local name is always in `name`.
return element.name.text;
}
}
}
return null;
}
function getProjectFromWorkspace(workspace, projectName) {
if (!projectName) {
throw new schematics_1.SchematicsException('Project name is required.');
}
const project = workspace.projects.get(projectName);
if (!project) {
throw new schematics_1.SchematicsException(`Could not find project in workspace: ${projectName}`);
}
return project;
}
function getProjectMainFile(project) {
const buildOptions = getProjectTargetOptions(project, 'build');
const mainPath = (buildOptions['browser'] || buildOptions['main']);
if (!mainPath) {
throw new schematics_1.SchematicsException(`Could not find the project main file inside of the ` +
`workspace config (${project.sourceRoot})`);
}
return mainPath;
}
function getProjectTargetOptions(project, buildTarget) {
var _a, _b;
const options = (_b = (_a = project.targets) === null || _a === void 0 ? void 0 : _a.get(buildTarget)) === null || _b === void 0 ? void 0 : _b.options;
if (!options) {
throw new schematics_1.SchematicsException(`Cannot determine project target configuration for: ${buildTarget}.`);
}
return options;
}
//# sourceMappingURL=angular-utils.js.map
;