Skip to content

Commit

Permalink
[Feat] Add support of data connector for "new", "init" and "publish" …
Browse files Browse the repository at this point in the history
…commands (#75)
  • Loading branch information
psamusev authored Oct 17, 2024
1 parent 614ed52 commit 3399cc2
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/connector-cli/resources/schemas/connector-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"type": {
"type": "string",
"enum": ["media"]
"enum": ["media", "data"]
},
"iconUrl": {
"type": "string"
Expand Down
23 changes: 14 additions & 9 deletions src/connector-cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path';
import { info, startCommand, verbose } from '../../core';
import { Type as ConnectorType, ExecutionError } from '../../core/types';
import {
getDataConnectorFile,
getGitIgnoreFile,
getMediaConnectorFile,
getMediaConnectorTestFile,
Expand Down Expand Up @@ -71,19 +72,23 @@ export async function runInit(options: InitCommandOptions): Promise<void> {
);

// 4. create connector file
const { content, fileName } = getMediaConnectorFile();
const { content, fileName } =
options.type === ConnectorType.Media
? getMediaConnectorFile()
: getDataConnectorFile();

info('Creating ' + fileName);
fs.writeFileSync(path.join(projectDir, fileName), content);

// 5. create tests.json
const testsJson = getMediaConnectorTestFile();

info('Creating tests.json');
fs.writeFileSync(
path.join(projectDir, './tests.json'),
JSON.stringify(testsJson, null, 2)
);
if (options.type === ConnectorType.Media) {
// 5. create tests.json
const testsJson = getMediaConnectorTestFile();
info('Creating tests.json');
fs.writeFileSync(
path.join(projectDir, './tests.json'),
JSON.stringify(testsJson, null, 2)
);
}

// 6. create .gitignore
const gitIgnore = getGitIgnoreFile();
Expand Down
39 changes: 39 additions & 0 deletions src/connector-cli/src/commands/init/templates/data-connector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { connectorFileName } from '../../../utils/connector-project';

const connectorFileContent = `import { Connector, Data } from "@chili-publish/studio-connectors";
export default class MyConnector implements Data.DataConnector {
private runtime: Connector.ConnectorRuntimeContext;
constructor(runtime: Connector.ConnectorRuntimeContext) {
this.runtime = runtime;
}
getPage(
config: Data.PageConfig,
context: Connector.Dictionary
): Promise<Data.DataPage> {
throw new Error('Method not implemented.');
}
getModel(context: Connector.Dictionary): Promise<Data.DataModel> {
throw new Error('Method not implemented.');
}
getConfigurationOptions(): Connector.ConnectorConfigValue[] | null {
return [];
}
getCapabilities(): Data.DataConnectorCapabilities {
return {
filtering: false,
sorting: false,
model: false,
}
}
}`;
export const getDataConnectorFile = () => ({
content: connectorFileContent,
fileName: connectorFileName,
});
4 changes: 4 additions & 0 deletions src/connector-cli/src/commands/init/templates/gitignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { outputDirectory } from '../../../utils/connector-project';

export const getGitIgnoreFile = () => `node_modules
${outputDirectory}`;
10 changes: 10 additions & 0 deletions src/connector-cli/src/commands/init/templates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Generic
export * from './gitignore';
export * from './package.json';
export * from './tsconfig';

// Media connector
export * from './media-connector';

// Data connector
export * from './data-connector';
Original file line number Diff line number Diff line change
@@ -1,58 +1,6 @@
import { Type } from '../../core/types';
import {
connectorFileName,
outputDirectory,
outputFilename,
} from '../../utils/connector-project';
import { connectorFileName } from '../../../utils/connector-project';

export const getPackageJson = (projectName: string, type: Type) => ({
name: projectName,
description: '',
version: '1.0.0',
author: {
name: 'CHILI publish',
email: '[email protected]',
url: 'https://github.com/chili-publish',
},
config: {
type: type,
options: {},
mappings: {},
supportedAuth: [],
},
license: 'MIT',
main: `${outputDirectory}/${outputFilename}`,
dependencies: {
typescript: '^5.2.2',
'@chili-publish/studio-connectors': '^1',
},
scripts: {
build: 'yarn connector-cli build',
test: 'yarn connector-cli test -t tests.json && yarn connector-cli stress',
},
devDependencies: {
'@chili-publish/connector-cli': '^1',
},
});

export const getTsConfig = () => ({
compilerOptions: {
lib: ['ES2020'],
noEmitHelpers: true,
module: 'ES2020',
outDir: `${outputDirectory}`,
target: 'ES2020',
moduleResolution: 'Node',
preserveConstEnums: false,
esModuleInterop: false,
removeComments: true,
declaration: false,
},
include: [connectorFileName],
exclude: ['node_modules', `${outputDirectory}`],
});

const mediaConnectorFileContent = `import { Connector, Media } from "@chili-publish/studio-connectors";
const connectorFileContent = `import { Connector, Media } from "@chili-publish/studio-connectors";
export default class MyConnector implements Media.MediaConnector {
Expand All @@ -61,19 +9,21 @@ export default class MyConnector implements Media.MediaConnector {
constructor(runtime: Connector.ConnectorRuntimeContext) {
this.runtime = runtime;
}
query(
options: Connector.QueryOptions,
context: Connector.Dictionary
): Promise<Media.MediaPage> {
throw new Error("Method not implemented.");
}
detail(
id: string,
context: Connector.Dictionary
): Promise<Media.MediaDetail> {
throw new Error("Method not implemented.");
}
download(
id: string,
previewType: Media.DownloadType,
Expand All @@ -82,9 +32,11 @@ export default class MyConnector implements Media.MediaConnector {
): Promise<Connector.ArrayBufferPointer> {
throw new Error("Method not implemented.");
}
getConfigurationOptions(): Connector.ConnectorConfigValue[] | null {
return [];
}
getCapabilities(): Media.MediaConnectorCapabilities {
return {
query: false,
Expand All @@ -95,7 +47,7 @@ export default class MyConnector implements Media.MediaConnector {
}
}`;
export const getMediaConnectorFile = () => ({
content: mediaConnectorFileContent,
content: connectorFileContent,
fileName: connectorFileName,
});

Expand Down Expand Up @@ -126,6 +78,3 @@ export const getMediaConnectorTestFile = () => ({
},
],
});

export const getGitIgnoreFile = () => `node_modules
${outputDirectory}`;
35 changes: 35 additions & 0 deletions src/connector-cli/src/commands/init/templates/package.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Type } from '../../../core/types';
import {
outputDirectory,
outputFilename,
} from '../../../utils/connector-project';

export const getPackageJson = (projectName: string, type: Type) => ({
name: projectName,
description: '',
version: '1.0.0',
author: {
name: 'CHILI publish',
email: '[email protected]',
url: 'https://github.com/chili-publish',
},
config: {
type: type,
options: {},
mappings: {},
supportedAuth: [],
},
license: 'MIT',
main: `${outputDirectory}/${outputFilename}`,
dependencies: {
typescript: '^5.2.2',
'@chili-publish/studio-connectors': '^1.16.0',
},
scripts: {
build: 'yarn connector-cli build',
test: 'yarn connector-cli test -t tests.json && yarn connector-cli stress',
},
devDependencies: {
'@chili-publish/connector-cli': '^1.9.0',
},
});
21 changes: 21 additions & 0 deletions src/connector-cli/src/commands/init/templates/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
connectorFileName,
outputDirectory,
} from '../../../utils/connector-project';

export const getTsConfig = () => ({
compilerOptions: {
lib: ['ES2020'],
noEmitHelpers: true,
module: 'ES2020',
outDir: `${outputDirectory}`,
target: 'ES2020',
moduleResolution: 'Node',
preserveConstEnums: false,
esModuleInterop: false,
removeComments: true,
declaration: false,
},
include: [connectorFileName],
exclude: ['node_modules', `${outputDirectory}`],
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function updateExistingConnector(
token: string,
payload: UpdateConnectorPayload
): Promise<void> {
info('Retrieving connector to remove...');
info('Retrieving connector to update...');
const existingConnector = await getConnectorById({
baseUrl: connectorEndpointBaseUrl,
connectorId,
Expand Down
2 changes: 1 addition & 1 deletion src/connector-cli/src/commands/publish/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface ProxyOptions {
interface ConnectorPayload {
name: string;
description: string;
type: 'media' | 'fonts';
type: 'media' | 'fonts' | 'data';
version: string;
iconUrl?: string;
script: string;
Expand Down
2 changes: 2 additions & 0 deletions src/connector-cli/src/core/types/gen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export enum SupportedAuth {
}

export enum Type {
Data = "data",
Media = "media",
}

Expand Down Expand Up @@ -406,6 +407,7 @@ const typeMap: any = {
"staticKey",
],
"Type": [
"data",
"media",
],
};

0 comments on commit 3399cc2

Please sign in to comment.