Batch mkdir using Yeoman - 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.

Related

Creating multiple similar genrules and using their output

I have a genrule that looks like the below. It basically runs a simple go template tool that gets a resource name, a json file and template and outputs the rendered file. I have a bunch of resources that all need to be in separate files and ultimately packaged into a container.
resources = ["foo", "bar"]
[genrule(
name = "generate_" + resource + "_config",
srcs = [
"//some:tool:config.tmpl",
"//some:json",
],
outs = [resource + ".hcl"],
cmd = "$(location //some/tool:template) -resource " + resource + " -json_path=$(location //some:json) -template=$(location //some/tool:config.tmpl) -out=$#",
tools = [
"/some/tool:template",
],
) for resource in resources]
The above will generate a few rules named generate_foo_config and generate_bar_config and the files output correctly. I however cannot figure out how to use each one without specifying them directly in a filegroup or pkg_tar rule without enumerating each one. I would like to be able to add a new thing to the resources variable, and have it automatically included in the filegroup or tar for use in a build rule later. Is this possible?
Use a list comprehension, like you've got creating the genrules. Something like this:
pkg_tar(
name = "all_resources",
srcs = [":generate_" + resource + "_config" for resource in resources],
)
You can also put the list in a variable in the BUILD file to use it multiple times:
all_resources = [":generate_" + resource + "_config" for resource in resources]
pkg_tar(
name = "all_resources",
srcs = all_resources,
)
filegroup(
name = "resources_filegroup",
srcs = all_resources,
)

gulp copy source files from another drive

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) );

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.

Podspec - Exclude all but a subfolder

I have a structure like this
target_files/
├──target1/
├──target2/
└──target3/
And I want to include only "target2" for example and exclude the other targets. How I write the spec.exclude_files?
I found this example for excluding files, but I can't understand how to write it for whole folders.
spec.exclude_files = '_private/**/*.{h,m}'
spec.source_files = [ 'target_files/**' ]
spec.exclude_files = [ 'target_files/target1/**', 'target_files/target3/**' ]
or for the case you ask about, more simply:
spec.source_files = [ 'target_files/target2/**' ]
Reference
CocoaPods exclude_files
[CocoaPods]
Pattern: ** - Matches directories recursively.
s.exclude_files = 'target_files/target1/', 'target_files/target3/'
The opposite is source_files[About]

What's the recommended way to copy multiple dotfiles with yeoman?

I am building a yeoman generator for a fairly typical node app:
/
|--package.json
|--.gitignore
|--.travis.yml
|--README.md
|--app/
|--index.js
|--models
|--views
|--controllers
In the templates folder of my yeoman generator, I have to rename the dotfiles (and the package.json) to prevent them from being processed as part of the generator:
templates/
|--_package.json
|--_gitignore
|--_travis.yml
|--README.md
|--app/
|--index.js
|--models
|--views
|--controllers
I see a lot of generators that copy dotfiles individually manually:
this.copy('_package.json', 'package.json')
this.copy('_gitignore', '.gitignore')
this.copy('_gitattributes', '.gitattributes')
I think it's a pain to manually change my generator code when I add new template files. I would like to automatically copy all files in the /templates folder, and rename the ones that are prefixed with _.
What's the best way to do this?
If I were to describe my intention in imaginary regex, this is what it would look like:
this.copy(/^_(.*)/, '.$1')
ths.copy(/^[^_]/)
EDIT
This is the best I can manage:
this.expandFiles('**', { cwd: this.sourceRoot() }).map(function() {
this.copy file, file.replace(/^_/, '.')
}, this);
I found this question through Google as I was looking for the solution, and then I figured it out myself.
Using the new fs API, you can use globs!
// Copy all non-dotfiles
this.fs.copy(
this.templatePath('static/**/*'),
this.destinationRoot()
);
// Copy all dotfiles
this.fs.copy(
this.templatePath('static/.*'),
this.destinationRoot()
);
Adding to #callumacrae 's answer: you can also define dot: true in the globOptions of copy(). That way a /** glob will include dotfiles. Example:
this.fs.copy(
this.templatePath('files/**'),
this.destinationPath('client'),
{ globOptions: { dot: true } }
);
A list of available Glob options can be found in the README of node-glob.
Just got this working for me: the globOptions needs to be in the fifth argument:
this.fs.copyTpl(
this.templatePath('sometemplate/**/*'),
this.destinationPath(this.destinationRoot()),
null,
null,
{ globOptions: { dot: true } }
);
If you don't want to use templates that starts with a dot, you can use the dive module to achieve something identical:
var templatePath = this.templatePath('static-dotfiles');
var destinationRoot = this.destinationRoot();
dive(templatePath, {all: true}, function (err, file, stat) {
if (err) throw err;
this.fs.copy(
file,
(destinationRoot + path.sep + path.relative(templatePath, file))
.replace(path.sep + '_', path.sep + '.')
);
}.bind(this));
where static-dotfiles is the name of your template folder for dotfiles where _ replaces . in filenames (ex: _gitignore).
Don't forget to add a requirement to dive at the top of your generator with
var dive = require('dive');
Of course, this also works for copyTpl.
Note that all subparts of paths that starts with a _ will be replaced by a . (ex: static-dotfiles/_config/_gitignore will be generated as .config/.gitignore)

Resources