lincd-cli 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,15 +4,15 @@
4
4
  "owl": "http://www.w3.org/2002/07/owl#",
5
5
  "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
6
6
  "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
7
- "test": "http://www.example.com/"
7
+ "${hyphen_name}": "${uri_base}"
8
8
  },
9
9
  "@graph": [
10
10
  {
11
- "@id": "example:ExampleClass",
11
+ "@id": "${hyphen_name}:ExampleClass",
12
12
  "@type": "rdfs:Class",
13
13
  "rdfs:comment": "This is an example class. You can remove or rename it",
14
14
  "rdfs:isDefinedBy": {
15
- "@id": "example:"
15
+ "@id": "${hyphen_name}:"
16
16
  },
17
17
  "rdfs:label": "Example Class"
18
18
  }
@@ -1,4 +1,4 @@
1
- import './ontologies/example-ontology';
1
+ import './ontologies/${hyphen_name}';
2
2
 
3
3
  //SHAPES FIRST
4
4
  import './shapes/ExampleShapeClass';
@@ -4,11 +4,10 @@ import {createNameSpace} from 'lincd/lib/utils/NameSpace';
4
4
  import {linkedOntology} from '../module';
5
5
  //finally, we pass on all the exports, plus the namespace, prefix and data loading function
6
6
  // as we register this ontology in the LINCD tree
7
- import * as _this from './example-ontology';
7
+ import * as _this from './${hyphen_name}';
8
8
 
9
- let dataFile = '../data/example-ontology.json';
10
9
  export var loadData = () => {
11
- return import(dataFile).then((data) => JSONLD.parse(data));
10
+ return import('../data/${hyphen_name}.json').then((data) => JSONLD.parse(data));
12
11
  };
13
12
 
14
13
  export var ns = createNameSpace('${uri_base}');
@@ -19,13 +18,13 @@ export var _self: NamedNode = ns('');
19
18
  //The main goal though of this file is to export a reference to NamedNode with the correct URI
20
19
  // for all the entities (Classes and properties) in the ontology
21
20
  export var ExampleClass: NamedNode = ns('ExampleClass');
22
- export var exampleName: NamedNode = ns('exampleName');
21
+ export var exampleProperty: NamedNode = ns('exampleProperty');
23
22
 
24
- export const exampleOntology = {
23
+ export const ${camel_module_name} = {
25
24
  ExampleClass,
26
- exampleName,
25
+ exampleProperty,
27
26
  };
28
27
 
29
28
  //as third parameter, provide a prefix (string) that can be used to refer to the full ontology URL.
30
29
  //for example: 'schema' refers to https://www.schema.org/
31
- linkedOntology(_this, ns, 'exampl', loadData, dataFile);
30
+ linkedOntology(_this, ns, '${hyphen_name}', loadData, '../data/${hyphen_name}.json');
@@ -2,28 +2,28 @@ import {Shape} from 'lincd/lib/shapes/Shape';
2
2
  import {Literal, NamedNode} from 'lincd/lib/models';
3
3
  import {linkedShape} from '../module';
4
4
  import {literalProperty} from 'lincd/lib/utils/ShapeDecorators';
5
- import {exampleOntology} from '../ontologies/example-ontology';
5
+ import {${camel_module_name}} from '../ontologies/${hyphen_name}';
6
6
 
7
7
  @linkedShape
8
8
  export class ExampleShapeClass extends Shape {
9
9
  /**
10
10
  * indicates that instances of this shape need to have this rdf.type
11
11
  */
12
- static targetClass: NamedNode = exampleOntology.ExampleClass;
12
+ static targetClass: NamedNode = ${camel_module_name}.ExampleClass;
13
13
 
14
14
  /**
15
15
  * instances of this shape need to have exactly one value defined for the given property
16
16
  */
17
17
  @literalProperty({
18
- path: exampleOntology.exampleName,
18
+ path: ${camel_module_name}.exampleProperty,
19
19
  required: true,
20
20
  maxCount: 1,
21
21
  })
22
22
  get name() {
23
- return this.getValue(exampleOntology.exampleName);
23
+ return this.getValue(${camel_module_name}.exampleProperty);
24
24
  }
25
25
 
26
26
  set name(val: string) {
27
- this.overwrite(exampleOntology.exampleName, new Literal(val));
27
+ this.overwrite(${camel_module_name}.exampleProperty, new Literal(val));
28
28
  }
29
29
  }
package/lib/cli.js CHANGED
@@ -36,6 +36,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
37
  }
38
38
  };
