Skip to content

Commit b28457b

Browse files
Integrate unibuild (#1376)
1 parent 0ddb42b commit b28457b

15 files changed

+276
-1159
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default tseslint.config(
5858
argsIgnorePattern: '^_',
5959
caughtErrorsIgnorePattern: '^_',
6060
destructuredArrayIgnorePattern: '^_',
61+
varsIgnorePattern: '^_',
6162
},
6263
],
6364
},

package.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@babel/preset-typescript": "^7.16.7",
3333
"@eslint/core": "^0.6.0",
3434
"@eslint/js": "^9.11.0",
35+
"@martijnversluis/unibuild": "0.3.3",
3536
"@parcel/packager-ts": "2.12.0",
3637
"@parcel/transformer-typescript-types": "2.12.0",
3738
"@types/eslint__js": "^8.42.3",
@@ -71,15 +72,8 @@
7172
"build:bundle:default": "esbuild lib/index.js --outfile=lib/bundle.js --bundle --global-name=ChordSheetJS",
7273
"build:bundle:min": "esbuild lib/index.js --outfile=lib/bundle.min.js --bundle --global-name=ChordSheetJS --minify-whitespace --minify-identifiers --minify-syntax",
7374
"build:check-types": "tsc lib/main.d.ts",
74-
"build:chord-suffix-grammar": "yarn tsx script/generate_chord_suffix_grammar.ts",
75-
"build:code-generate": "yarn build:suffix-normalize && yarn build:chord-suffix-grammar && yarn build:pegjs && yarn build:scales",
76-
"build:pegjs": "yarn build:pegjs:chord && yarn build:pegjs:chordpro && yarn build:pegjs:chords-over-words",
77-
"build:pegjs:chord": "tsx script/combine_files.ts src/parser/chord/base_grammar.pegjs src/parser/chord/suffix_grammar.pegjs src/parser/chord/combined_grammer.pegjs && peggy --plugin ts-pegjs -o src/parser/chord/peg_parser.ts src/parser/chord/combined_grammer.pegjs",
78-
"build:pegjs:chordpro": "tsx script/generate_parser.ts chord_pro --skip-chord-grammar",
79-
"build:pegjs:chords-over-words": "tsx script/generate_parser.ts chords_over_words",
80-
"build:scales": "tsx script/generate_scales.ts && yarn linter:fix src/scales.ts",
75+
"build:code-generate": "tsx ./unibuild.ts",
8176
"build:sources": "parcel build",
82-
"build:suffix-normalize": "shx rm -rf src/normalize_mappings/suffix-normalize-mapping.ts && tsx script/generate-suffix-normalize-mapping.ts",
8377
"ci": "yarn install && yarn lint && yarn test && yarn build && yarn readme",
8478
"clean": "shx rm -rf node_modules && shx rm -rf lib",
8579
"debug:chordpro": "tsx script/debug_parser.ts chord_pro --skip-chord-grammar",
@@ -95,7 +89,7 @@
9589
"postpublish": "pinst --enable",
9690
"prelint": "yarn pretest",
9791
"prepare": "husky install",
98-
"prepublish": "pinst --disable && yarn install && yarn test && yarn build",
92+
"prepublish": "pinst --disable && yarn install && yarn test && yarn build:code-generate",
9993
"pretest": "NODE_ENV=test yarn build:code-generate",
10094
"readme": "node_modules/.bin/jsdoc2md -f src/**/*.ts -f src/*.ts --configure ./jsdoc2md.json --template doc/README.hbs > README.md",
10195
"test": "yarn pretest && yarn linter:run && yarn jest:run"

