diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 98131d7..728eba4 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x, 14.x] + node-version: [20.x, 22.x] os: [ubuntu-latest] steps: @@ -46,11 +46,11 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: - node-version: 12.x + node-version: 20.x - run: yarn - run: yarn build - name: Semantic Release - uses: cycjimmy/semantic-release-action@v2 + uses: cycjimmy/semantic-release-action@v4 with: branches: | [ diff --git a/.gitignore b/.gitignore index 28bb2be..c330d96 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ node_modules/ # Source .vscode +.idea dist **/*.css !*.config.js diff --git a/package.json b/package.json index 3a7b34b..93e9ef6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@treatwell/wti", - "version": "1.3.1", + "version": "2.0.0", "description": "A WebTranslateIt command-line tool in Node.js", "keywords": [ "wti", @@ -59,12 +59,10 @@ "@oclif/plugin-not-found": "^1.2.4", "@oclif/plugin-warn-if-update-available": "^1.7.0", "cli-ux": "^5.5.0", - "form-data": "^3.0.0", "git-root-dir": "^1.0.2", "kleur": "^4.1.1", "listr": "^0.14.3", - "make-error": "^1.3.6", - "node-fetch": "3.0.0-beta.9" + "make-error": "^1.3.6" }, "devDependencies": { "@commitlint/cli": "^11.0.0", @@ -85,7 +83,6 @@ "husky": "^4.3.0", "import-sort-cli": "^6.0.0", "import-sort-parser-typescript": "^6.0.0", - "import-sort-style-leboncoin": "^1.0.3", "jest": "^26.4.2", "jest-junit": "^11.1.0", "lint-staged": "^10.4.0", @@ -97,7 +94,7 @@ "typescript": "^4.0.2" }, "engines": { - "node": ">= 8.0.0" + "node": ">= 18.0.0" }, "preferGlobal": true, "publishConfig": { @@ -106,8 +103,7 @@ }, "importSort": { ".ts": { - "parser": "typescript", - "style": "leboncoin" + "parser": "typescript" } }, "oclif": { diff --git a/src/helpers/wti.ts b/src/helpers/wti.ts index 409b947..0f59b2e 100644 --- a/src/helpers/wti.ts +++ b/src/helpers/wti.ts @@ -1,5 +1,3 @@ -import fetch from 'node-fetch'; - import { getConfig } from '.'; const WTI_API_URL = 'https://webtranslateit.com/api/projects'; @@ -17,11 +15,9 @@ export type WtiErrorResponse = { export const wtiGet = async (path: string) => { const config = await getConfig(); - const response = await fetch( + return await fetch( `${WTI_API_URL}/${config.project.apiKey}${path}` ); - - return response; }; /** @@ -30,18 +26,16 @@ export const wtiGet = async (path: string) => { * @param path path of the API call * @param data data to be sent in the request body */ -export const wtiPost = async (path: string, data: fetch.BodyInit) => { +export const wtiPost = async (path: string, data: BodyInit) => { const config = await getConfig(); - const response = await fetch( + return fetch( `${WTI_API_URL}/${config.project.apiKey}${path}`, { method: 'POST', body: data, } ); - - return response; }; /** @@ -50,18 +44,16 @@ export const wtiPost = async (path: string, data: fetch.BodyInit) => { * @param path path of the API call * @param data data to be sent in the request body */ -export const wtiPut = async (path: string, data: fetch.BodyInit) => { +export const wtiPut = async (path: string, data: BodyInit) => { const config = await getConfig(); - const response = await fetch( + return fetch( `${WTI_API_URL}/${config.project.apiKey}${path}`, { method: 'PUT', body: data, } ); - - return response; }; /** @@ -72,12 +64,10 @@ export const wtiPut = async (path: string, data: fetch.BodyInit) => { export const wtiDelete = async (path: string) => { const config = await getConfig(); - const response = await fetch( + return fetch( `${WTI_API_URL}/${config.project.apiKey}${path}`, { method: 'DELETE', } ); - - return response; }; diff --git a/src/models/Locale.ts b/src/models/Locale.ts index 98cde1c..fd23bd4 100644 --- a/src/models/Locale.ts +++ b/src/models/Locale.ts @@ -1,6 +1,4 @@ -import FormData from 'form-data'; - -import { wtiPost, WtiErrorResponse, wtiDelete } from '../helpers'; +import { WtiErrorResponse, wtiDelete, wtiPost } from '../helpers'; export class Locale { private _locale: string; diff --git a/src/models/MasterFile.ts b/src/models/MasterFile.ts index 8e63f99..851530d 100644 --- a/src/models/MasterFile.ts +++ b/src/models/MasterFile.ts @@ -1,15 +1,14 @@ -import fs from 'fs'; -import FormData from 'form-data'; - -import { MasterFileAlreadyExistsError } from '../errors'; import { - wtiPost, - wtiPut, + WtiErrorResponse, wtiDelete, wtiGet, - WtiErrorResponse, + wtiPost, + wtiPut, } from '../helpers'; + +import { MasterFileAlreadyExistsError } from '../errors'; import { WTIProjectFile } from './types'; +import fs from 'fs'; type Translations = { [key: string]: Translations | string; @@ -52,13 +51,17 @@ export class MasterFile { public static async create(name: string) { try { - const file = fs.createReadStream(process.cwd() + `/${name}`); + const file = fs.readFileSync(process.cwd() + `/${name}`); const formData = new FormData(); - formData.append('file', file); + formData.append('file', new Blob([file]), name); formData.append('name', name); const response = await wtiPost('/files', formData); + if (!response.ok) { + throw new Error(`Failed to push new master file: ${await response.text()}`); + } + const fileId = await response.text(); if (fileId) { @@ -79,6 +82,9 @@ export class MasterFile { const response = await wtiGet( `/files/${this._masterFileId}/locales/${file.locale_code}` ); + if (!response.ok) { + throw new Error(`Failed to get master file: ${await response.text()}`); + } const result = (await response.json()) as Translations; @@ -102,10 +108,10 @@ export class MasterFile { } ) { try { - const file = fs.createReadStream(process.cwd() + `/${name}`); + const file = fs.readFileSync(process.cwd() + `/${name}`); const formData = new FormData(); - formData.append('file', file); + formData.append('file', new Blob([file]), name); formData.append('name', name); if (options.merge) { formData.append('merge', 'true'); @@ -117,7 +123,10 @@ export class MasterFile { formData.append('label', options.label); } - await wtiPut(`/files/${this._masterFileId}/locales/${locale}`, formData); + const response = await wtiPut(`/files/${this._masterFileId}/locales/${locale}`, formData); + if (!response.ok) { + throw new Error(`Failed to get master file: ${await response.text()}`); + } } catch (err) { console.error(String(err)); process.exit(1); diff --git a/yarn.lock b/yarn.lock index ad83243..b70facd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1937,7 +1937,7 @@ colors@^1.1.2: resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78" integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA== -combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6: +combined-stream@^1.0.6, combined-stream@~1.0.6: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== @@ -2184,11 +2184,6 @@ dashdash@^1.12.0: dependencies: assert-plus "^1.0.0" -data-uri-to-buffer@^3.0.1: - version "3.0.1" - resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz#594b8973938c5bc2c33046535785341abc4f3636" - integrity sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og== - data-urls@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/data-urls/-/data-urls-2.0.0.tgz#156485a72963a970f5d5821aaf642bef2bf2db9b" @@ -2857,11 +2852,6 @@ fb-watchman@^2.0.0: dependencies: bser "2.1.1" -fetch-blob@^2.1.1: - version "2.1.1" - resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-2.1.1.tgz#a54ab0d5ed7ccdb0691db77b6674308b23fb2237" - integrity sha512-Uf+gxPCe1hTOFXwkxYyckn8iUSk6CFXGy5VENZKifovUTZC9eUODWSBhOBS7zICGrAetKzdwLMr85KhIcePMAQ== - figures@^1.7.0: version "1.7.0" resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e" @@ -3002,15 +2992,6 @@ forever-agent@~0.6.1: resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE= -form-data@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682" - integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg== - dependencies: - asynckit "^0.4.0" - combined-stream "^1.0.8" - mime-types "^2.1.12" - form-data@~2.3.2: version "2.3.3" resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6" @@ -3573,11 +3554,6 @@ import-sort-style-eslint@^6.0.0: eslint "^5.0.0" lodash "^4.17.10" -import-sort-style-leboncoin@^1.0.3: - version "1.0.3" - resolved "https://registry.yarnpkg.com/import-sort-style-leboncoin/-/import-sort-style-leboncoin-1.0.3.tgz#b04b23f60598324e5dd3a3f35907567c4ef80b44" - integrity sha512-n7IbYGctk3OMJrnfHDDzKinwN/xtBUjgIGGe+TD+Rcc7loBZR08ZRJNjM8JPkNwL46NTMTY+9aaiqZn2eyA1Zw== - import-sort-style@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/import-sort-style/-/import-sort-style-6.0.0.tgz#088523f056e5064c34a6426f4733674d81b42e6a" @@ -5006,14 +4982,6 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -node-fetch@3.0.0-beta.9: - version "3.0.0-beta.9" - resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.0.0-beta.9.tgz#0a7554cfb824380dd6812864389923c783c80d9b" - integrity sha512-RdbZCEynH2tH46+tj0ua9caUHVWrd/RHnRfvly2EVdqGmI3ndS1Vn/xjm5KuGejDt2RNDQsVRLPNd2QPwcewVg== - dependencies: - data-uri-to-buffer "^3.0.1" - fetch-blob "^2.1.1" - node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"