Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions scripts/extract-android-device-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import fs from 'node:fs';

// @ts-expect-error csv-parser has no types
import csv from 'csv-parser';
const LF = String.fromCharCode(10);

/**
* Transform CSV results into device mapping object
Expand All @@ -14,17 +13,76 @@ const transformResults = res => {
/** @type {Record<string, string>} */
const deviceMapping = {};
res.forEach(({name, model}) => {
if (name && model) {
if (name && model && name !== model) {
deviceMapping[model] = name;
}
});
return deviceMapping;
};

/**
* Parse a CSV line
* @param {string} line
* @returns {string[]}
*/
const parseCsvLine = line => {
/** @type {string[]} */
const fields = [];
let current = '';
let inQuotes = false;

for (let i = 0; i < line.length; i++) {
const ch = line[i];
if (inQuotes) {
if (ch === '"' && line[i + 1] === '"') {
current += '"';
i++;
} else if (ch === '"') {
inQuotes = false;
} else {
current += ch;
}
} else if (ch === '"') {
inQuotes = true;
} else if (ch === ',') {
fields.push(current);
current = '';
} else {
current += ch;
}
}
fields.push(current);
return fields;
};

const content = fs.readFileSync('supported_devices.csv');
const text = content.toString('utf16le').slice(1);
const lines = text.split(LF);

const headers = lines[0] ? parseCsvLine(lines[0].trim()) : [];

const nameIdx = headers.indexOf('Marketing Name');
const modelIdx = headers.indexOf('Model');

if (nameIdx === -1 || modelIdx === -1) {
console.error('Could not find "Marketing Name" or "Model" columns in CSV headers');
console.error('Found headers:', headers);
process.exit(1);
}

/** @type {Array<{name: string, model: string}>} */
const results = [];
fs.createReadStream('supported_devices.csv')
.pipe(csv())
.on('data', /** @param {any} data */ data => results.push(data))
.on('end', () => {
fs.writeFileSync('devices.json', JSON.stringify(transformResults(results)));
});

for (let i = 1; i < lines.length; i++) {
const line = lines[i]?.trim();
if (!line) {
continue;
}
const fields = parseCsvLine(line);
if (!fields[nameIdx] || !fields[modelIdx]) {
continue;
}
results.push({name: fields[nameIdx], model: fields[modelIdx]});
}

fs.writeFileSync('devices.json', JSON.stringify(transformResults(results), null, 4));
Loading
Loading