From 05e96698bf6f219e52a370161f2b332895411f4c Mon Sep 17 00:00:00 2001 From: Jonathan Olson Date: Tue, 14 Apr 2020 18:51:29 -0600 Subject: [PATCH] Including some aqua-related code snippets, see https://github.com/phetsims/aqua/issues/88 --- js/common/asyncFilter.js | 28 ++++++++++++++++++++++++++ js/common/asyncForEach.js | 25 +++++++++++++++++++++++ js/common/asyncMap.js | 27 +++++++++++++++++++++++++ js/common/cloneMissingRepos.js | 24 +++++++++++++++++++++++ js/common/copyDirectory.js | 36 ++++++++++++++++++++++++++++++++++ js/common/createDirectory.js | 34 ++++++++++++++++++++++++++++++++ js/common/deleteDirectory.js | 34 ++++++++++++++++++++++++++++++++ js/common/isStale.js | 27 +++++++++++++++++++++++++ js/listContinuousTests.js | 10 +++++++--- package.json | 2 ++ 10 files changed, 244 insertions(+), 3 deletions(-) create mode 100644 js/common/asyncFilter.js create mode 100644 js/common/asyncForEach.js create mode 100644 js/common/asyncMap.js create mode 100644 js/common/cloneMissingRepos.js create mode 100644 js/common/copyDirectory.js create mode 100644 js/common/createDirectory.js create mode 100644 js/common/deleteDirectory.js create mode 100644 js/common/isStale.js diff --git a/js/common/asyncFilter.js b/js/common/asyncFilter.js new file mode 100644 index 00000000..13b8cb75 --- /dev/null +++ b/js/common/asyncFilter.js @@ -0,0 +1,28 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Returns an array filtered asynchronously + * + * @author Jonathan Olson + */ + +'use strict'; + +/** + * Returns an array filtered asynchronously + * + * @param {Array.<*>} list + * @param {function({*}):*})} f + * @returns {Promise.>} + */ +const asyncFilter = async ( list, f ) => { + const items = []; + for ( const item of list ) { + if ( await f( item ) ) { + items.push( item ); + } + } + return items; +}; + +module.exports = asyncFilter; diff --git a/js/common/asyncForEach.js b/js/common/asyncForEach.js new file mode 100644 index 00000000..f3662c62 --- /dev/null +++ b/js/common/asyncForEach.js @@ -0,0 +1,25 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Executes async functions on each element in an array. + * + * @author Jonathan Olson + */ + +'use strict'; + +/** + * Executes async functions on each element in an array. + * + * @param {Array.<*>} list + * @param {function({*})})} f + * @returns {Promise} + */ +const asyncForEach = async ( list, f ) => { + let index = 0; + for ( const item of list ) { + await f( item, index++ ); + } +}; + +module.exports = asyncForEach; diff --git a/js/common/asyncMap.js b/js/common/asyncMap.js new file mode 100644 index 00000000..58bd953b --- /dev/null +++ b/js/common/asyncMap.js @@ -0,0 +1,27 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Returns an array mapped asynchronously + * + * @author Jonathan Olson + */ + +'use strict'; + +/** + * Returns an array mapped asynchronously + * + * @param {Array.<*>} list + * @param {function({*}):*})} f + * @returns {Promise.>} + */ +const asyncMap = async ( list, f ) => { + const items = []; + let index = 0; + for ( const item of list ) { + items.push( await f( item, index++ ) ); + } + return items; +}; + +module.exports = asyncMap; diff --git a/js/common/cloneMissingRepos.js b/js/common/cloneMissingRepos.js new file mode 100644 index 00000000..ebe0ae8d --- /dev/null +++ b/js/common/cloneMissingRepos.js @@ -0,0 +1,24 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Clones missing repositories + * + * @author Jonathan Olson + */ + +'use strict'; + +const execute = require( './execute' ); +const winston = require( 'winston' ); + +/** + * Clones missing repositories + * @public + * + * @returns {Promise} - Resolves with no value + */ +module.exports = async () => { + winston.info( 'Cloning missing repos' ); + + return execute( 'bash', [ 'perennial/bin/clone-missing-repos.sh' ], '../' ); +}; diff --git a/js/common/copyDirectory.js b/js/common/copyDirectory.js new file mode 100644 index 00000000..0d1c834a --- /dev/null +++ b/js/common/copyDirectory.js @@ -0,0 +1,36 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Copies a directory (recursively) to another location + * + * @author Jonathan Olson + */ + +'use strict'; + +const ncp = require( 'ncp' ); +const winston = require( 'winston' ); + +/** + * Copies a directory (recursively) to another location + * @public + * + * @param {string} path + * @param {string} location + * @param {Object} [options] + * @returns {Promise} + */ +module.exports = function( pathToCopy, location, options ) { + winston.info( `copying ${pathToCopy} into ${location}` ); + + return new Promise( ( resolve, reject ) => { + ncp.ncp( pathToCopy, location, options, err => { + if ( err ) { + reject( `copyDirectory error: ${err}` ); + } + else { + resolve(); + } + } ); + } ); +}; diff --git a/js/common/createDirectory.js b/js/common/createDirectory.js new file mode 100644 index 00000000..5020d2bb --- /dev/null +++ b/js/common/createDirectory.js @@ -0,0 +1,34 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Creates a directory at the given path + * + * @author Jonathan Olson + */ + +'use strict'; + +const fs = require( 'fs' ); +const winston = require( 'winston' ); + +/** + * Creates a directory at the given path + * @public + * + * @param {string} path + * @returns {Promise} + */ +module.exports = function( path ) { + winston.info( `Creating directory ${path}` ); + + return new Promise( ( resolve, reject ) => { + fs.mkdir( path, err => { + if ( err ) { + reject( `createDirectory: ${err}` ); + } + else { + resolve(); + } + } ); + } ); +}; diff --git a/js/common/deleteDirectory.js b/js/common/deleteDirectory.js new file mode 100644 index 00000000..c302e3a3 --- /dev/null +++ b/js/common/deleteDirectory.js @@ -0,0 +1,34 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Deletes a path recursively + * + * @author Jonathan Olson + */ + +'use strict'; + +const rimraf = require( 'rimraf' ); +const winston = require( 'winston' ); + +/** + * Deletes a path recursively + * @public + * + * @param {string} path - The path to delete recursively + * @returns {Promise} + */ +module.exports = function( path ) { + winston.info( `Deleting directory ${path}` ); + + return new Promise( ( resolve, reject ) => { + rimraf( path, err => { + if ( err ) { + reject( `rimraf: ${err}` ); + } + else { + resolve(); + } + } ); + } ); +}; diff --git a/js/common/isStale.js b/js/common/isStale.js new file mode 100644 index 00000000..983cb75d --- /dev/null +++ b/js/common/isStale.js @@ -0,0 +1,27 @@ +// Copyright 2020, University of Colorado Boulder + +/** + * Asynchronously checks whether a repo is not up-to-date with origin/master + * + * @author Jonathan Olson + */ + +'use strict'; + +const getRemoteBranchSHAs = require( './getRemoteBranchSHAs' ); +const gitRevParse = require( './gitRevParse' ); + +/** + * Asynchronously checks whether a repo is not up-to-date with origin/master + * @public + * + * @param {string} repo - The repository name + * @returns {Promise.} + * @rejects {ExecuteError} + */ +module.exports = async function( repo ) { + const currentSHA = await gitRevParse( repo, 'master' ); + const remoteSHA = ( await getRemoteBranchSHAs( repo ) ).master; + + return currentSHA !== remoteSHA; +}; diff --git a/js/listContinuousTests.js b/js/listContinuousTests.js index f2daaa5e..6af258fa 100644 --- a/js/listContinuousTests.js +++ b/js/listContinuousTests.js @@ -23,6 +23,7 @@ const interactiveDescriptionRepos = getRepoList( 'interactive-descriptions' ); * {Array.} test * {string} type * {string} [url] + * {string} [repo] * {string} [queryParameters] * {boolean} [es5] * {string} [brand] @@ -40,7 +41,8 @@ const tests = []; tests.push( { test: [ repo, 'build', 'phet' ], type: 'build', - brand: 'phet' + brand: 'phet', + repo: repo } ); } ); @@ -49,7 +51,8 @@ phetioRepos.forEach( repo => { tests.push( { test: [ repo, 'build', 'phet-io' ], type: 'build', - brand: 'phet-io' + brand: 'phet-io', + repo: repo } ); } ); @@ -58,7 +61,8 @@ repos.forEach( repo => { if ( fs.existsSync( `../${repo}/Gruntfile.js` ) ) { tests.push( { test: [ repo, 'lint' ], - type: 'lint' + type: 'lint', + repo: repo } ); } } ); diff --git a/package.json b/package.json index 6ab7162e..cb724e63 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,10 @@ "lodash": "^4.17.10", "mimelib": "~0.2.19", "minimist": "^1.1.1", + "ncp": "^2.0.0", "qunit": "~2.4.1", "request": "^2.55.0", + "rimraf": "^2.5.4", "rsync": "^0.6.1", "request-promise-native": "^1.0.5", "thread-sleep": "^2.0.0",