Skip to content

Commit

Permalink
🏗 Update CircleCI hash for .karma-cache (#32312)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha committed Jan 30, 2021
1 parent b74e527 commit ca438ad
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
7 changes: 5 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ commands:
steps:
- save_cache:
name: 'Save Karma Cache'
key: karma-cache-<<parameters.cache_name>>-{{ checksum "package-lock.json" }}
key: karma-cache-<<parameters.cache_name>>-{{ checksum ".karma-cache-hash" }}
paths:
- .karma-cache
restore_karma_cache:
parameters:
cache_name:
type: string
steps:
- run:
name: 'Compute Karma Cache Hash'
command: node ./build-system/common/compute-karma-cache-hash.js
- restore_cache:
name: 'Restore Karma Cache'
key: karma-cache-<<parameters.cache_name>>-{{ checksum "package-lock.json" }}
key: karma-cache-<<parameters.cache_name>>-{{ checksum ".karma-cache-hash" }}
setup_vm:
steps:
- checkout
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
.g4ignore
build/
.karma-cache
.karma-cache*
.amp-build
c
/dist
Expand Down
24 changes: 16 additions & 8 deletions build-system/common/browserify-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ const globby = require('globby');
const {dotWrappingWidth} = require('./logging');

/**
* Used for persistent babel caching during tests. Created using a hash object
* that includes the repo package lockfile and various parts of the build-system
* so that the cache is invalidated if any of them changes, and files are
* retransformed.
* @return {function}
* The hash object includes the repo package lockfile and various parts of the
* build-system so that the cache is invalidated if any of them changes, and
* files are retransformed.
* @return {!Object}
*/
function getPersistentBrowserifyCache() {
let wrapCounter = 0;
function getHashObject() {
const createHash = (input) =>
crypto.createHash('sha1').update(input).digest('hex');
const hashObject = {
Expand All @@ -41,14 +39,23 @@ function getPersistentBrowserifyCache() {
])
.map((f) => createHash(fs.readFileSync(f))),
};
return hashObject;
}

/**
* Used for persistent babel caching during tests.
* @return {function}
*/
function getPersistentBrowserifyCache() {
let wrapCounter = 0;
const logger = () => {
process.stdout.write('.');
if (++wrapCounter >= dotWrappingWidth) {
wrapCounter = 0;
process.stdout.write('\n');
}
};
const cache = browserifyPersistFs('.karma-cache', hashObject, logger);
const cache = browserifyPersistFs('.karma-cache', getHashObject(), logger);
cache.gc(
{maxAge: 1000 * 60 * 60 * 24 * 7}, // Refresh cache if more than a week old
() => {} // swallow errors
Expand All @@ -57,5 +64,6 @@ function getPersistentBrowserifyCache() {
}

module.exports = {
getHashObject,
getPersistentBrowserifyCache,
};
30 changes: 30 additions & 0 deletions build-system/common/compute-karma-cache-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node
/**
* Copyright 2021 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.
*/

const fs = require('fs-extra');
const {getHashObject} = require('./browserify-cache');

/**
* Writes the browserify hashObject representing all the test files we care
* about to a file. Used during CI to determine when to refresh .karma-cache.
*/
function main() {
const hashObject = getHashObject();
fs.writeJSONSync('.karma-cache-hash', hashObject, {spaces: 2});
}

main();
2 changes: 1 addition & 1 deletion build-system/tasks/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const ROOT_DIR = path.resolve(__dirname, '../../');
async function clean() {
const pathsToDelete = [
'.amp-build',
'.karma-cache',
'.karma-cache*',
'build',
'build-system/server/new-server/transforms/dist',
'deps.txt',
Expand Down

0 comments on commit ca438ad

Please sign in to comment.