Skip to content

Commit

Permalink
ci(angular): fail build when an angular directive or component is not…
Browse files Browse the repository at this point in the history
… exported (#561)
  • Loading branch information
quentinderoubaix authored Mar 6, 2024
1 parent 0d22e56 commit d6583a6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
6 changes: 5 additions & 1 deletion angular/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"scripts": {
"ng": "ng",
"build": "wireit"
"build": "wireit",
"check": "wireit"
},
"wireit": {
"build:src": {
Expand Down Expand Up @@ -52,6 +53,9 @@
"dependencies": [
"build:pkg"
]
},
"check": {
"command": "node scripts/verify-module.mjs"
}
},
"peerDependencies": {
Expand Down
50 changes: 50 additions & 0 deletions angular/lib/scripts/verify-module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {glob} from 'glob';
import path from 'path';
import {fileURLToPath} from 'url';
import {readFile} from 'fs/promises';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const pathRegExp = /\\/g;
function normalizePath(str) {
return str.replace(pathRegExp, '/');
}

const COMPONENTS_FOLDER = path.join(__dirname, '../src/components');

const files = (
await glob('**/*.component.ts', {
cwd: COMPONENTS_FOLDER,
nodir: true,
})
).map((file) => normalizePath(path.join(COMPONENTS_FOLDER, file)));

const stuffToExportRegex = /(@Directive|@Component)\({[\S\s]+?}\)\s*export class ([a-zA-Z]*)/g;

const classesToExport = [];
for (const file of files) {
const content = await readFile(file, {encoding: 'utf-8'});
const matches = [...content.matchAll(stuffToExportRegex)];
for (const match of matches) {
classesToExport.push(match[2]);
}
}

const angularModuleContent = await readFile(normalizePath(path.join(__dirname, '../src/agnos-ui-angular.module.ts')), {encoding: 'utf-8'});
const componentsFromAngularModule = new Set(
angularModuleContent
.match(/const components = \[([^\]]+)\];/)[1]
.split(',')
.map((component) => component.trim())
.filter((component) => !!component),
);

const defaultSlotRegex = /^[a-zA-Z]*Default[a-zA-Z]*Slots?Component$/;
const missingComponents = classesToExport.filter(
(classToExport) => !componentsFromAngularModule.has(classToExport) && !classToExport.match(defaultSlotRegex),
);

if (missingComponents.length) {
console.error(`ERROR ! The following Angular components / directives are not exported in the main angular module: ${missingComponents.join(', ')}`);
process.exit(1);
}
3 changes: 2 additions & 1 deletion angular/lib/src/agnos-ui-angular.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from './components/accordion/accordion.component';
import {SliderComponent, SliderHandleDirective, SliderLabelDirective, SliderStructureDirective} from './components/slider/slider.component';
import {ProgressbarComponent, ProgressbarStructureDirective} from './components/progressbar/progressbar.component';
import {ToastBodyDirective, ToastComponent, ToastStructureDirective} from './components/toast/toast.component';
import {ToastBodyDirective, ToastComponent, ToastHeaderDirective, ToastStructureDirective} from './components/toast/toast.component';
/* istanbul ignore next */
const components = [
SlotDirective,
Expand Down Expand Up @@ -73,6 +73,7 @@ const components = [
ToastComponent,
ToastStructureDirective,
ToastBodyDirective,
ToastHeaderDirective,
];

@NgModule({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"./svelte/headless:check",
"./svelte/lib:check",
"./svelte/demo:check",
"./angular/lib:check",
"./demo:check"
]
},
Expand Down

0 comments on commit d6583a6

Please sign in to comment.