initial commit
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
1314830576
commit
15aae9a57c
@@ -1,38 +1,38 @@
|
||||
const crypto = require('crypto');
|
||||
|
||||
function createChallengeNodes(
|
||||
path,
|
||||
pluginOptions = {}
|
||||
) {
|
||||
const { source } = pluginOptions;
|
||||
return new Promise(resolve => {
|
||||
const challengeNodes = source(path)
|
||||
.reduce((nodes, { challenges, name }) => {
|
||||
const challengeNodes = challenges.map(challenge => {
|
||||
const contentDigest = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(challenge))
|
||||
.digest('hex');
|
||||
const internal = {
|
||||
contentDigest,
|
||||
type: 'ChallengeNode'
|
||||
};
|
||||
function createChallengeNodes(challenge, reporter) {
|
||||
if (typeof challenge.description[0] !== 'string') {
|
||||
reporter.panic(`
|
||||
|
||||
return JSON.parse(
|
||||
JSON.stringify({
|
||||
id: challenge.id,
|
||||
children: [],
|
||||
parent: null,
|
||||
internal,
|
||||
sourceInstanceName: pluginOptions.name || '__PROGRAMATTIC__',
|
||||
...challenge
|
||||
})
|
||||
);
|
||||
});
|
||||
return nodes.concat(challengeNodes);
|
||||
}, []);
|
||||
resolve(challengeNodes);
|
||||
});
|
||||
${challenge.title} has a description that will break things!
|
||||
|
||||
`);
|
||||
}
|
||||
const contentDigest = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(challenge))
|
||||
.digest('hex');
|
||||
const internal = {
|
||||
contentDigest,
|
||||
type: 'ChallengeNode'
|
||||
};
|
||||
|
||||
exports.createChallengeNodes = createChallengeNodes;
|
||||
/* eslint-disable prefer-object-spread/prefer-object-spread */
|
||||
return JSON.parse(
|
||||
JSON.stringify(
|
||||
Object.assign(
|
||||
{},
|
||||
{
|
||||
id: challenge.id + ' >>>> ChallengeNode',
|
||||
children: [],
|
||||
parent: null,
|
||||
internal,
|
||||
sourceInstanceName: 'challenge'
|
||||
},
|
||||
challenge
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
exports.createChallengeNodes = createChallengeNodes;
|
||||
|
@@ -1,45 +0,0 @@
|
||||
const { GraphQLString } = require('graphql');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = '' }) => {
|
||||
if (type.name !== 'File') {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
publicURL: {
|
||||
type: GraphQLString,
|
||||
args: {},
|
||||
description: 'Copy file to static directory and return public url to it',
|
||||
resolve: (file, fieldArgs, context) => {
|
||||
const details = getNodeAndSavePathDependency(file.id, context.path);
|
||||
const fileName = `${file.name}-${file.internal.contentDigest}${
|
||||
details.ext
|
||||
}`;
|
||||
|
||||
const publicPath = path.join(
|
||||
process.cwd(),
|
||||
'public',
|
||||
'static',
|
||||
fileName
|
||||
);
|
||||
|
||||
if (!fs.existsSync(publicPath)) {
|
||||
fs.copy(details.absolutePath, publicPath, err => {
|
||||
if (err) {
|
||||
console.error(
|
||||
`error copying file from ${
|
||||
details.absolutePath
|
||||
} to ${publicPath}`,
|
||||
err
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return `${pathPrefix}/static/${fileName}`;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
@@ -1,12 +1,9 @@
|
||||
const chokidar = require('chokidar');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
const { createId, createChallengeNodes } = require('./create-Challenge-nodes');
|
||||
const { createChallengeNodes } = require('./create-Challenge-nodes');
|
||||
|
||||
exports.sourceNodes = (
|
||||
{ boundActionCreators, getNode, reporter },
|
||||
pluginOptions
|
||||
) => {
|
||||
exports.sourceNodes = ({ boundActionCreators, reporter }, pluginOptions) => {
|
||||
if (!(pluginOptions && pluginOptions.path)) {
|
||||
reporter.panic(`
|
||||
"path" is a required option for gatsby-source-filesystem
|
||||
@@ -29,8 +26,8 @@ Please use the path to the seed directory.
|
||||
that delivers challenge files to the plugin
|
||||
`);
|
||||
}
|
||||
|
||||
const { createNode, deleteNode } = boundActionCreators;
|
||||
// TODO: Add live seed updates
|
||||
const { createNode } = boundActionCreators;
|
||||
|
||||
let ready = false;
|
||||
|
||||
@@ -45,11 +42,14 @@ that delivers challenge files to the plugin
|
||||
'../**/dist/**'
|
||||
]
|
||||
});
|
||||
const { source } = pluginOptions;
|
||||
const createAndProcessNodes = () =>
|
||||
source()
|
||||
.map(nodes => nodes.map(node => createChallengeNodes(node, reporter)))
|
||||
.map(nodes => nodes.map(node => createNode(node)))
|
||||
.subscribe();
|
||||
|
||||
const createAndProcessNodes = path =>
|
||||
createChallengeNodes(path, pluginOptions).then(nodes => nodes.forEach(node => createNode(node))
|
||||
);
|
||||
|
||||
createAndProcessNodes();
|
||||
// For every path that is reported before the 'ready' event, we throw them
|
||||
// into a queue and then flush the queue when 'ready' event arrives.
|
||||
// After 'ready', we handle the 'add' event without putting it into a queue.
|
||||
@@ -60,44 +60,20 @@ that delivers challenge files to the plugin
|
||||
return Promise.all(queue.map(createAndProcessNodes));
|
||||
};
|
||||
|
||||
watcher.on('add', path => {
|
||||
if (ready) {
|
||||
reporter.info(`added file at ${path}`);
|
||||
createAndProcessNodes(path).catch(err => reporter.error(err));
|
||||
} else {
|
||||
pathQueue.push(path);
|
||||
}
|
||||
});
|
||||
// watcher.on('change', path => {
|
||||
// reporter.info(`changed file at ${path}`);
|
||||
// createAndProcessNodes().catch(err => reporter.error(err));
|
||||
// });
|
||||
|
||||
watcher.on('change', path => {
|
||||
reporter.info(`changed file at ${path}`);
|
||||
createAndProcessNodes(path).catch(err => reporter.error(err));
|
||||
});
|
||||
|
||||
watcher.on('unlink', path => {
|
||||
reporter.info(`file deleted at ${path}`);
|
||||
const node = getNode(createId(path));
|
||||
// It's possible the file node was never created as sometimes tools will
|
||||
// write and then immediately delete temporary files to the file system.
|
||||
if (node) {
|
||||
deleteNode(node.id, node);
|
||||
}
|
||||
});
|
||||
|
||||
watcher.on('addDir', path => {
|
||||
if (ready) {
|
||||
reporter.info(`added directory at ${path}`);
|
||||
createAndProcessNodes(path).catch(err => reporter.error(err));
|
||||
} else {
|
||||
pathQueue.push(path);
|
||||
}
|
||||
});
|
||||
|
||||
watcher.on('unlinkDir', path => {
|
||||
reporter.info(`directory deleted at ${path}`);
|
||||
const node = getNode(createId(path));
|
||||
deleteNode(node.id, node);
|
||||
});
|
||||
// watcher.on('unlink', path => {
|
||||
// reporter.info(`file deleted at ${path}`);
|
||||
// const node = getNode(createId(path));
|
||||
// // It's possible the file node was never created as sometimes tools will
|
||||
// // write and then immediately delete temporary files to the file system.
|
||||
// if (node) {
|
||||
// deleteNode(node.id, node);
|
||||
// }
|
||||
// });
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
watcher.on('ready', () => {
|
||||
|
Reference in New Issue
Block a user