diff --git a/scripts/import/store.js b/scripts/import/store.js index d4588a7..8e2d038 100644 --- a/scripts/import/store.js +++ b/scripts/import/store.js @@ -133,11 +133,21 @@ function fillGaps(brand, current, { aliases, bootloader = {}, ...rest }) { claim(brand, newAliases); } +/** Lenovo's "ZUK Z2 Plus" is ZUK's Z2 Plus, as ZUK is a brand of its own here */ +function ownBrand(vendor, device) { + const [first, ...rest] = device.name.trim().split(/\s+/); + const brands = read(join(DATA, 'brands.yml')) ?? []; + const owner = brands.find(({ key }) => key === slugify(first)); + if (!owner || !rest.length || owner.key === brandKey(vendor)) return [vendor, device]; + return [owner.name, { ...device, name: rest.join(' ') }]; +} + /** * Finds or creates a device, only ever filling fields that are missing or unknown. * Returns its brand/codename, or null when it's out of scope */ -export function upsertDevice(brandName, device) { +export function upsertDevice(vendor, imported) { + const [brandName, device] = ownBrand(vendor, imported); const brand = brandKey(brandName); const id = `${brand}/${device.codenames[0]}`; if (device.type === 'skip') outOfScope.add(id); diff --git a/tests/import/store.test.js b/tests/import/store.test.js index 22445c1..5ab4160 100644 --- a/tests/import/store.test.js +++ b/tests/import/store.test.js @@ -53,6 +53,12 @@ test('a codename another brand already uses gets a device of its own', () => { assert.ok(existsSync(join(process.env.DATA_DIR, 'devices/tidy/twin.yml'))); }); +test('a name that opens with another brand we list goes to that brand', () => { + upsertDevice('Acme', { name: 'Rocket', codenames: ['rocket'] }); + assert.equal(upsertDevice('Tidy', { name: 'Acme Rocket', codenames: ['rocket'] }), 'acme/rocket'); + assert.ok(!existsSync(join(process.env.DATA_DIR, 'devices/tidy/rocket.yml'))); +}); + test('a source that loses a fifth of its devices fails and leaves the file alone', () => { writeUpstream('floor', rows(10)); assert.throws(() => writeUpstream('floor', rows(7)), /only 7 active devices, last import had 10/);