From 53f13354ff63ab1d85c8dbc4e2e947b3555d2b3e Mon Sep 17 00:00:00 2001 From: Samuel Ryan Date: Wed, 13 Nov 2024 15:57:25 +0800 Subject: [PATCH] ci: Require code to be linted --- .github/workflows/cd_npm.yml | 56 ++++++++++++++++---------------- .github/workflows/lint.yaml | 17 ++++++++++ .prettierignore | 3 ++ .prettierrc.js | 1 - __tests__/ipinfoWrapper.test.ts | 6 ++-- src/ipinfoWrapper.ts | 57 +++++++++++++++++++++------------ test-app/index.js | 4 +-- test-app/ipinfo.ts | 9 ++++-- 8 files changed, 97 insertions(+), 56 deletions(-) create mode 100644 .github/workflows/lint.yaml create mode 100644 .prettierignore diff --git a/.github/workflows/cd_npm.yml b/.github/workflows/cd_npm.yml index 298648e..441c87c 100644 --- a/.github/workflows/cd_npm.yml +++ b/.github/workflows/cd_npm.yml @@ -1,33 +1,33 @@ name: Release package to npm on: - push: - tags: - - 'v*' + push: + tags: + - "v*" jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 16 - - run: npm ci - - run: npm test - env: - IPINFO_TOKEN: ${{secrets.IPINFO_TOKEN}} - publish: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 16 - registry-url: https://registry.npmjs.org/ - - run: npm ci - - run: npm run build - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + - run: npm ci + - run: npm test + env: + IPINFO_TOKEN: ${{secrets.IPINFO_TOKEN}} + publish: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm run build + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..4d6ebb4 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,17 @@ +name: "Lint Code" + +on: + push: + workflow_dispatch: + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: "latest" + - run: npm ci + - run: npx prettier . --check diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..3ea7a14 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +**/*.md +config/**/* +package-lock.json diff --git a/.prettierrc.js b/.prettierrc.js index 6fe46ee..94c4fcb 100644 --- a/.prettierrc.js +++ b/.prettierrc.js @@ -8,7 +8,6 @@ module.exports = { jsxSingleQuote: false, trailingComma: "none", bracketSpacing: true, - jsxBracketSameLine: false, arrowParens: "always", rangeStart: 0, rangeEnd: Infinity, diff --git a/__tests__/ipinfoWrapper.test.ts b/__tests__/ipinfoWrapper.test.ts index 54498e2..6172210 100644 --- a/__tests__/ipinfoWrapper.test.ts +++ b/__tests__/ipinfoWrapper.test.ts @@ -24,7 +24,9 @@ describe("IPinfoWrapper", () => { expect(data.countryCode).toEqual("US"); expect(data.countryFlag.emoji).toEqual("🇺🇸"); expect(data.countryFlag.unicode).toEqual("U+1F1FA U+1F1F8"); - expect(data.countryFlagURL).toEqual("https://cdn.ipinfo.io/static/images/countries-flags/US.svg"); + expect(data.countryFlagURL).toEqual( + "https://cdn.ipinfo.io/static/images/countries-flags/US.svg" + ); expect(data.countryCurrency.code).toEqual("USD"); expect(data.countryCurrency.symbol).toEqual("$"); expect(data.continent.code).toEqual("NA"); @@ -152,7 +154,7 @@ describe("IPinfoWrapper", () => { "safermoto.com", "progeni.com", "grahamhostedservices.com", - "bhcentral.tech", + "bhcentral.tech" ] }, countryCode: "ID" diff --git a/src/ipinfoWrapper.ts b/src/ipinfoWrapper.ts index f4ef843..a27c855 100644 --- a/src/ipinfoWrapper.ts +++ b/src/ipinfoWrapper.ts @@ -60,21 +60,30 @@ export default class IPinfoWrapper { cache?: Cache, timeout?: number, i18nData?: { - countries?: any, - countriesFlags?: any, - countriesCurrencies?: any, - continents?: any, - euCountries?: Array, + countries?: any; + countriesFlags?: any; + countriesCurrencies?: any; + continents?: any; + euCountries?: Array; } ) { this.token = token; - this.countries = i18nData?.countries ? i18nData.countries : defaultCountries; - this.countriesFlags = i18nData?.countriesFlags ? i18nData.countriesFlags: defaultCountriesFlags; - this.countriesCurrencies = i18nData?.countriesCurrencies ? i18nData.countriesCurrencies: defaultCountriesCurrencies; - this.continents = i18nData?.continents ? i18nData.continents : defaultContinents; + this.countries = i18nData?.countries + ? i18nData.countries + : defaultCountries; + this.countriesFlags = i18nData?.countriesFlags + ? i18nData.countriesFlags + : defaultCountriesFlags; + this.countriesCurrencies = i18nData?.countriesCurrencies + ? i18nData.countriesCurrencies + : defaultCountriesCurrencies; + this.continents = i18nData?.continents + ? i18nData.continents + : defaultContinents; this.euCountries = i18nData?.euCountries && i18nData?.euCountries.length !== 0 - ? i18nData.euCountries : defaultEuCountries; + ? i18nData.euCountries + : defaultEuCountries; this.cache = cache ? cache : new LruCache(); this.timeout = timeout === null || timeout === undefined @@ -131,11 +140,13 @@ export default class IPinfoWrapper { res.on("close", () => { let ipinfo: IPinfo; try { - ipinfo = JSON.parse(data) + ipinfo = JSON.parse(data); } catch { - reject(new Error("error parsing JSON response")); + reject( + new Error("error parsing JSON response") + ); return; - }; + } /* convert country code to full country name */ // NOTE: always do this _before_ setting cache. @@ -238,11 +249,13 @@ export default class IPinfoWrapper { res.on("close", () => { let asnResp: AsnResponse; try { - asnResp = JSON.parse(data) + asnResp = JSON.parse(data); } catch { - reject(new Error("error parsing JSON response")); + reject( + new Error("error parsing JSON response") + ); return; - }; + } /* convert country code to full country name */ // NOTE: always do this _before_ setting cache. @@ -331,11 +344,13 @@ export default class IPinfoWrapper { res.on("close", () => { let response; try { - response = JSON.parse(data) + response = JSON.parse(data); } catch { - reject(new Error("error parsing JSON response")); + reject( + new Error("error parsing JSON response") + ); return; - }; + } resolve(response); }); @@ -503,8 +518,8 @@ export default class IPinfoWrapper { try { batchResp = JSON.parse(el); } catch { - batchResp = {} - }; + batchResp = {}; + } for (var key in batchResp) { if (batchResp.hasOwnProperty(key)) { diff --git a/test-app/index.js b/test-app/index.js index 87f432e..ca7e1a0 100644 --- a/test-app/index.js +++ b/test-app/index.js @@ -2,11 +2,11 @@ let { IPinfoWrapper, LruCache } = require("node-ipinfo"); let cacheOptions = { max: 5000, - ttl: 24 * 1000 * 60 * 60, + ttl: 24 * 1000 * 60 * 60 }; let cache = new LruCache(cacheOptions); -let token = process.env.IPINFO_TOKEN +let token = process.env.IPINFO_TOKEN; let ipinfo = new IPinfoWrapper(token, cache); ipinfo.lookupIp("1.1.1.1").then((response) => { diff --git a/test-app/ipinfo.ts b/test-app/ipinfo.ts index 1bf16e1..2bec547 100644 --- a/test-app/ipinfo.ts +++ b/test-app/ipinfo.ts @@ -1,8 +1,13 @@ -import IPinfoWrapper, { LruCache, Options, IPinfo, AsnResponse } from "node-ipinfo"; +import IPinfoWrapper, { + LruCache, + Options, + IPinfo, + AsnResponse +} from "node-ipinfo"; const cacheOptions: Options = { max: 5000, - ttl: 24 * 1000 * 60 * 60, + ttl: 24 * 1000 * 60 * 60 }; const cache = new LruCache(cacheOptions);