gulp copy source files from another drive - path

Hi I have piece of gulp snippet that works great and copies entire folders when the source and destination folders are on the same drive. If I try to run it to copy files from another drive say D:/path/to/project with an absolute path to the folder, the code runs without any errors but no files are copied.
`So the path = '../../myproject/' works while 'd:/path/to/Project' does not.
Any idea how this can be achieved in gulp?
Thanks.
UPDATED WITH CODE
Of the two paths, the first one, targeting folder in drive f, fails to copy while the 2nd one does so successfully. Has to be tried one at a time, commenting the other.
subB = 'src/webtest'
subC = 'pro_miles/eb_aws'
var pathsToProj = [
['f:/pro_miles/eb_aws/**/*.*', '/src/'+subC, 'src/'+subC]
// ['../../pro_miles/eb_aws/**/*.*', '/src/'+subC, 'src/'+subC]
];
`// gulp.task('copyFiles', function (cb) {`
tasks = function copyFiles(cb) {
var paths = new Array();
for (const pathToProj of pathsToProj) {
paths.push(gulp.src(pathToProj[0], {base: pathToProj[1]})
.pipe(gulp.dest(pathToProj[2])));
};
cb();
return paths
};
gulp.task('default', gulp.series(tasks) );

Related

How to populate Documents folder at build time

