Skip to content

Commit efd8dfb

Browse files
committed
chore: build local translation while developing
1 parent 29f53c5 commit efd8dfb

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

rollup.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import typescript from '@rollup/plugin-typescript';
1414

1515
import { version } from './package.json';
1616
import importString from './scripts/import-string.js';
17+
import bundle from './scripts/lang.js';
1718

1819
config();
1920

@@ -75,6 +76,14 @@ export default [{
7576
}],
7677
external: ['luxon', 'showdown', 'axios', 'tippy.js'],
7778
plugins: [
79+
{
80+
name: 'bundle',
81+
buildStart() {
82+
if (!debug) return;
83+
this.addWatchFile('lang/en');
84+
bundle('lang/underscript.ignore.json');
85+
},
86+
},
7887
alias({
7988
entries: [{
8089
find: 'src',

scripts/lang.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import glob from 'fast-glob';
2-
import { assign, readJSON, sortKeys, writeFile } from './utils.js';
2+
import { assign, isMain, readJSON, sortKeys, writeFile } from './utils.js';
33

4-
async function main() {
4+
export default async function main(filePath = 'lang/underscript.json') {
55
// Load base "en" first
66
const data = await bundle(
77
await glob('lang/en/*.json'),
@@ -30,7 +30,7 @@ async function main() {
3030
const file = JSON.stringify(sortKeys(data), null, 2);
3131
if (!file) return;
3232

33-
await writeFile('lang/underscript.json', file);
33+
await writeFile(filePath, file);
3434
}
3535

3636
/**
@@ -57,4 +57,7 @@ function getFileParts(file = '') {
5757
return [lang, name.substring(0, name.lastIndexOf('.'))];
5858
}
5959

60-
main();
60+
// Only automatically run if main script...
61+
if (isMain(import.meta)) {
62+
main();
63+
}

scripts/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs/promises';
2+
import nodePath from 'node:path';
23

34
export function assign(obj = {}, lang = '', key = '', value = '') {
45
// Only add key if value exists
@@ -28,3 +29,11 @@ export function sortKeys(obj = {}) {
2829
);
2930
return sorted;
3031
}
32+
33+
/**
34+
* Checks if module is the main module
35+
* @param {ImportMeta} meta
36+
*/
37+
export function isMain({ url }) {
38+
return nodePath.normalize(url).endsWith(process.argv[1]);
39+
}

0 commit comments

Comments
 (0)