39
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
40
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
41
+ if (ar || !(i in from)) {
42
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
43
+ ar[i] = from[i];
44
+ }
45
+ }
46
+ return to.concat(ar || Array.prototype.slice.call(from));
47
+ };
39
48
  exports.__esModule = true;
40
49
  var utils_1 = require("./utils");
41
50
  require('require-extensions');
@@ -268,10 +277,90 @@ program.command('create-app [name] [uribase]').action(function (name, uriBase) {
268
277
  program.command('create-module [name] [uribase]').action(function (name, uriBase) {
269
278
  return createModule(name, uriBase);
270
279
  });
280
+ program.command('create-ontology [suggested-prefix] [uribase]').action(function (prefix, uriBase) {
281
+ return createOntology(prefix, uriBase);
282
+ });
283
+ var createOntology = function (prefix, uriBase, basePath) {
284
+ if (basePath === void 0) { basePath = process.cwd(); }
285
+ return __awaiter(void 0, void 0, void 0, function () {
286
+ var targetFolder, codeName, hyphenName, cameCaseName, underscoreName, targetFile, targetDataFile, targetDataFile2, indexPath, indexContents, lines, newContents, key;
287
+ return __generator(this, function (_a) {
288
+ switch (_a.label) {
289
+ case 0:
290
+ if (!prefix) {
291
+ console.warn("Please provide a suggested prefix as the first argument");
292
+ return [2 /*return*/];
293
+ }
294
+ targetFolder = path.join(basePath, 'src', 'ontologies');
295
+ if (!fs.existsSync(targetFolder)) {
296
+ if (fs.existsSync(path.join(basePath, 'src'))) {
297
+ fs.mkdirSync(targetFolder);
298
+ }
299
+ else {
300
+ warn("Please run this command from the root of your module. This command expects [modules]/src/ontologies to exists from that folder");
301
+ }
302
+ }
303
+ if (!uriBase) {
304
+ uriBase = 'http://lincd.org/ont/' + prefix;
305
+ }
306
+ codeName = prefix.replace(/\-/g, '_');
307
+ hyphenName = prefix.replace(/[-_\s]+/g, '-');
308
+ cameCaseName = camelCase(prefix);
309
+ underscoreName = prefix.replace(/[-\s]+/g, '_');
310
+ targetFile = path.join(targetFolder, hyphenName + '.ts');
311
+ fs.copySync(path.join(__dirname, '..', 'defaults', 'module', 'src', 'ontologies', 'example-ontology.ts'), targetFile);
312
+ targetDataFile = path.join(targetFolder, '..', 'data', hyphenName + '.json');
313
+ targetDataFile2 = path.join(targetFolder, '..', 'data', hyphenName + '.json.d.ts');
314
+ fs.copySync(path.join(__dirname, '..', 'defaults', 'module', 'src', 'data', 'example-ontology.json'), targetDataFile);
315
+ fs.copySync(path.join(__dirname, '..', 'defaults', 'module', 'src', 'data', 'example-ontology.json.d.ts'), targetDataFile2);
316
+ setVariable('uri_base', uriBase);
317
+ setVariable('camel_module_name', cameCaseName);
318
+ setVariable('underscore_module_name', underscoreName);
319
+ setVariable('hyphen_name', hyphenName);
320
+ setVariable('module_name', prefix);
321
+ log("Creating files for ontology '" + prefix + "'");
322
+ //replace variables in some of the copied files
323
+ return [4 /*yield*/, Promise.all([targetFile, targetDataFile, targetDataFile2]
324
+ .map(function (file) {
325
+ return replaceVariablesInFile(file);
326
+ }))];
327
+ case 1:
328
+ //replace variables in some of the copied files
329
+ _a.sent();
330
+ indexPath = ['index.ts', 'index.tsx'].map(function (f) {
331
+ return path.join('src', f);
332
+ }).find(function (indexFileName) {
333
+ return fs.existsSync(indexFileName);
334
+ });
335
+ if (indexPath) {
336
+ indexContents = fs.readFileSync(indexPath, 'utf-8');
337
+ lines = indexContents.split(/\n/g);
338
+ newContents = void 0;
339
+ for (key in lines) {
340
+ if (lines[key].indexOf('ontologies') !== -1) {
341
+ //remove lines after this line and insert new line in its place
342
+ lines[key] += "\nimport './ontologies/".concat(hyphenName, "';");
343
+ newContents = lines.join("\n");
344
+ // log("Found at "+key,lines,newContents);
345
+ break;
346
+ }
347
+ }
348
+ if (!newContents) {
349
+ newContents = indexContents + "\nimport './ontologies/".concat(hyphenName, "';");
350
+ // log("Added at end",newContents);
351
+ }
352
+ fs.writeFileSync(indexPath, newContents);
353
+ }
354
+ log("Prepared a new ontology data files in ".concat(chalk.magenta(targetDataFile.replace(basePath, ''))), "And an ontology accessor file in ".concat(chalk.magenta(targetFile.replace(basePath, ''))), "Added an import of this file from ".concat(chalk.magenta(indexPath)));
355
+ return [2 /*return*/];
356
+ }
357
+ });
358
+ });
359
+ };
271
360
  var createModule = function (name, uriBase, basePath) {
272
361
  if (basePath === void 0) { basePath = process.cwd(); }
273
362
  return __awaiter(void 0, void 0, void 0, function () {
274
- var targetFolder, _a, packageName, scope, cleanPackageName, codeName, cameCaseName, underscoreName, version, installCommand;
363
+ var targetFolder, _a, packageName, scope, cleanPackageName, codeName, cameCaseName, underscoreName, hyphenName, version, installCommand;
275
364
  return __generator(this, function (_b) {
276
365
  switch (_b.label) {
277
366
  case 0:
@@ -302,29 +391,43 @@ var createModule = function (name, uriBase, basePath) {
302
391
  codeName = cleanPackageName.replace(/\-/g, '_');
303
392
  cameCaseName = camelCase(name);
304
393
  underscoreName = name.replace(/[-\s]+/g, '_');
394
+ hyphenName = name.replace(/[-_\s]+/g, '-');
305
395
  setVariable('uri_base', uriBase);
306
396
  //longer similar variables names should come before the shorter ones
307
397
  setVariable('camel_module_name', cameCaseName);
308
398
  setVariable('underscore_module_name', underscoreName);
309
399
  setVariable('module_name', name);
400
+ setVariable('hyphen_name', hyphenName);
310
401
  log("Creating new LINCD module '" + name + "'");
311
402
  //replace variables in some of the copied files
312
- ['package.json', 'Gruntfile.js', 'src/module.ts']
313
- .map(function (f) { return path.join(targetFolder, f); })
314
- .forEach(function (file) {
315
- replaceVariablesInFile(file);
403
+ return [4 /*yield*/, Promise.all(['src/index.ts', 'package.json', 'Gruntfile.js', 'src/module.ts', 'src/shapes/ExampleShapeClass.ts', 'src/ontologies/example-ontology.ts', 'src/data/example-ontology.json']
404
+ .map(function (f) { return path.join(targetFolder, f); })
405
+ .map(function (file) {
406
+ return replaceVariablesInFile(file);
407
+ }))];
408
+ case 1:
409
+ //replace variables in some of the copied files
410
+ _b.sent();
411
+ //rename these to a file name similar to the module name
412
+ ['src/ontologies/example-ontology.ts', 'src/data/example-ontology.json', 'src/data/example-ontology.json.d.ts'].forEach(function (f) {
413
+ var parts = f.split('/');
414
+ var newParts = __spreadArray([], parts, true);
415
+ var _a = newParts.pop().split('.'), name = _a[0], extensions = _a.slice(1);
416
+ var newName = hyphenName + '.' + extensions.join(".");
417
+ console.log('rename ', path.join(targetFolder, f), path.join.apply(path, __spreadArray(__spreadArray([targetFolder], newParts, false), [newName], false)));
418
+ fs.renameSync(path.join(targetFolder, f), path.join.apply(path, __spreadArray(__spreadArray([targetFolder], newParts, false), [newName], false)));
316
419
  });
317
420
  return [4 /*yield*/, execPromise('yarn --version')["catch"](function (err) {
318
421
  console.log("yarn probably not working");
319
422
  return '';
320
423
  })];
321
- case 1:
424
+ case 2:
322
425
  version = _b.sent();
323
426
  installCommand = version.toString().match(/[0-9]+/) ? 'yarn install' : 'npm install';
324
427
  return [4 /*yield*/, execp("cd ".concat(targetFolder, " && ").concat(installCommand, " && npm exec lincd build"), true)["catch"](function (err) {
325
428
  console.warn("Could not install dependencies");
326
429
  })];
327
- case 2:
430
+ case 3:
328
431
  _b.sent();
329
432
  log("Prepared a new LINCD module in ".concat(chalk.magenta(targetFolder)), "Run ".concat(chalk.blueBright('yarn build'), " from this directory to build once"), "Or ".concat(chalk.blueBright('yarn dev'), " to continuously rebuild on file changes"));
330
433
  return [2 /*return*/];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lincd-cli",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Command line tools for the lincd.js library",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {