Skip to content

Commit

Permalink
chore: update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoffer Åström committed Jun 28, 2018
1 parent 119a843 commit 65bdbf1
Show file tree
Hide file tree
Showing 29 changed files with 234 additions and 102 deletions.
2 changes: 1 addition & 1 deletion commands-common/register/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Register hooks with require",
"main": "src/index.js",
"scripts": {
"lint": "eslint src test"
"lint": "eslint src"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion commands-common/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Simple http server",
"main": "src/index.js",
"scripts": {
"lint": "eslint src test"
"lint": "eslint src"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion commands-common/transform-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Transform files",
"main": "src/index.js",
"scripts": {
"lint": "eslint src test"
"lint": "eslint src"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion commands-common/transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Transform files",
"main": "src/index.js",
"scripts": {
"lint": "eslint src test"
"lint": "eslint src"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions commands-common/transform/src/file-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ class FileCache {
this.cacheDir = findCacheDir({ name: '@after-work.js/transform', create: true });
this.transform = new Map();
}

getCacheFilename(filename) {
const hash = crypto.createHash('md5').update(filename).digest('hex');
return path.join(this.cacheDir, `${hash}.json.gz`);
}

getCacheHash(filename, options) {
const data = JSON.stringify({
filename,
options,
});
return crypto.createHash('md5').update(data).digest('hex');
}

getStringifiedValue(filename) {
const value = this.transform.get(filename);
const str = JSON.stringify(value, null, 2);
return str;
}

setSync(filename, transformItem, options) {
const {
virtualMock,
Expand All @@ -41,6 +45,7 @@ class FileCache {
this.transform.set(filename, transformItem);
this.safeSaveCacheSync(filename);
}

getSync(filename, options = {}) {
const {
ignoreCacheInvalidation = false,
Expand Down Expand Up @@ -70,6 +75,7 @@ class FileCache {
}
return value;
}

safeLoadCacheSync(filename) {
try {
const gz = fs.readFileSync(filename);
Expand All @@ -79,6 +85,7 @@ class FileCache {
return null;
}
}

safeSaveCacheSync(filename) {
try {
const str = this.getStringifiedValue(filename);
Expand Down
6 changes: 3 additions & 3 deletions commands-common/transform/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const fileCache = new FileCache();
function getBabelOpts(filename, argv) {
const { options: { sourceRoot, only, ignore }, babelPluginIstanbul } = argv.babel;
const addCoverage = argv.instrument.testExclude.shouldInstrument(filename);
const plugins = addCoverage ?
[[babelPluginIstanbul, {}]] :
[];
const plugins = addCoverage
? [[babelPluginIstanbul, {}]]
: [];
const sourceMaps = 'both';
const retainLines = true;
return { filename, sourceRoot, plugins, only, ignore, sourceMaps, retainLines };
Expand Down
2 changes: 1 addition & 1 deletion commands-common/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "File utils",
"main": "src/index.js",
"scripts": {
"lint": "eslint src test"
"lint": "eslint src"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion commands/cdp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Run tests with Chrome Debugging Protocol",
"main": "src/index.js",
"scripts": {
"lint": "eslint src test"
"lint": "eslint src"
},
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions commands/cdp/src/mediator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Mediator {
constructor() {
this.events = new Map();
}

bind(DOMStorage) {
DOMStorage.domStorageItemUpdated(({ key, newValue }) => {
if (key !== 'aw') {
Expand All @@ -14,10 +15,12 @@ class Mediator {
this.emit(event.name, event.data);
});
}

emit(event, data) {
const callbacks = this.events.get(event) || [];
callbacks.forEach(cb => cb(data));
}

on(event, callback) {
let callbacks = this.events.get(event);
callbacks = callbacks || [];
Expand Down
35 changes: 33 additions & 2 deletions commands/cdp/src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ const precinct = require('precinct');
// const createHttpServer = require('./http-server');
const createServer = require('@after-work.js/server');
const NYC = require('nyc');
const Mediator = require('./mediator');
const connect = require('./connect');
const utils = require('@after-work.js/utils');
const { deleteTransform } = require('@after-work.js/transform');
const Mediator = require('./mediator');
const connect = require('./connect');


class Runner {
constructor(argv = { chrome: { chromeFlags: [] }, client: {} }) {
Expand All @@ -34,6 +35,7 @@ class Runner {
this.bind();
this.debugging = false;
}

logInfo(mode, testFiles) {
if (this.debugging) {
return this;
Expand All @@ -50,24 +52,28 @@ class Runner {
console.log('\u001b[90mPress\u001b[0m a \u001b[90mto run all tests\u001b[0m');
return this;
}

log(msg) {
if (this.argv.outputReporterOnly) {
return;
}
console.log(msg);
}

logLine(msg) {
if (this.argv.outputReporterOnly) {
return;
}
utils.writeLine(msg);
}

logClearLine() {
if (this.argv.outputReporterOnly) {
return;
}
utils.clearLine();
}

bind() {
if (!this.argv.url) {
this.fail('`options.url` must be specified to run tests');
Expand Down Expand Up @@ -101,13 +107,16 @@ class Runner {
await this.exit(stats.failures);
});
}

on(event, callback) {
this.mediator.on(event, callback);
}

fail(msg) {
console.error(msg);
this.exit(1);
}

pipeOut(Runtime) {
Runtime.exceptionThrown((exception) => {
console.error('[chrome-exception]', exception);
Expand All @@ -128,6 +137,7 @@ class Runner {
console[type](...data);
});
}

pipeNetwork(Network) {
Network.requestWillBeSent((info) => {
this.requests.set(info.requestId, info.request);
Expand All @@ -144,9 +154,11 @@ class Runner {
this.loadError = true;
});
}

launch(options) {
return this.chromeLauncher.launch(options);
}

async setup() {
if (this.argv.chrome.launch) {
this.chrome = await this.launch(this.argv.chrome);
Expand All @@ -164,6 +176,7 @@ class Runner {
this.pipeNetwork(Network);
Network.clearBrowserCache();
}

async navigate() {
if (this.loadError) {
this.fail(`Failed to load the url: ${this.argv.url}`);
Expand All @@ -173,6 +186,7 @@ class Runner {
this.isRunning = true;
await this.client.Page.navigate({ url: this.argv.url });
}

getDependencies(f) {
const cached = this.depMap.get(f);
if (cached) {
Expand All @@ -183,6 +197,7 @@ class Runner {
this.depMap.set(f, deps);
return deps;
}

matchDependencyName(srcName, deps) {
for (const dep of deps) {
const name = path.basename(dep);
Expand All @@ -192,6 +207,7 @@ class Runner {
}
return false;
}

matchDependency(srcFile) {
const cache = this.srcTestMap.get(srcFile);
if (cache) {
Expand All @@ -211,11 +227,13 @@ class Runner {
this.log(`Couldn't find a test file for ${srcFile}`);
return [];
}

async reloadAndRunTests(relativeFiles) {
this.isRunning = true;
const injectAwFiles = `window.awFiles = ${JSON.stringify(relativeFiles)};`;
await this.client.Page.reload({ ignoreCache: true, scriptToEvaluateOnLoad: injectAwFiles });
}

async onWatch(virtualAbs, virtualRel) {
if (this.isRunning) {
return;
Expand All @@ -241,9 +259,11 @@ class Runner {
this.onlyTestFilesBrowser = this.relativeBaseUrlFiles(testFiles);
await this.reloadAndRunTests(this.onlyTestFilesBrowser);
}

watch() {
chokidar.watch(this.argv.watchGlob).on('change', f => this.onWatch(path.resolve(f), f));
}

setupKeyPress() {
if (!this.argv.watch) {
return this;
Expand Down Expand Up @@ -272,6 +292,7 @@ class Runner {
});
return this;
}

autoDetectDebug() {
const exv = process.execArgv.join();
const debug = exv.includes('inspect') || exv.includes('debug');
Expand All @@ -280,18 +301,23 @@ class Runner {
this.debugging = true;
}
}

relativeRootFile(file) {
return path.relative(path.relative(this.argv.url, process.cwd()), file);
}

relativeRootFiles(files) {
return files.map(file => this.relativeRootFile(file));
}

relativeBaseUrlFile(file) {
return path.relative(path.dirname(this.argv.url), path.resolve(file));
}

relativeBaseUrlFiles(files) {
return files.map(file => this.relativeBaseUrlFile(file));
}

setTestFiles() {
this.testFiles = globby.sync(this.argv.glob).map(f => path.resolve(f)).map((f) => {
const parts = f.split('.');
Expand All @@ -309,6 +335,7 @@ class Runner {
.map(f => f.split('\\').join('/'));
return this;
}

setUrl(url) {
if (!/^(file|http(s?)):\/\//.test(url)) {
if (!fs.existsSync(url)) {
Expand All @@ -323,23 +350,27 @@ class Runner {
this.argv.url = url;
return this;
}

maybeCreateServer() {
if (/^(http(s?)):\/\//.test(this.argv.url)) {
createServer(this.argv);
}
return this;
}

async run() {
await this.setup();
await this.navigate();
if (this.argv.watch) {
this.watch();
}
}

async extractCoverage() {
const { result: { value } } = await this.client.Runtime.evaluate({ expression: 'window.__coverage__', returnByValue: true });
return value;
}

async exit(code, force) {
if (!force && this.argv.coverage) {
const coverage = await this.extractCoverage();
Expand Down
1 change: 0 additions & 1 deletion commands/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ yargs
})
.wrap(Math.min(120, yargs.terminalWidth()))
.argv;

Loading

0 comments on commit 65bdbf1

Please sign in to comment.