Gulp task that write all filenames in another file - gulp

I'm getting started with Gulp and I want to write a task that does the following:
Browse a directory and sub directories to find files using the pattern *.doc.cjsx
Write all the file path found in another file named components.coffee
I'm using gulp-inject. I know that I eventually will need to write to a proper JS file instead of a coffee file (to avoid track a content-generated file on git)
Here is my task: (coffee)
gulp.task 'react-components', ->
gulp.src('src/apis/front/commons/components.coffee')
.pipe(plumber())
.pipe(
inject(
gulp.src(
[
"src/apis/front/commons/button/*.doc.cjsx"
],
read: false
),
{
ignorePath: 'src/apis/front/commons/'
addPrefix: 'require ./' # I need to make the path relative at runtime, I haven't tested this yet because it doesn't generate any output, yet.
starttag: '# inject:components'
}
)
)
.pipe(debug({minimal: false}))
.pipe(gulp.dest('src/apis/front/commons/'))
Here is the components.coffee file:
module.exports = [
require "./button/button.doc.js" # This should be auto-generated inside the starting tag and closing tag.
# inject:components
# endinject
]
Here is the output when I run the task:
[10:15:24] Using gulpfile ~/service/www/hapi-rho/gulpfile.coffee
[10:15:24] Starting 'react-components'...
[10:15:24] gulp-inject 1 files into components.coffee.
[10:15:24] gulp-debug:
cwd: ~/service/www/hapi-rho
base: ~/service/www/hapi-rho/src/apis/front/commons/
path: ~/service/www/hapi-rho/src/apis/front/commons/components.coffee
[10:15:24] gulp-debug: 1 item
[10:15:24] Finished 'react-components' after 31 ms
It seems to be working fine because gulp says that it injected 1 file into components.coffee, if I add another *.doc.cjsx file in the folder then it says it injected two files.
But the content of components.coffee isn't changed, so I'm obviously missing something. Either the starttag isn't found, or it's something else.
Solution:
Here is my solution, based on Sven's answer. The paths have changed since and I'm generating an object instead of an array now, but the principle is the same.
gulp.task 'react-components', ->
gulp.src('src/apis/front/components/components.coffee')
.pipe(plumber())
.pipe(
inject(
gulp.src('src/apis/front/components/**/*.doc.cjsx', read: false),
ignorePath: 'src/apis/front/components/'
starttag: '# inject:components'
endtag: '# endinject'
transform: (filepath, file, i, length) ->
filename = filepath.replace(/cjsx$/, 'js')
suffix = if i + 1 < length then ',' else ''
'"' + filename + '": require ".' + filename + '"'# + suffix
)
)
.pipe(debug(minimal: false))
.pipe gulp.dest('src/apis/front/components/')

First off, you forgot to specify your endtag.
Secondly, you need to provide a transform option. If you don't explicitly provide one the default transform function will be used, which uses the target file type (coffee in your case) and the source file type (cjsx in your case) to choose a sensible transformation for you.
Unfortunately there is no default transformation for the coffee/cjsx pairing available. That means you have to write one yourself.
Here's what that might look like in your case (regular JavaScript, since I'm not well-versed in CoffeeScript):
gulp.task('react-components', function() {
return gulp.src('src/apis/front/commons/components.coffee')
.pipe(plumber())
.pipe(inject(
gulp.src("src/apis/front/commons/button/*.doc.cjsx", {read:false}),
{
ignorePath: 'src/apis/front/commons/',
starttag: '# inject:components',
endtag: '# endinject',
transform: function (filepath, file, i, length) {
return 'require ".' + filepath.replace(/cjsx$/, "js") +
'"' + (i + 1 < length ? ',' : '');
}
}
))
.pipe(debug({minimal: false}))
.pipe(gulp.dest('src/apis/front/commons/'));
});

Related

Avoid generation of build.gradle when using swagger-codegen-plugin

We are using gradle.plugin.org.detoeuf:swagger-codegen-plugin.
We want to change the content of the build.gradle file in the output directory.
We added the gradle.build file to the .swagger-codegen-ignore but BOTH .swagger-codegen-ignore file and gradle.build file are re-created every time we call the swagger task.
Our swagger section looks like this
swagger {
inputSpec = "${project.projectDir}/swagger/backoffice-service-api-swagger.json"
outputDir = file("${project.projectDir}/../backoffice-service-api-client/")
lang = 'java'
additionalProperties = [
'invokerPackage' : 'com.aaa.bbb.backoffice.service',
'modelPackage' : 'com.aaa.bbb.backoffice.service.model',
'apiPackage' : 'com.aaa.bbb.backoffice.service.api',
'dateLibrary' : 'joda',
'groupId' : 'com.aaa.bbb',
'artifactId' : 'backoffice-service-api-client',
'artifactVersion' : '1.0.0',
'hideGenerationTimestamp': 'true',
'dateLibrary' : 'java8'
]
systemProperties = ["apiTests" : "false"]
}
.swagger-codegen-ignore file looks like this -
# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.
# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
build.gradle
You can add ignoreFileOverride option in additionalProperties as below. The files provided in ignoreFileOverride option do not exists in project, then swagger-codegen will generate them and if files exist in project then swagger-codegen will ignore them.
swagger {
inputSpec = "${project.projectDir}/swagger/backoffice-service-api-swagger.json"
outputDir = file("${project.projectDir}/../backoffice-service-api-client/")
lang = 'java'
additionalProperties = [
'invokerPackage' : 'com.aaa.bbb.backoffice.service',
'modelPackage' : 'com.aaa.bbb.backoffice.service.model',
'apiPackage' : 'com.aaa.bbb.backoffice.service.api',
'dateLibrary' : 'joda',
'groupId' : 'com.aaa.bbb',
'artifactId' : 'backoffice-service-api-client',
'artifactVersion' : '1.0.0',
'hideGenerationTimestamp': 'true',
'dateLibrary' : 'java8',
'ignoreFileOverride' : '.swagger-codegen-ignore,build.gradle'
]
systemProperties = ["apiTests" : "false"]
}

