Skip to content

Commit

Permalink
Merge pull request zotero#1239 from tnajdek/master
Browse files Browse the repository at this point in the history
Enable running tests against babelized code in build/dir
  • Loading branch information
dstillman committed Jun 1, 2017
2 parents a55852e + a59b78e commit 04db119
Show file tree
Hide file tree
Showing 31 changed files with 278 additions and 6,837 deletions.
9 changes: 7 additions & 2 deletions .babelrc
Expand Up @@ -2,12 +2,17 @@
"compact": false,
"presets": [],
"ignore": [
"resource/require.js",
"chrome/content/zotero/include.js",
"resource/tinymce/tinymce.js",
"chrome/content/zotero/xpcom/citeproc.js",
"resource/csl-validator.js",
"resource/react.js",
"resource/react-dom.js"
"resource/react-dom.js",
"resource/bluebird.js",
"test/resource/httpd.js",
"test/resource/mocha.js",
"test/resource/co-mocha.js"
],
"plugins": [
"syntax-flow",
Expand All @@ -25,7 +30,7 @@
[
"transform-async-to-module-method",
{
"module": "resource://zotero/bluebird/bluebird.js",
"module": "resource://zotero/bluebird.js",
"method": "coroutine"
}
],
Expand Down
22 changes: 1 addition & 21 deletions chrome/content/zotero/include.js
Expand Up @@ -7,24 +7,4 @@ var Zotero = Components.classes['@zotero.org/Zotero;1']
.getService(Components.interfaces.nsISupports)
.wrappedJSObject;


var require = (function() {
var { Loader, Require, Module } = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js');
var requirer = Module('/', '/');

var loader = Loader({
id: 'zotero/require',
paths: {
'': 'resource://zotero/',
},
globals: {
document,
console,
navigator,
window,
Zotero
}
});

return Require(loader, requirer);
})();
Components.utils.import('resource://zotero/require.js');
29 changes: 7 additions & 22 deletions chrome/content/zotero/xpcom/zotero.js
Expand Up @@ -62,27 +62,7 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
this.isMac;
this.isWin;
this.initialURL; // used by Schema to show the changelog on upgrades

// Load and configure Bluebird
this.Promise = require('resource://zotero/bluebird/bluebird.js');
this.Promise.config({
warnings: true,
longStackTraces: true,
cancellation: true
});
this.Promise.onPossiblyUnhandledRejection(function (e, promise) {
if (e.name == 'ZoteroPromiseInterrupt' || e.handledRejection) {
return;
}
Zotero.debug('Possibly unhandled rejection:\n\n'
+ (e.message
? e.message + "\n\n" + e.stack.split(/\n/)
// Filter out internal Bluebird calls
.filter(line => !line.includes('bluebird'))
.join('\n')
: e), 1);
throw e;
});
this.Promise = require('resource://zotero/bluebird.js');

this.getActiveZoteroPane = function() {
var win = Services.wm.getMostRecentWindow("navigator:browser");
Expand Down Expand Up @@ -1958,7 +1938,12 @@ Zotero.Prefs = new function(){

// Register observer to handle pref changes
this.register();


// Unregister observer handling pref changes
if (Zotero.addShutdownListener) {
Zotero.addShutdownListener(this.unregister.bind(this));
}

// Process pref version updates
var fromVersion = this.get('prefVersion');
if (!fromVersion) {
Expand Down
16 changes: 2 additions & 14 deletions components/zotero-service.js
Expand Up @@ -152,23 +152,11 @@ var isFirstLoadThisSession = true;
var zContext = null;
var initCallbacks = [];
var zInitOptions = {};
Components.utils.import('resource://zotero/require.js');

ZoteroContext = function() {}
ZoteroContext.prototype = {
require: (target) => {
var { Loader, Require, Module } = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js');
var requirer = Module('/', '/');
var globals = {};

Components.utils.import("resource://gre/modules/Timer.jsm", globals);

var loader = Loader({
id: 'zotero/requireminimal',
globals
});

return (Require(loader, requirer))(target);
},
require,

/**
* Convenience method to replicate window.alert()
Expand Down
100 changes: 89 additions & 11 deletions gulpfile.js
Expand Up @@ -10,25 +10,67 @@ const sass = require('gulp-sass');
const os = require('os');
const glob = require('glob');
const Worker = require('tiny-worker');
const NODE_ENV = process.env.NODE_ENV;
const merge = require('merge-stream');
const tap = require('gulp-tap');
const rename = require("gulp-rename");
const browserify = require('browserify');
const reactPatcher = require('./gulp/gulp-react-patcher');
const NODE_ENV = process.env.NODE_ENV;

const formatDirsforMatcher = dirs => {
return dirs.length > 1 ? `{${dirs.join(',')}}` : dirs[0];
};

// list of folders from where .js files are compiled and non-js files are symlinked
const dirs = [
'chrome', 'components', 'defaults', 'resource', 'resource/web-library'
'chrome',
'components',
'defaults',
'resource',
'resource/web-library',
'test',
'test/resource/chai',
'test/resource/chai-as-promised',
'test/resource/mocha'
];

// list of folders from where all files are symlinked
// list of folders from which all files are symlinked
const symlinkDirs = [
'styles', 'translators'
'styles',
'translators',
];

// list of folders which are copied to the build folder
const copyDirs = [
'test/tests/data' // browser follows symlinks when loading test data
// triggering false-positive test results with mismatched URIs
];

// list of files from root folder to symlink
const symlinkFiles = [
'chrome.manifest', 'install.rdf', 'update.rdf'
];

// these files will be browserified during the build
const browserifyConfigs = [
{
src: 'node_modules/sinon/lib/sinon.js',
dest: 'test/resource/sinon.js',
config: {
standalone: 'sinon'
}
},
{
src: 'node_modules/chai-as-promised/lib/chai-as-promised.js',
dest: 'test/resource/chai-as-promised.js',
config: {
standalone: 'chaiAsPromised'
}
}
];

const jsGlob = `./\{${dirs.join(',')}\}/**/*.js`;
const jsGlobIgnore = `./\{${symlinkDirs.concat(copyDirs).join(',')}\}/**/*.js`;

function onError(err) {
gutil.log(gutil.colors.red('Error:'), err);
Expand All @@ -39,7 +81,25 @@ function onSuccess(msg) {
gutil.log(gutil.colors.green('Build:'), msg);
}

function getJS(source = jsGlob) {
function getBrowserify() {
const streams = browserifyConfigs.map(config => {
return gulp
.src(config.src)
.pipe(tap(file => {
file.contents = browserify(file.path, config.config).bundle();
}))
.pipe(rename(config.dest))
.pipe(gulp.dest('build'));
});

return merge.apply(merge, streams);
}

function getJS(source, sourceIgnore) {
if (sourceIgnore) {
source = [source, '!' + sourceIgnore];
}

return gulp.src(source, { base: '.' })
.pipe(babel())
.pipe(reactPatcher())
Expand All @@ -50,8 +110,8 @@ function getJS(source = jsGlob) {
.pipe(gulp.dest('./build'));
}

function getJSParallel(source = jsGlob) {
const jsFiles = glob.sync(source);
function getJSParallel(source, sourceIgnore) {
const jsFiles = glob.sync(source, { ignore: sourceIgnore });
const cpuCount = os.cpus().length;
const threadCount = Math.min(cpuCount, jsFiles.length);
let threadsActive = threadCount;
Expand Down Expand Up @@ -93,7 +153,8 @@ function getSymlinks() {
const match = symlinkFiles
.concat(dirs.map(d => `${d}/**`))
.concat(symlinkDirs.map(d => `${d}/**`))
.concat([`!{${dirs.join(',')}}/**/*.js`]);
.concat([`!./${formatDirsforMatcher(dirs)}/**/*.js`])
.concat([`!./${formatDirsforMatcher(copyDirs)}/**`]);

return gulp
.src(match, { nodir: true, base: '.', read: false })
Expand All @@ -104,6 +165,15 @@ function getSymlinks() {
.pipe(vfs.symlink('build/'));
}

function getCopy() {
return gulp
.src(copyDirs.map(d => `${d}/**`), { base: '.' })
.on('data', file => {
onSuccess(`[cp] ${file.path.substr(__dirname.length + 1)}`);
})
.pipe(gulp.dest('build/'));
}

function getSass() {
return gulp
.src('scss/*.scss')
Expand All @@ -122,22 +192,30 @@ gulp.task('symlink', ['clean'], () => {
});

gulp.task('js', done => {
getJSParallel(jsGlob).then(() => done());
getJSParallel(jsGlob, jsGlobIgnore).then(() => done());
});

gulp.task('browserify', () => {
getBrowserify();
});

gulp.task('copy', () => {
getCopy();
});

gulp.task('sass', () => {
return getSass();
});

gulp.task('build', ['js', 'sass', 'symlink']);
gulp.task('build', ['js', 'sass', 'symlink', 'browserify', 'copy']);

gulp.task('dev', ['clean'], () => {
var interval = 750;

let watcher = gulp.watch(jsGlob, { interval });

watcher.on('change', function(event) {
getJS(event.path);
getJS(event.path, jsGlobIgnore);
});

gulp.watch('src/styles/*.scss', { interval }, ['sass']);
Expand Down
14 changes: 11 additions & 3 deletions package.json
Expand Up @@ -12,7 +12,7 @@
},
"license": "",
"dependencies": {
"bluebird": "^3.4.6",
"bluebird": "3.4.7",
"zotero-web-library": "next",
"react": "^15.3.2",
"react-dom": "^15.3.2"
Expand All @@ -30,17 +30,25 @@
"babel-plugin-transform-async-to-module-method": "^6.16.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
"babel-preset-react": "^6.16.0",
"browserify": "^14.3.0",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"co-mocha": "^1.2.0",
"del": "^2.2.2",
"glob": "^7.1.2",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-rename": "^1.2.2",
"gulp-sass": "^3.1.0",
"gulp-tap": "^1.0.1",
"gulp-util": "^3.0.7",
"merge-stream": "^1.0.1",
"mocha": "^3.4.2",
"sinon": "^2.3.1",
"sinon-as-promised": "^4.0.3",
"through2": "^2.0.1",
"tiny-worker": "^2.1.1",
"vinyl-buffer": "^1.0.0",
"vinyl-fs": "^2.4.4",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.7.0"
}
}
29 changes: 29 additions & 0 deletions resource/bluebird.js
@@ -0,0 +1,29 @@
'use strict';

var EXPORTED_SYMBOLS = ['Promise'];

var Promise = require('bluebird/promise')();

Promise.config({
warnings: true,
longStackTraces: true,
cancellation: true
});

// TEMP: Only turn on if debug logging enabled?
Promise.onPossiblyUnhandledRejection((e, promise) => {
if (e.name == 'ZoteroPromiseInterrupt' || e.handledRejection) {
return;
}

typeof Zotero !== 'undefined' && Zotero.debug('Possibly unhandled rejection:\n\n'
+ (e.message
? e.message + "\n\n" + e.stack.split(/\n/)
// Filter out internal Bluebird calls
.filter(line => !line.includes('bluebird'))
.join('\n')
: e), 1);
throw e;
});

module.exports = Promise;
18 changes: 2 additions & 16 deletions resource/concurrentCaller.js
Expand Up @@ -24,23 +24,9 @@
*/

EXPORTED_SYMBOLS = ["ConcurrentCaller"];
Components.utils.import('resource://zotero/require.js');

var require = (target) => {
var { Loader, Require, Module } = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js');
var requirer = Module('/', '/');
var globals = {};

Components.utils.import("resource://gre/modules/Timer.jsm", globals);

var loader = Loader({
id: 'zotero/requireminimal',
globals
});

return (Require(loader, requirer))(target);
};

var Promise = require('resource://zotero/bluebird/bluebird.js');
var Promise = require('resource://zotero/bluebird.js');

/**
* Call a fixed number of functions at once, queueing the rest until slots
Expand Down

0 comments on commit 04db119

Please sign in to comment.