chore(common/web): more import path fun

This commit is contained in:
Joshua A. Horton 2023-02-07 13:49:08 +07:00
parent e6fe2b73bf
commit 8b57e28a34
2 changed files with 18 additions and 5 deletions

View file

@ -0,0 +1,3 @@
// Exists to facilitate relative path resolution via Node's require.resolve function.
export { };

View file

@ -1,16 +1,26 @@
import { assert } from 'chai';
import fs from 'fs';
import path from 'path';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { KeyboardProperties, SpacebarText } from '@keymanapp/keyboard-processor';
describe('Keyboard Properties', function() {
let rootCommonStubPath = '../../test/resources/json/keyboards/';
let commonResourcesPackage = '@keymanapp/common-test-resources';
let commonStubsSubpath = 'json/keyboards';
let rootCommonStubPath = `${commonResourcesPackage}/${commonStubsSubpath}`;
it('initialization from KMW\'s addKeyboards() API spec', () => {
let files = fs.readdirSync(rootCommonStubPath);
// require.resolve cannot resolve a directory directly, unfortunately.
// Needs a file to 'root' the resolution mechanism. See https://github.com/nodejs/node/issues/42219.
const resolvedResourcesPath = path.dirname(require.resolve(`${commonResourcesPackage}/index.mjs`));
const resolvedCommonStubPath = `${resolvedResourcesPath}/${commonStubsSubpath}`;
let files = fs.readdirSync(resolvedCommonStubPath);
for(let file of files) {
let stub = JSON.parse(fs.readFileSync(`${rootCommonStubPath}/${file}`));
let stub = JSON.parse(fs.readFileSync(`${resolvedCommonStubPath}/${file}`));
let dataset = [];
if(stub.languages instanceof Array) {
@ -35,7 +45,7 @@ describe('Keyboard Properties', function() {
it('generates display-name text if not directly-specified', () => {
// Could convert to run on all stubs, but... this should be fine as-is.
let stub = JSON.parse(fs.readFileSync(`${rootCommonStubPath}/khmer_angkor.json`));
let stub = JSON.parse(fs.readFileSync(require.resolve(`${rootCommonStubPath}/khmer_angkor.json`)));
let propObject = new KeyboardProperties(stub);
// Without a configured SpacebarText value, will display the keyboard name.
@ -56,7 +66,7 @@ describe('Keyboard Properties', function() {
it('does not override directly-specified display-name text', () => {
// Could convert to run on all stubs, but... this should be fine as-is.
let stub = JSON.parse(fs.readFileSync(`${rootCommonStubPath}/khmer_angkor.json`));
let stub = JSON.parse(fs.readFileSync(require.resolve(`${rootCommonStubPath}/khmer_angkor.json`)));
const customDisplayName = "(custom)";
let propObject = new KeyboardProperties(stub);