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.
npm run import -- "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;
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
copyFileBucket
collectExternalResources
collectRoutes
getTemplateByUrl
test stream to output
(only imported in non-DEPLOY environment)upload files google cloud
(only imported in DEPLOY environment)Main Function: collectTemplateResources
This function takes six parameters:
path
: the current path being processedpage
: the current page being processedproperties
: an object containing template propertiestemplates
: an object containing template databucketName
: the name of the Google Cloud bucket to upload files torendered
: an array to store the rendered templatesThe function performs the following tasks:
test stream to output
or upload files google cloud
accordingly.rendered
array is empty, initializes it.index.html
if it's the first page in the template.collectExternalResources
to collect external resources and render the template.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
.