pdfix-sdk
Version:
Take the full control over the PDF documents with PDFix SDK. Leverage the advantages of the PDFix SDK WebAssembly build for use in both Node.js and web applications
104 lines (98 loc) • 2.56 kB
JavaScript
const fs = require('fs');
const sourcePathNode = './bin/node';
const sourcePathWeb = './bin/web';
const targetPath = (process.env.npm_config_path) ? process.env.npm_config_path : './dist/web';
const others = [
{
source: sourcePathNode + '/pdfix_wasm.d.ts',
target: targetPath + '/pdfix_wasm.d.ts'
},
{
source: './index.js',
target: targetPath + '/index.js'
},
{
source: './index.d.ts',
target: targetPath + '/index.d.ts'
}
];
const patchesForIndex = [
{
replace: 'const PDFIX_WASM = require("./bin/node/pdfix_wasm.js");',
with: "import PDFIX_WASM from './pdfix_wasm';"
},
{
replace: 'class Pdfix {',
with: 'export class Pdfix {'
},
{
replace: 'module.exports = Pdfix;',
with: ''
},
{
replace: 'const fs = require("fs");',
with: '//const fs = require("fs");'
},
{
replace: 'const buffer = new Uint8Array( fs.readFileSync(path, null).buffer );',
with: '//const buffer = new Uint8Array( fs.readFileSync(path, null).buffer );'
},
{
replace: 'return this.openDocumentFromBuffer(buffer);',
with: '//return this.openDocumentFromBuffer(buffer);'
}
];
const patchesForIndexDTs = [
{
replace: "declare module 'pdfix-sdk' {",
with: ''
},
{
replace: 'class Pdfix {',
with: 'export class Pdfix {'
},
{
replace: 'openDocumentFromPath(path: string): PdfDoc;',
with: 'openDocumentFromPath(path: string): PdfDoc | undefined;'
},
{
replace: 'export = Pdfix;',
with: ''
},
{
replace: '} // npm-end',
with: ''
}
];
function copy(source, destination) {
console.log('Copying', source, 'to', destination);
fs.writeFileSync(destination, fs.readFileSync(source));
}
function patch(file, patches) {
console.log('Patching', file);
const r = fs.readFileSync(file, 'utf-8');
const lines = r.split('\n');
let out = '';
for (let line of lines) {
for (let find of patches) {
if (line.includes(find.replace)) {
line = line.replace(find.replace, find.with);
}
}
out += line;
}
fs.writeFileSync(file, out);
}
if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath, { recursive: true });
console.log('Folder created (or already exists):', targetPath);
}
const sourceFiles = fs.readdirSync(sourcePathWeb);
for (let file of sourceFiles) {
copy(sourcePathWeb + '/' + file, targetPath + '/' + file);
}
for (let file of others) {
copy(file.source, file.target);
}
patch(targetPath + '/index.js', patchesForIndex);
patch(targetPath + '/index.d.ts', patchesForIndexDTs);