2 const fs = require('fs');
3 const path = require('path');
4 const axios = require('axios');
5 const mammoth = require('mammoth');
7 // BookStack API variables
8 // Uses values on the environment unless hardcoded
9 // To hardcode, add values to the empty strings in the below.
10 const bookStackConfig = {
11 base_url: '' || process.env.BS_URL,
12 token_id: '' || process.env.BS_TOKEN_ID,
13 token_secret: '' || process.env.BS_TOKEN_SECRET,
19 // Check arguments provided
20 if (process.argv.length < 4) {
21 console.error('Both <docx_file> and <book_slug> arguments need to be provided');
25 // Get arguments passed via command
26 const [_exec, _script, docxFile, bookSlug] = process.argv;
28 // Check the docx file exists
29 if (!fs.existsSync(docxFile)) {
30 console.error(`Provided docx file "${docxFile}" could not be found`);
34 // Create an axios instance for our API
35 const api = axios.create({
36 baseURL: bookStackConfig.base_url.replace(/\/$/, '') + '/api/',
38 headers: { 'Authorization' : `Token ${bookStackConfig.token_id}:${bookStackConfig.token_secret}` },
41 // Wrap the rest of our code in an async function so we can await within.
44 // Fetch the related book to ensure it exists
45 const {data: bookSearch} = await api.get(`/books?filter[slug]=${encodeURIComponent(bookSlug)}`);
46 if (bookSearch.data.length === 0) {
47 console.error(`Book with a slug of "${bookSlug}" could not be found`);
50 const book = bookSearch.data[0];
52 // Convert our document
53 const {value: html, messages} = await mammoth.convertToHtml({path: docxFile});
55 // Create a name from our document file name
56 let {name} = path.parse(docxFile);
57 name = name.replace(/[-_]/g, ' ');
60 const {data: page} = await api.post('/pages', {
67 console.info(`File converted and created as a page.`);
68 console.info(` - Page ID: ${page.id}`);
69 console.info(` - Page Name: ${page.name}`);
70 console.info(`====================================`);
71 console.info(`Conversion occurred with ${messages.length} message(s):`);
72 for (const message of messages) {
73 console.warn(`[${message.type}] ${message.message}`);
79 console.error(`Request failed with status ${err.response.status} [${err.response.statusText}]`);
82 // Output all other errors