Skip to content

Commit

Permalink
upgraded, made small fixes afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
alkrobinson authored and zjkipping committed Mar 27, 2024
1 parent 0891c0b commit b0365f7
Show file tree
Hide file tree
Showing 18 changed files with 6,222 additions and 4,738 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ Thumbs.db
.angular

**/doc-page-loaders.ts

.nx/cache
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/dist
/coverage
**/project.json

/.nx/cache
17 changes: 5 additions & 12 deletions apps/component-doc-portal/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,28 @@
"executor": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "component-doc-portal:ng-build:production"
"buildTarget": "component-doc-portal:ng-build:production"
},
"development": {
"browserTarget": "component-doc-portal:ng-build:development"
"buildTarget": "component-doc-portal:ng-build:development"
}
},
"defaultConfiguration": "development"
},
"ng-extract-i18n": {
"executor": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "component-doc-portal:ng-build"
"buildTarget": "component-doc-portal:ng-build"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"options": {
"lintFilePatterns": [
"apps/component-doc-portal/src/**/*.ts",
"apps/component-doc-portal/src/**/*.html"
]
}
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/apps/component-doc-portal"],
"options": {
"jestConfig": "apps/component-doc-portal/jest.config.ts",
"passWithNoTests": true
"jestConfig": "apps/component-doc-portal/jest.config.ts"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion apps/ng-doc-portal-plugin-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "apps/ng-doc-portal-plugin-e2e/jest.config.ts",
"runInBand": true
"runInBand": true,
"passWithNoTests": false
},
"dependsOn": ["ng-doc-portal-plugin:build"]
}
Expand Down
10 changes: 5 additions & 5 deletions libs/ng-doc-portal-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"generators": "./generators.json",
"executors": "./executors.json",
"peerDependencies": {
"prettier": "^2.6.2",
"@nx/devkit": "^16.0.0",
"@nx/angular": "^16.0.0"
"prettier": "^3.2.5",
"@nx/devkit": "^18.0.0",
"@nx/angular": "^18.0.0"
},
"dependencies": {
"@phenomnomnominal/tsquery": "^6.0.0",
"chalk": "^4.1.2",
"chokidar": "^3.5.3",
"date-fns": "2.30.0",
"fast-glob": "^3.3.0",
"date-fns": "^3.6.0",
"fast-glob": "^3.3.2",
"minimatch": "^9.0.3"
},
"publishConfig": {
Expand Down
15 changes: 3 additions & 12 deletions libs/ng-doc-portal-plugin/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,14 @@
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/ng-doc-portal-plugin/**/*.ts",
"libs/ng-doc-portal-plugin/generators.json",
"libs/ng-doc-portal-plugin/executors.json",
"libs/ng-doc-portal-plugin/package.json"
]
}
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/libs/ng-doc-portal-plugin"],
"options": {
"jestConfig": "libs/ng-doc-portal-plugin/jest.config.ts",
"passWithNoTests": true
"jestConfig": "libs/ng-doc-portal-plugin/jest.config.ts"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export class DocPageLoadersCompiler {
constructor(
private readonly configFileLocation: string,
private readonly docPageLoadersFileLocation: string,
private readonly silenced = false
private readonly silenced = false,
) {
if (!existsSync(this.configFileLocation)) {
throw new Error(
`Could not find config file at ${this.configFileLocation}`
`Could not find config file at ${this.configFileLocation}`,
);
}

Expand Down Expand Up @@ -77,11 +77,11 @@ export class DocPageLoadersCompiler {
watch() {
const fileEvents = concat(
this.buildInitialFileEvent(),
this.buildWatcher()
this.buildWatcher(),
);

const handledFileEvents = this.handleFileEvents(fileEvents).pipe(
shareReplay(1)
shareReplay(1),
);

handledFileEvents.pipe(take(1)).subscribe(() => {
Expand All @@ -100,12 +100,12 @@ export class DocPageLoadersCompiler {
map((payloads) => this.detectAndHandleDuplicateTitles(payloads)),
map((eventPayloads) =>
eventPayloads.map((eventPayload) =>
generateDocPageLoader(eventPayload.filePath, eventPayload.title)
)
generateDocPageLoader(eventPayload.filePath, eventPayload.title),
),
),
map(wrapTypescriptBoilerplate),
map(formatContent),
switchMap((content) => this.writeDynamicPageContentToFile(content))
switchMap((content) => formatContent(content)),
switchMap((content) => this.writeDynamicPageContentToFile(content)),
);
}

Expand All @@ -125,13 +125,13 @@ export class DocPageLoadersCompiler {
dot: true,
onlyFiles: true,
ignore: ['node_modules'],
})
}),
).pipe(
map((filePaths): RawInitEvent => ({ type: 'init', filePaths })),
tap(() => {
const endTime = Date.now();
this.log(green(`Searching complete in ${endTime - startTime}ms\n`));
})
}),
);
}

