Skip to content

Commit

Permalink
better handle the duality of @greenwood node modules resolution (#722)
Browse files Browse the repository at this point in the history
* better handle the duality of greenwood

* add specs
  • Loading branch information
thescientist13 authored Sep 10, 2021
1 parent ea2ab9d commit ac5baee
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ class NodeModulesResource extends ResourceInterface {
const packageEntryLocation = require.resolve(packageName).replace(/\\/g, '/'); // force / for consistency and path matching

if (packageName.indexOf('@greenwood') === 0) {
const subPackage = packageName.split('/')[1];
const subPackage = packageEntryLocation.indexOf('@greenwood') > 0
? packageName // we are in the user's node modules
: packageName.split('/')[1]; // else we are in our monorepo
const packageRootPath = packageEntryLocation.indexOf('@greenwood') > 0
? packageEntryLocation.split(packageName)[0] // we are in the user's node modules
: packageEntryLocation.split(subPackage)[0]; // else we are in our monorepo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ describe('Develop Greenwood With: ', function() {
});
});

it('should return the correct content type', function(done) {
expect(response.headers['content-type']).to.contain('text/html');
done();
});

it('should return a 200', function(done) {
expect(response.statusCode).to.equal(200);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Runner = require('gallinago').Runner;
const runSmokeTest = require('../../../../../test/smoke-test');

describe('Develop Greenwood With: ', function() {
const LABEL = 'GraphQL plugin for resolving .gql files';
const LABEL = 'GraphQL plugin for resolving client facing files';
const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js');
const outputPath = __dirname;
const hostname = 'http://localhost';
Expand Down Expand Up @@ -55,6 +55,93 @@ describe('Develop Greenwood With: ', function() {

runSmokeTest(['serve'], LABEL);

describe('Develop command import map for GraphQL', function() {
let response = {};
let dom;

before(async function() {
return new Promise((resolve, reject) => {
request.get({
url: `http://127.0.0.1:${port}`,
headers: {
accept: 'text/html'
}
}, (err, res, body) => {
if (err) {
reject();
}

response = res;

dom = new JSDOM(body);
resolve();
});
});
});

it('should return a 200', function(done) {
expect(response.statusCode).to.equal(200);

done();
});

it('should return the correct content type', function(done) {
expect(response.headers['content-type']).to.contain('text/html');
done();
});

it('should return an import map shim <script> in the <head> of the document', function(done) {
const importMapTag = dom.window.document.querySelectorAll('head > script[type="importmap-shim"]')[0];
const importMap = JSON.parse(importMapTag.textContent).imports;

expect(importMap['@greenwood/plugin-graphql/core/client']).to.equal('/node_modules/@greenwood/plugin-graphql/src/core/client.js');
expect(importMap['@greenwood/plugin-graphql/core/common']).to.equal('/node_modules/@greenwood/plugin-graphql/src/core/common.client.js');
expect(importMap['@greenwood/plugin-graphql/queries/children']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/children.gql');
expect(importMap['@greenwood/plugin-graphql/queries/config']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/config.gql');
expect(importMap['@greenwood/plugin-graphql/queries/graph']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/graph.gql');
expect(importMap['@greenwood/plugin-graphql/queries/menu']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/menu.gql');

done();
});
});

describe('Develop command specific client node_modules resolution', function() {
let response = {};

before(async function() {
return new Promise((resolve, reject) => {
request.get({
url: `http://127.0.0.1:${port}/node_modules/@greenwood/plugin-graphql/src/core/client.js`
}, (err, res, body) => {
if (err) {
reject();
}

response = res;

dom = new JSDOM(body);
resolve();
});
});
});

it('should return a 200', function(done) {
expect(response.statusCode).to.equal(200);

done();
});

it('should return the correct content type', function(done) {
expect(response.headers['content-type']).to.equal('text/javascript');
done();
});

it('should return an ECMASCript module', function(done) {
expect(response.body).to.contain('export default client;');
done();
});
});

describe('Develop command specific .gql behaviors', function() {
let response = {};

Expand All @@ -68,7 +155,7 @@ describe('Develop Greenwood With: ', function() {
}

response = res;

dom = new JSDOM(body);
resolve();
});
Expand Down

0 comments on commit ac5baee

Please sign in to comment.