1
0
mirror of https://github.com/twirl/The-API-Book.git synced 2025-05-13 21:26:26 +02:00
The-API-Book/scripts/build.mjs

135 lines
4.3 KiB
JavaScript
Raw Normal View History

2023-07-30 15:25:21 +03:00
import { readFileSync, readdirSync, unlinkSync } from 'fs';
2021-11-15 21:28:32 +03:00
import { resolve as pathResolve } from 'path';
2023-08-31 00:02:49 +03:00
import { init, plugins } from '@twirl/book-builder';
2023-08-29 00:00:38 +03:00
import { templates } from '../src/templates.mjs';
import { apiHighlight } from '../src/api-highlight.mjs';
2023-07-30 15:25:21 +03:00
import { buildLanding } from './build-landing.mjs';
2023-05-28 20:06:49 +03:00
2023-08-31 00:02:49 +03:00
const { flags, args } = process.argv.slice(2).reduce(
({ flags, args }, v) => {
switch (v) {
case '--no-cache':
flags.noCache = true;
break;
}
if (!v.startsWith('--')) {
args.push(v);
}
return { flags, args };
},
{
args: [],
flags: {}
2023-08-27 22:58:31 +03:00
}
2023-08-31 00:02:49 +03:00
);
2021-11-15 21:28:32 +03:00
const l10n = {
en: JSON.parse(readFileSync('./src/en/l10n.json', 'utf-8')),
ru: JSON.parse(readFileSync('./src/ru/l10n.json', 'utf-8'))
};
2023-08-31 00:02:49 +03:00
const langsToBuild = (args[0] && args[0].split(',').map((s) => s.trim())) || [
'ru',
'en'
];
2021-11-15 21:28:32 +03:00
const targets = (
2023-08-31 00:02:49 +03:00
(args[1] && args[1].split(',')) || ['html', 'pdf', 'epub', 'landing']
2021-11-15 21:28:32 +03:00
).reduce((targets, arg) => {
targets[arg.trim()] = true;
return targets;
}, {});
2023-08-31 00:02:49 +03:00
const chapters = args[2];
const noCache = flags.noCache;
if (flags.noCache) {
clean();
}
2022-08-27 23:07:11 +03:00
2021-11-15 21:28:32 +03:00
console.log(`Building langs: ${langsToBuild.join(', ')}`);
2023-08-27 22:58:31 +03:00
(async () => {
for (const lang of langsToBuild) {
await init({
l10n: l10n[lang],
basePath: pathResolve(`src`),
path: pathResolve(`src/${lang}/clean-copy`),
templates,
pipeline: {
css: {
beforeAll: [
plugins.css.backgroundImageDataUri,
plugins.css.fontFaceDataUri
]
},
ast: {
preProcess: [
plugins.ast.h3ToTitle,
plugins.ast.h5Counter,
plugins.ast.aImg,
plugins.ast.imgSrcResolve,
plugins.ast.highlighter({
2023-08-29 00:00:38 +03:00
languages: ['javascript', 'typescript', 'json'],
languageDefinitions: {
json: apiHighlight
}
2023-08-27 22:58:31 +03:00
}),
plugins.ast.ref,
plugins.ast.ghTableFix,
plugins.ast.stat
]
},
htmlSourceValidator: {
validator: 'WHATWG',
ignore: [
'heading-level',
'no-raw-characters',
'wcag/h37',
'no-missing-references'
]
},
html: {
postProcess: [plugins.html.imgDataUri]
}
2021-11-15 21:28:32 +03:00
},
2023-08-27 22:58:31 +03:00
chapters,
noCache
}).then(async (builder) => {
for (const target of Object.keys(targets)) {
if (target !== 'landing') {
await builder.build(
target,
pathResolve(
'docs',
`${l10n[lang].file}.${lang}.${target}`
)
);
console.log(
`Finished lang=${lang} target=${target}\n${Object.entries(
{
sources: 'Sources',
references: 'references',
words: 'words',
characters: 'characters'
}
)
.map(([k, v]) => `${v}: ${builder.structure[k]}`)
.join(', ')}`
);
} else {
buildLanding(builder.structure, lang, l10n, templates);
}
2022-07-07 00:41:03 +03:00
}
2021-11-15 21:28:32 +03:00
});
2023-08-27 22:58:31 +03:00
}
})();
2023-05-28 20:06:49 +03:00
function clean() {
const tmpDir = pathResolve('.', '.tmp');
const files = readdirSync(tmpDir);
for (const fileName of files) {
const file = pathResolve(tmpDir, fileName);
unlinkSync(file);
}
}