The code requires several modules for file system operations and importing functionality, and defines three main functions: initSync, copyAllFiles, and importTest. The importTest function imports sheet resources, filters out existing files, and returns a promise resolving with new files to be copied, using copyAllFiles and initSync functions.
npm run import -- "sheet to web"var fs = require('fs');
var path = require('path');
var importer = require('../Core');
var getEnvironment = importer.import("get environment");
var importSheet = importer.import("sheet marketing import");
var {glob} = importer.import("glob files")
var streamToGoogle
function initSync(env) {
getEnvironment(env || process.env.ENVIRONMENT || 'TEST_SPREADSHEET');
if((process.env.ENVIRONMENT || '').includes('LOCAL')
|| (process.env.ENVIRONMENT || '').includes('TEST')
|| !(process.env.ENVIRONMENT || '').includes('DEPLOY')) {
streamToGoogle = importer.import("test stream to output");
} else {
streamToGoogle = importer.import("upload files google cloud");
}
}
initSync(process.env.ENVIRONMENT);
var copyAllFiles = (resources, bucketName) => Promise.all(resources
.map(f => f && streamToGoogle(path.basename(f),
bucketName,
fs.createReadStream(f))
.catch(e => console.error(e))))
async function importTest(link, domain, env) {
if(env) {
initSync(env)
}
var resources = await importSheet(link, domain)
// TODO: fix google storage check
if(process.env.PROJECT_OUTPUT) {
var existingFiles = glob(resources
.map(r => `**/*${path.basename(r)}*`), process.env.PROJECT_OUTPUT)
resources = resources.filter(r => existingFiles.filter(e => e.includes(path.basename(r))).length == 0)
console.log(resources)
}
var newFiles = glob(resources
.map(r => `**/*${path.basename(r)}*`), process.env.DOWNLOAD_PATH)
resources = resources.filter(r => newFiles.filter(e => e.includes(path.basename(r))).length == 0)
var desktopFiles = glob(resources
.map(r => `**/*${path.basename(r)}*`), path.join(process.env.DOWNLOAD_PATH, '../Desktop'))
var pictureFiles = glob(resources
.map(r => `**/*${path.basename(r)}*`), path.join(process.env.DOWNLOAD_PATH, '../Pictures'))
return copyAllFiles(newFiles.concat(desktopFiles).concat(pictureFiles), domain)
}
module.exports = importTest;
const fs = require('fs');
const path = require('path');
const { glob } = require('glob');
const importer = require('../Core');
const getEnvironment = importer.import('get environment');
const importSheet = importer.import('sheet marketing import');
const streamToGoogle = importer.import('stream to google');
class FileExporter {
constructor(env) {
this.env = env;
this.streamToGoogle = this.getStreamToGoogle();
}
getStreamToGoogle() {
if ([
'LOCAL',
'TEST',
].some(mode => this.env.includes(mode)) &&!this.env.includes('DEPLOY')) {
return importer.import('test stream to output');
}
return importer.import('upload files google cloud');
}
initSync() {
getEnvironment(this.env || process.env.ENVIRONMENT || 'TEST_SPREADSHEET');
}
async importTest(link, domain) {
this.initSync();
const resources = await importSheet(link, domain);
const filesToCopy = this.getFilesToCopy(resources, process.env.PROJECT_OUTPUT || '');
const newFiles = this.getFilesToCopy(resources, process.env.DOWNLOAD_PATH);
const desktopFiles = this.getFilesToCopy(resources, path.join(process.env.DOWNLOAD_PATH, '../Desktop'));
const pictureFiles = this.getFilesToCopy(resources, path.join(process.env.DOWNLOAD_PATH, '../Pictures'));
return Promise.all(newFiles.concat(desktopFiles).concat(pictureFiles)
.map(file => this.streamToGoogle(path.basename(file), domain, fs.createReadStream(file)))
.catch(e => console.error(e)));
}
getFilesToCopy(resources, directory) {
const existingFiles = glob(resources.map(r => `**/*${path.basename(r)}*`), directory);
return resources.filter(r => existingFiles.filter(e => e.includes(path.basename(r))).length === 0);
}
}
module.exports = (link, domain, env = process.env.ENVIRONMENT) => {
return new FileExporter(env).importTest(link, domain);
};fs and path modules are required for file system operations.importer module is required for importing functionality from other modules.getEnvironment, importSheet, and glob functionalities are imported from other modules using importer.streamToGoogle is not initialized at this point.env as input or defaults to process.env.ENVIRONMENT or 'TEST_SPREADSHEET' if it's not provided.streamToGoogle to either 'test stream to output' or 'upload files google cloud' based on this condition.process.env.ENVIRONMENT as the argument.Promise.all to create a promise that resolves when all resources have been processed.fs.createReadStream, and streams the file to Google using streamToGoogle.console.error.initSync with the environment variable if it's provided.importSheet and filters out files that already exist in the project output directory (process.env.PROJECT_OUTPUT) or the download path (process.env.DOWNLOAD_PATH).copyAllFiles with the remaining resources and the bucket name.initSync function modifies a global variable streamToGoogle based on the environment. This might not be the intended behavior. Consider making it a local variable within the function.copyAllFiles function uses Promise.all to process resources asynchronously. However, it doesn't handle any errors that might occur during the processing of resources. Consider adding error handling to this function as well.importTest function filters out files that already exist in the project output directory or the download path. However, it doesn't handle any edge cases, such as files being deleted between the import and the copy operation. Consider adding error handling and logging to this function as well.