Skip to content

Commit 6ca663d

Browse files
committed
feat: switch to TypeScript
1 parent 46423ee commit 6ca663d

File tree

9 files changed

+70
-27
lines changed

9 files changed

+70
-27
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"root": true,
33
"extends": [
4-
"@shelf/eslint-config/backend"
4+
"@shelf/eslint-config/typescript"
55
]
66
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ temp
55
yarn.lock
66
*.log
77
.DS_Store
8+
lib/

babel.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
plugins: ['@babel/plugin-proposal-object-rest-spread'],
3+
presets: [
4+
'@babel/preset-typescript',
5+
[
6+
'@babel/preset-env',
7+
{
8+
targets: {
9+
node: '8.10'
10+
}
11+
}
12+
]
13+
]
14+
};

package.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,38 @@
1313
"node": ">=8.10"
1414
},
1515
"scripts": {
16+
"build": "rm -rf lib/ && yarn build:types && babel src --out-dir lib --ignore '**/*.test.ts' --extensions '.ts' --copy-files",
17+
"build:types": "tsc --emitDeclarationOnly --declaration --isolatedModules false --declarationDir lib",
18+
"coverage": "jest --coverage",
1619
"lint": "eslint . --fix",
17-
"test": "echo ok"
20+
"prepack": "yarn build",
21+
"test": "echo ok",
22+
"type-check": "tsc --noEmit",
23+
"type-check:watch": "npm run type-check -- --watch"
1824
},
1925
"files": [
20-
"src",
26+
"lib",
2127
"bin"
2228
],
2329
"main": "src/index.js",
2430
"keywords": [],
2531
"dependencies": {
2632
"@shelf/aws-lambda-brotli-unpacker": "0.0.2",
27-
"del": "^4.0.0"
33+
"del": "4.1.1"
2834
},
2935
"devDependencies": {
36+
"@babel/cli": "7.4.4",
37+
"@babel/core": "7.4.5",
38+
"@babel/preset-env": "7.4.5",
39+
"@babel/preset-typescript": "7.3.3",
3040
"@shelf/eslint-config": "0.3.5",
41+
"@types/jest": "24.0.13",
42+
"@types/node": "8",
3143
"eslint": "5.16.0",
3244
"husky": "2.4.0",
3345
"lint-staged": "8.2.0",
34-
"prettier": "1.18.2"
46+
"prettier": "1.18.2",
47+
"typescript": "3.5.1"
3548
},
3649
"husky": {
3750
"hooks": {

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ execSync(`${loBinary} ${defaultArgs.join(' ')} --convert-to pdf file.docx --outd
4646

4747
### Test
4848

49-
Smoke test that it works: `docker run --rm -v "$PWD":/var/task lambci/lambda:nodejs8.10 src/test.handler`
49+
Smoke test that it works: `yarn build && docker run --rm -v "$PWD":/var/task lambci/lambda:nodejs8.10 lambda.test.handler`
5050

5151
## License
5252

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs');
2-
const del = require('del');
1+
import fs from 'fs';
2+
import del from 'del';
33

4-
module.exports.cleanupTempFiles = function() {
4+
export function cleanupTempFiles() {
55
for (let file of fs.readdirSync(`/tmp`)) {
66
if (file.endsWith('.tmp') === true || file.startsWith('OSL_PIPE')) {
77
try {
@@ -10,4 +10,4 @@ module.exports.cleanupTempFiles = function() {
1010
} catch (error) {}
1111
}
1212
}
13-
};
13+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
const {unpack} = require('@shelf/aws-lambda-brotli-unpacker');
2-
const {execSync} = require('child_process');
3-
const path = require('path');
4-
const defaultArgs = require('./args');
5-
const {cleanupTempFiles} = require('./cleanup');
1+
import {unpack} from '@shelf/aws-lambda-brotli-unpacker';
2+
import {execSync} from 'child_process';
3+
import path from 'path';
4+
import {cleanupTempFiles} from './cleanup';
5+
import defaultArgsJSON from './args.json';
66

7-
module.exports.defaultArgs = defaultArgs;
7+
export const defaultArgs = defaultArgsJSON;
88

99
const inputPath = path.join(__dirname, '..', 'bin', 'lo.tar.br');
1010
const outputPath = '/tmp/instdir/program/soffice';
1111

12-
// see https://github.com/alixaxel/chrome-aws-lambda
13-
module.exports.getExecutablePath = async function() {
14-
cleanupTempFiles();
15-
16-
return unpack({inputPath, outputPath});
17-
};
18-
1912
/**
2013
* Converts a file in /tmp to PDF
2114
* @param {String} filePath Absolute path to file to convert located in /tmp directory
2215
* @return {Promise<String>} Logs from spawning LibreOffice process
2316
*/
24-
module.exports.convertFileToPDF = async function(filePath) {
25-
const binary = await module.exports.getExecutablePath();
17+
export async function convertFileToPDF(filePath): Promise<string> {
18+
const binary = await getExecutablePath();
2619

2720
const logs = execSync(
2821
`cd /tmp && ${binary} ${defaultArgs.join(' ')} --convert-to pdf --outdir /tmp ${filePath}`
@@ -31,4 +24,11 @@ module.exports.convertFileToPDF = async function(filePath) {
3124
execSync(`rm /tmp/${filePath}`);
3225

3326
return logs.toString('utf8');
34-
};
27+
}
28+
29+
// see https://github.com/alixaxel/chrome-aws-lambda
30+
export async function getExecutablePath(): Promise<string> {
31+
cleanupTempFiles();
32+
33+
return unpack({inputPath, outputPath});
34+
}

src/test.js renamed to test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
12
const {writeFileSync} = require('fs');
2-
const {convertFileToPDF} = require('./');
3+
const {convertFileToPDF} = require('./lib');
34

45
module.exports.handler = async () => {
56
writeFileSync('/tmp/test.txt', Buffer.from('Hello World!'));

tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": true,
4+
"esModuleInterop": true,
5+
"isolatedModules": true,
6+
"module": "commonjs",
7+
"resolveJsonModule": true,
8+
"skipLibCheck": true,
9+
"sourceMap": false,
10+
"target": "esnext"
11+
},
12+
"exclude": ["node_modules", "lambda.test.js"],
13+
"include": ["src"]
14+
}

0 commit comments

Comments
 (0)