Skip to content

Commit

Permalink
Commit the build result for local operation check
Browse files Browse the repository at this point in the history
  • Loading branch information
daido1976 committed Nov 13, 2024
1 parent e969b90 commit f0887ea
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.vscode/
.serverless/
coverage/
lib/
node_modules/

*.log
Expand Down
1 change: 1 addition & 0 deletions lib/cleanup.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function cleanupTempFiles(): Promise<void>;
23 changes: 23 additions & 0 deletions lib/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cleanupTempFiles = cleanupTempFiles;
var _promises = _interopRequireDefault(require("fs/promises"));
var _del = _interopRequireDefault(require("del"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// Removes temp files generated by LibreOffice
async function cleanupTempFiles() {
const files = await _promises.default.readdir(`/tmp`);
for (const file of files) {
if (file.endsWith('.tmp') === true || file.startsWith('OSL_PIPE')) {
try {
await (0, _del.default)([`/tmp/${file}`, `/tmp/${file}/*`], {
force: true
});
// eslint-disable-next-line no-empty
} catch (error) {}
}
}
}
2 changes: 2 additions & 0 deletions lib/convert.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare const DEFAULT_ARGS: string[];
export declare function convertTo(filename: string, format: string): Promise<string>;
33 changes: 33 additions & 0 deletions lib/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DEFAULT_ARGS = void 0;
exports.convertTo = convertTo;
var _child_process = _interopRequireDefault(require("child_process"));
var _util = _interopRequireDefault(require("util"));
var _cleanup = require("./cleanup");
var _logs = require("./logs");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const exec = _util.default.promisify(_child_process.default.exec);
const DEFAULT_ARGS = ['--headless', '--invisible', '--nodefault', '--view', '--nolockcheck', '--nologo', '--norestore'];
exports.DEFAULT_ARGS = DEFAULT_ARGS;
const LO_BINARY_PATH = process.env.LO_BINARY_PATH ?? 'libreoffice7.6';
async function convertTo(filename, format) {
await (0, _cleanup.cleanupTempFiles)();
const argumentsString = DEFAULT_ARGS.join(' ');
const outputFilename = filename.split(/\\ /).join(' ');
const cmd = `cd /tmp && ${LO_BINARY_PATH} ${argumentsString} --convert-to ${format} --outdir /tmp '/tmp/${outputFilename}'`;
let logs;

// due to an unknown issue, we need to run command twice
try {
logs = (await exec(cmd)).stdout;
} catch (e) {
logs = (await exec(cmd)).stdout;
}
await exec(`rm '/tmp/${outputFilename}'`);
await (0, _cleanup.cleanupTempFiles)();
return (0, _logs.getConvertedFilePath)(logs.toString());
}
2 changes: 2 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './convert';
export * from './validations';
27 changes: 27 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
var _convert = require("./convert");
Object.keys(_convert).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _convert[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _convert[key];
}
});
});
var _validations = require("./validations");
Object.keys(_validations).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _validations[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _validations[key];
}
});
});
1 change: 1 addition & 0 deletions lib/logs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function getConvertedFilePath(logs: string): string;
15 changes: 15 additions & 0 deletions lib/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getConvertedFilePath = getConvertedFilePath;
function getConvertedFilePath(logs) {
try {
return logs.match(/\/tmp\/.+->\s(\/tmp\/.+) using/)[1];
} catch (e) {
const ErrorWithExtendedMessage = new Error(e);
ErrorWithExtendedMessage.message += `;\tTried to parse string: "${logs}"`;
throw ErrorWithExtendedMessage;
}
}
1 change: 1 addition & 0 deletions lib/validations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function canBeConvertedToPDF(filename: string): boolean;
19 changes: 19 additions & 0 deletions lib/validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.canBeConvertedToPDF = canBeConvertedToPDF;
var _isVideo = _interopRequireDefault(require("is-video"));
var _isImage = _interopRequireDefault(require("is-image"));
var _isAudioFilepath = _interopRequireDefault(require("@shelf/is-audio-filepath"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const UNSUPPORTED_FILE_EXTENSIONS = ['.chm', '.heic', '.gdoc', '.gsheet', '.gslides', '.zip', '.dwg'];
function canBeConvertedToPDF(filename) {
filename = filename.toLowerCase();
const isFileExtensionUnsupported = UNSUPPORTED_FILE_EXTENSIONS.some(ext => filename.endsWith(ext));
if (isFileExtensionUnsupported) {
return false;
}
return !(0, _isImage.default)(filename) && !(0, _isVideo.default)(filename) && !(0, _isAudioFilepath.default)(filename);
}
1 change: 1 addition & 0 deletions test/lib/cleanup.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function cleanupTempFiles(): Promise<void>;
23 changes: 23 additions & 0 deletions test/lib/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cleanupTempFiles = cleanupTempFiles;
var _promises = _interopRequireDefault(require("fs/promises"));
var _del = _interopRequireDefault(require("del"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// Removes temp files generated by LibreOffice
async function cleanupTempFiles() {
const files = await _promises.default.readdir(`/tmp`);
for (const file of files) {
if (file.endsWith('.tmp') === true || file.startsWith('OSL_PIPE')) {
try {
await (0, _del.default)([`/tmp/${file}`, `/tmp/${file}/*`], {
force: true
});
// eslint-disable-next-line no-empty
} catch (error) {}
}
}
}
2 changes: 2 additions & 0 deletions test/lib/convert.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare const DEFAULT_ARGS: string[];
export declare function convertTo(filename: string, format: string): Promise<string>;
33 changes: 33 additions & 0 deletions test/lib/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DEFAULT_ARGS = void 0;
exports.convertTo = convertTo;
var _child_process = _interopRequireDefault(require("child_process"));
var _util = _interopRequireDefault(require("util"));
var _cleanup = require("./cleanup");
var _logs = require("./logs");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const exec = _util.default.promisify(_child_process.default.exec);
const DEFAULT_ARGS = ['--headless', '--invisible', '--nodefault', '--view', '--nolockcheck', '--nologo', '--norestore'];
exports.DEFAULT_ARGS = DEFAULT_ARGS;
const LO_BINARY_PATH = process.env.LO_BINARY_PATH ?? 'libreoffice7.6';
async function convertTo(filename, format) {
await (0, _cleanup.cleanupTempFiles)();
const argumentsString = DEFAULT_ARGS.join(' ');
const outputFilename = filename.split(/\\ /).join(' ');
const cmd = `cd /tmp && ${LO_BINARY_PATH} ${argumentsString} --convert-to ${format} --outdir /tmp '/tmp/${outputFilename}'`;
let logs;

// due to an unknown issue, we need to run command twice
try {
logs = (await exec(cmd)).stdout;
} catch (e) {
logs = (await exec(cmd)).stdout;
}
await exec(`rm '/tmp/${outputFilename}'`);
await (0, _cleanup.cleanupTempFiles)();
return (0, _logs.getConvertedFilePath)(logs.toString());
}
2 changes: 2 additions & 0 deletions test/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './convert';
export * from './validations';
27 changes: 27 additions & 0 deletions test/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
var _convert = require("./convert");
Object.keys(_convert).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _convert[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _convert[key];
}
});
});
var _validations = require("./validations");
Object.keys(_validations).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _validations[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _validations[key];
}
});
});
1 change: 1 addition & 0 deletions test/lib/logs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function getConvertedFilePath(logs: string): string;
15 changes: 15 additions & 0 deletions test/lib/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getConvertedFilePath = getConvertedFilePath;
function getConvertedFilePath(logs) {
try {
return logs.match(/\/tmp\/.+->\s(\/tmp\/.+) using/)[1];
} catch (e) {
const ErrorWithExtendedMessage = new Error(e);
ErrorWithExtendedMessage.message += `;\tTried to parse string: "${logs}"`;
throw ErrorWithExtendedMessage;
}
}
1 change: 1 addition & 0 deletions test/lib/validations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function canBeConvertedToPDF(filename: string): boolean;
19 changes: 19 additions & 0 deletions test/lib/validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.canBeConvertedToPDF = canBeConvertedToPDF;
var _isVideo = _interopRequireDefault(require("is-video"));
var _isImage = _interopRequireDefault(require("is-image"));
var _isAudioFilepath = _interopRequireDefault(require("@shelf/is-audio-filepath"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const UNSUPPORTED_FILE_EXTENSIONS = ['.chm', '.heic', '.gdoc', '.gsheet', '.gslides', '.zip', '.dwg'];
function canBeConvertedToPDF(filename) {
filename = filename.toLowerCase();
const isFileExtensionUnsupported = UNSUPPORTED_FILE_EXTENSIONS.some(ext => filename.endsWith(ext));
if (isFileExtensionUnsupported) {
return false;
}
return !(0, _isImage.default)(filename) && !(0, _isVideo.default)(filename) && !(0, _isAudioFilepath.default)(filename);
}

0 comments on commit f0887ea

Please sign in to comment.