Skip to content

Commit

Permalink
Update scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kelly committed Jan 31, 2024
1 parent 2356947 commit d134592
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 138 deletions.
96 changes: 0 additions & 96 deletions scripts/find-and-replace.js

This file was deleted.

59 changes: 17 additions & 42 deletions scripts/move-replace.js → scripts/move-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
//
// Useful for re-organizing locale keys in this repo
//
// node ./scripts/move-replace.js '../components/components/**/*.liquid' some.old.path some.new.path
// node ./scripts/move-key.js locales/en.default.schema.json some.old.path some.new.path
//

const fs = require('fs');
const path = require('path');
const glob = require('glob');

function getKeyPathValue(obj, keyPath) {
Expand Down Expand Up @@ -41,8 +40,12 @@ function sortObjectKeys(obj) {
Object.keys(obj).sort().forEach(key => {
// Check if the value is an object and not an array
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
// Recursively sort the keys of the nested object
sortedObject[key] = sortObjectKeys(obj[key]);
if (Object.entries(obj[key]).length === 0) {
delete obj[key]
} else {
// Recursively sort the keys of the nested object
sortedObject[key] = sortObjectKeys(obj[key]);
}
} else {
// Directly assign the value if it's not an object
sortedObject[key] = obj[key];
Expand Down Expand Up @@ -78,7 +81,13 @@ function findSimilarKeys(obj, keyPath, prefix = '') {
}

function modifyJsonFiles(oldPath, newPath, similarKeys) {
const files = glob.sync('./locales/*.json');
let files;

if (defaultFile.endsWith('.schema.json')) {
files = glob.sync('./locales/*.schema.json');
} else {
files = glob.sync('./locales/*.json', {ignore: './locales/*.schema.json'});
}

files.forEach(file => {
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
Expand All @@ -101,45 +110,11 @@ function modifyJsonFiles(oldPath, newPath, similarKeys) {
});
}

function onlyUnique(value, index, array) {
return array.indexOf(value) === index;
}

function updateLiquidFiles(liquidFilesPattern, similarKeys, oldPath, newPath) {
const files = glob.sync(liquidFilesPattern);
console.log(`Found ${files.length} .liquid files to process.`);

files.forEach(file => {
let content = fs.readFileSync(file, 'utf8');
let modified = false;
similarKeys.filter(onlyUnique).forEach(key => {
// Regex pattern to match {{ 'old.path' | t: }} pattern
const regex = new RegExp(`'${key.replace(/\./g, '\\.')}'`, 'g');
const updatedKey = key.replace(oldPath, newPath);

// Check if the current file contains the old path
if (regex.test(content)) {
console.log(`Updating key '${key}' to '${updatedKey}' in ${file}`);
content = content.replace(regex, `'${updatedKey}'`);
modified = true;
}
});

if (modified) {
fs.writeFileSync(file, content);
console.log(`File ${file} has been updated.`);
} else {
// console.log(`No changes made to ${file}.`);
}
});
}

const [liquidFilesPattern, oldPath, newPath] = process.argv.slice(2);
const [defaultFile, oldPath, newPath] = process.argv.slice(2);

const enDefaultData = JSON.parse(fs.readFileSync('./locales/en.default.json', 'utf8'));
const enDefaultData = JSON.parse(fs.readFileSync(defaultFile, 'utf8'));
const similarKeys = findSimilarKeys(enDefaultData, oldPath);

similarKeys.push(oldPath); // Include the original oldPath in similarKeys for replacement
console.log(similarKeys)
modifyJsonFiles(oldPath, newPath, similarKeys);
updateLiquidFiles(liquidFilesPattern, similarKeys, oldPath, newPath);
modifyJsonFiles(oldPath, newPath, similarKeys);
109 changes: 109 additions & 0 deletions scripts/update-to-new-locales.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// Utility script to compare locales files across this repo + theme project for matching values
// and then update liquid files in theme project with locale repo keys
//
// Useful for updating your theme project to use locale keys from this project.
//
// node scripts/update-to-new-locales.js ../fetch/locales/en.default.schema.json locales/en.default.schema.json ../fetch
//


const fs = require('fs');
const path = require('path');

Expand All @@ -13,6 +22,44 @@ function flattenObject(obj, prefix = '') {
}, {});
}

function findMissingValues(oldObj, newObj) {
const flatOldObj = flattenObject(oldObj);
const flatNewObj = flattenObject(newObj);
const missingValues = [];

// Collect values from the new JSON for comparison
const newValues = new Set(Object.values(flatNewObj));

for (const [keyOld, valueOld] of Object.entries(flatOldObj)) {
if (!newValues.has(valueOld)) {
missingValues.push(keyOld);
}
}
return missingValues;
}

function checkKeysInFiles(missingKeys, directory) {
const files = fs.readdirSync(directory, { withFileTypes: true });
const usedKeys = new Set();

files.forEach(file => {
if (file.isFile()) {
const filePath = path.join(directory, file.name);
const fileContent = fs.readFileSync(filePath, 'utf8');
missingKeys.forEach(key => {
const keyWithPrefix = `t:${key}`;
if (fileContent.includes(keyWithPrefix)) {
usedKeys.add(key);
}
});
} else if (file.isDirectory()) {
const subdirUsedKeys = checkKeysInFiles(missingKeys, path.join(directory, file.name));
subdirUsedKeys.forEach(key => usedKeys.add(key));
}
});

return usedKeys;
}

function createValueMapping(oldObj, newObj) {
const flatOldObj = flattenObject(oldObj);
Expand Down Expand Up @@ -120,6 +167,53 @@ function updateFilesInDirectory(directory, mapping) {
});
}

function addMissingKeysToJson(newJson, oldJson, missingKeys) {
if (!newJson.unsorted) {
newJson.missing = {};
}

let count = 0;
missingKeys.forEach((key) => {
// Handle nested keys
const keys = key.split('.');
let currentLevel = oldJson;
let keyFound = true;

for (let i = 0; i < keys.length; i++) {
if (currentLevel[keys[i]] !== undefined) {
currentLevel = currentLevel[keys[i]];
} else {
keyFound = false;
break;
}
}

if (keyFound && !Object.values(newJson.missing).includes(currentLevel)) {
count += 1
newJson.missing[`${count}`] = currentLevel;
}
});

return newJson;
}

function updateAssociatedJsonFiles(oldDirectory, newDirectory, oldJson, missingKeys) {
const schemaFiles = fs.readdirSync(oldDirectory).filter(file => file.endsWith('.schema.json'));

schemaFiles.forEach(file => {
const oldFilePath = path.join(oldDirectory, file);
const newFilePath = path.join(newDirectory, file);

if (fs.existsSync(newFilePath)) {
const oldFileJson = JSON.parse(fs.readFileSync(oldFilePath, 'utf8'));
let newFileJson = JSON.parse(fs.readFileSync(newFilePath, 'utf8'));

newFileJson = addMissingKeysToJson(newFileJson, oldFileJson, missingKeys);
fs.writeFileSync(newFilePath, JSON.stringify(newFileJson, null, 2), 'utf8');
}
});
}

function main() {
const [jsonFileOldPath, jsonFileNewPath, filesDirectory] = process.argv.slice(2);

Expand All @@ -130,6 +224,21 @@ function main() {

const jsonFileOld = JSON.parse(fs.readFileSync(jsonFileOldPath, 'utf8'));
const jsonFileNew = JSON.parse(fs.readFileSync(jsonFileNewPath, 'utf8'));
const oldDirectory = path.dirname(jsonFileOldPath);
const newDirectory = path.dirname(jsonFileNewPath);

const potentiallyMissingValues = findMissingValues(jsonFileOld, jsonFileNew);
const actuallyUsedKeys = checkKeysInFiles(potentiallyMissingValues, filesDirectory);

actuallyUsedKeys.forEach(key => console.log(`Key with missing value in new JSON and used in files: ${key}`));

if (actuallyUsedKeys.size > 0) {
addMissingKeysToJson(jsonFileNew, jsonFileOld, actuallyUsedKeys);
updateAssociatedJsonFiles(oldDirectory, newDirectory, jsonFileOld, actuallyUsedKeys);
console.log('Missing keys in new locales file detected. Added missing keys to new locales file in a "unsorted" nested object. Please confirm key name of new keys and rerun this script.')
return;
}


const valueMapping = createValueMapping(jsonFileOld, jsonFileNew);

Expand Down

0 comments on commit d134592

Please sign in to comment.