Joining 2 strings in my Gulp file gives a non-sensical error? - gulp

Using node 16.2.0 on OSX. I new to Gulp.
I want to update the version in my package.json file that will contain the Git hash if on a feature branch, so I have this in my Gulp file
// pkg is defined somewhere else to be my Gulp file
// I have a "semVer" key in my package.json file
const {src, dest} = require('gulp');
const semver = require('semver');
...
const semVer = semver.parse(pkg.semVer);
// Functions getBranch() and getHash() are defined somewhere else,
// and they return the Git branch and hash respectively.
function manageVersion() {
var newVersion = semver.inc(semVer, 'patch');
var hash = "";
if (getBranch().includes('feature')) {
hash = getHash().toString();
newVersion = [newVersion, hash].join('-');
}
src(['../package*.json'])
.pipe(gulp_bump({
version: newVersion
}))
.pipe(dest('./'));
}
exports.manageVersion = manageVersion
I then do this, and get the non-sensical error
$ gulp manageVersion -f gulpfiles/version.js
[16:20:20] Working directory changed to /path/gulpfiles
[16:20:20] Using gulpfile /path/gulpfiles/version.js
[16:20:20] Starting 'manageVersion'...
[16:20:20] Finished 'manageVersion' after 99 ms
/path/node_modules/semver/semver.js:564
if (v1[key] !== v2[key]) {
^
TypeError: Cannot read property 'major' of null
at Function.diff (/path/node_modules/semver/semver.js:564:27)
at /path/node_modules/bump-regex/index.js:66:26
at String.replace (<anonymous>)
at module.exports (/path/node_modules/bump-regex/index.js:54:23)
at DestroyableTransform._transform (/path/node_modules/gulp-bump/index.js:29:5)
at DestroyableTransform.Transform._read (/path/node_modules/gulp-bump/node_modules/readable-stream/lib/_stream_transform.js:184:10)
at DestroyableTransform.Transform._write (/path/node_modules/gulp-bump/node_modules/readable-stream/lib/_stream_transform.js:172:83)
at doWrite (/path/node_modules/gulp-bump/node_modules/readable-stream/lib/_stream_writable.js:428:64)
at writeOrBuffer (/path/node_modules/gulp-bump/node_modules/readable-stream/lib/_stream_writable.js:417:5)
at DestroyableTransform.Writable.write (/path/node_modules/gulp-bump/node_modules/readable-stream/lib/_stream_writable.js:334:11)
I've checked ALL my values for newVersion and hash and they're valid strings. What am I missing?

Turns out I had to trim the string from the getHash() function
hash = getHash().trim();

Related

create Json object in lua

I have attempted to follow the following article and I keep getting module Json module not found.
Convert JSON String to Lua Table?
I am installing Json-lua from luarocks as per the article. Then I am just copying and pasting the code. When I run my lua script I get the following error message.
What am I doing wrong??
root#fusion01:# luarocks install json-lua
Installing https://rocks.moonscript.org/json-lua-0.1-3.rockspec...
Using https://rocks.moonscript.org/json-lua-0.1-3.rockspec... switching to 'build' mode
Cloning into 'json-lua'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 0), reused 4 (delta 0), pack-reused 0
Receiving objects: 100% (7/7), 16.29 KiB | 0 bytes/s, done.
Checking connectivity... done.
Do not use 'module' as a build type. Use 'builtin' instead.
Updating manifest for /usr/local/lib/luarocks/rocks
json-lua 0.1-3 is now built and installed in /usr/local (license: CC)
root#fusion01:# lua sendjson.lua
lua: sendjson.lua:21: module 'json' not found:
no field package.preload['json']
no file '/usr/local/share/lua/5.2/json.lua'
no file '/usr/local/share/lua/5.2/json/init.lua'
no file '/usr/local/lib/lua/5.2/json.lua'
no file '/usr/local/lib/lua/5.2/json/init.lua'
no file '/usr/share/lua/5.2/json.lua'
no file '/usr/share/lua/5.2/json/init.lua'
no file './json.lua'
no file '/usr/local/lib/lua/5.2/json.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.2/json.so'
no file '/usr/lib/lua/5.2/json.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './json.so'
stack traceback:
[C]: in function 'require'
sendjson.lua:21: in main chunk
[C]: in ?
root#fusion01:# cat sendjson.lua
#!/bin/lua
--local raw_json_text = JSON:encode(data)
--local pretty_json_text = JSON:encode_pretty(data)
local json = require "json"
local t = {
["name1"] = "value1",
["name2"] = { 1, false, true, 23.54, "a \021 string" },
name3 = json.null
}
local encode = json:encode (t)
print (encode) --> {"name1":"value1","name3":null,"name2":[1,false,true,23.54,"a \u0015 string"]}
local decode = json:decode( encode )
root#fusion01:#

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;

ES6: Tests pass in browser, fail with phantomJS, "Can't find variable: Reflect"

