Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit 1686175

Browse files
blackbaud-brandonstirnamanBobby Earl
authored and
Bobby Earl
committed
Stop resolving, fix windows guard paths (#179)
* Stop resolving, fix windows guard paths * Add new tests for windows guard, prefix issue
1 parent adc8fa3 commit 1686175

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/sky-pages-route-generator.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ function generateDeclarations(routes) {
142142
return `[\n${declarations}\n]`;
143143
}
144144

145-
function generateRuntimeImports(routes) {
145+
function generateRuntimeImports(skyAppConfig, routes) {
146+
const alias = skyAppConfig.runtime.spaPathAlias;
146147
return routes
147148
.filter(r => r.guard)
148-
.map(r => `import { ${r.guard.name} } from '${r.guard.path.replace(/\.ts$/, '')}';`);
149+
.map(r =>`import { ${r.guard.name} } from '${alias}/${r.guard.path.replace(/\.ts$/, '')}';`);
149150
}
150151

151152
function generateProviders(routes) {
@@ -172,7 +173,7 @@ function getRoutes(skyAppConfig) {
172173
return {
173174
declarations: generateDeclarations(routes),
174175
definitions: generateDefinitions(routes),
175-
imports: generateRuntimeImports(routes),
176+
imports: generateRuntimeImports(skyAppConfig, routes),
176177
providers: generateProviders(routes),
177178
names: generateNames(routes),
178179
routesForConfig: getRoutesForConfig(routes)
@@ -190,7 +191,7 @@ function extractGuard(file) {
190191
throw new Error(`As a best practice, only export one guard per file in ${file}`);
191192
}
192193

193-
result = { path: path.resolve(file), name: match[1] };
194+
result = { path: file.replace(/\\/g, '/'), name: match[1] };
194195
}
195196

196197
return result;

test/sky-pages-route-generator.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,41 @@ describe('SKY UX Builder route generator', () => {
104104
expect(suppliedPattern).toEqual('my-custom-src/my-custom-pattern');
105105
});
106106

107+
it('should handle windows guard paths correctly', () => {
108+
spyOn(glob, 'sync').and.callFake(() => ['my-src\\my-custom-route\\index.html']);
109+
spyOn(fs, 'readFileSync').and.returnValue('@Injectable() export class Guard {}');
110+
spyOn(fs, 'existsSync').and.returnValue(true);
111+
112+
let routes = generator.getRoutes({
113+
runtime: {
114+
srcPath: 'my-src',
115+
routesPattern: '**/index.html'
116+
}
117+
});
118+
119+
expect(routes.imports[0]).toContain(
120+
`my-src/my-custom-route/index.guard`
121+
);
122+
});
123+
124+
it('should prefix guard imports with spaPathAlias', () => {
125+
spyOn(glob, 'sync').and.callFake(() => ['my-src/my-custom-route/index.html']);
126+
spyOn(fs, 'readFileSync').and.returnValue('@Injectable() export class Guard {}');
127+
spyOn(fs, 'existsSync').and.returnValue(true);
128+
129+
let routes = generator.getRoutes({
130+
runtime: {
131+
srcPath: '',
132+
routesPattern: '**/index.html',
133+
spaPathAlias: 'spa-path-alias'
134+
}
135+
});
136+
137+
expect(routes.imports[0]).toContain(
138+
`import { Guard } from \'spa-path-alias/my-src/my-custom-route/index.guard\';`
139+
);
140+
});
141+
107142
it('should support guards with custom routesPattern', () => {
108143
spyOn(glob, 'sync').and.callFake(() => ['my-custom-src/my-custom-route/index.html']);
109144
spyOn(fs, 'readFileSync').and.returnValue('@Injectable() export class Guard {}');

0 commit comments

Comments
 (0)