scripts/module.js
async function build() {
  console.time('Packager');

  let requires = [];
  let esmExportString = '';
  let cjsExportString = '';

  try {
    if (!fs.existsSync(DIST_PATH)) fs.mkdirSync(DIST_PATH);
    fs.writeFileSync(ROLLUP_INPUT_FILE, '');
    fs.writeFileSync(TEST_MODULE_FILE, '');

    // All the snippets that are Node.js-based and will break in a browser
    // environment
    const nodeSnippets = fs
      .readFileSync('tag_database', 'utf8')
      .split('\n')
      .filter(v => v.search(/:.*node/g) !== -1)
      .map(v => v.slice(0, v.indexOf(':')));

    const snippets = fs.readdirSync(SNIPPETS_PATH);
    const archivedSnippets = fs
      .readdirSync(SNIPPETS_ARCHIVE_PATH)
      .filter(v => v !== 'README.md');

    snippets.forEach(snippet => {
      const rawSnippetString = getRawSnippetString(SNIPPETS_PATH, snippet);
      const snippetName = snippet.replace('.md', '');
      let code = getCode(rawSnippetString);
      if (nodeSnippets.includes(snippetName)) {
        requires.push(code.match(/const.*=.*require\(([^\)]*)\);/g));
        code = code.replace(/const.*=.*require\(([^\)]*)\);/g, '');
      }
      esmExportString += `export ${code}`;
      cjsExportString += code;
    });
    archivedSnippets.forEach(snippet => {
      const rawSnippetString = getRawSnippetString(
        SNIPPETS_ARCHIVE_PATH,
        snippet
      );
      cjsExportString += getCode(rawSnippetString);
    });

    requires = [
      ...new Set(
        requires
          .filter(Boolean)
          .map(v =>
            v[0].replace(
              'require(',
              'typeof require !== "undefined" && require('
            )
          )
      )
    ].join('\n');

    fs.writeFileSync(ROLLUP_INPUT_FILE, `${requires}\n\n${esmExportString}`);

    const testExports = `module.exports = {${[...snippets, ...archivedSnippets]
      .map(v => v.replace('.md', ''))
      .join(',')}}`;

    fs.writeFileSync(
      TEST_MODULE_FILE,
      `${requires}\n\n${cjsExportString}\n\n${testExports}`
    );

    // Check Travis builds - Will skip builds on Travis if not CRON/API
    if (util.isTravisCI() && util.isNotTravisCronOrAPI()) {
      fs.unlink(ROLLUP_INPUT_FILE);
      console.log(
        `${chalk.green(
          'NOBUILD'
        )} Module build terminated, not a cron job or a custom build!`
      );
      console.timeEnd('Packager');
      process.exit(0);
    }

    await doRollup();

    // Clean up the temporary input file Rollup used for building the module
    fs.unlink(ROLLUP_INPUT_FILE);

    console.log(`${chalk.green('SUCCESS!')} Snippet module built!`);
    console.timeEnd('Packager');
  } catch (err) {
    console.log(`${chalk.red('ERROR!')} During module creation: ${err}`);
    process.exit(1);
  }
}
async function build() {
  console.time('Packager');

  let requires = [];
  let esmExportString = '';
  let cjsExportString = '';

  try {
    if (!fs.existsSync(DIST_PATH)) fs.mkdirSync(DIST_PATH);
    fs.writeFileSync(ROLLUP_INPUT_FILE, '');
    fs.writeFileSync(TEST_MODULE_FILE, '');

    // All the snippets that are Node.js-based and will break in a browser
    // environment
    const nodeSnippets = fs
      .readFileSync('tag_database', 'utf8')
      .split('\n')
      .filter(v => v.search(/:.*node/g) !== -1)
      .map(v => v.slice(0, v.indexOf(':')));

    const snippets = fs.readdirSync(SNIPPETS_PATH);
    const archivedSnippets = fs
      .readdirSync(SNIPPETS_ARCHIVE_PATH)
      .filter(v => v !== 'README.md');

    snippets.forEach(snippet => {
      const rawSnippetString = getRawSnippetString(SNIPPETS_PATH, snippet);
      const snippetName = snippet.replace('.md', '');
      let code = getCode(rawSnippetString);
      if (nodeSnippets.includes(snippetName)) {
        requires.push(code.match(/const.*=.*require\(([^\)]*)\);/g));
        code = code.replace(/const.*=.*require\(([^\)]*)\);/g, '');
      }
      esmExportString += `export ${code}`;
      cjsExportString += code;
    });
    archivedSnippets.forEach(snippet => {
      const rawSnippetString = getRawSnippetString(
        SNIPPETS_ARCHIVE_PATH,
        snippet
      );
      cjsExportString += getCode(rawSnippetString);
    });

    requires = [
      ...new Set(
        requires
          .filter(Boolean)
          .map(v =>
            v[0].replace(
              'require(',
              'typeof require !== "undefined" && require('
            )
          )
      )
    ].join('\n');

    fs.writeFileSync(ROLLUP_INPUT_FILE, `${requires}\n\n${esmExportString}`);

    const testExports = `module.exports = {${[...snippets, ...archivedSnippets]
      .map(v => v.replace('.md', ''))
      .join(',')}}`;

    fs.writeFileSync(
      TEST_MODULE_FILE,
      `${requires}\n\n${cjsExportString}\n\n${testExports}`
    );

    // Check Travis builds - Will skip builds on Travis if not CRON/API
    if (util.isTravisCI() && util.isNotTravisCronOrAPI()) {
      fs.unlink(ROLLUP_INPUT_FILE);
      console.log(
        `${chalk.green(
          'NOBUILD'
        )} Module build terminated, not a cron job or a custom build!`
      );
      console.timeEnd('Packager');
      process.exit(0);
    }

    await doRollup();

    // Clean up the temporary input file Rollup used for building the module
    fs.unlink(ROLLUP_INPUT_FILE);

    console.log(`${chalk.green('SUCCESS!')} Snippet module built!`);
    console.timeEnd('Packager');
  } catch (err) {
    console.log(`${chalk.red('ERROR!')} During module creation: ${err}`);
    process.exit(1);
  }
}
async function build ( ) { console . time ( 'Packager' ) ; let requires = [ ] ; let esmExportString = '' ; let cjsExportString = '' ; try { if ( ! fs . existsSync ( DIST_PATH ) ) fs . mkdirSync ( DIST_PATH ) ; fs . writeFileSync ( ROLLUP_INPUT_FILE , '' ) ; fs . writeFileSync ( TEST_MODULE_FILE , '' ) ; // All the snippets that are Node.js-based and will break in a browser // environment const nodeSnippets = fs . readFileSync ( 'tag_database' , 'utf8' ) . split ( '\n' ) . filter ( v => v . search ( / :.*node / g ) !== - 1 ) . map ( v => v . slice ( 0 , v . indexOf ( ':' ) ) ) ; const snippets = fs . readdirSync ( SNIPPETS_PATH ) ; const archivedSnippets = fs . readdirSync ( SNIPPETS_ARCHIVE_PATH ) . filter ( v => v !== 'README.md' ) ; snippets . forEach ( snippet => { const rawSnippetString = getRawSnippetString ( SNIPPETS_PATH , snippet ) ; const snippetName = snippet . replace ( '.md' , '' ) ; let code = getCode ( rawSnippetString ) ; if ( nodeSnippets . includes ( snippetName ) ) { requires . push ( code . match ( / const.*=.*require\(([^\)]*)\); / g ) ) ; code = code . replace ( / const.*=.*require\(([^\)]*)\); / g , '' ) ; } esmExportString += ` ${ code } ` ; cjsExportString += code ; } ) ; archivedSnippets . forEach ( snippet => { const rawSnippetString = getRawSnippetString ( SNIPPETS_ARCHIVE_PATH , snippet ) ; cjsExportString += getCode ( rawSnippetString ) ; } ) ; requires = [ ... new Set ( requires . filter ( Boolean ) . map ( v => v [ 0 ] . replace ( 'require(' , 'typeof require !== "undefined" && require(' ) ) ) ] . join ( '\n' ) ; fs . writeFileSync ( ROLLUP_INPUT_FILE , ` ${ requires } \n \n ${ esmExportString } ` ) ; const testExports = ` ${ [ ... snippets , ... archivedSnippets ] . map ( v => v . replace ( '.md' , '' ) ) . join ( ',' ) } ` ; fs . writeFileSync ( TEST_MODULE_FILE , ` ${ requires } \n \n ${ cjsExportString } \n \n ${ testExports } ` ) ; // Check Travis builds - Will skip builds on Travis if not CRON/API if ( util . isTravisCI ( ) && util . isNotTravisCronOrAPI ( ) ) { fs . unlink ( ROLLUP_INPUT_FILE ) ; console . log ( ` ${ chalk . green ( 'NOBUILD' ) } ` ) ; console . timeEnd ( 'Packager' ) ; process . exit ( 0 ) ; } await doRollup ( ) ; // Clean up the temporary input file Rollup used for building the module fs . unlink ( ROLLUP_INPUT_FILE ) ; console . log ( ` ${ chalk . green ( 'SUCCESS!' ) } ` ) ; console . timeEnd ( 'Packager' ) ; } catch ( err ) { console . log ( ` ${ chalk . red ( 'ERROR!' ) } ${ err } ` ) ; process . exit ( 1 ) ; } }
Starts the build process.
Starts the build process .