Gulp: Recursively copy a file to every sub-directory

I have a file called foo and I want to copy it to every sub-directory.
For example if the current directory structure is:
- files
- A/
- B/
- C/
- D/
- D1/
- D2/
Then after the operation, it should be:
-files
- foo
- A/
- foo
- B/
- foo
- C/
- foo
- D/
- foo
- D1/
- foo
- D2/
- foo
How can I do this using Gulp
Note that I do not know what the sub-directories will be before hand, so it needs to be done dynamically, and the paths cannot be hard-coded.
You can accomplish this using the gulp-multi-dest and glob packages:
const gulp = require('gulp'),
multiDest = require('gulp-multi-dest'),
glob = require('glob');
function copyToAll(done) {
glob('files/**/', (err, matches) => {
if (err) {
console.log('Error', err);
} else {
gulp.src('files/foo').pipe(multiDest(matches));
}
done();
});
}
exports.default = copyToAll;

How to define config file variables?

I have a configuration file with:
{path, "/mnt/test/"}.
{name, "Joe"}.
The path and the name could be changed by a user. As I know, there is a way to save those variables in a module by usage of file:consult/1 in
-define(VARIABLE, <parsing of the config file>).
Are there any better ways to read a config file when the module begins to work without making a parsing function in -define? (As I know, according to Erlang developers, it's not the best way to make a complicated functions in -define)
If you need to store config only when you start the application - you may use application config file which is defined in 'rebar.config'
{profiles, [
{local,
[{relx, [
{dev_mode, false},
{include_erts, true},
{include_src, false},
{vm_args, "config/local/vm.args"}]
{sys_config, "config/local/yourapplication.config"}]
}]
}
]}.
more info about this here: rebar3 configuration
next step to create yourapplication.config - store it in your application folder /app/config/local/yourapplication.config
this configuration should have structure like this example
[
{
yourapplicationname, [
{path, "/mnt/test/"},
{name, "Joe"}
]
}
].
so when your application is started
you can get the whole config data with
{ok, "/mnt/test/"} = application:get_env(yourapplicationname, path)
{ok, "Joe"} = application:get_env(yourapplicationname, name)
and now you may -define this variables like:
-define(VARIABLE,
case application:get_env(yourapplicationname, path) of
{ok, Data} -> Data
_ -> undefined
end
).

Play 2.2 minify javascripts

How to tell Play to compile and create a minified version of my.js? After running 'play stage', I can't find it anywhere under target/scala-2.10/resource_managed or target/scala-2.10/classes.
app/
assets/
javascripts/
main/
some.js
EDIT 1: follow Raunak's comment, I tried this. But the minified js is still not created.
val main = play.Project(appName, appVersion, appDependencies)
.settings(
ebeanEnabled := true,
requireJs += "mainProd.js", // files specified in requireJs will be optimized
requireJsShim += "build.js", // build options for optimizer
requireJsFolder := "js",
routesImport += "se.radley.plugin.salat.Binders._",
templatesImport ++= Seq("org.bson.types.ObjectId"),
resolvers += "Mave2" at "http://repo1.maven.org/maven2",
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
resolvers += Resolver.sonatypeRepo("snapshots"),
parallelExecution in Test := false,
javascriptEntryPoints <<= baseDirectory(base =>
base / "app" / "assets" / "javascripts" / "main" ** "*.js"
)
)
EDIT 2: I found out all of js files in requireJsFolder would not be compiled at all if I gave javascriptEntryPoints.
To run the minify process you can run the following command as per their documentation.
For example, to compile only .js file from the app/assets/javascripts/main directory:
val main = PlayProject(appName, appVersion, mainLang = SCALA).settings(
javascriptEntryPoints <<= baseDirectory(base =>
base / "app" / "assets" / "javascripts" / "main" ** "*.js"
)
)
To check the minify is generated normally during staging
you can use a play dist command to prepare standalone version of application similar to play stage command, it will generate a zip file, instead writing the code into target directory. Then you can check the minified file exists or not

batch convert HTML to Markdown

I have a whole lot of html files that live in one folder. I need to convert these to markdown I found a couple gems out there that does this great one by one.
my question is...
How can I loop though each file in the folder and run the command to convert these to md on a separate folder.
UPDATE
#!/usr/bin/ruby
root = 'C:/Doc'
inDir = File.join(root, '/input')
outDir = File.join(root, '/output')
extension = nil
fileName = nil
Dir.foreach(inDir) do |file|
# Dir.foreach will always show current and parent directories
if file == '.' or item == '..' then
next
end
# makes sure the current iteration is not a sub directory
if not File.directory?(file) then
extension = File.extname(file)
fileName = File.basename(file, extension)
end
# strips off the last string if it contains a period
if fileName[fileName.length - 1] == "." then
fileName = fileName[0..-1]
end
# this is where I got stuck
reverse_markdown File.join(inDir, fileName, '.html') > File.join(outDir, fileName, '.md')
Dir.glob(directory) {|f| ... } will loop through all files inside a directory. For example using the Redcarpet library you could do something like this:
require 'redcarpet'
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true)
Dir.glob('*.md') do |in_filename|
out_filename = File.join(File.dirname(in_filename), "#{File.basename(in_filename,'.*')}.html")
File.open(in_filename, 'r') do |in_file|
File.open(out_filename, 'w') do |out_file|
out_file.write markdown.render(in_file.read)
end
end
end