Skip to content

Commit 41d4b11

Browse files
committed
🐛 with corpus build script for columns
1 parent 3c6e813 commit 41d4b11

File tree

13 files changed

+239
-63
lines changed

13 files changed

+239
-63
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ jobs:
4141
id-token: write
4242
steps:
4343
- uses: actions/checkout@v4
44-
- run: npx jsr publish
44+
- uses: denoland/setup-deno@v2
45+
with:
46+
deno-version: v2.x
47+
- run: npm install -g browserify uglify-js
48+
- run: deno --allow-all build.ts ci
49+
- run: npx jsr publish --allow-dirty

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Generated files
22
.build/
33
*.map
4-
example.js
4+
corpus.js
5+
*.umd.js
6+
*.min.js
57

68
# Logs
79
logs

about/build.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
// Copyright (C) 2025 Subhajit Sahu
22
// SPDX-License-Identifier: AGPL-3.0-or-later
33
// See LICENSE for full terms
4-
function readTextFiles(dir: string) {
4+
import * as path from "jsr:@std/path@1.0.9";
5+
6+
7+
async function readTextFiles(dir: string) {
58
const a = new Map<string, string>();
69
for (const f of Deno.readDirSync(dir)) {
710
if (!f.isFile || !f.name.endsWith('.txt')) continue;
811
const name = f.name.replace('.txt', '');
9-
const content = Deno.readTextFileSync(`assets/${f.name}`);
12+
const content = await Deno.readTextFile(path.join(dir, f.name));
1013
a.set(name, content);
1114
}
1215
return a;
1316
}
1417

1518

16-
function writeIndex(file: string, data: Map<string, string>) {
19+
async function writeIndex(file: string, data: Map<string, string>) {
1720
let a = '';
1821
for (const [k, v] of data)
19-
a += `[[${k}]]\n${v}\n\n\n`;
20-
Deno.writeTextFileSync(file, a);
22+
a += `[[${k}]]\n${v}\n\n`;
23+
await Deno.writeTextFile(file, a.trim() + '\n');
2124
console.log(`Wrote ${data.size} entries to ${file}`);
2225
}
2326

2427

25-
function main() {
26-
const data = readTextFiles('assets');
27-
writeIndex('index.txt', data);
28+
export async function build() {
29+
const data = await readTextFiles(path.join(import.meta.dirname || '', 'assets'));
30+
await writeIndex(path.join(import.meta.dirname || '', 'index.txt'), data);
2831
}
29-
main();

build.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as about from "./about/build.ts";
2+
import * as columns from "./columns/build.ts";
3+
import * as columndescriptions from "./columndescriptions/build.ts";
4+
import * as hierarchy from "./hierarchy/build.ts";
5+
import * as representations from "./representations/build.ts";
6+
import * as compositions from "./compositions/build.ts";
7+
import * as compositionstats from "./compositionstats/build.ts";
8+
9+
10+
// Create the corpus.min.js file.
11+
async function writeCorpus() {
12+
console.log('Writing corpus.min.js...');
13+
await Deno.writeTextFile('corpus.js',
14+
`exports.columns = require('./columns/corpus.js');\n` +
15+
`exports.hierarchy = require('./hierarchy/corpus.js');\n` +
16+
`exports.representations = require('./representations/corpus.js');\n`
17+
);
18+
await (new Deno.Command('browserify', {
19+
args: ['corpus.js', '-s', 'ifct2017', '-o', 'corpus.umd.js'],
20+
stdin: 'piped', stdout: 'piped', stderr: 'piped'
21+
})).spawn().output();
22+
await (new Deno.Command('uglifyjs', {
23+
args: ['corpus.umd.js', '-c', '-m', '-o', 'corpus.min.js'],
24+
stdin: 'piped', stdout: 'piped', stderr: 'piped'
25+
})).spawn().output();
26+
await Deno.remove('corpus.js');
27+
await Deno.remove('corpus.umd.js');
28+
}
29+
30+
31+
// Set up the package for npm.
32+
async function setupNpmPackage() {
33+
console.log('Setting up npm package...');
34+
try { await Deno.remove('.build', {recursive: true}); }
35+
catch { /* ignore */ }
36+
const name = '@ifct2017/corpus';
37+
const version = JSON.parse(await Deno.readTextFile('deno.json')).version;
38+
const description = 'IFCT 2017 Corpus';
39+
const main = 'index.js';
40+
const keywords = ['ifct', '2017', 'corpus'];
41+
const author = 'wolfram77@gmail.com';
42+
const license = 'AGPL-3.0';
43+
await Deno.mkdir('.build', {recursive: true});
44+
const meta = {name, version, description, main, keywords, author, license};
45+
await Deno.writeTextFile('.build/package.json', JSON.stringify(meta, null, 2));
46+
await Deno.copyFile('README.md', '.build/README.md');
47+
await Deno.copyFile('LICENSE', '.build/LICENSE');
48+
await Deno.rename('corpus.min.js', '.build/index.js');
49+
}
50+
51+
52+
// Publish the package to npm.
53+
async function publishNpmPackage() {
54+
console.log('Publishing npm package...');
55+
await (new Deno.Command('npm', {
56+
args: ['publish', '--access', 'public'], cwd: '.build',
57+
stdin: 'piped', stdout: 'piped', stderr: 'piped'
58+
})).spawn().output();
59+
}
60+
61+
62+
// Main function, of course.
63+
async function main(corpus=false) {
64+
await about.build();
65+
await columns.build(corpus? './columns/corpus.js' : '');
66+
await columndescriptions.build(corpus? './columndescriptions/corpus.js' : '');
67+
await hierarchy.build(corpus? './hierarchy/corpus.js' : '');
68+
await representations.build(corpus? './representations/corpus.js' : '');
69+
await compositions.build();
70+
await compositionstats.build();
71+
if (!corpus) return;
72+
await writeCorpus();
73+
await setupNpmPackage();
74+
await publishNpmPackage();
75+
}
76+
main(Deno.args[0] === 'corpus');

columndescriptions/build.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import * as csv from "jsr:@std/csv@1.0.6";
33

44

55

6+
async function writeCorpus(outfile: string, records: Record<string, string>[]) {
7+
let a = `var CORPUS = new Map([\n`;
8+
for (const r of records)
9+
a += ` ["${r.code}", ${JSON.stringify(r)}],\n`;
10+
a += `]);\n`;
11+
a += `module.exports = CORPUS;\n`;
12+
await Deno.writeTextFile(outfile, a);
13+
}
14+
615

7-
async function main() {
16+
export async function build(corpus='corpus.js') {
17+
if (!corpus) return;
818
const file = path.join(import.meta.dirname || "", "index.csv");
919
const data = await Deno.readTextFile(file);
1020
const records = csv.parse(data, {skipFirstRow: true, comment: "#"});
11-
const jsonfile = path.join(import.meta.dirname || "", "index.json");
12-
await Deno.writeTextFile(jsonfile, JSON.stringify(records, null, 2));
21+
await writeCorpus(corpus, records);
1322
}
14-
main();

columns/build.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as path from "jsr:@std/path@1.0.9";
2+
import * as csv from "jsr:@std/csv@1.0.6";
3+
4+
5+
async function writeCorpus(outfile: string) {
6+
const infile = path.join(import.meta.dirname || "", "index.csv");
7+
const data = await Deno.readTextFile(infile);
8+
const records = csv.parse(data, {skipFirstRow: true, comment: "#"});
9+
const map = new Map();
10+
for (const r of records)
11+
map.set(r.code, r);
12+
let a = `var CORPUS = new Map([\n`;
13+
for (const [k, v] of map)
14+
a += ` ["${k}", ${JSON.stringify(v).replace(/\"(\w+)\":/g, '$1:')}],\n`;
15+
a += `]);\n`;
16+
a += `module.exports = CORPUS;\n`;
17+
await Deno.writeTextFile(outfile, a);
18+
}
19+
20+
21+
export async function build(corpus='corpus.js') {
22+
if (corpus) await writeCorpus(corpus);
23+
}

compositions/build.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// See LICENSE for full terms
44
import * as path from "jsr:@std/path@1.0.9";
55
import * as csv from "jsr:@std/csv@1.0.6";
6-
import {loadColumns, columns} from "../columns/index.ts";
6+
import {loadColumns} from "../columns/index.ts";
77
import {type Description, loadDescriptions} from "../descriptions/index.ts";
88
import {type Group, loadGroups} from "../groups/index.ts";
99

@@ -51,7 +51,7 @@ interface Dat {
5151

5252

5353

54-
async function generateIndexCsv() {
54+
async function writeIndex() {
5555
let dat: Dat = {
5656
code: [],
5757
name: [],
@@ -167,21 +167,21 @@ async function generateIndexCsv() {
167167
}
168168

169169

170-
async function main() {
170+
async function writeIndexMain() {
171171
await loadColumns();
172172
grupCorpus = await loadGroups();
173173
descCorpus = await loadDescriptions();
174-
factors = await readCsv('configs/factors.csv', (acc, r) => acc.set(r.code, r.factor), new Map<string, string>());
175-
renames = await readCsv('configs/renames.csv', (acc, r) => acc.set(r.code, r.actual), new Map<string, string>());
176-
sums = await readCsv('configs/sums.csv', (acc, r) => acc.set(r.code, r.expression), new Map<string, string>());
177-
orders = await readCsv('configs/orders.csv', (acc, r) => {
174+
factors = await readCsv(path.join(import.meta.dirname || '', 'configs/factors.csv'), (acc, r) => acc.set(r.code, r.factor), new Map<string, string>());
175+
renames = await readCsv(path.join(import.meta.dirname || '', 'configs/renames.csv'), (acc, r) => acc.set(r.code, r.actual), new Map<string, string>());
176+
sums = await readCsv(path.join(import.meta.dirname || '', 'configs/sums.csv'), (acc, r) => acc.set(r.code, r.expression), new Map<string, string>());
177+
orders = await readCsv(path.join(import.meta.dirname || '', 'configs/orders.csv'), (acc, r) => {
178178
const arr = acc.get(r.before) || [];
179179
acc.set(r.before, arr);
180180
arr.push(r.code);
181181
}, new Map<string, string[]>());
182-
for (const file of Deno.readDirSync('assets')) {
182+
for (const file of Deno.readDirSync(path.join(import.meta.dirname || '', 'assets'))) {
183183
if (!file.name.endsWith('.csv')) continue;
184-
await readAsset(path.join('assets', file.name));
184+
await readAsset(path.join(import.meta.dirname || '', 'assets', file.name));
185185
}
186186
nullToZero(dat);
187187
sumAll(dat);
@@ -196,16 +196,12 @@ async function generateIndexCsv() {
196196
}
197197
a = a.substring(0, a.length-1) + '\n';
198198
}
199-
Deno.writeTextFileSync('index.csv', a);
199+
await Deno.writeTextFile(path.join(import.meta.dirname || '', 'index.csv'), a);
200200
}
201-
await main();
201+
await writeIndexMain();
202202
}
203203

204204

205-
206-
207-
// Finally.
208-
async function main() {
209-
await generateIndexCsv();
205+
export async function build() {
206+
await writeIndex();
210207
}
211-
main();

compositionstats/build.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function formatCompositionRow(row: Record<string, string | number>, code: string
6161
/** Get the minimum, maximum and average composition of all foods. */
6262
async function minMaxAvgComposition() {
6363
// Load the compositions of all foods.
64-
const data = await Deno.readTextFile(path.join(import.meta.dirname || "", "index.csv"));
64+
const data = await Deno.readTextFile(path.join(import.meta.dirname || "", "../compositions/index.csv"));
6565
const records: Record<string, string | number>[] = csv.parse(data, {skipFirstRow: true, comment: "#"});
6666
for (const row of records) {
6767
for (const k in row) {
@@ -88,12 +88,16 @@ async function minMaxAvgComposition() {
8888

8989

9090
/** Generate the stats CSV file. */
91-
async function main() {
91+
async function writeIndex() {
9292
const [min, max, avg] = await minMaxAvgComposition();
9393
const keys = Object.keys(min);
9494
const data = keys.join(",") + "\n" + [min, max, avg].map(row => {
9595
return keys.map(k => typeof row[k] === "number"? `${round(row[k] as number)}` : `"${row[k]}"`);
9696
}).join("\n") + "\n";
9797
await Deno.writeTextFile(path.join(import.meta.dirname || "", "index.csv"), data);
9898
}
99-
main();
99+
100+
101+
export async function build() {
102+
await writeIndex();
103+
}

0 commit comments

Comments
 (0)