Skip to content

Commit

Permalink
feat(cli): lb4 service/repository shared scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
marioestradarosa committed Sep 27, 2018
1 parent 7add894 commit 9bafc6d
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 210 deletions.
22 changes: 13 additions & 9 deletions packages/cli/generators/service/index.js
Expand Up @@ -63,10 +63,11 @@ module.exports = class ServiceGenerator extends ArtifactGenerator {
if (!fs.existsSync(this.artifactInfo.datasourcesDir)) {
return this.exit(
new Error(
`${ERROR_NO_DATA_SOURCES_FOUND} ${this.artifactInfo.datasourcesDir}.
${chalk.yellow(
'Please visit https://loopback.io/doc/en/lb4/DataSource-generator.html for information on how datasources are discovered',
)}`,
`${ERROR_NO_DATA_SOURCES_FOUND} ${
this.artifactInfo.datasourcesDir
}.${chalk.yellow(
'Please visit https://loopback.io/doc/en/lb4/DataSource-generator.html for information on how datasources are discovered',
)}`,
),
);
}
Expand Down Expand Up @@ -142,10 +143,11 @@ module.exports = class ServiceGenerator extends ArtifactGenerator {
if (availableDatasources.length === 0) {
return this.exit(
new Error(
`${ERROR_NO_DATA_SOURCES_FOUND} ${this.artifactInfo.datasourcesDir}.
${chalk.yellow(
'Please visit https://loopback.io/doc/en/lb4/DataSource-generator.html for information on how datasources are discovered',
)}`,
`${ERROR_NO_DATA_SOURCES_FOUND} ${
this.artifactInfo.datasourcesDir
}.${chalk.yellow(
'Please visit https://loopback.io/doc/en/lb4/DataSource-generator.html for information on how datasources are discovered',
)}`,
),
);
}
Expand Down Expand Up @@ -183,7 +185,9 @@ module.exports = class ServiceGenerator extends ArtifactGenerator {
/**
* this method will be in charge of inferring and create
* the remote interfaces from either SOAP wsdl or REST openApi json
* TODO: @marioestradarosa
*
* TODO: add functionality to inspect service specs to generate
* strongly-typed service proxies and corresponding model definitions.
*/
async inferInterfaces() {
let connectorType = utils.getDataSourceConnectorName(
Expand Down
Expand Up @@ -2,13 +2,10 @@ import {getService, juggler} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {<%= dataSourceClassName %>} from '../datasources';

/**
* this is where you define the Node.js methods that will be
* mapped to the SOAP operations as stated in the datasource
* json file.
*/
export interface <%= className %>Service {

// this is where you define the Node.js methods that will be
// mapped to the SOAP operations as stated in the datasource
// json file.
}

export class <%= className %>ServiceProvider implements Provider<<%= className %>Service> {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/utils.js
Expand Up @@ -419,7 +419,7 @@ exports.getDataSourceConnectorName = function(datasourcesDir, dataSourceClass) {
jsonFileContent = JSON.parse(fs.readFileSync(datasourceJSONFile, 'utf8'));
} catch (err) {
debug(`Error reading file ${datasourceJSONFile}: ${err.message}`);
throw new Error(err);
throw err;
}

if (jsonFileContent.connector) {
Expand Down Expand Up @@ -458,7 +458,7 @@ exports.isConnectorOfType = function(
jsonFileContent = JSON.parse(fs.readFileSync(datasourceJSONFile, 'utf8'));
} catch (err) {
debug(`Error reading file ${datasourceJSONFile}: ${err.message}`);
throw new Error(err);
throw err;
}

for (let connector of Object.values(connectors)) {
Expand Down
165 changes: 75 additions & 90 deletions packages/cli/test/integration/generators/repository.integration.js
Expand Up @@ -36,9 +36,12 @@ describe('lb4 repository', () => {

await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withPrompts(multiItemPrompt);

Expand Down Expand Up @@ -93,9 +96,12 @@ describe('lb4 repository', () => {

await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withPrompts(multiItemPrompt);

Expand All @@ -121,9 +127,12 @@ describe('lb4 repository', () => {
it('generates a custom name repository', async () => {
await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withArguments('myrepo --datasource dbmem --model MultiWord');
const expectedFile = path.join(
Expand All @@ -145,9 +154,12 @@ describe('lb4 repository', () => {
it('generates a crud repository from a config file', async () => {
await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withArguments('--config myconfig.json');
const expectedFile = path.join(
Expand Down Expand Up @@ -180,9 +192,12 @@ describe('lb4 repository', () => {

await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withPrompts(multiItemPrompt);

Expand Down Expand Up @@ -214,9 +229,12 @@ describe('lb4 repository', () => {
return expect(
testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withPrompts(basicPrompt)
.withArguments(' --model InvalidModel'),
Expand All @@ -230,23 +248,28 @@ describe('lb4 repository', () => {
return expect(
testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withPrompts(basicPrompt),
).to.be.rejectedWith(/You did not select a valid model/);
});

it('does not run with empty datasource list', async () => {
return expect(
testUtils.executeGenerator(generator).inDir(
SANDBOX_PATH,
async () =>
await prepareGeneratorForRepository(SANDBOX_PATH, {
noFixtures: true,
}),
),
testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: false},
SANDBOX_FILES,
),
),
).to.be.rejectedWith(/No datasources found/);
});
});
Expand All @@ -258,9 +281,12 @@ describe('lb4 repository', () => {
};
await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withPrompts(basicPrompt)
.withArguments(' --model Defaultmodel');
Expand All @@ -285,9 +311,12 @@ describe('lb4 repository', () => {
it('generates a crud repository from decorator defined model', async () => {
await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withArguments('--datasource dbmem --model decoratordefined');
const expectedFile = path.join(
Expand Down Expand Up @@ -316,9 +345,12 @@ describe('lb4 repository', () => {
it('generates a kv repository from default model', async () => {
await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withArguments('--datasource dbkv --model Defaultmodel');
const expectedFile = path.join(
Expand All @@ -344,9 +376,12 @@ describe('lb4 repository', () => {
};
await testUtils
.executeGenerator(generator)
.inDir(
SANDBOX_PATH,
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(
SANDBOX_PATH,
{includeSandboxFilesFixtures: true},
SANDBOX_FILES,
),
)
.withPrompts(basicPrompt)
.withArguments('--model decoratordefined');
Expand Down Expand Up @@ -534,53 +569,3 @@ const SANDBOX_FILES = [
`,
},
];

async function prepareGeneratorForRepository(rootDir, options) {
options = options || {};
const content = {};
if (!options.excludeKeyword) {
content.keywords = ['loopback'];
}

if (!options.excludePackageJSON) {
fs.writeFileSync(
path.join(rootDir, 'package.json'),
JSON.stringify(content),
);
}

if (!options.excludeYoRcJSON) {
fs.writeFileSync(path.join(rootDir, '.yo-rc.json'), JSON.stringify({}));
}

fs.mkdirSync(path.join(rootDir, 'src'));

if (!options.excludeControllersDir) {
fs.mkdirSync(path.join(rootDir, 'src', 'controllers'));
}

if (!options.excludeModelsDir) {
fs.mkdirSync(path.join(rootDir, 'src', 'models'));
}

if (!options.excludeRepositoriesDir) {
fs.mkdirSync(path.join(rootDir, 'src', 'repositories'));
}

if (!options.excludeDataSourcesDir) {
fs.mkdirSync(path.join(rootDir, 'src', 'datasources'));
}

if (!options.noFixtures) {
copyFixtures();
}
}

function copyFixtures() {
for (let theFile of SANDBOX_FILES) {
const fullPath = path.join(SANDBOX_PATH, theFile.path, theFile.file);
if (!fs.existsSync(fullPath)) {
fs.writeFileSync(fullPath, theFile.content);
}
}
}

0 comments on commit 9bafc6d

Please sign in to comment.