convert spreadsheet | collect external content and resources | sheet to web | Search

This Node.js script collects and renders template resources, streams the rendered template to Google Cloud, and returns a promise chain that resolves when the file has been streamed successfully, with environment-specific logic to determine which streaming function to use.

Run example

npm run import -- "collect google sheets resources"

collect google sheets resources

var importer = require('../Core');
if((process.env.ENVIRONMENT || '').includes('LOCAL')
   || (process.env.ENVIRONMENT || '').includes('TEST')
   || !(process.env.ENVIRONMENT || '').includes('DEPLOY')) {
 }
var copyFileBucket = importer.import("copy file storage bucket");
var collectExternalResources = importer.import("collect external content and resources");
var collectRoutes = importer.import("find known routes to sheets");
var getTemplateByUrl = importer.import("convert sheet helper functions");

var timestamp = (new Date()).getTime();

// detect links and write out every part of the site
function collectTemplateResources(path, page, properties, templates, bucketName, rendered) {
    var streamToGoogle
    if(!(process.env.ENVIRONMENT || '').includes('DEPLOY')) {
        streamToGoogle = importer.import("test stream to output");
    } else {
        streamToGoogle = importer.import("upload files google cloud");
    }

    if(!rendered) {
        rendered = [];
    }

    // if it is the first page in the template, rename it to index.html
    if((path.split('/').length < 2 || path === '/' || path === '')
       && getTemplateByUrl(templates, path) === getTemplateByUrl(templates, '/')) {
        console.log(`using ${path} as index.html`);
        path = 'index';
    }
    
    var trimmedBase = (properties['base'] || '').replace(/^\/|\/$/ig, '');
    if(path.substr(0, trimmedBase.length) !== trimmedBase) {
        path = trimmedBase + '/' + path;
    }

    // TODO: add timestamps to generated content
    // TODO: set different permissions on files streamed to google.
    
    var routes = [];
    return collectExternalResources(page, rendered, routes)
        .then(stream => (console.log(`emitting ${path}`), stream))
        .then(stream => streamToGoogle(path + '.html', bucketName, stream, {
            contentType: 'text/html; charset=utf-8'
        } /* TODO: insert permission settings for user directory */))
        .then(() => !(process.env.ENVIRONMENT || '').includes('DEPLOY')
              ? {}
              : copyFileBucket(bucketName, path + '.html'))
        .then(() => collectRoutes(routes, properties, templates, rendered))
        .then(pages => importer.runAllPromises(Object.keys(pages).map(fileName => resolve => 
            collectTemplateResources(fileName, pages[fileName], properties, templates, bucketName, rendered)
                .then(() => resolve()))))
        .then(() => rendered)
}

module.exports = collectTemplateResources;

What the code could have been:

const Core = require('../Core');

const importModule = (moduleName, env) => {
    if (env.includes('LOCAL') || env.includes('TEST') ||!env.includes('DEPLOY')) {
        return Core.import(`test ${moduleName}`);
    }
    return Core.import(moduleName);
};

const STREAM_TO_GOOGLE = importModule('stream to output', process.env.ENVIRONMENT || '');
const COPY_FILE_BUCKET = importModule('copy file storage bucket', process.env.ENVIRONMENT || '');
const COLLECT_EXTERNAL_RESOURCES = importModule('collect external content and resources', process.env.ENVIRONMENT || '');
const COLLECT_ROUTES = importModule('find known routes to sheets', process.env.ENVIRONMENT || '');
const GET_TEMPLATE_BY_URL = importModule('convert sheet helper functions', process.env.ENVIRONMENT || '');

const timestamp = new Date().getTime();

const collectTemplateResources = (path, page, properties, templates, bucketName, rendered = []) => {
    if (!path || (path.split('/').length < 2 && path === '/' && path === '')) {
        path = 'index';
    }

    const trimmedBase = (properties['base'] || '').replace(/^\/|\/$/ig, '');
    const adjustedPath = trimmedBase === '/'? trimmedBase : trimmedBase + (path.startsWith(trimmedBase)? '' : '/') + path;
    path = adjustedPath;

    const streamToGoogle = STREAM_TO_GOOGLE;
    return COLLECT_EXTERNAL_RESOURCES(page, rendered, [])
       .then(stream => {
            console.log(`emitting ${path}`);
            return stream;
        })
       .then(stream => streamToGoogle(path + '.html', bucketName, stream, { contentType: 'text/html; charset=utf-8' }))
       .then(() => {
            if (!process.env.ENVIRONMENT ||!process.env.ENVIRONMENT.includes('DEPLOY')) {
                return {};
            }
            return COPY_FILE_BUCKET(bucketName, path + '.html');
        })
       .then(() => COLLECT_ROUTES([], properties, templates, rendered))
       .then(pages => Core.runAllPromises(Object.keys(pages).map(fileName => resolve => (
            collectTemplateResources(fileName, pages[fileName], properties, templates, bucketName, rendered)
               .then(() => resolve())
        ))))
       .then(() => rendered);
};

module.exports = collectTemplateResources;

Code Breakdown

This is a Node.js script that appears to be part of a larger application. It imports various modules and functions from another module (../Core) and uses them to perform tasks related to template rendering, resource collection, and file streaming to Google Cloud.

Imported Modules and Functions

  1. copyFileBucket
  2. collectExternalResources
  3. collectRoutes
  4. getTemplateByUrl
  5. test stream to output (only imported in non-DEPLOY environment)
  6. upload files google cloud (only imported in DEPLOY environment)

Main Function: collectTemplateResources

This function takes six parameters:

  1. path: the current path being processed
  2. page: the current page being processed
  3. properties: an object containing template properties
  4. templates: an object containing template data
  5. bucketName: the name of the Google Cloud bucket to upload files to
  6. rendered: an array to store the rendered templates

The function performs the following tasks:

  1. Checks if it's running in a non-DEPLOY environment and imports either test stream to output or upload files google cloud accordingly.
  2. If the rendered array is empty, initializes it.
  3. Renames the current path to index.html if it's the first page in the template.
  4. Trims the base path from the current path if necessary.
  5. Calls collectExternalResources to collect external resources and render the template.
  6. Streams the rendered template to Google Cloud using the imported streaming function.
  7. Returns a promise chain that resolves when the file has been streamed successfully.

Environment-Specific Logic

The script checks the ENVIRONMENT environment variable to determine which environment it's running in. If it's not DEPLOY, it imports test stream to output instead of upload files google cloud.