Skip to content

Commit

Permalink
fix: apply lint suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Jul 10, 2024
1 parent 849e520 commit 9b12f96
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 86 deletions.
6 changes: 5 additions & 1 deletion .stylelintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"stylelint-config-standard-scss",
"stylelint-config-recommended-vue/scss"
],
"ignoreFiles": ["**/static-ui/dist/*", "**/ati-smarttag.js", "**/skycons.js"],
"ignoreFiles": [
"**/static-ui/dist/*",
"**/ati-smarttag.js",
"**/skycons.js"
],
"rules": {
"declaration-empty-line-before": null,
"indentation": "tab",
Expand Down
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default audiolab(
// a bit more complex to fix (but nice idea in general)
'import/no-default-export': 'off',
'no-console': 'off',
'func-names': 'off',
'jsonc/sort-keys': 'off',

// harder to fix
},
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"url": "https://github.com/swrlab/node-utils"
},
"author": "SWR Audio Lab <[email protected]>",
"engines": {
"node": ">=20"
},
"license": "MIT",
"main": "./index.js",
"scripts": {
"ard:coreId": "node ./scripts/ard/coreId.js",
Expand All @@ -21,7 +19,6 @@
"outdated": "yarn upgrade-interactive",
"reinstall": "rm -rf node_modules && rm yarn.lock && touch yarn.lock && yarn"
},
"license": "MIT",
"dependencies": {
"@google-cloud/storage": "^7.11.3",
"abort-controller": "^3.0.0",
Expand All @@ -38,6 +35,9 @@
"mocha": "^10.6.0",
"prettier": "^3.3.2"
},
"engines": {
"node": ">=20"
},
"packageManager": "[email protected]",
"prettier": "@swrlab/style-guide/prettier"
}
9 changes: 7 additions & 2 deletions packages/storage-wrapper/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ module.exports = async function (uri, _logPrefix, options) {
const path = structure.join('/')

// load file
const file = await this.sdk.gs.bucket(bucket).file(path).download()
const file = await this.sdk.gs
.bucket(bucket)
.file(path)
.download()

// return file
return Promise.resolve(file[0])
Expand All @@ -45,7 +48,9 @@ module.exports = async function (uri, _logPrefix, options) {
}

return Promise.reject(
new Error(`fetching url failed with status > ${file.statusCode}`)
new Error(
`fetching url failed with status > ${file.statusCode}`
)
)
}

Expand Down
10 changes: 8 additions & 2 deletions packages/storage-wrapper/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) {
// move file within gcs
if (keepOriginal !== true) {
// move file
await this.sdk.gs.bucket(bucket).file(path).move(destinationUri)
await this.sdk.gs
.bucket(bucket)
.file(path)
.move(destinationUri)
} else {
// copy file
await this.sdk.gs.bucket(bucket).file(path).copy(destinationUri)
await this.sdk.gs
.bucket(bucket)
.file(path)
.copy(destinationUri)
}

// return ok
Expand Down
8 changes: 4 additions & 4 deletions packages/storage-wrapper/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ const deleteLocalFile = (that, filePath) =>
})