script/build_chord_suffix_grammar.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { EOL } from 'os';
2+
import { BuildOptions } from 'esbuild';
3+
4+
export default function buildChordSuffixGrammar(_: BuildOptions, data: string): string {
5+
const suffixes: string[] = data
6+
.split(EOL)
7+
.filter((s) => s.trim().length > 0)
8+
.flatMap((line) => line.split(/,\s*/))
9+
.sort((a, b) => b.length - a.length)
10+
.map((suffix) => `"${suffix}"`);
11+
12+
const groups: string[][] = [];
13+
14+
const copy = [...suffixes];
15+
16+
while (copy.length > 0) {
17+
const chunk = copy.splice(0, 100) as string[];
18+
groups.push(chunk);
19+
}
20+
21+
const groupsGrammar = groups.map((groupSuffixes, i) => (
22+
`ChordSuffix${i}\n = ${groupSuffixes.join('\n / ')}\n`
23+
));
24+
25+
return `
26+
ChordSuffix
27+
= (${groupsGrammar.map((_grammar, i) => `ChordSuffix${i}`).join(' / ')})?
28+
29+
${groupsGrammar.join('\n')}
30+
`;
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { EOL } from 'os';
2+
import { BuildOptions } from 'esbuild';
3+
4+
export default function buildChordSuffixNormalizeMapping(_: BuildOptions, data: string): string {
5+
const suffixes = data
6+
.split(EOL)
7+
.map((line) => {
8+
const variants = line.split(/,\s*/);
9+
return variants.reduce((acc, variant) => ({ ...acc, [variant]: variants[0] }), {});
10+
})
11+
.reduce((acc, set) => ({ ...acc, ...set }), {});
12+
13+
const json = JSON.stringify(suffixes, null, 2);
14+
return `const mapping: Record<string, string> = ${json};${EOL}${EOL}export default mapping;`;
15+
}

script/build_scales.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* eslint indent: 0 */
2+
3+
import SCALES from '../data/scales';
4+
5+
import {
6+
FLAT,
7+
MAJOR,
8+
MINOR,
9+
NO_MODIFIER,
10+
NUMERAL,
11+
NUMERIC,
12+
SHARP,
13+
SYMBOL,
14+
SOLFEGE,
15+
} from '../src/constants';
16+
17+
interface BuildOptions {
18+
force: boolean;
19+
release: boolean;
20+
}
21+
22+
export default function buildScales(_: BuildOptions): string {
23+
const MODIFIERS = { NO_MODIFIER, SHARP, FLAT };
24+
const MODES = { MAJOR, MINOR };
25+
26+
const KEY_TYPES = {
27+
SYMBOL,
28+
SOLFEGE,
29+
NUMERIC,
30+
NUMERAL,
31+
};
32+
33+
return `
34+
/*
35+
⚠️⚠️⚠️ NOTE: this file has been generated automatically.
36+
Please do NOT edit this file directly, but instead:
37+
- edit the data source, located in \`data/scales.ts\`
38+
- run \`yarn build:code-generate\` to re-generate this file.
39+
*/
40+
41+
import {
42+
ChordType,
43+
Mode,
44+
ModifierMaybe,
45+
FLAT,
46+
MAJOR,
47+
MINOR,
48+
NO_MODIFIER,
49+
NUMERAL,
50+
NUMERIC,
51+
SHARP,
52+
SYMBOL,
53+
SOLFEGE
54+
} from './constants';
55+
56+
export const KEY_TO_GRADE: Record<ChordType, Record<Mode, Record<ModifierMaybe, Record<string, number>>>> = {
57+
${
58+
Object.entries(KEY_TYPES).map(([keyTypeName, keyTypeValue]) => `
59+
[${keyTypeName}]: {
60+
${
61+
Object.entries(MODES).map(([modeName, modeValue]) => `
62+
[${modeName}]: {
63+
${
64+
Object.entries(MODIFIERS).map(([modifierName, modifierValue]) => `
65+
[${modifierName}]: {
66+
${
67+
SCALES[keyTypeValue][modeValue][modifierValue].map((keyString, grade) => {
68+
if (keyString === null) {
69+
return null;
70+
}
71+
72+
const keyWithoutModifier = keyString.replace(/#|b/g, '');
73+
return `${keyWithoutModifier}: ${grade}`;
74+
}).filter((v) => v).join(',\n')
75+
}
76+
},
77+
`).join('\n')
78+
}
79+
},
80+
`).join('\n')
81+
}
82+
},
83+
`).join('\n')
84+
}
85+
};
86+
87+
export const GRADE_TO_KEY: Record<ChordType, Record<Mode, Record<ModifierMaybe, Record<number, string>>>> = {
88+
${
89+
Object.entries(KEY_TYPES).map(([keyTypeName, keyTypeValue]) => `
90+
[${keyTypeName}]: {
91+
${
92+
Object.entries(MODES).map(([modeName, modeValue]) => `
93+
[${modeName}]: {
94+
${
95+
Object.entries(MODIFIERS).map(([modifierName, modifierValue]) => `
96+
[${modifierName}]: {
97+
${
98+
SCALES[keyTypeValue][modeValue][modifierValue].map((keyString, grade) => {
99+
if (keyString === null) {
100+
return null;
101+
}
102+
103+
return `${grade}: '${keyString}',`;
104+
}).filter((v) => v).join('\n')
105+
}
106+
},
107+
`).join('\n')
108+
}
109+
},
110+
`).join('\n')
111+
}
112+
},
113+
`).join('\n')
114+
}
115+
};
116+
`.substring(1);
117+
}

script/combine_files.ts

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

script/generate-suffix-normalize-mapping.ts

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

script/generate_chord_suffix_grammar.ts

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

script/generate_parser.ts

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

0 commit comments

Comments
 (0)