@tolgee/cli
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -26,5 +26,6 @@ "use strict"; | ||
if (diff.added.length) { | ||
console.log(ansi_colors_1.default.green.bold(`${diff.added.length} new strings`)); | ||
const key = diff.added.length === 1 ? 'key' : 'keys'; | ||
console.log(ansi_colors_1.default.green.bold(`${diff.added.length} new ${key} found`)); | ||
for (const key of diff.added) { | ||
(0, syncUtils_1.printKey)(key, 'added'); | ||
(0, syncUtils_1.printKey)(key, false); | ||
} | ||
@@ -35,5 +36,6 @@ // Line break | ||
if (diff.removed.length) { | ||
console.log(ansi_colors_1.default.red.bold(`${diff.removed.length} removed strings`)); | ||
const key = diff.removed.length === 1 ? 'key' : 'keys'; | ||
console.log(ansi_colors_1.default.red.bold(`${diff.removed.length} unused ${key}`)); | ||
for (const key of diff.removed) { | ||
(0, syncUtils_1.printKey)(key, 'removed'); | ||
(0, syncUtils_1.printKey)(key, true); | ||
} | ||
@@ -40,0 +42,0 @@ // Line break |
@@ -30,4 +30,4 @@ "use strict"; | ||
const str = `The following keys will be ${operation}:`; | ||
console.log(operation === 'added' ? ansi_colors_1.default.bold.green(str) : ansi_colors_1.default.bold.red(str)); | ||
keys.forEach((k) => (0, syncUtils_1.printKey)(k, operation)); | ||
console.log(operation === 'created' ? ansi_colors_1.default.bold.green(str) : ansi_colors_1.default.bold.red(str)); | ||
keys.forEach((k) => (0, syncUtils_1.printKey)(k, operation === 'deleted')); | ||
const shouldContinue = await (0, ask_1.askBoolean)('Does this look correct?', true); | ||
@@ -69,3 +69,3 @@ if (!shouldContinue) { | ||
if (!opts.yes) { | ||
await askForConfirmation(diff.added, 'added'); | ||
await askForConfirmation(diff.added, 'created'); | ||
} | ||
@@ -85,3 +85,3 @@ const keys = diff.added.map((key) => ({ | ||
if (!opts.yes) { | ||
await askForConfirmation(diff.removed, 'removed'); | ||
await askForConfirmation(diff.removed, 'deleted'); | ||
} | ||
@@ -88,0 +88,0 @@ const ids = await diff.removed.map((k) => k.id); |
@@ -13,13 +13,13 @@ "use strict"; | ||
* @param key The key to print. | ||
* @param type Whether this is an addition or a removal. | ||
* @param deletion True if the key is about to be deleted. | ||
*/ | ||
function printKey(key, type) { | ||
function printKey(key, deletion) { | ||
const namespace = key.namespace | ||
? ` ${ansi_colors_1.default.italic(`(namespace: ${key.namespace})`)}` | ||
: ''; | ||
if (type === 'added') { | ||
console.log(`${ansi_colors_1.default.green(`+ ${key.keyName}`)}${namespace}`); | ||
if (deletion) { | ||
console.log(`${ansi_colors_1.default.red(`- ${key.keyName}`)}${namespace}`); | ||
} | ||
else { | ||
console.log(`${ansi_colors_1.default.red(`- ${key.keyName}`)}${namespace}`); | ||
console.log(`${ansi_colors_1.default.green(`+ ${key.keyName}`)}${namespace}`); | ||
} | ||
@@ -51,10 +51,12 @@ } | ||
// Added keys | ||
const namespaces = [...Object.keys(local), runner_1.NullNamespace]; | ||
const namespaces = [runner_1.NullNamespace, ...Object.keys(local).sort()]; | ||
for (const namespace of namespaces) { | ||
if (namespace in local && local[namespace].size) { | ||
for (const [keyName, defaultValue] of local[namespace].entries()) { | ||
const keys = local[namespace]; | ||
const keyNames = Array.from(local[namespace].keys()).sort(); | ||
for (const keyName of keyNames) { | ||
result.added.push({ | ||
keyName: keyName, | ||
namespace: namespace === runner_1.NullNamespace ? undefined : namespace, | ||
defaultValue: defaultValue || undefined, | ||
defaultValue: keys.get(keyName) || undefined, | ||
}); | ||
@@ -64,4 +66,20 @@ } | ||
} | ||
// Sort keys | ||
// This is only necessary for unused keys, because the added keys are sorted directly as they're added. | ||
result.removed.sort((a, b) => { | ||
if (a.namespace === b.namespace) { | ||
return a.keyName > b.keyName ? 1 : a.keyName < b.keyName ? -1 : 0; | ||
} | ||
if (!a.namespace && b.namespace) | ||
return -1; | ||
if (a.namespace && !b.namespace) | ||
return 1; | ||
return a.namespace > b.namespace | ||
? 1 | ||
: a.namespace < b.namespace | ||
? -1 | ||
: 0; | ||
}); | ||
return result; | ||
} | ||
exports.compareKeys = compareKeys; |
@@ -29,6 +29,6 @@ "use strict"; | ||
if ('projectId' in rc) { | ||
if (typeof rc.projectId !== 'number') { | ||
throw new Error('Invalid config: projectId is not a number'); | ||
cfg.projectId = Number(rc.projectId); // Number("") returns 0 | ||
if (!Number.isInteger(cfg.projectId) || cfg.projectId <= 0) { | ||
throw new Error('Invalid config: projectId should be an integer representing your project Id'); | ||
} | ||
cfg.projectId = rc.projectId; | ||
} | ||
@@ -35,0 +35,0 @@ if ('sdk' in rc) { |
@@ -396,2 +396,26 @@ "use strict"; | ||
}, | ||
'variable.other.object.ts': { | ||
target: 'idle', | ||
actions: ['dynamicChildren', 'pushKey'], | ||
}, | ||
'entity.name.function.ts': { | ||
target: 'idle', | ||
actions: ['dynamicChildren', 'pushKey'], | ||
}, | ||
'storage.type.function.ts': { | ||
target: 'idle', | ||
actions: ['dynamicChildren', 'pushKey'], | ||
}, | ||
'storage.type.class.ts': { | ||
target: 'idle', | ||
actions: ['dynamicChildren', 'pushKey'], | ||
}, | ||
'keyword.operator.new.ts': { | ||
target: 'idle', | ||
actions: ['dynamicChildren', 'pushKey'], | ||
}, | ||
'punctuation.definition.block.ts': { | ||
target: 'idle', | ||
actions: ['dynamicChildren', 'pushKey'], | ||
}, | ||
'punctuation.definition.template-expression.begin.ts': { | ||
@@ -398,0 +422,0 @@ target: 'idle', |
@@ -29,32 +29,27 @@ #!/usr/bin/env node | ||
} | ||
async function validateApiKey(cmd) { | ||
async function loadApiKey(cmd) { | ||
const opts = cmd.optsWithGlobals(); | ||
if (!opts.apiKey) { | ||
// Attempt to load --api-key from config store if not specified | ||
// This is not done as part of the init routine or via the mandatory flag, as this is dependent on the API URL. | ||
const key = await (0, credentials_1.getApiKey)(opts.apiUrl, opts.projectId); | ||
if (!key) { | ||
(0, logger_1.error)('No API key has been provided. You must either provide one via --api-key, or login via `tolgee login`.'); | ||
process.exit(1); | ||
// API Key is already loaded | ||
if (opts.apiKey) | ||
return; | ||
// Attempt to load --api-key from config store if not specified | ||
// This is not done as part of the init routine or via the mandatory flag, as this is dependent on the API URL. | ||
const key = await (0, credentials_1.getApiKey)(opts.apiUrl, opts.projectId); | ||
// No key in store, stop here. | ||
if (!key) | ||
return; | ||
cmd.setOptionValue('apiKey', key); | ||
program.setOptionValue('_removeApiKeyFromStore', () => { | ||
if (key.startsWith(constants_1.API_KEY_PAT_PREFIX)) { | ||
(0, credentials_1.savePat)(opts.apiUrl); | ||
} | ||
cmd.setOptionValue('apiKey', key); | ||
program.setOptionValue('_removeApiKeyFromStore', () => { | ||
if (key.startsWith(constants_1.API_KEY_PAT_PREFIX)) { | ||
(0, credentials_1.savePat)(opts.apiUrl); | ||
} | ||
else { | ||
(0, credentials_1.savePak)(opts.apiUrl, opts.projectId); | ||
} | ||
}); | ||
} | ||
else { | ||
(0, credentials_1.savePak)(opts.apiUrl, opts.projectId); | ||
} | ||
}); | ||
} | ||
function validateProjectId(cmd) { | ||
function loadProjectId(cmd) { | ||
const opts = cmd.optsWithGlobals(); | ||
// Validate --project-id is present when using Project API keys | ||
if (opts.projectId === -1 && opts.apiKey.startsWith(constants_1.API_KEY_PAT_PREFIX)) { | ||
(0, logger_1.error)('You must specify a Project ID.'); | ||
process.exit(1); | ||
} | ||
if (opts.apiKey.startsWith(constants_1.API_KEY_PAK_PREFIX)) { | ||
// Parse the key to ensure we can access the specified Project ID | ||
// Parse the key and ensure we can access the specified Project ID | ||
const projectId = client_1.default.projectIdFromKey(opts.apiKey); | ||
@@ -70,6 +65,19 @@ program.setOptionValue('projectId', projectId); | ||
} | ||
function validateOptions(cmd) { | ||
const opts = cmd.optsWithGlobals(); | ||
if (opts.projectId === -1) { | ||
(0, logger_1.error)('No Project ID have been specified. You must either provide one via --project-ir, or by setting up a `.tolgeerc` file.'); | ||
(0, logger_1.info)('Learn more about configuring the CLI here: https://tolgee.io/tolgee-cli/project-configuration'); | ||
process.exit(1); | ||
} | ||
if (!opts.apiKey) { | ||
(0, logger_1.error)('No API key has been provided. You must either provide one via --api-key, or login via `tolgee login`.'); | ||
process.exit(1); | ||
} | ||
} | ||
async function preHandler(prog, cmd) { | ||
if (!NO_KEY_COMMANDS.includes(topLevelName(cmd))) { | ||
await validateApiKey(cmd); | ||
await validateProjectId(cmd); | ||
await loadApiKey(cmd); | ||
loadProjectId(cmd); | ||
validateOptions(cmd); | ||
const opts = cmd.optsWithGlobals(); | ||
@@ -76,0 +84,0 @@ const client = new client_1.default({ |
@@ -10,3 +10,3 @@ "use strict"; | ||
const val = Number(v); | ||
if (isNaN(val) || val < 1) { | ||
if (!Number.isInteger(val) || val < 1) { | ||
throw new commander_1.InvalidArgumentError('Not a valid project ID.'); | ||
@@ -13,0 +13,0 @@ } |
{ | ||
"name": "@tolgee/cli", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"type": "commonjs", | ||
@@ -5,0 +5,0 @@ "description": "A tool to interact with the Tolgee Platform through CLI", |
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
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
905052
3140