module.exports = async function (uri, buffer, _logPrefix, resumable) {
let structure
let bucket
let path
let structure, bucket, path

if (uri.substr(0, 5).toLowerCase() === 'gs://') {
// google cloud storage
Expand All @@ -53,7 +51,9 @@ module.exports = async function (uri, buffer, _logPrefix, resumable) {
}

// upload file to gcs
await this.sdk.gs.bucket(bucket).upload(tempFilePath, bucketConfig)
await this.sdk.gs
.bucket(bucket)
.upload(tempFilePath, bucketConfig)

// delete local temp file
await deleteLocalFile(this, tempFilePath)
Expand Down
29 changes: 21 additions & 8 deletions packages/undici/request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable prefer-promise-reject-errors */

// load node utils
const undici = require('undici')
const AbortController = require('abort-controller')
Expand All @@ -19,7 +17,8 @@ module.exports = async (url, options) => {

// calculcate redirect
const maxRedirections =
options?.maxRedirections !== null && options?.maxRedirections !== undefined
options?.maxRedirections !== null &&
options?.maxRedirections !== undefined
? options.maxRedirections
: 5

Expand All @@ -31,32 +30,46 @@ module.exports = async (url, options) => {
signal: abortController.signal,
maxRedirections,
}
if (options?.headers) requestOptions.headers = { ...requestOptions.headers, ...options.headers }
if (options?.headers)
requestOptions.headers = {
...requestOptions.headers,
...options.headers,
}

// make actual request
const { statusCode, headers, trailers, body } = await undici.request(url, requestOptions)
const { statusCode, headers, trailers, body } = await undici.request(
url,
requestOptions
)

// remove timeout since request finished beforehand
clearTimeout(abortTimeout)

// set ok
const ok = statusCode >= 200 && statusCode < 300
if (!ok && (!options || options?.reject !== false)) return Promise.reject({ statusCode, ok, headers, url })
if (!ok && (!options || options?.reject !== false))
return Promise.reject({ statusCode, ok, headers, url })

// turn stream into string
const { string, buffer } = await convertReadableStream(body)

// detect/ set redirect
const redirect =
statusCode >= 300 && statusCode < 400 && headers.location ? new URL(headers.location, url) : null
statusCode >= 300 && statusCode < 400 && headers.location
? new URL(headers.location, url)
: null

// fetch header vars
const contentType = headers['content-type']

// parse json if set
let json
try {
json = contentType?.indexOf('application/json') !== -1 ? JSON.parse(string) : null
json =
contentType?.indexOf('application/json') !== -1
? JSON.parse(string)
: null
// eslint-disable-next-line no-unused-vars
} catch (error) {
json = null
}
Expand Down
13 changes: 10 additions & 3 deletions scripts/ard/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ const crawl = async () => {

const remapItem = (item) => {
// remap children if available
const children = item.children ? item.children.map(remapItem) : null
const children = item.children
? item.children.map(remapItem)
: null

// set main or sub prefix
const prefix = item.children ? CORE_PREFIX_GENRE : CORE_PREFIX_SUBGENRE
const prefix = item.children
? CORE_PREFIX_GENRE
: CORE_PREFIX_SUBGENRE

// build item
const category = {
Expand All @@ -45,7 +49,10 @@ const crawl = async () => {

const tree = categories.map(remapItem)

await storage.save('data/ard/categories.json', JSON.stringify({ tree, list }, null, '\t'))
await storage.save(
'data/ard/categories.json',
JSON.stringify({ tree, list }, null, '\t')
)
}

crawl()
44 changes: 31 additions & 13 deletions scripts/ard/publishers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,44 @@ const storage = new Storage({
logging: true,
})

const ARD_API_URL = process.env.ARD_DELIVER_API || 'https://deliver-test.ard.de/organization-service/'
const ARD_API_HEADERS = { Authorization: `Basic ${Buffer.from(process.env.ARD_AUTH).toString('base64')}` }
const ARD_API_URL =
process.env.ARD_DELIVER_API ||
'https://deliver-test.ard.de/organization-service/'
const ARD_API_HEADERS = {
Authorization: `Basic ${Buffer.from(process.env.ARD_AUTH).toString('base64')}`,
}

const crawl = async () => {
const output = []

try {
const { string: publisherString } = await undici(`${ARD_API_URL}publishers?page=0&size=500`, {
headers: ARD_API_HEADERS,
})
const { string: publisherString } = await undici(
`${ARD_API_URL}publishers?page=0&size=500`,
{
headers: ARD_API_HEADERS,
}
)

const publishers = JSON.parse(publisherString)

for await (const publisher of publishers.elements) {
const { string: publisherInfoString } = await undici(publisher.href, {
headers: ARD_API_HEADERS,
})
const { string: publisherInfoString } = await undici(
publisher.href,
{
headers: ARD_API_HEADERS,
}
)
const publisherInfo = JSON.parse(publisherInfoString)

const { string: institutionInfoString } = await undici(publisherInfo.institution.href, {
headers: ARD_API_HEADERS,
})
const institutionInfo = JSON.parse(institutionInfoString)
const { string: institutionInfoString } = await undici(
publisherInfo.institution.href,
{
headers: ARD_API_HEADERS,
}
)
const institutionInfo = JSON.parse(
institutionInfoString
)

const details = {
_type: publisherInfo._type,
Expand All @@ -59,7 +74,10 @@ const crawl = async () => {
console.log(details)
}

await storage.save('tmp/ard-publishers.json', JSON.stringify(output))
await storage.save(
'tmp/ard-publishers.json',
JSON.stringify(output)
)
} catch (error) {
console.log(error)
}
Expand Down
10 changes: 8 additions & 2 deletions scripts/ard/sortPubByExtId.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ const sort = async () => {
const publisher = JSON.parse(input)

publisher.sort((a, b) => {
return parseInt(a.externalId) - parseInt(b.externalId)
return (
Number.parseInt(a.externalId) -
Number.parseInt(b.externalId)
)
})

await storage.save('tmp/ard-publishers-ordered.json', JSON.stringify(publisher))
await storage.save(
'tmp/ard-publishers-ordered.json',
JSON.stringify(publisher)
)
} catch (error) {
console.log(error)
}
Expand Down
4 changes: 3 additions & 1 deletion tests/ard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const { createHashedId } = require('../packages/ard')
describe('Test ARD Package', () => {
describe('Test ARD-CoreID Hash', () => {
it("createHashedId('test') = 0c171b2e54a30c11", () => {
expect(createHashedId('test')).to.equal('0c171b2e54a30c11')
expect(createHashedId('test')).to.equal(
'0c171b2e54a30c11'
)
})
})
})
28 changes: 21 additions & 7 deletions tests/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,63 @@ describe('Test DateTime Package', () => {
describe('Test getDateHourMinutes', () => {
const testResult = 'Di, 19. Januar 2038 - 03:14 Uhr'
it(`getDateHourMinutes('${testDate}') = '${testResult}'`, () => {
expect(date.getDateHourMinutes(testDate)).to.equal(testResult)
expect(date.getDateHourMinutes(testDate)).to.equal(
testResult
)
})
})

describe('Test getDayMonthYear', () => {
const testResult = 'Di, 19. Januar 2038'
it(`getDayMonthYear('${testDate}') = '${testResult}'`, () => {
expect(date.getDayMonthYear(testDate)).to.equal(testResult)
expect(date.getDayMonthYear(testDate)).to.equal(
testResult
)
})
})

describe('Test getFullRelativeTime', () => {
const testResult = `Di, 19. Januar 2038 - 03:14 Uhr (in ${relativeYears} Jahren)`
it(`getFullRelativeTime('${testDate}') = '${testResult}'`, () => {
expect(date.getFullRelativeTime(testDate)).to.equal(testResult)
expect(date.getFullRelativeTime(testDate)).to.equal(
testResult
)
})
})

describe('Test getHourMinutes', () => {
const testResult = '03:14'
it(`getHourMinutes('${testDate}') = '${testResult}'`, () => {
expect(date.getHourMinutes(testDate)).to.equal(testResult)
expect(date.getHourMinutes(testDate)).to.equal(
testResult
)
})
})

describe('Test getIsoRelativeTime', () => {
const testResult = `${testDate} (in ${relativeYears} Jahren)`
it(`getIsoRelativeTime('${testDate}') = '${testResult}'`, () => {
expect(date.getIsoRelativeTime(testDate)).to.equal(testResult)
expect(date.getIsoRelativeTime(testDate)).to.equal(
testResult
)
})
})

describe('Test getRelativeTime', () => {
const testResult = `in ${relativeYears} Jahren`
it(`getRelativeTime('${testDate}') = '${testResult}'`, () => {
expect(date.getRelativeTime(testDate)).to.equal(testResult)
expect(date.getRelativeTime(testDate)).to.equal(
testResult
)
})
})

describe('Test getYearMonthDay', () => {
const testResult = '20380119'
it(`getYearMonthDay('${testDate}') = '${testResult}'`, () => {
expect(date.getYearMonthDay(testDate)).to.equal(testResult)
expect(date.getYearMonthDay(testDate)).to.equal(
testResult
)
})
})

Expand Down
Loading

0 comments on commit 9b12f96

Please sign in to comment.