@opensea/i18n-upload
Advanced tools
Comparing version 0.0.13 to 0.0.14
#!/usr/bin/env node | ||
export {}; | ||
//# sourceMappingURL=cli.d.ts.map |
263
dist/cli.js
#!/usr/bin/env node | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
// src/cli.ts | ||
import { program } from "commander"; | ||
import ora from "ora"; | ||
// src/upload.ts | ||
import { glob } from "glob"; | ||
// src/client.ts | ||
import ky from "ky"; | ||
import fs from "fs"; | ||
var BASE_URL = "https://api.smartling.com"; | ||
var SmartlingClient = class { | ||
userSecret; | ||
userIdentifier; | ||
ref; | ||
projectId; | ||
client; | ||
accessToken = ""; | ||
constructor({ userSecret, userIdentifier, ref, projectId }) { | ||
this.userSecret = userSecret; | ||
this.userIdentifier = userIdentifier; | ||
this.ref = ref; | ||
this.projectId = projectId; | ||
this.client = ky.create({ | ||
prefixUrl: BASE_URL, | ||
hooks: { | ||
beforeRequest: [ | ||
(request) => { | ||
if (this.accessToken) { | ||
request.headers.set( | ||
"Authorization", | ||
`Bearer ${this.accessToken}` | ||
); | ||
} | ||
} | ||
], | ||
afterResponse: [ | ||
async (request, _options, response) => { | ||
if (response.status >= 400) { | ||
const json = await response.json(); | ||
throw new Error( | ||
`There was a problem with the request: | ||
${request.method} ${request.url} | ||
Status: ${response.status} | ||
Body: ${JSON.stringify(json)} | ||
` | ||
); | ||
} | ||
} | ||
] | ||
} | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
prefix(value) { | ||
return `${this.ref}-${value}`; | ||
} | ||
async authenticate() { | ||
const response = await this.client.post("auth-api/v2/authenticate", { | ||
json: { userSecret: this.userSecret, userIdentifier: this.userIdentifier } | ||
}); | ||
const { | ||
response: { | ||
data: { accessToken } | ||
} | ||
} = await response.json(); | ||
this.accessToken = accessToken; | ||
return accessToken; | ||
} | ||
async createJob(jobName, targetLocaleIds) { | ||
const uniqueJobName = this.prefix(jobName); | ||
const jobsEndpoint = `jobs-api/v3/projects/${this.projectId}/jobs`; | ||
const { | ||
response: { | ||
data: { items: jobs } | ||
} | ||
} = await this.client.get(`${jobsEndpoint}?jobName=${uniqueJobName}`).json(); | ||
if (jobs.length > 1) { | ||
throw new Error( | ||
`[Error] There is more than one job that matches ${jobName}. ${JSON.stringify( | ||
jobs | ||
)}` | ||
); | ||
} | ||
if (jobs[0]) { | ||
return jobs[0]; | ||
} | ||
const { | ||
response: { data: job } | ||
} = await this.client.post(jobsEndpoint, { json: { jobName, targetLocaleIds } }).json(); | ||
return job; | ||
} | ||
async createBatch(job) { | ||
const { | ||
response: { data } | ||
} = await this.client.post(`jobs-batch-api/v1/projects/${this.projectId}/batches`, { | ||
json: { translationJobUid: job.translationJobUid, authorize: true } | ||
}).json(); | ||
return data; | ||
} | ||
async addFilesToBatch(batch, targetLocaleIds, files) { | ||
const serialPromises = files.map((file) => async () => { | ||
const form = new FormData(); | ||
const fileContents = await fs.promises.readFile(file); | ||
form.append("file", new Blob([fileContents])); | ||
form.append("fileUri", this.prefix(file)); | ||
form.append("fileType", "json"); | ||
targetLocaleIds.map( | ||
(locale) => form.append("localeIdsToAuthorize[]", locale) | ||
); | ||
return this.client.post( | ||
`jobs-batch-api/v1/projects/${this.projectId}/batches/${batch.batchUid}/file`, | ||
{ | ||
body: form, | ||
headers: { contentType: "multipart/form-data" } | ||
} | ||
); | ||
}); | ||
return await this.runSerialPromises(serialPromises); | ||
} | ||
getProjectDetails = () => { | ||
return this.client.get(`projects-api/v2/projects/${this.projectId}`).json().then((response) => response.response.data); | ||
}; | ||
async startBatch(batch) { | ||
return await this.client.post( | ||
`jobs-batch-api/v1/projects/${this.projectId}/batches/${batch.batchUid}`, | ||
{ json: { action: "execute" } } | ||
).json(); | ||
} | ||
// Prevents getting 429's throttling from Smartling | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async runSerialPromises(fns) { | ||
return fns.reduce( | ||
(previous, current) => previous.then(() => current()), | ||
Promise.resolve() | ||
); | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
// src/upload.ts | ||
var upload = async ({ | ||
userSecret, | ||
userIdentifier, | ||
ref, | ||
projectId, | ||
directory, | ||
jobName | ||
}) => { | ||
const client = new SmartlingClient({ | ||
userSecret, | ||
userIdentifier, | ||
ref, | ||
projectId | ||
}); | ||
await client.authenticate(); | ||
const { sourceLocaleId, targetLocales } = await client.getProjectDetails(); | ||
const targetLocaleIds = targetLocales.filter((l) => l.enabled).map((l) => l.localeId); | ||
const files = await glob(`${directory}/${sourceLocaleId}/**/*.json`); | ||
const job = await client.createJob(jobName, targetLocaleIds); | ||
const batch = await client.createBatch(job); | ||
await client.addFilesToBatch(batch, targetLocaleIds, files); | ||
return await client.startBatch(batch); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var commander_1 = require("commander"); | ||
var ora_1 = __importDefault(require("ora")); | ||
var upload_1 = require("./upload"); | ||
var run = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, userSecret, userIdentifier, ref, projectId, directory, jobName, spinner; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
commander_1.program | ||
.name('uploadTranslationKeys') | ||
.description('CLI to upload i18n translation keys') | ||
.requiredOption('-s, --userSecret <userSecret>', 'Smartling user secret') | ||
.requiredOption('-i, --userIdentifier <userIdentifier>', 'Smartling user identifier') | ||
.requiredOption('-r, --ref <ref>', 'Reference for the upload used to prevent job collisions') | ||
.requiredOption('-p, --projectId <projectId>', 'Project ID to interact with') | ||
.requiredOption('-D, --directory <directory>', 'Path to the locales directory') | ||
.requiredOption('-j, --jobName <jobName>', 'Name of the translation job'); | ||
_a = commander_1.program.parse().opts(), userSecret = _a.userSecret, userIdentifier = _a.userIdentifier, ref = _a.ref, projectId = _a.projectId, directory = _a.directory, jobName = _a.jobName; | ||
spinner = (0, ora_1.default)('Processing files for translations...').start(); | ||
return [4 /*yield*/, (0, upload_1.upload)({ | ||
jobName: jobName, | ||
userSecret: userSecret, | ||
userIdentifier: userIdentifier, | ||
ref: ref, | ||
projectId: projectId, | ||
directory: directory | ||
})]; | ||
case 1: | ||
_b.sent(); | ||
spinner.succeed('Files for translation uploaded!'); | ||
process.exit(0); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
// src/cli.ts | ||
var run = async () => { | ||
program.name("uploadTranslationKeys").description("CLI to upload i18n translation keys").requiredOption("-s, --userSecret <userSecret>", "Smartling user secret").requiredOption( | ||
"-i, --userIdentifier <userIdentifier>", | ||
"Smartling user identifier" | ||
).requiredOption( | ||
"-r, --ref <ref>", | ||
"Reference for the upload used to prevent job collisions" | ||
).requiredOption( | ||
"-p, --projectId <projectId>", | ||
"Project ID to interact with" | ||
).requiredOption( | ||
"-D, --directory <directory>", | ||
"Path to the locales directory" | ||
).requiredOption("-j, --jobName <jobName>", "Name of the translation job"); | ||
const { userSecret, userIdentifier, ref, projectId, directory, jobName } = program.parse().opts(); | ||
const spinner = ora("Processing files for translations...").start(); | ||
await upload({ | ||
jobName, | ||
userSecret, | ||
userIdentifier, | ||
ref, | ||
projectId, | ||
directory | ||
}); | ||
spinner.succeed("Files for translation uploaded!"); | ||
process.exit(0); | ||
}; | ||
run(); |
{ | ||
"name": "@opensea/i18n-upload", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "Package for i18n key upload", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.build.json", | ||
"build": "tsup src/cli.ts --format esm", | ||
"lint": "concurrently \"npm run typecheck\" \"npm run prettier:package.json:check\"", | ||
@@ -30,2 +30,3 @@ "prettier:package.json:check": "prettier-package-json --list-different", | ||
"msw": "1.2.5", | ||
"tsup": "7.2.0", | ||
"uuid": "9.0.0" | ||
@@ -32,0 +33,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6731
4
4
190
1