From 982d4292f95994cf8c49da4e34b8bfd9023a1837 Mon Sep 17 00:00:00 2001 From: Jan Hapke Date: Tue, 5 Jul 2016 17:57:43 +0200 Subject: [PATCH 1/2] added '-s' flag to pass the includeSchema option to loopback-sdk-angular --- bin/lb-ng | 7 +++++-- package.json | 2 +- test/fixtures/app.js | 30 ++++++++++++++++++++++++++++++ test/lb-ng.test.js | 8 ++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/bin/lb-ng b/bin/lb-ng index 7b2dc09..a923cff 100755 --- a/bin/lb-ng +++ b/bin/lb-ng @@ -13,7 +13,9 @@ var argv = optimist .describe('m', 'The name for generated Angular module.') .default('m', 'lbServices') .describe('u', 'URL of the REST API end-point') - .alias({ u : 'url', m: 'module-name' }) + .describe('s', 'Include schema definition in generated models') + .boolean('s') + .alias({ u : 'url', m: 'module-name', s: 'include-schema' }) .demand(1) .argv; @@ -33,9 +35,10 @@ if (app.booting) { function runGenerator() { var ngModuleName = argv['module-name'] || 'lbServices'; var apiUrl = argv['url'] || app.get('restApiRoot') || '/api'; + var includeSchema = argv['include-schema'] || false; console.error('Generating %j for the API endpoint %j', ngModuleName, apiUrl); - var result = generator.services(app, ngModuleName, apiUrl); + var result = generator.services(app, {ngModuleName: ngModuleName, apiUrl: apiUrl, includeSchema: includeSchema}); if (outputFile) { outputFile = path.resolve(outputFile); diff --git a/package.json b/package.json index fab81e8..dbbc469 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "node": ">= 0.8.0" }, "dependencies": { - "loopback-sdk-angular": "^1.1.1", + "loopback-sdk-angular": "strongloop/loopback-sdk-angular", "optimist": "^0.6.1", "semver": "^2.2.1" }, diff --git a/test/fixtures/app.js b/test/fixtures/app.js index f48302e..6de6ee7 100644 --- a/test/fixtures/app.js +++ b/test/fixtures/app.js @@ -8,4 +8,34 @@ var app = loopback(); app.set('restApiRoot', '/rest-api-root'); +// this is a hack to give the angular SDK a model to export +// TODO: create and inject a model the "proper" way +var TestClass = { + name: 'TestClass', + sharedClass: { + ctor: { + settings: { + description: 'Test Class', + }, + }, + }, + ctor: { + getFullPath: function() {} + }, + methods: [], + +}; + +app.models.TestClass = { + definition: { + properties: { + name: 'string', + }, + }, +}; + +app.handler('rest').adapter.getClasses = function() { + return [TestClass]; +}; + module.exports = app; diff --git a/test/lb-ng.test.js b/test/lb-ng.test.js index 4d8cefe..1ad540e 100644 --- a/test/lb-ng.test.js +++ b/test/lb-ng.test.js @@ -54,6 +54,14 @@ describe('lb-ng', function() { }); }); + it('passes the include-schema flag from the command-line', function() { + return runLbNg('-s', sampleAppJs) + .spread(function(script, stderr) { + expect(script).to.contain('R\.schema ='); + expect(script).to.contain('schema of the model'); + }); + }); + it('saves the script to a file', function() { var outfile = path.resolve(SANDBOX, 'lb-services.js'); return runLbNg('-m', 'a-module', sampleAppJs, outfile) From d58658306e17a5cb1e1bfa853710c46fcac7e9f3 Mon Sep 17 00:00:00 2001 From: Jan Hapke Date: Wed, 13 Jul 2016 18:21:45 +0200 Subject: [PATCH 2/2] implemented feedback from pull request --- bin/lb-ng | 6 +++++- package.json | 2 +- test/fixtures/app.js | 21 ++++++++++----------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/bin/lb-ng b/bin/lb-ng index a923cff..6750239 100755 --- a/bin/lb-ng +++ b/bin/lb-ng @@ -38,7 +38,11 @@ function runGenerator() { var includeSchema = argv['include-schema'] || false; console.error('Generating %j for the API endpoint %j', ngModuleName, apiUrl); - var result = generator.services(app, {ngModuleName: ngModuleName, apiUrl: apiUrl, includeSchema: includeSchema}); + var result = generator.services(app, { + ngModuleName: ngModuleName, + apiUrl: apiUrl, + includeSchema: includeSchema + }); if (outputFile) { outputFile = path.resolve(outputFile); diff --git a/package.json b/package.json index dbbc469..6cc72a5 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "node": ">= 0.8.0" }, "dependencies": { - "loopback-sdk-angular": "strongloop/loopback-sdk-angular", + "loopback-sdk-angular": "^1.8.0", "optimist": "^0.6.1", "semver": "^2.2.1" }, diff --git a/test/fixtures/app.js b/test/fixtures/app.js index 6de6ee7..e9358d3 100644 --- a/test/fixtures/app.js +++ b/test/fixtures/app.js @@ -8,10 +8,18 @@ var app = loopback(); app.set('restApiRoot', '/rest-api-root'); +app.dataSource('db', { connector: 'memory' }); +var TestModel = app.registry.createModel( + 'TestModel', + { foobaz: 'string' } +); +app.model(TestModel, { dataSource: 'db' }); + // this is a hack to give the angular SDK a model to export -// TODO: create and inject a model the "proper" way +// TODO: find out why the model created above is not +// returned by the rest adapter var TestClass = { - name: 'TestClass', + name: 'TestModel', sharedClass: { ctor: { settings: { @@ -23,15 +31,6 @@ var TestClass = { getFullPath: function() {} }, methods: [], - -}; - -app.models.TestClass = { - definition: { - properties: { - name: 'string', - }, - }, }; app.handler('rest').adapter.getClasses = function() {