I have an NativeScript 6.8 Javascript app that downloads newer data files. I'm discovering that on iOS I cannot create files within the app folder. (At least, in release builds; in debug builds I can.) I can change my code to read data files from the Documents folder, but how can I pre-populate the Documents folder at build time with the original data files? I'd rather not copy all the data files at run time.
Or, have I misinterpreted the restriction that files cannot be created in the app folder (or subfolders) in iOS release builds?
Live-updating files on iOS is more involved than one might expect. So, yes, you need to access the live-updated files from the Documents folder, not back the files up to iCloud, and handle numerous timing conditions, such as the live-update running just before what would seem to be the initial copy of the file to the Documents folder (seems unlikely, but I've seen it happen while testing).
I've included the function I developed, below. For context, when I find a file online to be live-updated, I use an appSetting to save the file's date as a string (storing as a value loses precision).
The function isn't perfect, but for now, it gets the job done. I call this from app.js in a NativeScript 6.8 JavaScript project.
/**
* Copy files in app/files folder to Documents folder so they can be updated
*/
async function copyFilesToDocuments() {
let filesCopied = appSettings.getBoolean("copyFilesToDocuments", false);
if (!filesCopied) { // only copy files on first invocation
let filesFolder = fs.knownFolders.currentApp().getFolder("files"); // Folder object
let documentsFolder = fs.knownFolders.documents(); // Folder object
let fileEntities = await filesFolder.getEntities();
for (entity of fileEntities) {
let sourceFile = fs.File.fromPath(entity.path);
let targetFilePath = fs.path.join(documentsFolder.path, entity.name);
let targetDate = parseInt(appSettings.getString(entity.name, "0"), 10); // live-update date or 0
if (fs.Folder.exists(targetFilePath) && targetDate > global.dataDate ) { // if file has been live-updated
console.log("app.js copyFilesToDocuments: file '" + entity.name + "' skipped to avoid overwrite. ");
continue; // don't overwrite newer file
}
appSettings.remove(entity.name); // remove any live-update timestamp
let targetFile = fs.File.fromPath(targetFilePath);
let content = await sourceFile.read();
try {
await targetFile.write(content);
if (platform.isIOS) {
// Prevent file from being backed up to iCloud
// See https://stackoverflow.com/questions/58363089/using-nsurlisexcludedfrombackupkey-in-nativescript
// See https://stackoverflow.com/questions/26080120/cfurlcopyresourcepropertyforkey-failed-because-passed-url-no-scheme
NSURL.fileURLWithPath(targetFilePath).setResourceValueForKeyError(true, NSURLIsExcludedFromBackupKey);
}
// console.log("app.js copyFilesToDocuments file copied: " + entity.name);
} catch(e) {
console.warn("app.js copyFilesToDocuments error: " + e);
//TODO: app will fail at this point with some files not found :-(
} // end catch
} // end for
appSettings.setBoolean("copyFilesToDocuments", true);
} // end files not yet copied
} // end copyFilesToDocuments

google sheet: make a local copy of image link from a shared sheets

my frient shared his google sheet to me and the table contains image which is a link (url). How can i make a copy of this sheet and make all the image link to be local, so i want the image is copying to my local google drive automatically (so the link won't be broken if he delete his images files in future). Right now, if i make a copy of this document, then it still link to original image source.
How is it possible ? of course i don't want to manually copy them one by one from the link. Is there any better and faster way ?
https://docs.google.com/spreadsheets/d/1TkXwAd8rKbjnGfYEJVaOYBJwCZ7G7YfuSvmcDE6g8No/edit?usp=sharing
The OP wants to extract the image URL from a hyperlink formula, and save a copy of the image to their own Google Drive account.
This answer combines several elements from precedents on StackOverflow.
Since the images metadata is in the formula, the code uses the getFormulas() method rather than the "conventional" getValues(). Cells with no formula are empty strings; hence the test if (formula.length !=0){.
Get the file name without extension: REGEX: Capture Filename from URL without file extension. Ironically, this precedent doesn't use regular expressions but finds the position of the last / and the last . using lastIndexOf and getting a substring between those points. Note this solution fails on filenames with multiple periods, though there is an alternative solution for this scenario.
Get the file name from the url: Getting a Google Spreadsheet Cell's Image URL which combines regex and Javascript match.
Save a file to Google Drive: Need sheets script to save img to drive which is a simple and elegant solution for saving files.
Saving the file to Google Drive: When copying files using Apps Script from one folder to another any “Apps Script” files being copied end up in MyDrive not the specified folder - why? explains why the API is required to write the files to My Drive.
Note: In order to use this script, enable Drive API v2 at Advanced Google Services
On script editor, Resources -> Advanced Google Services; Turn on Drive API v2
function so5811567402() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetName = "Table";
var sh = ss.getSheetByName(sheetName);
var rg=sh.getDataRange();
var lastColumn = sh.getLastColumn();
var lastRow = sh.getLastRow();
var formulas = rg.getFormulas();
for (var i in formulas) {
for (var j in formulas[i]) {
var formula = formulas[i][j];
if (formula.length !=0){
var regex = /image\("(.*)"/i;
var matches = formula.match(regex);
var imgurl = matches[1];
var filename = imgurl.substring(imgurl.lastIndexOf("/") + 1, imgurl.lastIndexOf("."));
//Logger.log(filename);
var image = UrlFetchApp.fetch(imgurl).getBlob().getAs('image/jpeg').setName(filename);
var FolderId = "Folder ID goes here";
var folder = DriveApp.getFolderById(FolderId);
var file = DriveApp.createFile(image);
Drive.Files.update({"parents": [{"id": folder.getId()}]}, file.getId());
}
}
}
}

Require json file dynamically in react-native (from thousands of files)

I googled so far and tried to find out the solution but not yet.
I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.
Please advise me the best approach to handle such case.
Background
I have thousand of json files that containing app data, and declared all the file path dynamically like below:
export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....
As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.
For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.
I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.
Any suggestions please.
Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:
// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}
//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');
const requiredFile = require(myFile);
then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.

Save spreadsheet in a Specific Folder of GDrive

I need help with this Google AdWords script: https://developers.google.com/adwords/scripts/docs/solutions/keyword-performance.
Every time I run this script in Google AdWords account (for my PPC campaign), it creates report (spreadsheet) and save it on my google drive.
I would like to save spreadsheet file to specific subfolder on my google drive (for example Report/Campaign01). I found the article which describes how to save spreadsheet to specific folder. But I don't know how edit this script and use it. Article describing this function is here:
http://www.freeadwordsscripts.com/2014/07/save-file-or-spreadsheet-in-specific.html
Can you help me solve the problem?
Assuming your folder and file names are unique, use can save to a folder this way
function myFunction() {
var folderIterator = DriveApp.getFoldersByName("YOUR_FOLDER_NAME");
while ( folderIterator.hasNext() ) {
var folder = folderIterator.next();
folder_count++;
var spreadsheet = SpreadsheetApp.create("YOUR_SPREADSHEET_NAME");
// INSERT YOUR SPREADSHEET LOGIC HERE.
// YOU CAN CREATE YOUR SHEET HOWEVER YOU WANT,
// SO LONG AS YOU HAVE YOUR ACTIVE SPREADSHEET OBJECT.
// EITHER WAY, IT WILL SAVE TO YOUR ROOT DIRECTORY.
// THIS LOGIC MOVES YOUR FILE BY ID FROM ROOT TO YOUR SELECTED FOLDER.
var files = DriveApp.getRootFolder().getFilesByName("YOUR_SPREADSHEET_NAME");
while ( files.hasNext() ) {
var file = files.next();
if ( spreadsheet.getId() == file.getId() ) {
Logger.log("Found File! Moving...");
folder.addFile(file);
DriveApp.getRootFolder().removeFile(file);
} else {
Logger.log("Wrong File, id[%s]" , file.getId());
}
}
}
}

Batch mkdir using Yeoman

I am creating a Yeoman generator app. I want to create a set of parent directories and each parent directory has the same set of child templates.
Right now I am using the below commands repeatedly to achieve this. Is there a better way to loop over an array and achieve the same?
this.mkdir('app/scss/modules/tables');
this.mkdir('app/scss/modules/navigation');
this.mkdir('app/scss/modules/pagination');
this.copy('_extends.scss', 'app/scss/modules/navigation/_extends.scss');
this.copy('_mixins.scss', 'app/scss/modules/navigation/_mixins.scss');
this.copy('_variables.scss', 'app/scss/modules/navigation/_variables.scss');
this.copy('_extends.scss', 'app/scss/modules/pagination/_extends.scss');
this.copy('_mixins.scss', 'app/scss/modules/pagination/_mixins.scss');
this.copy('_variables.scss', 'app/scss/modules/pagination/_variables.scss');
this.copy('_extends.scss', 'app/scss/modules/tables/_extends.scss');
this.copy('_mixins.scss', 'app/scss/modules/tables/_mixins.scss');
this.copy('_variables.scss', 'app/scss/modules/tables/_variables.scss');
I reckon you'd need two arrays, and at least two loops.
In pseudocode:
dirs = [ ... directories ... ];
files = [ ... files ... ];
for (directory in dirs) {
mkdir (d);
for (file in files) {
copy(file, directory + file);
}
}
If you ever need another directory with all files, or another file to go in all directories you'd just add it to the corresponding array.
Hope you find this useful!
You could also do something like this:
dirs = [ "folder1", "folder2", "etc" ];
files = [ "file1", "file2", "etc" ];
dirs.forEach(function(directory){
this.mkdir(directory);
files.forEach(function(file){
this.copy(file, directory + file);
}.bind(this));
}.bind(this));
...if you want to avoid using a 'for-in' loop, since they're somewhat error-prone.

Resources