Skip to content

Commit

Permalink
Merge pull request #24 from fs-webdev/from-fs
Browse files Browse the repository at this point in the history
Merging changes that were made at FamilySearch.
  • Loading branch information
intervalia authored Oct 24, 2017
2 parents e5526b5 + 7096d0f commit 08b9d63
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 14 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,20 @@ gulp.task('assemble', function() {

```js
gulp.task('watch', function() {
compasm.watch('./assembly.json', gulp.series('assemble'));
compasm.watch('./assembly.json', gulp.series('assemble'));
});
```

## Use with gulp-changed for incremental builds

`gulp-component-assembler` provides it's own changed function that should be passed to `gulp-changed` as an option. This will properly detect whether the files in the assembly.json have changed.

```js
gulp.task('assemble', function() {
return gulp.src('./assembly.json')
.pipe(changed(dest, {hasChanged: compasm.hasChanged}))
.pipe(compasm.assemble())
.pipe(gulp.dest('./dist'))
});
```

Expand Down Expand Up @@ -344,7 +357,7 @@ Then the component output file `myComponent.js` would include the contents of th
>If the user does not provide a value for `localeFileName` then `gulp-component-assembler` attempts to use the value of `'strings'`. If files using that value do not exist then it attempts to use the value of the containing folder.
>One locale file is needed per language. *At this time, `2015-08-03`, we only support the two letter ([ISO-639-1](http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)) locale names, like `'en'`, `'fr'`, `'de'`, etc.*
>One locale file is needed per language. *At this time, `2016-05-17`, we support the two letter ([ISO-639-1](http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)) locale names, like `'en'`, `'fr'`, `'de'`, etc. We also support four letter codes with fallback to two letter codes. For example, `'zh-cn'` and `'en-us'` are valid codes that fallback to `'zh'` and `'en'`.*
>___TODO: Provide more information here___
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-component-assembler",
"version": "4.0.0",
"version": "4.1.0",
"author": "Michael G Collins <[email protected]>",
"contributors": [
{
Expand Down
52 changes: 52 additions & 0 deletions src/hasChanged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');

function compareLastModifiedTime(stream, cb, basePath, sourceFile, targetPath) {
if (sourceFile.isNull()) {
cb(null, sourceFile);
return;
}

fs.stat(targetPath, function(err, targetStat) {
if (err) {
if (err.code === 'ENOENT') {
// try reading the file using the oldDest path
var target = path.join(basePath, path.basename(basePath)+'.js');

if (target !== targetPath) {
return compareLastModifiedTime(stream, cb, basePath, sourceFile, target);
}
else {
stream.push(sourceFile);
}
}
else {
stream.emit('error', new gutil.PluginError('gulp-changed', err, {
fileName: sourceFile.path
}));

stream.push(sourceFile);
}
}

// file changed
else if (sourceFile.stat.mtime > targetStat.mtime) {
stream.push(sourceFile);
}

cb();
});
}

/**
* Use in conjunction with gulp-changed to properly compare the assembly.json file
* to it's output file.
* @see https://github.com/sindresorhus/gulp-changed#haschanged
*/
module.exports = function (stream, cb, sourceFile, targetPath) {
var basePath = path.dirname(targetPath);
targetPath = path.join(path.dirname(basePath), path.basename(basePath)+'.js');

compareLastModifiedTime(stream, cb, basePath, sourceFile, targetPath);
};
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var externalFuncs = require("./externalFunctions");
var PluginError = require('gulp-util').PluginError;
var plugin = require("./plugin");
var PLUGIN_NAME = require("./pluginName");
var watcher = require("./watcher");

function assemble(options) {
"use strict";
Expand Down Expand Up @@ -64,5 +63,6 @@ module.exports = {
"assemble": assemble,
"loadPlugin": plugin.load,
"pluginTypes": plugin.types,
"watch": watcher.watch
"watch": require("./watcher").watch,
"hasChanged": require("./hasChanged")
};
9 changes: 8 additions & 1 deletion src/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ function processLocales(baseLocalePath, localeFileName, assemblyName, options) {
" var locale, language, i, len = langKeys.length, lang = {};\n"+
" locales = (typeof locales === 'string' ? [locales] : locales);\n"+
" for (i = 0; i < locales.length; i++) {\n"+
" language = locales[i].split('-')[0];\n"+
" language = locales[i];\n"+
" if (validLocales.indexOf(language) !== -1) {\n"+
" locale = language;\n"+
" break;\n"+
" } else {\n"+
" //Check if the first two characters are a valid locale\n"+
" language = locales[i].split('-')[0];\n"+
" if (validLocales.indexOf(language) !== -1) {\n"+
" locale = language;\n"+
" break;\n"+
" }\n"+
" }\n"+
" }\n"+
" locale = locale || '" + options.locale + "';\n";
Expand Down
4 changes: 3 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var types = {
"BEFORE_JS_FILE": "BEFORE_JS_FILE", // before each js file is processed
"AFTER_JS_FILE": "AFTER_JS_FILE", // after each js file is processed
"AFTER_ASSEMBLY": "AFTER_ASSEMBLY", // after each assembly file is processed
"AFTER": "AFTER" // after the entire file is processed
"AFTER": "AFTER", // after the entire file is processed

"JS_TRANSPILE": "JS_TRANSPILE" // for each js file
};

var plugins = {};
Expand Down
19 changes: 12 additions & 7 deletions src/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ var PLUGIN_NAME = require("./pluginName");

function processOneScript(scriptPath, includeName, options, pluginParams) {
var contents = "/*\n * Included File: " + includeName + "\n */\n";
var temp;

if (!fs.existsSync(scriptPath)) {
return contents + "console.warn('" + scriptPath + " does not match any file');";
}
var temp, fileContents;

// Process any BEFORE_JS_FILE plug-ins
temp = plugin.process(plugin.types.BEFORE_JS_FILE, pluginParams);
if (temp) {
contents += temp + "\n\n";
}

contents += fs.readFileSync(scriptPath, {"encoding": "utf-8"}).replace(/[\n\r]/g, "\n");
if (!fs.existsSync(scriptPath)) {
contents += "console.warn('" + scriptPath + " does not match any file');";
}
else {
fileContents = fs.readFileSync(scriptPath, {"encoding": "utf-8"}).replace(/[\n\r]/g, "\n");

// Process any JS_TRANSPILE plug-ins
pluginParams.fileContents = fileContents;
contents += plugin.process(plugin.types.JS_TRANSPILE, pluginParams) || fileContents;
}

// Process any AFTER_JS_FILE plug-ins
temp = plugin.process(plugin.types.AFTER_JS_FILE, pluginParams);
if (temp) {
contents += temp;
contents += '\n\n' + temp;
}

return contents;
Expand Down
15 changes: 15 additions & 0 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ var fileWatcher;
// the associated assemblies
var watchedFiles = {};

// Chokidar will fire a changed event twice for a file sometimes, possibly due to the
// system creating a backup of the file and touching it. We'll create a cache of when
// the last time the file was touched and prevent it from firing the 'alltouched' event
// if the file was changed within the last half second
// @see https://github.com/paulmillr/chokidar/issues/298
var lastChanged = {};

// Boolean state that plugins and other files can use to determine if they can add files or
// assemblies to be watched during the assemble task
var watchStarted = false;
Expand Down Expand Up @@ -129,6 +136,14 @@ function fileChanged(event, file) {
var watchedPaths = fileWatcher.getWatched();
var promises = [];

// only process files that have changed more than a 0.5 seconds ago
if (lastChanged[file] && new Date() - lastChanged[file] < 500) {
return;
}
else {
lastChanged[file] = new Date();
}

// Assembly.json file was changed
if (assemblyRegex.test(file)) {

Expand Down
30 changes: 30 additions & 0 deletions test/specs/locales.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ describe('\n Testing the file locales.js', function () {
should.deepEqual(getLang('en'), getLang(acceptLanguage));
});

it('getLang() should return 4-letter codes when supported', function() {
var langKeys, langs, validLocales, lang, getLang;

var acceptLanguage = ['ca-de', 'fj-ja', 'zh-cn', 'de'];

/*
will add to scope:
variables: langKeys, langs, validLocales, lang
functions: getLang()
*/
eval(locales.process(baseLocalePath, localeFileName, assemblyName, options));

should.deepEqual(getLang('zh-cn'), getLang(acceptLanguage));
});

it('getLang() should fallback to a 2-letter code when the 4-letter code is unsupported', function() {
var langKeys, langs, validLocales, lang, getLang;

var acceptLanguage = ['ca-de', 'fj-ja', 'zh', 'de'];

/*
will add to scope:
variables: langKeys, langs, validLocales, lang
functions: getLang()
*/
eval(locales.process(baseLocalePath, localeFileName, assemblyName, options));

should.deepEqual(getLang('zh-cn'), getLang(acceptLanguage));
});

it('lang should use "window.locale" if it exists', function() {
var langKeys, langs, validLocales, lang, getLang;

Expand Down

0 comments on commit 08b9d63

Please sign in to comment.