Skip to content

Instantly share code, notes, and snippets.

@ewanharris
Last active June 20, 2023 22:09
Show Gist options
  • Save ewanharris/d6021ab4e12a2518196a2ec7001f698f to your computer and use it in GitHub Desktop.
Save ewanharris/d6021ab4e12a2518196a2ec7001f698f to your computer and use it in GitHub Desktop.
Script to extract from package.json and write to package.nls.json
const fs = require('fs-extra');
const packageJson = require('./package.json');
const existing = require('./package.nls.json');
function handleStringEdit (str, obj, key) {
if (!obj[key] || obj[key]?.startsWith('%')) {
return;
}
existing[str] = obj[key];
obj[key] = `%${str}%`;
}
const { contributes: { commands, configuration, debuggers, taskDefinitions, views, viewsWelcome } } = packageJson;
for (let i = 0; i < commands.length; i++) {
const command = commands[i];
const leading = 'titanium.commands';
const suffix = command.command.replace('titanium.', '');
const cmdString = `${leading}.${suffix}`;
handleStringEdit(`${cmdString}.title`, command, 'title');
handleStringEdit(`${cmdString}.description`, command, 'description');
}
for (const [ key, value ] of Object.entries(configuration.properties)) {
const leading = 'titanium.config';
const suffix = key.replace('titanium.', '');
const configString = `${leading}.${suffix}`;
handleStringEdit(configString, value, 'description');
}
for (let i = 0; i < taskDefinitions.length; i++) {
const { type, properties: { titaniumBuild } } = taskDefinitions[i];
const leading = `titanium.tasks.${type}`;
handleStringEdit(`${leading}.titaniumBuild`, titaniumBuild, 'description');
for (const [ key, value ] of Object.entries(titaniumBuild.properties)) {
let str = `${leading}.${key}`;
handleStringEdit(str, value, 'description');
if (value.type === 'object' || value.properties) {
for (const [ k, v ] of Object.entries(value.properties)) {
str = `${str}.${k}`;
handleStringEdit(str, v, 'description');
}
}
}
}
for (const [ key, value ] of Object.entries(debuggers[0].configurationAttributes.launch.properties)) {
handleStringEdit(`titanium.debug.${key}`, value, 'description');
}
for (const value of views.titanium) {
handleStringEdit(value.id, value, 'name');
}
for (const value of viewsWelcome) {
const when = value.when.split(':')[1];
handleStringEdit(`${value.view}.${when}`, value, 'contents');
}
fs.writeJsonSync('package.json', packageJson, { spaces: 2 });
fs.writeJsonSync('package.nls.json', existing, { spaces: 2 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment