forked from martijnversluis/ChordSheetJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unibuild.ts
executable file
·94 lines (81 loc) · 3 KB
/
unibuild.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env yarn tsx
import peggy from 'peggy';
import process from 'process';
import tspegjs from 'ts-pegjs';
import unibuild, { Builder } from '@martijnversluis/unibuild';
import buildChordSuffixNormalizeMapping from './script/build_chord_suffix_normalize_mapping';
import buildChordSuffixGrammar from './script/build_chord_suffix_grammar';
import buildScales from './script/build_scales';
import buildChordProSectionGrammar from './script/build_chord_pro_section_grammar';
interface BuildOptions {
force: boolean;
release: boolean;
}
function peggyGenerate(grammar: string, release: boolean): string {
return peggy.generate(
grammar,
{
output: 'source',
format: 'commonjs',
plugins: [tspegjs],
trace: process.env.NODE_ENV === 'test' && !release,
},
);
}
unibuild((u: Builder) => {
const suffixNormalizeMapping = u.asset('suffixNormalizeMapping', {
input: 'src/normalize_mappings/suffix-mapping.txt',
outfile: 'src/normalize_mappings/suffix-normalize-mapping.ts',
build: buildChordSuffixNormalizeMapping,
});
const chordSuffixGrammar = u.asset('chordSuffixGrammar', {
input: 'src/normalize_mappings/suffix-mapping.txt',
outfile: 'src/parser/chord/suffix_grammar.pegjs',
build: buildChordSuffixGrammar,
});
const scales = u.asset('scales', {
input: 'data/scales.ts',
outfile: 'src/scales.ts',
build: buildScales,
command: (outfile: string) => `yarn linter:fix ${outfile}`,
});
u.asset('chordParser', {
input: ['src/parser/chord/base_grammar.pegjs', chordSuffixGrammar],
outfile: 'src/parser/chord/peg_parser.ts',
build: ({ release }: BuildOptions, baseGrammar: string, suffixGrammar: string) => (
peggyGenerate(`${baseGrammar}\n\n${suffixGrammar}`, release)
),
});
const sectionsGrammar = u.asset('sectionsGrammar', {
input: 'data/sections.ts',
outfile: 'src/parser/chord_pro/sections_grammar.pegjs',
build: buildChordProSectionGrammar,
});
u.asset('chordProParser', {
input: ['src/parser/chord_pro/grammar.pegjs', sectionsGrammar],
outfile: 'src/parser/chord_pro/peg_parser.ts',
build: ({ release }: BuildOptions, baseGrammar: string, sections: string) => {
const parserSource = peggyGenerate(`${baseGrammar}\n\n${sections}`, release);
return `import * as helpers from './helpers';\n\n${parserSource}`;
},
});
u.asset('chordsOverWordsParser', {
input: [
'src/parser/chords_over_words/grammar.pegjs',
'src/parser/chord/base_grammar.pegjs',
chordSuffixGrammar,
],
outfile: 'src/parser/chords_over_words/peg_parser.ts',
build: ({ release }: BuildOptions, ...grammars: string[]) => {
const grammar = grammars.join('\n\n');
const parserSource = peggyGenerate(grammar, release);
return `import * as helpers from './helpers';\n\n${parserSource}`;
},
});
u.asset('sources', {
input: [suffixNormalizeMapping, chordSuffixGrammar, scales],
outfile: 'lib/index.js',
command: 'parcel build',
releaseOnly: true,
});
});