diff --git a/packages/cli/src/data/schema/graph.js b/packages/cli/src/data/schema/graph.js index 8901fca27..7840af4c5 100644 --- a/packages/cli/src/data/schema/graph.js +++ b/packages/cli/src/data/schema/graph.js @@ -56,7 +56,7 @@ const getNavigationFromGraph = async (root, query, context) => { } }); - // TODO best format for users, hash map? #271 + // TODO best format for users, hash map? #288 return Object.keys(navigation).map((key) => { return navigation[key]; }); diff --git a/packages/cli/src/lifecycles/graph.js b/packages/cli/src/lifecycles/graph.js index 8022c7183..b457e5629 100644 --- a/packages/cli/src/lifecycles/graph.js +++ b/packages/cli/src/lifecycles/graph.js @@ -9,17 +9,26 @@ const createGraphFromPages = async (pagesDir, config) => { return new Promise(async (resolve, reject) => { try { + const pagesIndexMap = new Map(); + let pagesIndex = 0; const walkDirectory = async(directory) => { let files = await fs.readdir(directory); - return Promise.all(files.map(async (file) => { + return Promise.all(files.map((file) => { + const filenameHash = crypto.createHash('md5').update(`${directory}/${file}`).digest('hex'); + const filePath = path.join(directory, file); + const stats = fs.statSync(filePath); + const isMdFile = file.substr(file.length - 2, file.length) === 'md'; + + // map each page to a (0 based) index based on filesystem order + if (isMdFile) { + pagesIndexMap.set(filenameHash, pagesIndex); + pagesIndex += 1; + } + return new Promise(async (resolve, reject) => { try { - const filePath = path.join(directory, file); - const stats = await fs.stat(filePath); - const isMdFile = file.substr(file.length - 2, file.length) === 'md'; - if (isMdFile && !stats.isDirectory()) { const fileContents = await fs.readFile(filePath, 'utf8'); const { attributes } = fm(fileContents); @@ -80,12 +89,14 @@ const createGraphFromPages = async (pagesDir, config) => { * meta: og graph meta array of objects { property/name, content } */ - pages.push({ mdFile, label, route, template, filePath, fileName, relativeExpectedPath, title, meta }); + pages[pagesIndexMap.get(filenameHash)] = { mdFile, label, route, template, filePath, fileName, relativeExpectedPath, title, meta }; } + if (stats.isDirectory()) { await walkDirectory(filePath); resolve(); } + resolve(); } catch (err) { reject(err); diff --git a/packages/cli/src/lifecycles/serialize.js b/packages/cli/src/lifecycles/serialize.js index 30b5896c5..6e75b05df 100644 --- a/packages/cli/src/lifecycles/serialize.js +++ b/packages/cli/src/lifecycles/serialize.js @@ -24,9 +24,11 @@ const setDataForPages = async (context) => { cacheContents = deepmerge(cacheContents, require(file)); }); + const serialzedCacheContents = JSON.stringify(cacheContents); + // TODO could optimize this probably - #277 - fs.writeFileSync(`${publicDir}/${cacheRoot}/cache.json`, JSON.stringify(cacheContents)); - fs.writeFileSync(pagePath, contents.replace('___DATA___', JSON.stringify(cacheContents))); + fs.writeFileSync(`${publicDir}/${cacheRoot}/cache.json`, serialzedCacheContents); + fs.writeFileSync(pagePath, contents.replace('___DATA___', serialzedCacheContents)); }); }; diff --git a/packages/cli/test/cases/build.data.graph/build.data.graph.spec.js b/packages/cli/test/cases/build.data.graph/build.data.graph.spec.js index c7ee32085..4e4ec88d2 100644 --- a/packages/cli/test/cases/build.data.graph/build.data.graph.spec.js +++ b/packages/cli/test/cases/build.data.graph/build.data.graph.spec.js @@ -32,6 +32,7 @@ const TestBed = require('../../../../../test/test-bed'); describe('Build Greenwood With: ', function() { const LABEL = 'Data from GraphQL'; + const apolloStateRegex = /window.__APOLLO_STATE__=({.*?});/; let setup; before(async function() { @@ -48,8 +49,6 @@ describe('Build Greenwood With: ', function() { runSmokeTest(['public', 'not-found'], LABEL); describe('Home (Page Template) w/ Navigation Query', function() { - const expectedCache = {"ROOT_QUERY.navigation.0":{"label":"Blog","link":"/blog/","__typename":"Navigation"},"ROOT_QUERY":{"navigation":[{"type":"id","generated":true,"id":"ROOT_QUERY.navigation.0","typename":"Navigation"}]}}; // eslint-disable-line - beforeEach(async function() { dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html')); }); @@ -74,21 +73,15 @@ describe('Build Greenwood With: ', function() { expect(await glob.promise(path.join(this.context.publicDir, './cache.json'))).to.have.lengthOf(1); }); - // TODO fixing the ordering issue would help make this test case more reliable - #271 - xit('should output one cache.json file with expected cache contents', async function() { - const cacheContents = require(path.join(this.context.publicDir, './cache.json')); - - expect(cacheContents).to.be.deep.equalInAnyOrder(expectedCache); - }); - - it('should have one window.__APOLLO_STATE__