Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Gulp task to convert analytic vendor JS configs to JSON (#22769)
* add gulp task to generate vendor jsons * finish adding gulp task to convert vendor configs * also generate _fake_.json * add moat canary json * change json spacing to 2 * add comment * add file to forbidden term whitelist * remove unecessary moat canary JSON
- Loading branch information
1 parent
e489418
commit 526eee1
Showing
4 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Copyright 2019 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TODO: @jonathantyng cleanup #22757 | ||
// this is a temporary gulp task while we migrate amp-analytics to use vendor | ||
// JSONs instead of JS files | ||
|
||
const del = require('del'); | ||
const file = require('gulp-file'); | ||
const gulp = require('gulp'); | ||
const {endBuildStep, toPromise, compileJs} = require('./helpers'); | ||
|
||
/** | ||
* Generate all the vendor config JSONs from their respective JS files | ||
* @return {!Promise} | ||
*/ | ||
function generateVendorJsons() { | ||
const startTime = Date.now(); | ||
|
||
const srcDir = 'extensions/amp-analytics/0.1'; | ||
const srcFile = 'vendors.js'; | ||
const tempPath = 'dist/temp-analytics/'; | ||
const destPath = 'extensions/amp-analytics/0.1/vendors/'; | ||
const compileOptions = { | ||
browserifyOptions: { | ||
// allow gulp to import this file after compile | ||
standalone: 'analyticsVendors', | ||
// need to stub self since we are not running this in the browser | ||
insertGlobalVars: { | ||
self: function() { | ||
// use test=true to generate _fake_.json for test-vendors.js to work | ||
return '{ location: {}, AMP_CONFIG: {test: true} }'; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
return compileJs(srcDir, srcFile, tempPath, compileOptions).then(() => { | ||
const promises = []; | ||
const {ANALYTICS_CONFIG} = require('../../dist/temp-analytics/vendors.js'); | ||
|
||
// iterate over each vendor config and write to JSON file | ||
Object.keys(ANALYTICS_CONFIG).forEach(vendorName => { | ||
if (vendorName === 'default') { | ||
return; | ||
} | ||
|
||
// convert object to JSON string with indentation of 2 spaces | ||
const configString = JSON.stringify( | ||
ANALYTICS_CONFIG[vendorName], | ||
null, | ||
2 | ||
); | ||
const fileName = vendorName + '.json'; | ||
|
||
promises.push( | ||
toPromise( | ||
file(fileName, configString, {src: true}).pipe(gulp.dest(destPath)) | ||
) | ||
); | ||
}); | ||
|
||
promises.push(generateCanaryBgJson_(ANALYTICS_CONFIG, destPath)); | ||
|
||
return Promise.all(promises) | ||
.then(() => { | ||
// cleanup temp directory | ||
return del([tempPath]); | ||
}) | ||
.then(() => { | ||
endBuildStep( | ||
'Generated all analytics vendor config JSONs into ', | ||
destPath, | ||
startTime | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
function generateCanaryBgJson_(analyticsConfig, destPath) { | ||
// generate separate canary JSON file for Bolt Guard (BG) | ||
const bgCanaryConfig = Object.assign({}, analyticsConfig['bg'], { | ||
'transport': { | ||
'iframe': | ||
'https://tpc.googlesyndication.com/b4a/experimental/b4a-runner.html', | ||
}, | ||
}); | ||
const bgCanaryConfigStr = JSON.stringify(bgCanaryConfig, null, 2); | ||
|
||
return toPromise( | ||
file('bg.canary.json', bgCanaryConfigStr, {src: true}).pipe( | ||
gulp.dest(destPath) | ||
) | ||
); | ||
} | ||
|
||
module.exports = { | ||
generateVendorJsons, | ||
}; | ||
|
||
generateVendorJsons.description = | ||
'Generate analytics vendor JSON files from their respective JS files'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters