Skip to content

Commit

Permalink
fix(icons): make it possible to build without env vars, while fetchin…
Browse files Browse the repository at this point in the history
…g icons throws an error if env vars are not defined
  • Loading branch information
oliverschuerch committed Feb 6, 2025
1 parent 8b82fbb commit 3082fb4
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 33 deletions.
11 changes: 3 additions & 8 deletions packages/icons/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IconSet, JsonReport } from './models/icon.model';
import iconSets from './iconsets.config';
import { IconSet, JsonReport } from './models/icon.model';

import { checkEnvVarsExist } from './utilities/environment';
import { setup } from './utilities/download/setup';
import { getBaseReport, coloredLogMessage } from './utilities/shared';
import { fetchPage } from './utilities/download/fetchPage';
Expand All @@ -9,8 +9,6 @@ import { format } from './utilities/download/format';
import { updateReport, writeReport } from './utilities/download/report';
import buildSVGs from './utilities/build';

import { MESSAGE_ENV_VARS_MISSING_ERROR } from './utilities/constants';

async function fetchSVGs() {
setup();

Expand Down Expand Up @@ -77,9 +75,6 @@ async function downloadIconSet(
return writeReport(iconSet, report);
}

// start fetching icons if every iconSet has an "apiUrl" defined
if (iconSets.every(iconSet => iconSet.apiUrl)) {
if (checkEnvVarsExist()) {
fetchSVGs();
} else {
throw new Error(MESSAGE_ENV_VARS_MISSING_ERROR);
}
2 changes: 1 addition & 1 deletion packages/icons/src/iconsets.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IconSet } from './models/icon.model';
import path from 'path';
import { IconSet } from './models/icon.model';
import { urls } from './utilities/environment';
import { SOURCE_PATH } from './utilities/constants';

Expand Down
2 changes: 1 addition & 1 deletion packages/icons/src/models/icon.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
Type,
TypeFilter,
VariantMIME,
Expand Down
4 changes: 2 additions & 2 deletions packages/icons/src/utilities/build/createFiles.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { IconSetGroups } from '../../models/icon.model';
import svgoOptions from '../../../svgo.config.ui';
import fs from 'fs';
import path from 'path';
import { HTMLElement, parse } from 'node-html-parser';
import { optimize } from 'svgo';
import svgoOptions from '../../../svgo.config.ui';
import { IconSetGroups } from '../../models/icon.model';

import {
ID_PREFIX,
Expand Down
4 changes: 2 additions & 2 deletions packages/icons/src/utilities/build/iconSetGroups.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Icon, JsonReport, IconSetGroups, GroupItem } from '../../models/icon.model';
import iconSets from '../../iconsets.config';
import fs from 'fs';
import path from 'path';
import iconSets from '../../iconsets.config';
import { Icon, JsonReport, IconSetGroups, GroupItem } from '../../models/icon.model';
import { getNameParts } from '../shared';

import { ID_SEPERATOR, ID_UNWANTED_PARTS, UI_ICON_SIZES } from '../constants';
Expand Down
1 change: 0 additions & 1 deletion packages/icons/src/utilities/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getIconSetGroups } from './iconSetGroups';
import { createFiles } from './createFiles';
import { writeReport } from './report';
import { coloredLogMessage } from '../shared';

import { OUTPUT_PATH, OUTPUT_PATH_ICONS } from '../constants';

const iconOutputDirectory = path.resolve(OUTPUT_PATH_ICONS);
Expand Down
2 changes: 1 addition & 1 deletion packages/icons/src/utilities/build/report.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OutputIcon, JsonReport, GroupItem, IconSetGroups } from '../../models/icon.model';
import fs from 'fs';
import path from 'path';
import { version } from '../../../package.json';
import { OutputIcon, JsonReport, GroupItem, IconSetGroups } from '../../models/icon.model';
import { getBaseReport, sortIcons } from '../shared';

