Skip to content

Commit

Permalink
chore: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdurdin committed Dec 5, 2024
1 parent 33b4fa3 commit 170282b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
18 changes: 18 additions & 0 deletions developer/src/common/web/utils/src/cloud-urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Matches a Keyman keyboard resource, based on the permanent home page for the
* keyboard on keyman.com, `https://keyman.com/keyboards/<id>`
*/
export const KEYMANCOM_CLOUD_URI = /^(?:http(?:s)?:\/\/)?keyman\.com\/keyboards\/(?<id>[a-z0-9_.-]+)/i;

/**
* Matches a `cloud:<id>` URI for a Keyman resource (e.g. keyboard or lexical
* model)
*/
export const CLOUD_URI = /^cloud:(?<id>.+)$/i;


export interface CloudUriRegexMatchArray extends RegExpMatchArray {
groups?: {
id?: string;
}
}
3 changes: 2 additions & 1 deletion developer/src/common/web/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ export { CommonTypesMessages } from './common-messages.js';
export * as SourceFilenamePatterns from './source-filename-patterns.js';
export { KeymanXMLType, KeymanXMLWriter, KeymanXMLReader } from './xml-utils.js';

export * as GitHubUrls from './github-urls.js';
export * as GitHubUrls from './github-urls.js';
export * as CloudUrls from './cloud-urls.js';
19 changes: 9 additions & 10 deletions developer/src/kmc-copy/src/KeymanProjectCopier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copy a keyboard or lexical model project
*/

import { GitHubUrls, CompilerCallbacks, CompilerLogLevel, KeymanCompiler, KeymanCompilerArtifact, KeymanCompilerArtifacts, KeymanCompilerResult, KeymanDeveloperProject, KeymanDeveloperProjectOptions, KPJFileReader, KPJFileWriter, KpsFileReader, KpsFileWriter } from "@keymanapp/developer-utils";
import { CloudUrls, GitHubUrls, CompilerCallbacks, CompilerLogLevel, KeymanCompiler, KeymanCompilerArtifact, KeymanCompilerArtifacts, KeymanCompilerResult, KeymanDeveloperProject, KeymanDeveloperProjectOptions, KPJFileReader, KPJFileWriter, KpsFileReader, KpsFileWriter } from "@keymanapp/developer-utils";
import { KeymanFileTypes } from "@keymanapp/common-types";

import { CopierMessages } from "./copier-messages.js";
Expand All @@ -15,9 +15,6 @@ type CopierFunction = (
project: KeymanDeveloperProject, filename: string, outputPath: string, source: string, result: CopierResult
) => Promise<boolean>;

const KEYMANCOM_CLOUD_URI = /^(?:http(?:s)?:\/\/)?keyman\.com\/keyboards\/(?<id>[a-z0-9_.-]+)/i;
const CLOUD_URI = /^cloud:(?<id>.+)$/i;

/**
* @public
* Options for the Keyman Developer project copier
Expand Down Expand Up @@ -160,7 +157,7 @@ export class KeymanProjectCopier implements KeymanCompiler {
if(source.match(GitHubUrls.GITHUB_URI_OPTIONAL_PROTOCOL) || source.match(GitHubUrls.GITHUB_RAW_URI)) {
// `[https://]github.com/owner/repo/[tree|blob|raw]/[refs/...]/branch/path/to/kpj`, referencing a .kpj file
return await this.getGitHubSourceProject(source);
} else if(source.match(CLOUD_URI) || source.match(KEYMANCOM_CLOUD_URI)) {
} else if(source.match(CloudUrls.CLOUD_URI) || source.match(CloudUrls.KEYMANCOM_CLOUD_URI)) {
// `cloud:id`, referencing a Keyman Cloud keyboard
return await this.getCloudSourceProject(source);
} else if(this.callbacks.fs.existsSync(source) && source.endsWith(KeymanFileTypes.Source.Project) && !this.callbacks.isDirectory(source)) {
Expand Down Expand Up @@ -224,8 +221,8 @@ export class KeymanProjectCopier implements KeymanCompiler {
}
}
if(!ref.path) {
ref.path = '/'
};
ref.path = '/';
}
if(!ref.path.startsWith('/')) {
ref.path = '/' + ref.path;
}
Expand Down Expand Up @@ -256,11 +253,13 @@ export class KeymanProjectCopier implements KeymanCompiler {
* @returns a promise: GitHub reference to the source for the keyboard, or null on failure
*/
private async getCloudSourceProject(source: string): Promise<GitHubRef> {
const parts = CLOUD_URI.exec(source) ?? KEYMANCOM_CLOUD_URI.exec(source);
const id: string = parts.groups.id;
const parts = CloudUrls.CLOUD_URI.exec(source) ?? CloudUrls.KEYMANCOM_CLOUD_URI.exec(source);
if(!parts) {
throw new Error('Expected CLOUD_URI or KEYMANCOM_CLOUD_URI to match');
}

const id: string = parts.groups.id;
const isModel = /^[^.]+\.[^.]+\.[^.]+$/.test(id);

const remote = await this.cloudSource.getSourceFromKeymanCloud(id, isModel);
if(!remote) {
return null;
Expand Down

0 comments on commit 170282b

Please sign in to comment.