Skip to content

Commit

Permalink
ts: activate linting
Browse files Browse the repository at this point in the history
  • Loading branch information
majodev committed Feb 20, 2023
1 parent baa5591 commit f506f07
Show file tree
Hide file tree
Showing 25 changed files with 1,180 additions and 449 deletions.
12 changes: 10 additions & 2 deletions .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@
"customizations": {
"vscode": {
"settings": {
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.formatOnType": false,
"editor.formatOnPaste": false
}
},
"extensions": [
"eamodio.gitlens"
"eamodio.gitlens",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
]
}
}
Expand Down
10 changes: 10 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
ignorePatterns: [".eslintrc.cjs", "Gruntfile.js", "dist/**/*.js", "client/**/*.js"],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
parserOptions: {
project: "./tsconfig.json"
}
};
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"singleQuote": false,
"printWidth": 140
}
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ RUN apt-get update && apt-get install -y -q --no-install-recommends \

# global npm installs
RUN npm install -g grunt-cli@1.2.0 \
&& npm cache clean --force

# git config --global --add safe.directory /app
&& npm cache clean --force

WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = function (grunt) {
tasks: ['injector:css']
},
mochaTest: {
files: ['server/**/*.ts', 'server/**/*.js'],
files: ['server/**/*.spec.ts', 'server/**/*.spec.js'],
tasks: ['env:test', 'mochaTest']
},
injectLess: {
Expand Down
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ services:
GOOGLE_FONTS_API_KEY: ${GOOGLE_FONTS_API_KEY}

# Overrides default command so things don't shut down after the process ends.
command: /bin/sh -c "while sleep 1000; do :; done"
command:
- /bin/sh
- -c
- |
git config --global --add safe.directory /app
while sleep 1000; do :; done
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
"@types/node": "18",
"@types/speakingurl": "^13.0.3",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"bower": "^1.3.8",
"connect-livereload": "~0.4.0",
"errorhandler": "~1.0.0",
"eslint": "^8.34.0",
"grunt": "~0.4.4",
"grunt-angular-templates": "^0.5.4",
"grunt-asset-injector": "^0.1.0",
Expand Down Expand Up @@ -75,6 +78,9 @@
"grunt-wiredep": "~1.8.0",
"jit-grunt": "^0.5.0",
"mocha": "^10.2.0",
"prettier": "^2.8.4",
"prettier-eslint": "^15.0.1",
"prettier-plugin-organize-imports": "^3.2.2",
"punycode": "^1.4.1",
"should": "13.2.3",
"supertest": "6.3.3",
Expand All @@ -84,6 +90,7 @@
},
"scripts": {
"start": "ts-node server/app.ts",
"lint": "eslint --ext .ts .",
"build": "grunt build",
"test": "grunt test",
"dev": "grunt serve",
Expand Down
63 changes: 30 additions & 33 deletions server/api/fonts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NextFunction, Request, Response } from "express";
import * as fs from "fs";
import * as JSZip from "jszip";
import * as _ from "lodash";
import * as path from "path";
import * as stream from "stream";
import { Request, Response, NextFunction } from "express";
import { loadFontBundle, loadFontItems, loadSubsetMap, loadVariantItems, loadFontFilePaths } from "../logic/core";
import { IUserAgents } from "../config";
import * as JSZip from "jszip";
import * as path from "path";
import * as fs from "fs";
import { loadFontBundle, loadFontFilePaths, loadFontItems, loadSubsetMap, loadVariantItems } from "../logic/core";

