Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release/0.5.0 #284

Merged
merged 13 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions nyc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module.exports = {
all: true,

include: [
'packages/cli/src/data/*.js',
'packages/cli/src/lib/*.js',
'packages/cli/src/lifecycles/*.js',
'packages/cli/src/plugins/*.js',
'packages/cli/src/tasks/*.js',
'packages/plugin-*/src/*.js'
],
Expand All @@ -19,10 +19,10 @@ module.exports = {

checkCoverage: true,

statements: 80,
branches: 65,
functions: 85,
lines: 80,
statements: 85,
branches: 75,
functions: 90,
lines: 85,

watermarks: {
statements: [75, 85],
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"@babel/preset-env": "^7.8.3",
"@babel/runtime": "^7.8.3",
"@webcomponents/webcomponentsjs": "^2.3.0",
"apollo-cache-inmemory": "^1.6.3",
"apollo-client": "^2.6.4",
"apollo-link-http": "^1.5.16",
"apollo-server": "^2.9.12",
"babel-loader": "^8.0.5",
"chalk": "^2.4.2",
"colors": "^1.3.3",
Expand All @@ -40,15 +44,20 @@
"css-loader": "^2.1.1",
"css-to-string-loader": "^0.1.3",
"cssnano": "^4.1.10",
"deepmerge": "^4.2.2",
"file-loader": "^3.0.1",
"filewatcher-webpack-plugin": "^1.2.0",
"front-matter": "^3.0.1",
"fs-extra": "^8.1.0",
"glob-promise": "^3.4.0",
"graphql": "^14.5.8",
"graphql-tag": "^2.10.1",
"html-webpack-plugin": "^3.2.0",
"lit-element": "^2.0.1",
"lit-redux-router": "^0.9.3",
"local-web-server": "^2.6.1",
"markdown-toc": "^1.2.0",
"node-fetch": "^2.6.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice if we could remove the dependency on this. will track in Trello

"postcss-loader": "^3.0.0",
"postcss-nested": "^4.1.2",
"postcss-preset-env": "^6.7.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/config/webpack.config.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ module.exports = ({ config, context }) => {
.map((plugin) => plugin.provider({ config, context }));

return {

resolve: {
extensions: ['.js', '.json', '.gql', '.graphql'],
alias: {
'@greenwood/cli/data': path.join(__dirname, '..', './data')
}
},

entry: {
index: path.join(context.scratchDir, 'app', 'app.js')
},
Expand Down Expand Up @@ -124,6 +132,9 @@ module.exports = ({ config, context }) => {
}, {
test: /\.(ttf|eot|svg|jpe?g|png|gif|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
}, {
test: /\.(graphql|gql)$/,
loader: 'graphql-tag/loader'
}]
},

Expand Down
51 changes: 51 additions & 0 deletions packages/cli/src/data/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { ApolloClient } = require('apollo-client');
const createHttpLink = require('apollo-link-http').createHttpLink;
const crypto = require('crypto');
const fetch = require('node-fetch');
const fs = require('fs-extra');
const { gql } = require('apollo-server');
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;
const path = require('path');

/* Extract cache server-side */
module.exports = async (req, context) => {

return new Promise(async(resolve, reject) => {
try {
const client = await new ApolloClient({
link: createHttpLink({
uri: 'http://localhost:4000?q=internal', /* internal flag to prevent looping cache on request */
fetch
}),
cache: new InMemoryCache()
});

/* Take the same query from request, and repeat the query for our server side cache */
const { query, variables } = req.body;

let { data } = await client.query({
query: gql`${query}`,
variables
});

if (data) {
const cache = JSON.stringify(client.extract());
const md5 = crypto.createHash('md5').update(cache).digest('hex');

/* Get the requests entire (full) route and rootRoute to use as reference for designated cache directory */
const { origin, referer } = req.headers;
const fullRoute = referer.substring(origin.length, referer.length);
const rootRoute = fullRoute.substring(0, fullRoute.substring(1, fullRoute.length).indexOf('/') + 1);
const targetDir = path.join(context.publicDir, rootRoute);
const targetFile = path.join(targetDir, `${md5}-cache.json`);

await fs.mkdirs(targetDir, { recursive: true });
await fs.writeFile(path.join(targetFile), cache, 'utf8');
}
resolve();
} catch (err) {
console.error('create cache error', err);
reject(err);
}
});
};
37 changes: 37 additions & 0 deletions packages/cli/src/data/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';

const APOLLO_STATE = window.__APOLLO_STATE__; // eslint-disable-line no-underscore-dangle
const client = new ApolloClient({
cache: new InMemoryCache().restore(APOLLO_STATE),
link: new HttpLink({
uri: 'http://localhost:4000'
})
});
const backupQuery = client.query;

client.query = (params) => {

if (APOLLO_STATE) {
// __APOLLO_STATE__ defined, in "SSG" mode...
const root = window.location.pathname.split('/')[1];
const rootSuffix = root === ''
? ''
: '/';

return fetch(`/${root}${rootSuffix}cache.json`)
.then(response => response.json())
.then((response) => {
// mock client.query response
return {
data: new InMemoryCache().restore(response).readQuery(params)
};
});
} else {
// __APOLLO_STATE__ NOT defined, in "SPA" mode
return backupQuery(params);
}
};

export default client;
10 changes: 10 additions & 0 deletions packages/cli/src/data/queries/children.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query($parent: String!) {
children(parent: $parent) {
id,
title,
link,
filePath,
fileName,
template
}
}
19 changes: 19 additions & 0 deletions packages/cli/src/data/queries/config.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
query {
config {
devServer {
port,
host
},
meta {
name,
rel,
content,
property,
value,
href
},
publicPath,
title,
workspace
}
}
10 changes: 10 additions & 0 deletions packages/cli/src/data/queries/graph.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query {
graph {
id,
title,
link,
filePath,
fileName,
template
}
}
20 changes: 20 additions & 0 deletions packages/cli/src/data/queries/menu.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
query($name: String, $route: String, $order: MenuOrderBy) {
menu(name: $name, pathname: $route, orderBy: $order) {
item {
label,
link
}
children {
item {
label,
link
},
children {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this need some logic to scale, or if I am reading this right, this only supports one level of nesting, right? Maybe related to #273

Copy link
Member

@hutchgrant hutchgrant Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, only one nested level

item {
label,
link
}
}
}
}
}
45 changes: 45 additions & 0 deletions packages/cli/src/data/schema/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const gql = require('graphql-tag');

const getConfiguration = async (root, query, context) => {
return context.config;
};

// https://www.greenwoodjs.io/docs/configuration
const configTypeDefs = gql`
type DevServer {
port: Int,
host: String
}

type Meta {
name: String,
value: String,
content: String,
rel: String,
property: String,
href: String
}

type Config {
devServer: DevServer,
meta: [Meta],
publicPath: String,
title: String,
workspace: String
}

extend type Query {
config: Config
}
`;

const configResolvers = {
Query: {
config: getConfiguration
}
};

module.exports = {
configTypeDefs,
configResolvers
};
Loading