Expand All @@ -142,7 +142,7 @@ export class DocPageLoadersCompiler {
private buildWatcher(): Observable<RawFileEvent> {
return new Observable<RawFileEvent>((observer) => {
const patternStrings = convertPatternOrGlobPatternArray(
this.globPatterns
this.globPatterns,
);

const watcher = watch(patternStrings, {
Expand All @@ -168,7 +168,7 @@ export class DocPageLoadersCompiler {
*/
private buildRawFileEvent(
rawFilePath: string,
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'
event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
): RawFileEvent {
const filePath = rawFilePath.replace(/\\/g, '/');
if (event === 'add' || event === 'addDir') {
Expand All @@ -191,7 +191,7 @@ export class DocPageLoadersCompiler {
* @private
*/
private async addPayloadToEvent(
fileEvent: RawFileEvent
fileEvent: RawFileEvent,
): Promise<ProcessedFileEvent | null> {
try {
if (fileEvent.type === 'init') {
Expand All @@ -201,7 +201,7 @@ export class DocPageLoadersCompiler {
for (const filePath of fileEvent.filePaths) {
const title = await extractTitleFromDocPageFile(
filePath,
this.globPatterns
this.globPatterns,
);
payload.push({ filePath, title });
}
Expand All @@ -210,21 +210,21 @@ export class DocPageLoadersCompiler {
green(
`Finished compiling component document page files in ${
endTime - startTime
}ms\n`
)
}ms\n`,
),
);
return { ...fileEvent, payload };
} else if (fileEvent.type === 'add' || fileEvent.type === 'change') {
const startTime = Date.now();
const title = await extractTitleFromDocPageFile(
fileEvent.filePath,
this.globPatterns
this.globPatterns,
);
const endTime = Date.now();
this.log(
blue(
`${timeNow()} - COMPILE - recompiled in ${endTime - startTime}ms`
)
`${timeNow()} - COMPILE - recompiled in ${endTime - startTime}ms`,
),
);
return { ...fileEvent, title };
} else {
Expand All @@ -244,16 +244,16 @@ export class DocPageLoadersCompiler {
duplicatesDetected = true;
this.log(
yellow(
`Duplicated title of "${payload.title}" detected in file '${payload.filePath}'`
)
`Duplicated title of "${payload.title}" detected in file '${payload.filePath}'`,
),
);
payload.title += ` ${++titles[payload.title]}`;
}
titles[payload.title] = 1;
}
if (duplicatesDetected) {
this.log(
yellow('\nPlease resolve duplicated titles before publishing!\n')
yellow('\nPlease resolve duplicated titles before publishing!\n'),
);
}
return eventPayloads;
Expand All @@ -272,8 +272,8 @@ export class DocPageLoadersCompiler {
console.error(e);
this.log(
red(
`\n\nUnexpected error occurred while generating doc-page-loaders.ts\n`
)
`\n\nUnexpected error occurred while generating doc-page-loaders.ts\n`,
),
);
}
}
Expand Down
26 changes: 13 additions & 13 deletions libs/ng-doc-portal-plugin/src/executors/compiler/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { basename } from 'path';
*/
export function accumulatePayloads(
acc: EventPayload[],
curr: ProcessedFileEvent
curr: ProcessedFileEvent,
): EventPayload[] {
if (curr.type === 'init') {
const list = sortFullPayloadList(curr.payload);
Expand Down Expand Up @@ -50,7 +50,7 @@ export function sortFullPayloadList(list: EventPayload[]) {

export function insertIntoSortedPayloadList(
list: EventPayload[],
item: EventPayload
item: EventPayload,
) {
let index = 0;
let low = 0;
Expand Down Expand Up @@ -86,7 +86,7 @@ export function comparePayloadTitles(a: EventPayload, b: EventPayload) {

export function findTitlePrefixFromGlobPatterns(
filePath: string,
globPatterns: (string | GlobPattern)[]
globPatterns: (string | GlobPattern)[],
): string | undefined {
const pattern = globPatterns.find((globPattern) => {
if (typeof globPattern === 'string') {
Expand Down Expand Up @@ -136,7 +136,7 @@ export async function getTitleFromTypeScript(filePath: string) {

const defaultExportClassDeclaration = tsquery(
ast,
'ClassDeclaration:has(ExportKeyword):has(DefaultKeyword)'
'ClassDeclaration:has(ExportKeyword):has(DefaultKeyword)',
).at(0);

if (defaultExportClassDeclaration) {
Expand All @@ -156,16 +156,16 @@ export async function getTitleFromTypeScript(filePath: string) {

let objectLiteralExpression = tsquery(
exportAssignment,
'ObjectLiteralExpression'
'ObjectLiteralExpression',
).at(0);

if (!objectLiteralExpression) {
const identifier = tsquery(exportAssignment, 'Identifier').at(0);
objectLiteralExpression = tsquery(
ast,
`VariableDeclaration:has(Identifier[name="${identifier?.getText(
ast
)}"]) > ObjectLiteralExpression`
ast,
)}"]) > ObjectLiteralExpression`,
).at(0);
}

Expand All @@ -175,7 +175,7 @@ export async function getTitleFromTypeScript(filePath: string) {

const title = tsquery(
objectLiteralExpression,
'PropertyAssignment:has(Identifier[name="title"]) > StringLiteral'
'PropertyAssignment:has(Identifier[name="title"]) > StringLiteral',
)
.at(0)
?.getText(ast);
Expand All @@ -185,7 +185,7 @@ export async function getTitleFromTypeScript(filePath: string) {
return title.replace(/'/g, '');
} else {
throw new Error(
`No property found for 'title' in exported object literal for file: ${filePath}`
`No property found for 'title' in exported object literal for file: ${filePath}`,
);
}
} else {
Expand All @@ -205,7 +205,7 @@ export async function getTitleFromTypeScript(filePath: string) {
*/
export async function extractTitleFromDocPageFile(
filePath: string,
globPatterns: (string | GlobPattern)[]
globPatterns: (string | GlobPattern)[],
) {
const title = await getTitleFromTypeScript(filePath);
const prefix = findTitlePrefixFromGlobPatterns(filePath, globPatterns);
Expand Down Expand Up @@ -240,7 +240,7 @@ export function generateDocPageLoader(filePath: string, title: string) {
*
* @param content The file content to be prettified
*/
export function formatContent(content: string): string {
export async function formatContent(content: string): Promise<string> {
return prettierFormat(content, {
parser: 'typescript',
printWidth: 100,
Expand All @@ -264,12 +264,12 @@ export function wrapTypescriptBoilerplate(configStrings: string[]) {
}

export function convertPatternOrGlobPatternArray(
patterns: (string | GlobPattern)[]
patterns: (string | GlobPattern)[],
) {
return patterns.map((strOrGlobPattern) =>
typeof strOrGlobPattern === 'string'
? strOrGlobPattern
: strOrGlobPattern.pattern
: strOrGlobPattern.pattern,
);
}

Expand Down
11 changes: 2 additions & 9 deletions libs/ng-doc-portal/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/libs/ng-doc-portal"],
"options": {
"jestConfig": "libs/ng-doc-portal/jest.config.ts",
"passWithNoTests": true
"jestConfig": "libs/ng-doc-portal/jest.config.ts"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"options": {
"lintFilePatterns": [
"libs/ng-doc-portal/**/*.ts",
"libs/ng-doc-portal/**/*.html"
]
}
"executor": "@nx/eslint:lint"
}
},
"tags": []
Expand Down
Loading

0 comments on commit b0365f7

Please sign in to comment.