Skip to content

Commit b05e59c

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

File tree

14 files changed

+368
-62
lines changed

14 files changed

+368
-62
lines changed

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