Repository: freeCodeCamp/freeCodeCamp
File: tools/challenge-helper-scripts/create-daily-challenges.ts (TypeScript)

Code before:
import ObjectID from 'bson-objectid';
import { Meta } from './helpers/project-metadata';
import { getArgValue } from './helpers/get-arg-value';
import {
  getDailyJavascriptChallengeTemplate,
  getDailyPythonChallengeTemplate
} from './helpers/get-challenge-template';

const numberOfChallengesToCreate = getArgValue(process.argv);

if (numberOfChallengesToCreate > 10) {
  throw new Error('Are you sure you want to create that many challenges?');
}

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const challengesPath = join(__dirname, '../../curriculum/challenges');
const metaPath = join(challengesPath, '_meta');
const devPlaygroundPath = join(challengesPath, 'english/99-dev-playground');

const jsMetaPath = join(
  metaPath,
  'daily-coding-challenges-javascript/meta.json'
);
const pyMetaPath = join(metaPath, 'daily-coding-challenges-python/meta.json');

for (let i = 0; i < numberOfChallengesToCreate; i++) {
  const jsMeta = JSON.parse(readFileSync(jsMetaPath, 'utf-8')) as Meta;
  const pyMeta = JSON.parse(readFileSync(pyMetaPath, 'utf-8')) as Meta;

  const numberOfJsChallenges = jsMeta.challengeOrder.length;
  const numberOfPyChallenges = pyMeta.challengeOrder.length;

  if (numberOfJsChallenges !== numberOfPyChallenges) {
    throw new Error(
      'Inconsistent number of challenges in each daily challenge language, cannot create new challenge.'
    );
  }

  const newChallengeNumber = numberOfJsChallenges + 1;

  createDailyJsChallenge({ challengeNumber: newChallengeNumber, meta: jsMeta });
  createDailyPyChallenge({ challengeNumber: newChallengeNumber, meta: pyMeta });
}

interface CreateDailyChallengeOptions {
  challengeNumber: number;
  meta: Meta;
}


Review comment (suggestion):
Oliver's PR is in, and the meta.json files have become `curriculum/structure/blocks/[block-dashed-name].json`.

The other file paths should be updated as well.


Code after:
import ObjectID from 'bson-objectid';
import { Meta } from './helpers/project-metadata';
import { getArgValue } from './helpers/get-arg-value';
import {
  getDailyJavascriptChallengeTemplate,
  getDailyPythonChallengeTemplate
} from './helpers/get-challenge-template';

const numberOfChallengesToCreate = getArgValue(process.argv);

if (numberOfChallengesToCreate > 10) {
  throw new Error('Are you sure you want to create that many challenges?');
}

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const curriculumPath = join(__dirname, '../../curriculum');

const structureBlocksPath = join(curriculumPath, '/structure/blocks');
const jsStructurePath = join(
  structureBlocksPath,
  '/daily-coding-challenges-javascript.json'
);
const pyStructurePath = join(
  structureBlocksPath,
  '/daily-coding-challenges-python.json'
);

const challengeBlocksPath = join(curriculumPath, '/challenges/english/blocks');
const jsChallengesPath = join(
  challengeBlocksPath,
  '/daily-coding-challenges-javascript'
);
const pyChallengesPath = join(
  challengeBlocksPath,
  '/daily-coding-challenges-python'
);

for (let i = 0; i < numberOfChallengesToCreate; i++) {
  const jsMeta = JSON.parse(readFileSync(jsStructurePath, 'utf-8')) as Meta;
  const pyMeta = JSON.parse(readFileSync(pyStructurePath, 'utf-8')) as Meta;

  const numberOfJsChallenges = jsMeta.challengeOrder.length;
  const numberOfPyChallenges = pyMeta.challengeOrder.length;

  if (numberOfJsChallenges !== numberOfPyChallenges) {
    throw new Error(
      'Inconsistent number of challenges in each daily challenge language, cannot create new challenge.'
    );
  }