Skip to content

Commit

Permalink
Upgrade Compiling/Packaging to Typescript/Webpack
Browse files Browse the repository at this point in the history
1. Slowly begin migrating code to Typescript from raw Javascript
2. Instead of relying on built-in node module bundling, use webpack

These are prerequisites (in a very yak-shaving way) for adding unit
testing to the code via Jest. See
OpenTechStrategies#20.
  • Loading branch information
hawkinsw committed May 1, 2021
1 parent 99bfcf6 commit 1a49b50
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 28 deletions.
10 changes: 7 additions & 3 deletions services/api/mw2pdf/INSTALL.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Installing

This script requires node.js to be installed.
In order to run the script, you will first need to build the dependencies using `npm install`
In order to run the script, you will first need to build the dependencies using `yarn install`

## Running the script
# Building

You can run the script using `node index.js`
`yarn build`

# Running

You can run the script using `node built/main.js`
13 changes: 10 additions & 3 deletions services/api/mw2pdf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "mw2pdf",
"version": "0.0.1",
"description": "Given mediawiki URL, output PDF",
"main": "index.js",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.config.js"
},
"type": "module",
"author": "Open Tech Strategies",
"license": "AGPL-3.0-or-later",
"dependencies": {
Expand All @@ -19,5 +19,12 @@
"pdfmake": "^0.1.68",
"puppeteer": "^5.5.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"awesome-typescript-loader": "^5.2.1",
"typescript": "^4.2.4",
"webpack": "^5.36.2",
"webpack-cli": "^4.6.0",
"webpack-node-externals": "^3.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import puppeteer from 'puppeteer'
import {
PdfGenerator
} from './PdfGenerator.js'
} from './PdfGenerator'
import {
makeLoginRequest,
makeLoginTokenRequest,
extractLoginToken,
} from '../utils/mediaWiki.js'
} from '../utils/mediaWiki'
import {
parseCookies,
toCookieString,
} from '../utils/cookies.js'
} from '../utils/cookies'
import {
deletePdfs,
} from '../utils/files.js'
} from '../utils/files'

export class MediaWikiSession {
constructor(settings) {

cookies: any;

constructor(settings?: any) {
this.cookies = {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'fs'
import pdfjsLib from 'pdfjs-dist/es5/build/pdf.js'
import { getDocument } from 'pdfjs-dist/es5/build/pdf'

export class Pdf {

path: any;

constructor(path, {
title = '',
} = {}) {
Expand All @@ -16,7 +19,7 @@ export class Pdf {

async getInfo() {
const data = fs.readFileSync(this.path)
const pdf = await (pdfjsLib.getDocument(data).promise)
const pdf = await (getDocument(data).promise)
const page = await pdf.getPage(1)
return {
numPages: pdf.numPages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import {
rgb,
StandardFonts,
} from 'pdf-lib'
import { Pdf } from './Pdf.js'
import { Pdf } from './Pdf'

// The below two lines are from
// https://stackoverflow.com/questions/32705219/nodejs-accessing-file-with-relative-path/32707530#32707530
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export class PdfGenerator {

directory: any;

constructor(directory = '') {
Object.assign(
this,
Expand Down Expand Up @@ -122,13 +125,13 @@ export class PdfGenerator {
async generatePdfFromScaffold(scaffold, outPdf = this.generatePdfObject()) {
const printer = new PdfPrinter({
Roboto: {
normal: path.join(__dirname, '../fonts/Roboto-Regular.ttf'),
bold: path.join(__dirname, '../fonts/Roboto-Medium.ttf'),
italics: path.join(__dirname, '../fonts/Roboto-Italic.ttf'),
bolditalics: path.join(__dirname, '../fonts/Roboto-MediumItalic.ttf'),
normal: path.join(__dirname, '../../fonts/Roboto-Regular.ttf'),
bold: path.join(__dirname, '../../fonts/Roboto-Medium.ttf'),
italics: path.join(__dirname, '../../fonts/Roboto-Italic.ttf'),
bolditalics: path.join(__dirname, '../../fonts/Roboto-MediumItalic.ttf'),
},
SourceCodePro: {
normal: path.join(__dirname, '../fonts/SourceCodePro-Regular.ttf'),
normal: path.join(__dirname, '../../fonts/SourceCodePro-Regular.ttf'),
}
})
// TODO: what is .end doing in this block -- is it sync? does it have to be called?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs'
import program from 'commander'
import { getApiUrlFromMediaWikiUrl } from './utils/mediaWiki.js'
import { MediaWikiSession } from './classes/MediaWikiSession.js'
import { getApiUrlFromMediaWikiUrl } from './utils/mediaWiki'
import { MediaWikiSession } from './classes/MediaWikiSession'

program.version('0.0.1')
program
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from 'node-fetch'
import { toCookieString } from './cookies.js'
import { toCookieString } from './cookies'

export async function makeLoginTokenRequest(apiUrl) {
const loginTokenRequestParams = new URLSearchParams({
Expand Down Expand Up @@ -27,11 +27,11 @@ export async function extractLoginToken(loginTokenRequestResult) {
}

export async function makeLoginRequest({
apiUrl,
username,
password,
loginToken,
cookies,
apiUrl = "",
username = "",
password = "",
loginToken = "",
cookies = "",
} = {}) {
const loginActionRequestBody = new URLSearchParams({
format: 'json',
Expand Down
23 changes: 23 additions & 0 deletions services/api/mw2pdf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "es2017",
"outDir": "compiled/",
"strict": false,
"baseUrl": "./",
"esModuleInterop": true,
"module": "esnext",
"lib": ["dom", "es2016"],
"sourceMap": true,
"moduleResolution": "node"
},
"include": [
"src",
"src/classes",
"src/utils"
],
"exclude": [
"node_modules",
"built",
".git"
]
}
26 changes: 26 additions & 0 deletions services/api/mw2pdf/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = [{
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'built'),
},
resolve: {
extensions: ['.js', '.ts'],
},
target: 'node',
entry: { main: path.resolve(__dirname, 'src/index.ts') },
module: {
rules: [{
test: /\.ts$/,
loader: 'awesome-typescript-loader',
options: {
configFile: 'tsconfig.json',
},
exclude: [/node_modules/],
}]
},
externals: [nodeExternals()],
mode: 'none',
}];

0 comments on commit 1a49b50

Please sign in to comment.