I am doing an ES6 rewrite for a js library.
class VerbalExpression extends RegExp {
// snipped for brevity
}
/**
* Alias for the constructor
* #return {VerbalExpression} new instance of VerbalExpression
*/
function instantiate() {
return new VerbalExpression();
}
// UMD (Universal Module Definition)
// https://github.com/umdjs/umd
if (typeof module !== 'undefined' && module.exports) { // CommonJS
module.exports = instantiate;
} else if (typeof define === 'function' && define.amd) { // AMD Module
define('VerEx', [], () => VerbalExpression);
} else { // Browser
this.VerEx = instantiate;
}
When I run the tests in my browser, they all pass.
However, when I run the tests in the terminal, I get errors.
❯ npm test
verbal-expressions#0.3.0 test /Users/shreyasminocha/dev/open source/JSVerbalExpressions
grunt test
Running "qunit:files" (qunit) task
Testing test/index.html FFFFFFFFFFFFFFFFFFFF
>> something
>> Message: Died on test #1 global code#file:///Users/shreyasminocha/dev/open%20source/JSVerbalExpressions/test/tests.js:7:5: Can't find variable: Reflect
>> Actual: null
>> Expected: undefined
>> ExtendableBuiltin#file:///Users/shreyasminocha/dev/open%20source/JSVerbalExpressions/dist/verbalexpressions.js:11:31
>> VerbalExpression#file:///Users/shreyasminocha/dev/open%20source/JSVerbalExpressions/dist/verbalexpressions.js:59:130
>> instantiate#file:///Users/shreyasminocha/dev/open%20source/JSVerbalExpressions/dist/verbalexpressions.js:588:32
>> somethingTest#file:///Users/shreyasminocha/dev/open%20source/JSVerbalExpressions/test/tests.js:8:26
...
Warning: 20 tests completed with 20 failed, 0 skipped, and 0 todo.
20 assertions (in 91ms), passed: 0, failed: 20 Use --force to continue.
Note: I am running the tests on compiled es6 code, that is, I run babel before running the tests.
I am guessing this is something to do with PhantomJS. How do I get the tests to pass in the terminal? Am I missing something? Any workarounds?
Stable PhantomJS doesn't support ES6 and is no longer in development, if possible migrate to puppeteer which is heavily inspired by PhantomJS.

Is there a way to get gulp to pass along error messages/position?

I've created a less task:
var gulp = require('gulp'),
less = require('gulp-less');
gulp.task('dktest-less', function () {
gulp.src('dktest/**/*.less')
.pipe(less());
});
and when I run it I get:
(dev) c:\work\dev\dkjs>gulp dktest-less
[16:07:46] Using gulpfile c:\work\dev\dkjs\gulpfile.js
[16:07:46] Starting 'dktest-less'...
[16:07:46] Finished 'dktest-less' after 4.22 ms
events.js:72
throw er; // Unhandled 'error' event
^
Error: Invalid HEX color code
at error (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:248:17)
at Object.Parser.parser.parsers.parsers.entities.color (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:966:29)
at Object.Parser.parser.parsers.parsers.operand (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:1984:34)
at Object.Parser.parser.parsers.parsers.multiplication (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:1882:26)
at Object.Parser.parser.parsers.parsers.addition (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:1911:26)
at Object.Parser.parser.parsers.parsers.expression (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:2006:30)
at Object.Parser.parser.parsers.parsers.value (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:1851:30)
at Object.Parser.parser.parsers.parsers.rule (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:1575:60)
at Object.Parser.parser.parsers.parsers.primary (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:731:76)
at Object.Parser.parser.parsers.parsers.block (c:\work\dev\dkjs\node_modules\gulp-less\node_modules\less\lib\less\parser.js:1492:51)
which isn't very useful (how do you search for an invalid hex value..? ..and where?)
If I run lessc manually it gives me:
(dev) c:\work\dev\dkjs\dktest>lessc diffview.less
SyntaxError: Invalid HEX color code in c:\work\dev\dkjs\dktest\diffview.less on line 821, column 48:
820
821 .white .beautify .data em.s16 { color: #880s }
822
What do I need to do to get this wonderfully informative error message through gulp?
Try adding an error handler function to your code like this:
var gulp = require('gulp'),
less = require('gulp-less');
gulp.task('dktest-less', function () {
gulp.src('dktest/**/*.less')
.pipe(less().on('error', errorHandler));
});
function errorHandler (error) {
console.log(error.toString());
this.emit('end');
}

Cannot find module 'commonjs-utils'?

I'm trying to get a JSON schema validator up and running per:
http://davidwalsh.name/json-validation
I installed node via brew, and installed commonjs-utils, but can't figure out how to require it:
bash-3.2$ npm install commonjs-utils
commonjs-utils#0.1.1 ./node_modules/commonjs-utils
bash-3.2$ node
> require('commonjs-utils')
Error: Cannot find module 'commonjs-utils'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at [object Context]:1:1
at Interface.<anonymous> (repl.js:171:22)
at Interface.emit (events.js:64:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream.<anonymous> (readline.js:73:12)
> var sys = require('sys'), fs = require('fs');
> var validate = require('commonjs-utils/json-schema').validate;
Error: Cannot find module 'commonjs-utils/json-schema'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at [object Context]:1:16
at Interface.<anonymous> (repl.js:171:22)
at Interface.emit (events.js:64:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream.<anonymous> (readline.js:73:12)
Any ideas?
Try this:
require('json-schema');
I tried the same code from "David Walsh" (link) and I got the same error. In my case, I can see the validation function works after changing the line 6 of the original code as the following.
[OLD] var validate = require('commonjs-utils/json-schema').validate;
[NEW] var validate = require('commonjs-utils/lib/json-schema').validate;