Repository: freeCodeCamp/freeCodeCamp
File: tools/client-plugins/gatsby-source-challenges/gatsby-node.js (JavaScript)

Code before:
    const buildCurriculumPath = path.resolve(
      curriculumPath,
      '..',
      '..',
      'build-curriculum'
    );
    const buildCurriculum = require(buildCurriculumPath);
    const superBlockToFilename = buildCurriculum.superBlockToFilename;

    if (!superBlockToFilename) {
      reporter.panic(
        'superBlockToFilename is missing from build-curriculum. This map is required.'
      );
    }

    Object.keys(superBlockToFilename).forEach(superBlock => {
      const filename = superBlockToFilename[superBlock] || superBlock;
      try {
        const structurePath = path.resolve(
          curriculumPath,
          '..',
          '..',
          'structure',
          'superblocks',
          `${filename}.json`
        );
        const structure = require(structurePath);

        const nodeId = createNodeId(`SuperBlockStructure-${superBlock}`);
        const nodeContent = JSON.stringify(structure);

        createNode({
          ...structure,
          superBlock,
          id: nodeId,
          parent: null,
          children: [],
          internal: {
            type: 'SuperBlockStructure',
            content: nodeContent,
            contentDigest: createContentDigest(structure)
          }
        });
      } catch (err) {
        reporter.warn(
          `Could not load structure for ${superBlock} (${filename}.json): ${err.message}`
        );
      }
    });
  }


Review comment (suggestion):
In general, it would be good to avoid reaching into the curriculum like this. Nothing outside the curriculum should need to know where these files live, since that's an implementation detail.

Could you use `getSuperblockStructure` in file-handler instead? It should do the job and it's a bit closer to being a proper api.

Code after:
  function createSuperBlockStructureNodes() {
    const buildCurriculumPath = path.resolve(
      curriculumPath,
      '..',
      '..',
      'build-curriculum'
    );
    const buildCurriculum = require(buildCurriculumPath);
    const superBlockToFilename = buildCurriculum.superBlockToFilename;

    if (!superBlockToFilename) {
      reporter.panic(
        'superBlockToFilename is missing from build-curriculum. This map is required.'
      );
    }

    Object.keys(superBlockToFilename).forEach(superBlock => {
      const filename = superBlockToFilename[superBlock] || superBlock;
      try {
        const structure = getSuperblockStructure(filename);

        const nodeId = createNodeId(`SuperBlockStructure-${superBlock}`);
        const nodeContent = JSON.stringify(structure);

        createNode({
          ...structure,
          superBlock,
          id: nodeId,
          parent: null,
          children: [],
          internal: {
            type: 'SuperBlockStructure',
            content: nodeContent,
            contentDigest: createContentDigest(structure)
          }
        });
      } catch (err) {
        reporter.warn(
          `Could not load structure for ${superBlock} (${filename}): ${err.message}`
        );
      }
    });
  }

  return new Promise((resolve, reject) => {
    watcher.on('ready', () => sourceAndCreateNodes().then(resolve, reject));
  });
};