|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// unfortunately we have to modify the file stored in .tmp/extensions.js |
| 7 | +// since our tests run after grunt does its its copy/build step. |
| 8 | +let filePath = path.join(__dirname, '../../../.tmp', 'scripts'); |
| 9 | +let fileName = path.join(filePath, 'extensions.js'); |
| 10 | +// a cache of the first read so we can reset easily |
| 11 | +let cacheFileContents; |
| 12 | + |
| 13 | +let read = (fileName, cb) => { |
| 14 | + return fs.readFile(fileName, 'utf-8', cb); |
| 15 | +}; |
| 16 | + |
| 17 | +let write = (fileName, contents, cb) => { |
| 18 | + return fs.writeFile(fileName, contents, 'utf-8', cb); |
| 19 | +}; |
| 20 | + |
| 21 | +// creates a cache of the .tmp/extensions.js file. |
| 22 | +// NOTE: addExtension() will also cache the first file read if it has never yet been cached. |
| 23 | +exports.cacheCurrentExtensions = () => { |
| 24 | + read(fileName, (readErr, fileContents) => { |
| 25 | + cacheFileContents = fileContents; |
| 26 | + if(readErr) { |
| 27 | + console.log('cacheCurrentExtensions() read error:', readErr); |
| 28 | + return; |
| 29 | + } |
| 30 | + }); |
| 31 | +}; |
| 32 | + |
| 33 | +// to use: a template literal is probably the easiest way to provide JS to inject into the |
| 34 | +// extensions.js file. Please wrap in IIFE as addExtension() may be called multiple times & |
| 35 | +// the file contents will be concatenated: |
| 36 | +// var jsToWrite = ` |
| 37 | +// (function() { |
| 38 | +// // createFromURL test file ${testTime} // (can use interpolation)) |
| 39 | +// window.OPENSHIFT_CONSTANTS.CREATE_FROM_URL_WHITELIST = ['openshift', 'template-dumpster']; |
| 40 | +// })(); |
| 41 | +// `; |
| 42 | +// adding to the extension.js file: |
| 43 | +// addExtension(jsToWrite) |
| 44 | +exports.addExtension = (fileContents) => { |
| 45 | + read(fileName, (readErr, originalFile) => { |
| 46 | + if(!cacheFileContents) { |
| 47 | + cacheFileContents = originalFile; |
| 48 | + } |
| 49 | + if(readErr) { |
| 50 | + console.log('addExtension() read error:', readErr); |
| 51 | + return; |
| 52 | + } |
| 53 | + write(fileName, originalFile + fileContents, (writeErr) => { |
| 54 | + if(writeErr) { |
| 55 | + console.log('addExtension() write error:', writeErr); |
| 56 | + return; |
| 57 | + } |
| 58 | + }); |
| 59 | + }); |
| 60 | +}; |
| 61 | + |
| 62 | +// sets the .tmp/extensions.js file either back to the original contents, or wipes it clean. |
| 63 | +exports.resetExtensions = () => { |
| 64 | + write(fileName, cacheFileContents || '', (writeErr) => { |
| 65 | + if(writeErr) { |
| 66 | + console.log('resetExtensions() write error:', writeErr); |
| 67 | + } |
| 68 | + }); |
| 69 | +}; |
0 commit comments