// Get list of fonts
// /api/fonts
Expand All @@ -23,7 +23,6 @@ interface IAPIListFont {
}
export async function getApiFonts(req: Request, res: Response<IAPIListFont[]>, next: NextFunction) {
try {

const fonts = loadFontItems();

const apiListFonts: IAPIListFont[] = _.map(fonts, (font) => {
Expand All @@ -37,7 +36,7 @@ export async function getApiFonts(req: Request, res: Response<IAPIListFont[]>, n
lastModified: font.lastModified,
popularity: font.popularity,
defSubset: font.defSubset,
defVariant: font.defVariant
defVariant: font.defVariant,
};
});

Expand All @@ -61,7 +60,7 @@ interface IAPIFont {
defVariant: string;
subsetMap: {
[subset: string]: boolean;
}
};
storeID: string;
variants: {
id: string;
Expand All @@ -76,29 +75,27 @@ interface IAPIFont {
}[];
}
export async function getApiFontsById(req: Request, res: Response<IAPIFont | string | NodeJS.WritableStream>, next: NextFunction) {

try {
// get the subset string if it was supplied...
// get the subset string if it was supplied...
// e.g. "subset=latin,latin-ext," will be transformed into ["latin","latin-ext"] (non whitespace arrays)
const subsets = _.isString(req.query.subsets) ? _.without(req.query.subsets.split(/[,]+/), '') : null;
const subsets = _.isString(req.query.subsets) ? _.without(req.query.subsets.split(/[,]+/), "") : null;

const fontBundle = await loadFontBundle(req.params.id, subsets);

if (_.isNil(fontBundle)) {
return res.status(404).send('Not found');
return res.status(404).send("Not found");
}

const subsetMap = loadSubsetMap(fontBundle);
const variantItems = await loadVariantItems(fontBundle);

if (_.isNil(variantItems)) {
return res.status(404).send('Not found');
return res.status(404).send("Not found");
}

// default case: json serialize...
if (req.query.download !== "zip") {

const { font, storeID } = fontBundle;
const { font } = fontBundle;

const apiFont: IAPIFont = {
id: font.id,
Expand All @@ -119,59 +116,59 @@ export async function getApiFontsById(req: Request, res: Response<IAPIFont | str
fontFamily: variant.fontFamily,
fontStyle: variant.fontStyle,
fontWeight: variant.fontWeight,
...(_.reduce(variant.urls, (sum, vurl) => {
sum[vurl.format] = vurl.url;
return sum;
}, {} as IUserAgents))
..._.reduce(
variant.urls,
(sum, vurl) => {
sum[vurl.format] = vurl.url;
return sum;
},
{} as IUserAgents
),
};
})
}),
};

return res.json(apiFont);
}

// otherwise: download as zip
const variants = _.isString(req.query.variants) ? _.without(req.query.variants.split(/[,]+/), '') : null;
const formats = _.isString(req.query.formats) ? _.without(req.query.formats.split(/[,]+/), '') : null;

const url = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
const variants = _.isString(req.query.variants) ? _.without(req.query.variants.split(/[,]+/), "") : null;
const formats = _.isString(req.query.formats) ? _.without(req.query.formats.split(/[,]+/), "") : null;

const fontFilePaths = await loadFontFilePaths(fontBundle, variantItems);

const filteredFiles = _.filter(fontFilePaths, (file) => {
return (_.isNil(variants) || _.includes(variants, file.variant))
&& (_.isNil(formats) || _.includes(formats, file.format));
return (_.isNil(variants) || _.includes(variants, file.variant)) && (_.isNil(formats) || _.includes(formats, file.format));
});

if (filteredFiles.length === 0) {
return res.status(404).send('Not found');
return res.status(404).send("Not found");
}

const archive = new JSZip();

_.each(filteredFiles, function (file) {
archive.file(path.basename(file.path), fs.createReadStream(file.path))
archive.file(path.basename(file.path), fs.createReadStream(file.path));
});

const zipFilename = fontBundle.font.id + "-" + fontBundle.font.version + "-" + fontBundle.storeID + '.zip';
const zipFilename = `${fontBundle.font.id}-${fontBundle.font.version}-${fontBundle.subsets.join("_")}.zip`

// Tell the browser that this is a zip file.
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': 'attachment; filename=' + zipFilename
"Content-Type": "application/zip",
"Content-disposition": `attachment; filename=${zipFilename}`,
});

const zipStream = archive.generateNodeStream({
streamFiles: true,
compression: 'DEFLATE'
compression: "DEFLATE",
});

return stream.pipeline(zipStream, res, function (err) {
if (err) {
// noop, client cancelled.
}
});

} catch (e) {
next(e);
}
Expand Down
Loading

0 comments on commit f506f07

Please sign in to comment.