Skip to content

Commit

Permalink
Build names file for keyboards
Browse files Browse the repository at this point in the history
  • Loading branch information
olivia committed Aug 29, 2022
1 parent ad98376 commit e094740
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"build-v2-isolated": "ts-node scripts/build-isolated-v2.ts",
"generate-v2": "ts-node scripts/generate-v2.ts",
"build-v3": "ts-node scripts/build-v3.ts",
"build-names": "ts-node scripts/build-names.ts",
"v2-to-v3": "ts-node scripts/v2-to-v3.ts",
"dev": "ts-node-dev --respawn scripts/serve.ts",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-all.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import rimraf from 'rimraf';
import {promisify} from 'bluebird';
import {buildV2} from './build-v2';
import {buildNames} from './build-names';
import {buildV3} from './build-v3';

async function build() {
await promisify(rimraf)('dist/*');

await buildV2();
await buildNames();
await buildV3();
}

Expand Down
45 changes: 45 additions & 0 deletions scripts/build-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import stringify from 'json-stringify-pretty-compact';
import * as glob from 'glob';
import * as fs from 'fs';
import {keyboardDefinitionV2ToVIADefinitionV2} from 'via-reader';
import process from 'process';
import path from 'path';
import {getDefinitionsPath, getOutputPath} from './get-path';

export async function buildNames() {
try {
const paths = glob.sync(getDefinitionsPath(), {absolute: true});
console.log(path.resolve('./'));

const [v2Definitions] = [paths].map((paths) =>
paths.map((f) => require(f))
);

const resV2 = Object.values(
v2Definitions
.map(keyboardDefinitionV2ToVIADefinitionV2)
.reduce((p, n) => {
if (n.vendorProductId in p) {
console.log(
`Duplicate id found: ${n.name} collides with ${
p[n.vendorProductId].name
}`
);
}
return {...p, [n.vendorProductId]: n};
}, {} as any)
)
.map((d: any) => d.name)
.sort();

const outputPath = getOutputPath();
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}

fs.writeFileSync(`${outputPath}/keyboard_names.json`, stringify(resV2));
} catch (error) {
console.error(error);
process.exit(1);
}
}

0 comments on commit e094740

Please sign in to comment.