Skip to content

Commit 0150add

Browse files
author
Salma Alam-Naylor
committed
Dynamically generate routes in prerender
1 parent bacfea6 commit 0150add

File tree

7 files changed

+137
-4
lines changed

7 files changed

+137
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ testem.log
3737
# System Files
3838
.DS_Store
3939
Thumbs.db
40+
41+
# Routes
42+
_routes.txt

_routes.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"dev:ssr": "ng run fretonator-web:serve-ssr",
3232
"serve:ssr": "node dist/fretonator-web/server/main.js",
3333
"build:ssr": "ng build --prod && ng run fretonator-web:server:production",
34-
"prerender": "ng run fretonator-web:prerender --routesFile _routes.txt"
34+
"prerender": "run-s prerender:*",
35+
"prerender:routes": "nx workspace-schematic generate-routes",
36+
"prerender:build": "ng run fretonator-web:prerender --routesFile _routes.txt"
3537
},
3638
"private": true,
3739
"dependencies": {
@@ -70,6 +72,7 @@
7072
"eslint": "6.8.0",
7173
"jest": "25.2.3",
7274
"jest-preset-angular": "8.1.2",
75+
"npm-run-all": "^4.1.5",
7376
"prettier": "2.0.4",
7477
"ts-jest": "^26.1.0",
7578
"ts-node": "~7.0.0",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Rule, Tree } from '@angular-devkit/schematics';
2+
import generateModeRoutes from './mode-routes';
3+
import writeRoutes from './write-routes';
4+
5+
export default function(schema: any): Rule {
6+
return (tree: Tree) => {
7+
const staticPages = [
8+
'/',
9+
'/about',
10+
'/404'
11+
];
12+
13+
const modeRoutes = generateModeRoutes();
14+
15+
// writeRoutes(tree, [...staticPages, ...modeRoutes]);
16+
17+
return writeRoutes(tree, [...staticPages, ...modeRoutes]);
18+
};
19+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function cartesianProduct(arr) {
2+
return arr.reduce(function(a, b) {
3+
return a.map(function(x) {
4+
return b.map(function(y) {
5+
return x.concat([y]);
6+
});
7+
}).reduce(function(a, b) {
8+
return a.concat(b);
9+
}, []);
10+
}, [[]]);
11+
}
12+
13+
enum NoteExtenderString {
14+
sharp = 'sharp',
15+
natural = 'natural',
16+
flat = 'flat'
17+
}
18+
19+
enum Mode {
20+
ionian = 'ionian',
21+
dorian = 'dorian',
22+
phrygian = 'phrygian',
23+
lydian = 'lydian',
24+
mixolydian = 'mixolydian',
25+
aolian = 'aolian',
26+
locrian = 'locrian',
27+
harmonicMinor = 'harmonicMinor',
28+
phrygianDominant = 'phrygianDominant',
29+
majorPentatonic = 'majorPentatonic',
30+
minorPentatonic = 'minorPentatonic'
31+
}
32+
33+
const Octave = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
34+
35+
enum NoteSymbol {
36+
aFlat = 'a_',
37+
a = 'a',
38+
aSharp = 'a#',
39+
bFlat = 'b_',
40+
b = 'b',
41+
c = 'c',
42+
cSharp = 'c#',
43+
dFlat = 'd_',
44+
d = 'd',
45+
dSharp = 'd#',
46+
eFlat = 'e_',
47+
e = 'e',
48+
f = 'f',
49+
fSharp = 'f#',
50+
gFlat = 'g_',
51+
g = 'g',
52+
gSharp = 'g#',
53+
}
54+
55+
enum NoteExtenderSymbol {
56+
sharp = '#',
57+
natural = '',
58+
flat = '_'
59+
}
60+
61+
export default (): string[] => {
62+
const octave = Octave;
63+
const noteExtender = Object.values(NoteExtenderString);
64+
const mode = Object.values(Mode);
65+
const product = cartesianProduct([octave, noteExtender, mode]);
66+
67+
return product
68+
.filter(([key, extender, mode]) => {
69+
const extenderSymbol = NoteExtenderSymbol[extender];
70+
const validCombination = Object.values(NoteSymbol).find(value => value === `${key}${extenderSymbol}`);
71+
return Boolean(validCombination);
72+
})
73+
.map(combination => ['', ...combination].join('/'));
74+
}
75+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"id": "generate-routes",
4+
"type": "object",
5+
"properties": {
6+
"name": {
7+
"type": "string",
8+
"description": "Library name",
9+
"$default": {
10+
"$source": "argv",
11+
"index": 0
12+
}
13+
}
14+
},
15+
"required": []
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Tree } from '@angular-devkit/schematics';
2+
3+
export default (tree: Tree, routes: string[]) => {
4+
const flatRoutes = routes.join('\n');
5+
const outFile = '_routes.txt';
6+
7+
if(!tree.exists(outFile)) {
8+
tree.create(outFile, flatRoutes);
9+
return null;
10+
}
11+
12+
const { content } = tree.get(outFile);
13+
14+
if(content.toString() !== flatRoutes) {
15+
tree.overwrite(outFile, flatRoutes);
16+
return null;
17+
}
18+
19+
return null;
20+
}

0 commit comments

Comments
 (0)