Skip to content

Commit

Permalink
test: run ng-add in example apps (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored Oct 23, 2024
1 parent d5f7d25 commit 4144c73
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Expression, SourceFile, SyntaxKind } from 'ts-morph'
import {
ArrayLiteralExpression,
CallExpression,
SourceFile,
SyntaxKind,
} from 'ts-morph'
import { Log } from './utils.js'
import { PROVIDERS_PROPERTY } from './constants.js'

Expand All @@ -12,29 +17,18 @@ export function addAppConfigProvidersFromTemplateIntoSourceFile({
const [templateProviders, destinationProviders] = [template, destination].map(
getAppConfigProvidersFromSourceFileOrThrow,
)
const destinationProviderExpressionsAsTexts = destinationProviders
.getElements()
.map((expression) => expression.getText())
const providersToAdd = templateProviders
.getElements()
.reduce<ReadonlyArray<Expression>>((accumulator, templateExpression) => {
// Skip if exists
if (
destinationProviderExpressionsAsTexts.includes(
templateExpression.getText(),
)
) {
return accumulator
}
// Add from template
return [...accumulator, templateExpression]
}, [])
destinationProviders.addElements(providersToAdd.map((p) => p.getText()))
removeProvidersSpecifiedInTemplate({
templateProviders,
destinationProviders,
})
addProvidersFromTemplate({ templateProviders, destinationProviders })
}

const APP_CONFIG_VARIABLE_NAME = 'appConfig'

function getAppConfigProvidersFromSourceFileOrThrow(sourceFile: SourceFile) {
function getAppConfigProvidersFromSourceFileOrThrow(
sourceFile: SourceFile,
): ArrayLiteralExpression {
const variableStatement = sourceFile.getVariableStatementOrThrow(
APP_CONFIG_VARIABLE_NAME,
)
Expand All @@ -59,3 +53,38 @@ function getAppConfigProvidersFromSourceFileOrThrow(sourceFile: SourceFile) {
SyntaxKind.ArrayLiteralExpression,
)
}

function removeProvidersSpecifiedInTemplate({
templateProviders,
destinationProviders,
}: {
templateProviders: ArrayLiteralExpression
destinationProviders: ArrayLiteralExpression
}) {
const destinationCallExpressions =
destinationProviders.getElements() as CallExpression[]
const templateCallExpressionsExpressionsAsTexts = (
templateProviders.getElements() as CallExpression[]
).map((callExpression) => callExpression.getExpression().getText())
destinationCallExpressions.forEach((destinationCallExpression) => {
if (
templateCallExpressionsExpressionsAsTexts.includes(
destinationCallExpression.getExpression().getText(),
)
) {
destinationProviders.removeElement(destinationCallExpression)
}
})
}

function addProvidersFromTemplate({
templateProviders,
destinationProviders,
}: {
templateProviders: ArrayLiteralExpression
destinationProviders: ArrayLiteralExpression
}) {
destinationProviders.addElements(
templateProviders.getElements().map((p) => p.getText()),
)
}
4 changes: 3 additions & 1 deletion projects/ngx-meta/example-apps/src/create-example-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { copyTemplates } from './copy-templates.js'
import { updateTsConfigToImportJsonFilesAndSetPathMappings } from './update-ts-config-to-import-json-files-and-set-path-mappings.js'
import { updateAppModuleOrAppConfigFromTemplates } from './update-app-module-or-app-config-from-templates.js'
import { isStandaloneDefaultForVersion } from './is-standalone-default-for-version.js'
import { ngAddLibrary } from './ng-add-library.js'

async function createExampleApp({
angularCliVersion,
Expand Down Expand Up @@ -53,9 +54,10 @@ async function createExampleApp({
})(),
copyTemplates({ appDir, standalone }),
updateTsConfigToImportJsonFilesAndSetPathMappings(appDir),
updateAppModuleOrAppConfigFromTemplates(appDir, standalone),
])
await install({ projectDir: appDir, what: 'app dependencies' })
await ngAddLibrary(appDir)
await updateAppModuleOrAppConfigFromTemplates(appDir, standalone)
}

if (isMain(import.meta.url)) {
Expand Down
27 changes: 27 additions & 0 deletions projects/ngx-meta/example-apps/src/ng-add-library.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getLibraryDistDir, Log } from './utils.js'
import { execa } from './execa.js'
import { readPackageJsonInDir } from './read-package-json-in-dir.js'

export const ngAddLibrary = async (appDir: string) => {
Log.step(`Running ng-add to set up library`)
const libraryPackageJson = await readPackageJsonInDir(getLibraryDistDir())
return execa(
'pnpm',
[
'ng',
'add',
libraryPackageJson.name,
'--skip-confirmation',
'--routing',
'--metadata-modules',
'json-ld',
'open-graph',
'open-graph-profile',
'standard',
'twitter-card',
],
{
cwd: appDir,
},
)
}
11 changes: 11 additions & 0 deletions projects/ngx-meta/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
"metadataModules": {
"type": "array",
"description": "Built-in metadata modules to use",
"items": {
"type": "string",
"enum": [
"json-ld",
"open-graph",
"open-graph-profile",
"standard",
"twitter-card"
]
},
"uniqueItems": true,
"default": [],
"//todo": "Enable x-prompts for this and metadata modules to include when both are ready",
"//x-prompt": {
Expand Down

0 comments on commit 4144c73

Please sign in to comment.