export function writeReport(
Expand Down
2 changes: 1 addition & 1 deletion packages/icons/src/utilities/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JsonReport } from '../models/icon.model';
import { version } from '../../package.json';
import { JsonReport } from '../models/icon.model';
import { coloredLogMessage } from './shared';

export const SOURCE_PATH = 'src/icons';
Expand Down
4 changes: 2 additions & 2 deletions packages/icons/src/utilities/download/fetchFile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fetch, { Response } from 'node-fetch';
import {
import type {
Businessfield,
Type,
TypeFilter,
VariantMIME,
} from '../../models/censhare-result-page.model';
import fetch, { Response } from 'node-fetch';
import mockFs from 'mock-fs';
import { fetchFile } from './fetchFile';

Expand Down
4 changes: 2 additions & 2 deletions packages/icons/src/utilities/download/fetchFile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Icon } from '../../models/icon.model';
import svgoOptions from '../../../svgo.config';
import fetch from 'node-fetch';
import fs from 'fs';
import path from 'path';
import { optimize } from 'svgo';
import svgoOptions from '../../../svgo.config';
import { requestInit } from '../environment';
import { Icon } from '../../models/icon.model';

export async function fetchFile(icon: Icon, output: string) {
if (!icon.meta.downloadLink) {
Expand Down
2 changes: 1 addition & 1 deletion packages/icons/src/utilities/download/fetchPage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CenshareError, CenshareResultPage } from '../../models/censhare-result-page.model';
import fetch from 'node-fetch';
import { requestInit } from '../environment';
import { CenshareError, CenshareResultPage } from '../../models/censhare-result-page.model';
import { coloredLogMessage } from '../shared';

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/icons/src/utilities/download/format.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import testResult from '../../../tests/fixtures/test-result.json';
import { CenshareResultPage } from '../../models/censhare-result-page.model';
import type { CenshareResultPage } from '../../models/censhare-result-page.model';
import { format } from './format';
import testResult from '../../../tests/fixtures/test-result.json';

describe('format', () => {
it('Should format the result object to a meaningful data structure', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/icons/src/utilities/download/format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Icon } from '../../models/icon.model';
import type { CenshareResultPage, CenshareResult } from '../../models/censhare-result-page.model';
import path from 'path';
import { CenshareResultPage, CenshareResult } from '../../models/censhare-result-page.model';
import { Icon } from '../../models/icon.model';

const excludedRanges = [[4000, 7999]];
const excludedKeywords = ['Piktogramme "Die Post" ab 2017', 'Piktogramme "Die Post" 2017'];
Expand Down
2 changes: 1 addition & 1 deletion packages/icons/src/utilities/download/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import iconSets from '../../iconsets.config';
import fs from 'fs';

export function setup() {
iconSets.forEach(iconsSet => {
Expand Down
12 changes: 8 additions & 4 deletions packages/icons/src/utilities/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const pw = process.env.CEN_PASSWORD;
const proxy = process.env.HTTPS_PROXY;
const passphrase = Buffer.from(`${user}:${pw}`).toString('base64');

if (!user || !pw) {
throw new Error(MESSAGE_ENV_VARS_MISSING_ERROR);
}

export const urls = {
post: process.env.CEN_URL_ICONSET_POST ?? '',
ui: process.env.CEN_URL_ICONSET_UI ?? '',
Expand All @@ -26,3 +22,11 @@ export const requestInit: RequestInit = {
},
agent: proxy ? new HttpsProxyAgent(proxy) : undefined,
};

export function checkEnvVarsExist(): boolean {
if (!user || !pw || Object.values(urls).some(v => !v)) {
throw new Error(MESSAGE_ENV_VARS_MISSING_ERROR);
}

return true;
}
3 changes: 1 addition & 2 deletions packages/icons/src/utilities/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Icon, OutputIcon, JsonReport } from '../models/icon.model';
import { styleText } from 'node:util';
import { Icon, OutputIcon, JsonReport } from '../models/icon.model';

import { REPORT } from './constants';

export function getBaseReport(): JsonReport {
Expand Down

0 comments on commit 3082fb4

Please sign in to comment.