Skip to content

Commit

Permalink
Merge branch 'the-via:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
KeychronMacro authored Jan 25, 2022
2 parents 8d3fa7f + 2959f1b commit 1fedbb8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
7 changes: 7 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
'use strict';
var tsNode = require('ts-node');
tsNode.register({
project: './node_modules/via-keyboards/tsconfig.json',
});
require('../scripts/build-all');
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "keyboards",
"name": "via-keyboards",
"version": "0.0.1",
"description": "",
"main": "index.js",
"bin": {
"via-keyboards": "./bin/cli.js"
},
"scripts": {
"build": "ts-node scripts/build-all.ts",
"build-v2": "ts-node scripts/build-v2.ts",
Expand Down
8 changes: 4 additions & 4 deletions scripts/build-isolated-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DefinitionVersion,
} from 'via-reader';
import {ValidateFunction} from 'via-reader/dist/validated-types/via-definition-v3.validator';
import {getDefinitionsPath, getOutputPath} from './get-path'

/**
* Builds keyboard definitions into separate valid VIA definitions
Expand All @@ -24,10 +25,9 @@ export const buildIsolatedDefinitions = async <
mapper: (definition: TInput) => TOutput,
validator: ValidateFunction<TOutput>
): Promise<number[]> => {
const outputPath = `dist/${version}`;

const globPath = version === 'v2' ? 'src/**/*.json' : `${version}/**/*.json`;
const paths = glob.sync(globPath, {absolute: true});
const outputPath = `${getOutputPath()}/${version}`;
const definitionsPath = getDefinitionsPath(version);
const paths = glob.sync(definitionsPath, {absolute: true});
const definitions: TInput[] = paths.map((f) => require(f));

// Map KeyboardDefinition to VIADefintion and valiate. Don't write invalid definitions.
Expand Down
15 changes: 11 additions & 4 deletions scripts/build-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import stringify from 'json-stringify-pretty-compact';
import * as glob from 'glob';
import * as fs from 'fs';
import {getTheme, keyboardDefinitionV2ToVIADefinitionV2} from 'via-reader';
import process from 'process';
import path from 'path';
import {getDefinitionsPath, getOutputPath} from './get-path';

const viaAPIVersionV2 = '0.1.2';

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

const [v2Definitions] = [paths].map((paths) =>
paths.map((f) => require(f))
Expand All @@ -22,11 +28,12 @@ export async function buildV2() {
.reduce((p, n) => ({...p, [n.vendorProductId]: n}), {}),
};

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

fs.writeFileSync('dist/keyboards.v2.json', stringify(resV2));
fs.writeFileSync(`${outputPath}/keyboards.v2.json`, stringify(resV2));
} catch (error) {
console.error(error);
process.exit(1);
Expand Down
7 changes: 4 additions & 3 deletions scripts/build-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'via-reader';
import stringify from 'json-stringify-pretty-compact';
import {buildIsolatedDefinitions} from './build-isolated-definitions';
import {getCommonMenusPath, getOutputPath } from './get-path';
var packageJson = require('../package.json');

export async function buildV3() {
Expand All @@ -36,10 +37,10 @@ export async function buildV3() {
},
};

fs.writeFileSync('dist/supported_kbs.json', stringify(definitionIndex));
fs.writeFileSync(`${getOutputPath()}/supported_kbs.json`, stringify(definitionIndex));

// Read all common-menus configurations asynchronously.
const commonMenusFiles = glob.sync('common-menus/**.json');
const commonMenusFiles = glob.sync(`${getCommonMenusPath()}/**.json`);
const commonMenusJson = {} as Record<string, string>;
const commonMenusReaders = commonMenusFiles.map((commonMenuFile) => {
return fs.promises.readFile(commonMenuFile, 'utf8');
Expand All @@ -54,7 +55,7 @@ export async function buildV3() {

commonMenusJson[fileName] = JSON.parse(menu);
});
fs.writeFileSync('dist/common-menus.json', stringify(commonMenusJson));
fs.writeFileSync(`${getOutputPath()}/common-menus.json`, stringify(commonMenusJson));
});
} catch (error) {
console.error(error);
Expand Down
11 changes: 11 additions & 0 deletions scripts/get-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import path from 'path';
export function getDefinitionsPath(version = 'v2') {
return path.join(__dirname, '..',version === 'v2' ? 'src/**/*.json' : `${version}/**/*.json`)
}
export function getCommonMenusPath() {
return path.resolve(__dirname, '..','common-menus')
}
export function getOutputPath() {
return path.resolve(process.argv[2] || 'dist');
}

0 comments on commit 1fedbb8

Please sign in to comment.