Skip to content

Commit

Permalink
#648 Add ontologies to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Polleps committed Oct 3, 2023
1 parent 3675c12 commit 4c298ef
Show file tree
Hide file tree
Showing 17 changed files with 687 additions and 17 deletions.
4 changes: 2 additions & 2 deletions browser/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const TEMPLATE_CONFIG_FILE = {
export const initCommand = async (args: string[]) => {
const forced = args.includes('--force') || args.includes('-f');
const filePath = path.join(process.cwd(), 'atomic.config.json');
const stat = fs.statSync(filePath);
const stat = fs.statSync(filePath, { throwIfNoEntry: false });

if (stat.isFile() && !forced) {
if (stat?.isFile() && !forced) {
return console.error(
chalk.red(
`ERROR: File already exists. If you meant to override the existing file, use the command with the ${chalk.cyan(
Expand Down
4 changes: 4 additions & 0 deletions browser/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export interface AtomicConfig {
* The default should be sufficient in most but if you have given the module an alias you should change this value
*/
moduleAlias?: string;
/**
* [OPTIONAL] By default we generate class types for ease of use (e.g: export type Property = typeof core.classes.property;). But these names can clash with other interfaces or classes in your project. When 'useNameSpaces' is set to true the types will be wrapped in a namespace.
*/
useNamespaces?: boolean;
/**
* [OPTIONAL] The secret of the agent that is used to access your atomic data server. This can also be provided as a command line argument if you don't want to store it in the config file.
* If left empty the public agent is used.
Expand Down
24 changes: 23 additions & 1 deletion browser/cli/src/generateClassExports.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import { Resource, urls } from '@tomic/lib';
import { atomicConfig } from './config.js';
import { ReverseMapping } from './generateBaseObject.js';
import { store } from './store.js';
import { camelCaseify } from './utils.js';

enum Inserts {
NamespaceName = '{{1}}',
NamespaceBody = '{{2}}',
}

const NAMESPACE_TEMPLATE = `
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace ${Inserts.NamespaceName} {
${Inserts.NamespaceBody}
}
`;

export const generateClassExports = (
ontology: Resource,
reverseMapping: ReverseMapping,
): string => {
const classes = ontology.getArray(urls.properties.classes) as string[];

return classes
const body = classes
.map(subject => {
const res = store.getResourceLoading(subject);
const objectPath = reverseMapping[subject];

return createExportLine(res.title, objectPath);
})
.join('\n');

if (atomicConfig.useNamespaces) {
return NAMESPACE_TEMPLATE.replace(
Inserts.NamespaceName,
capitalize(ontology.title),
).replace(Inserts.NamespaceBody, body);
} else {
return body;
}
};

const createExportLine = (title: string, objectPath: string) =>
Expand Down
12 changes: 12 additions & 0 deletions browser/lib/atomic.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"outputFolder": "./src/ontologies",
"moduleAlias": "../index.js",
"useNamespaces": true,
"ontologies": [
"https://atomicdata.dev/ontology/core",
"https://atomicdata.dev/ontology/commit",
"https://atomicdata.dev/ontology/collection",
"https://atomicdata.dev/ontology/data-browser",
"https://atomicdata.dev/ontology/server"
]
}
6 changes: 4 additions & 2 deletions browser/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"@types/fast-json-stable-stringify": "^2.1.0",
"chai": "^4.3.4",
"typescript": "^4.8",
"whatwg-fetch": "^3.6.2"
"whatwg-fetch": "^3.6.2",
"@tomic/cli": "^0.35.2"
},
"files": [
"dist"
Expand All @@ -34,7 +35,8 @@
"start": "pnpm watch",
"test": "NODE_OPTIONS='--experimental-vm-modules' ../node_modules/jest/bin/jest.js",
"tsc": "tsc --build",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"generate-ontologies": "ad-generate ontologies"
},
"source": "src/index.ts",
"type": "module",
Expand Down
9 changes: 9 additions & 0 deletions browser/lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
* @module
*/

import { initOntologies } from './ontologies/index.js';

initOntologies();

export * from './ontologies/core.js';
export * from './ontologies/collections.js';
export * from './ontologies/commits.js';
export * from './ontologies/dataBrowser.js';
export * from './ontologies/server.js';
export * from './agent.js';
export * from './authentication.js';
export * from './class.js';
Expand Down
66 changes: 66 additions & 0 deletions browser/lib/src/ontologies/collections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

/* -----------------------------------
* GENERATED WITH ATOMIC-GENERATE
* -------------------------------- */

import { BaseProps } from '../index.js'

export const collections = {
classes: {
collection: 'https://atomicdata.dev/classes/Collection',
},
properties: {
members: 'https://atomicdata.dev/properties/collection/members',
currentPage: 'https://atomicdata.dev/properties/collection/currentPage',
pageSize: 'https://atomicdata.dev/properties/collection/pageSize',
property: 'https://atomicdata.dev/properties/collection/property',
totalMembers: 'https://atomicdata.dev/properties/collection/totalMembers',
totalPages: 'https://atomicdata.dev/properties/collection/totalPages',
value: 'https://atomicdata.dev/properties/collection/value',
sortBy: 'https://atomicdata.dev/properties/collection/sortBy',
sortDesc: 'https://atomicdata.dev/properties/collection/sortDesc',
includeExternal: 'https://atomicdata.dev/properties/collection/includeExternal',
},
} as const


// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Collections {
export type Collection = typeof collections.classes.collection;
}


declare module '../index.js' {
interface Classes {
[collections.classes.collection]: {
requires: BaseProps;
recommends: 'https://atomicdata.dev/properties/name' | 'https://atomicdata.dev/properties/description' | typeof collections.properties.currentPage | typeof collections.properties.members | typeof collections.properties.pageSize | typeof collections.properties.property | typeof collections.properties.sortBy | typeof collections.properties.sortDesc | typeof collections.properties.totalMembers | typeof collections.properties.totalPages | typeof collections.properties.value | typeof collections.properties.includeExternal | 'https://atomicdata.dev/properties/incomplete';
};
}

interface PropTypeMapping {
[collections.properties.members]: string[]
[collections.properties.currentPage]: number
[collections.properties.pageSize]: number
[collections.properties.property]: string
[collections.properties.totalMembers]: number
[collections.properties.totalPages]: number
[collections.properties.value]: string
[collections.properties.sortBy]: string
[collections.properties.sortDesc]: boolean
[collections.properties.includeExternal]: boolean
}

interface PropSubjectToNameMapping {
[collections.properties.members]: 'members',
[collections.properties.currentPage]: 'currentPage',
[collections.properties.pageSize]: 'pageSize',
[collections.properties.property]: 'property',
[collections.properties.totalMembers]: 'totalMembers',
[collections.properties.totalPages]: 'totalPages',
[collections.properties.value]: 'value',
[collections.properties.sortBy]: 'sortBy',
[collections.properties.sortDesc]: 'sortDesc',
[collections.properties.includeExternal]: 'includeExternal',
}
}
66 changes: 66 additions & 0 deletions browser/lib/src/ontologies/commits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

/* -----------------------------------
* GENERATED WITH ATOMIC-GENERATE
* -------------------------------- */

import { BaseProps } from '../index.js'

export const commits = {
classes: {
commit: 'https://atomicdata.dev/classes/Commit',
},
properties: {
subject: 'https://atomicdata.dev/properties/subject',
createdAt: 'https://atomicdata.dev/properties/createdAt',
lastCommit: 'https://atomicdata.dev/properties/lastCommit',
previousCommit: 'https://atomicdata.dev/properties/previousCommit',
signer: 'https://atomicdata.dev/properties/signer',
set: 'https://atomicdata.dev/properties/set',
push: 'https://atomicdata.dev/properties/push',
remove: 'https://atomicdata.dev/properties/remove',
destroy: 'https://atomicdata.dev/properties/destroy',
signature: 'https://atomicdata.dev/properties/signature',
},
} as const


// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Commits {
export type Commit = typeof commits.classes.commit;
}


declare module '../index.js' {
interface Classes {
[commits.classes.commit]: {
requires: BaseProps | typeof commits.properties.createdAt | typeof commits.properties.signature | typeof commits.properties.signer | typeof commits.properties.subject;
recommends: typeof commits.properties.destroy | typeof commits.properties.remove | typeof commits.properties.set;
};
}

interface PropTypeMapping {
[commits.properties.subject]: string
[commits.properties.createdAt]: string
[commits.properties.lastCommit]: string
[commits.properties.previousCommit]: string
[commits.properties.signer]: string
[commits.properties.set]: string
[commits.properties.push]: string
[commits.properties.remove]: string[]
[commits.properties.destroy]: boolean
[commits.properties.signature]: string
}

interface PropSubjectToNameMapping {
[commits.properties.subject]: 'subject',
[commits.properties.createdAt]: 'createdAt',
[commits.properties.lastCommit]: 'lastCommit',
[commits.properties.previousCommit]: 'previousCommit',
[commits.properties.signer]: 'signer',
[commits.properties.set]: 'set',
[commits.properties.push]: 'push',
[commits.properties.remove]: 'remove',
[commits.properties.destroy]: 'destroy',
[commits.properties.signature]: 'signature',
}
}
117 changes: 117 additions & 0 deletions browser/lib/src/ontologies/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

/* -----------------------------------
* GENERATED WITH ATOMIC-GENERATE
* -------------------------------- */

import { BaseProps } from '../index.js'

export const core = {
classes: {
class: 'https://atomicdata.dev/classes/Class',
property: 'https://atomicdata.dev/classes/Property',
agent: 'https://atomicdata.dev/classes/Agent',
datatype: 'https://atomicdata.dev/classes/Datatype',
ontology: 'https://atomicdata.dev/class/ontology',
},
properties: {
allowsOnly: 'https://atomicdata.dev/properties/allowsOnly',
classtype: 'https://atomicdata.dev/properties/classtype',
datatype: 'https://atomicdata.dev/properties/datatype',
description: 'https://atomicdata.dev/properties/description',
incomplete: 'https://atomicdata.dev/properties/incomplete',
isA: 'https://atomicdata.dev/properties/isA',
isDynamic: 'https://atomicdata.dev/properties/isDynamic',
name: 'https://atomicdata.dev/properties/name',
parent: 'https://atomicdata.dev/properties/parent',
read: 'https://atomicdata.dev/properties/read',
recommends: 'https://atomicdata.dev/properties/recommends',
requires: 'https://atomicdata.dev/properties/requires',
shortname: 'https://atomicdata.dev/properties/shortname',
write: 'https://atomicdata.dev/properties/write',
publicKey: 'https://atomicdata.dev/properties/publicKey',
instances: 'https://atomicdata.dev/properties/instances',
properties: 'https://atomicdata.dev/properties/properties',
classes: 'https://atomicdata.dev/properties/classes',
isLocked: 'https://atomicdata.dev/properties/isLocked',
},
} as const


// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Core {
export type Class = typeof core.classes.class;
export type Property = typeof core.classes.property;
export type Agent = typeof core.classes.agent;
export type Datatype = typeof core.classes.datatype;
export type Ontology = typeof core.classes.ontology;
}


declare module '../index.js' {
interface Classes {
[core.classes.class]: {
requires: BaseProps | typeof core.properties.shortname | typeof core.properties.description;
recommends: typeof core.properties.recommends | typeof core.properties.requires;
};
[core.classes.property]: {
requires: BaseProps | typeof core.properties.shortname | typeof core.properties.datatype | typeof core.properties.description;
recommends: typeof core.properties.classtype | typeof core.properties.isDynamic | typeof core.properties.isLocked | typeof core.properties.allowsOnly;
};
[core.classes.agent]: {
requires: BaseProps | typeof core.properties.publicKey;
recommends: typeof core.properties.name | typeof core.properties.description | 'https://atomicdata.dev/properties/drives';
};
[core.classes.datatype]: {
requires: BaseProps | typeof core.properties.shortname | typeof core.properties.description;
recommends: never;
};
[core.classes.ontology]: {
requires: BaseProps | typeof core.properties.description | typeof core.properties.shortname;
recommends: typeof core.properties.classes | typeof core.properties.properties | typeof core.properties.instances;
};
}

interface PropTypeMapping {
[core.properties.allowsOnly]: string[]
[core.properties.classtype]: string
[core.properties.datatype]: string
[core.properties.description]: string
[core.properties.incomplete]: boolean
[core.properties.isA]: string[]
[core.properties.isDynamic]: boolean
[core.properties.name]: string
[core.properties.parent]: string
[core.properties.read]: string[]
[core.properties.recommends]: string[]
[core.properties.requires]: string[]
[core.properties.shortname]: string
[core.properties.write]: string[]
[core.properties.publicKey]: string
[core.properties.instances]: string[]
[core.properties.properties]: string[]
[core.properties.classes]: string[]
[core.properties.isLocked]: boolean
}

interface PropSubjectToNameMapping {
[core.properties.allowsOnly]: 'allowsOnly',
[core.properties.classtype]: 'classtype',
[core.properties.datatype]: 'datatype',
[core.properties.description]: 'description',
[core.properties.incomplete]: 'incomplete',
[core.properties.isA]: 'isA',
[core.properties.isDynamic]: 'isDynamic',
[core.properties.name]: 'name',
[core.properties.parent]: 'parent',
[core.properties.read]: 'read',
[core.properties.recommends]: 'recommends',
[core.properties.requires]: 'requires',
[core.properties.shortname]: 'shortname',
[core.properties.write]: 'write',
[core.properties.publicKey]: 'publicKey',
[core.properties.instances]: 'instances',
[core.properties.properties]: 'properties',
[core.properties.classes]: 'classes',
[core.properties.isLocked]: 'isLocked',
}
}
Loading

0 comments on commit 4c298ef

Please sign in to comment.