Skip to content

Commit

Permalink
make karma optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jun 2, 2018
1 parent e2cbb7a commit 2a716a3
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 55 deletions.
39 changes: 39 additions & 0 deletions packages/electrode-archetype-opt-karma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Electrode Archetype Option Karma

Allow [Electrode](https://github.com/electrode-io/electrode) apps to optionally choose to skip installing dependencies for karma.

The app can skip installing karma dependencies by adding a file `archetype/config/index.js` or `archetype/config.js` and set:

```js
module.exports = {
options: {
karma: false
}
};
```

If nothing is set, then default is to install.

# Usage

This module generally is included by other Electrode modules. An Electrode app should not need to install this directly.

## Install

In `package.json`:

```js
{
"optionalDependencies": {
"electrode-archetype-opt-karma": "^1.0.0"
}
}
```

And setup archetype config accordingly.

## License

Copyright (c) 2016-present, WalmartLabs

Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
40 changes: 40 additions & 0 deletions packages/electrode-archetype-opt-karma/optional-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";

const Path = require("path");

const cwd = process.env.PWD || process.cwd();

function findAppDir() {
if (cwd.indexOf("node_modules") > 0) {
const splits = cwd.split("node_modules");
return Path.dirname(Path.join(splits[0], "x")); // remove trailing slash
}
return cwd;
}

const appDir = findAppDir();
const myPkg = require("./package.json");

if (cwd === appDir) {
try {
const appPkg = require(Path.join(appDir, "package.json"));
if (myPkg.name === appPkg.name) {
process.exit(0);
}
} catch (e) {}
}

const name = myPkg.name;

try {
const config = require(Path.join(appDir, "archetype/config"));
const lib = config && config.options && config.options.karma;
if (lib === false) {
console.log(
`${name}: skipping install because archetype config set options.karma to false`
);
process.exit(1);
}
} catch (e) {}

process.exit(0);
36 changes: 36 additions & 0 deletions packages/electrode-archetype-opt-karma/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "electrode-archetype-opt-karma",
"version": "1.0.0",
"description": "Electrode Karma Optional Archetype",
"main": "index.js",
"homepage": "http://www.electrode.io",
"repository": {
"type": "git",
"url": "https://github.com/electrode-io/electrode.git"
},
"bugs": {
"url": "https://github.com/electrode-io/electrode/issues"
},
"license": "Apache-2.0",
"scripts": {
"preinstall": "node optional-check.js"
},
"dependencies": {
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.1.1",
"karma-coverage": "^1.1.1",
"karma-firefox-launcher": "^1.0.0",
"karma-ie-launcher": "^1.0.0",
"karma-intl-shim": "^1.0.3",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.2",
"karma-phantomjs-launcher": "^1.0.2",
"karma-phantomjs-shim": "^1.4.0",
"karma-safari-launcher": "^1.0.0",
"karma-sonarqube-unit-reporter": "0.0.14",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.26",
"karma-webpack": "^2.0.2",
"phantomjs-prebuilt": "^2.1.13"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Path = require("path");
const optionalRequire = require("optional-require")(require);
const archetypeOptions = optionalRequire(Path.resolve("archetype", "config"), { default: {} });
const userConfig = Object.assign({}, optionalRequire(Path.resolve("archetype/config")));

const devPkg = require("../package.json");
const devDir = Path.join(__dirname, "..");
Expand Down Expand Up @@ -31,8 +31,9 @@ module.exports = {
devDir,
devPkg,
devRequire,
webpack: xenvConfig(webpackConfigSpec, archetypeOptions.webpack),
karma: xenvConfig(karmaConfigSpec, archetypeOptions.karma),
webpack: xenvConfig(webpackConfigSpec, userConfig.webpack),
karma: xenvConfig(karmaConfigSpec, userConfig.karma),
jest: Object.assign({}, userConfig.jest),
config: Object.assign(
{},
{
Expand All @@ -44,6 +45,6 @@ module.exports = {
webpack: `${configDir}/webpack`,
jest: `${configDir}/jest`
},
archetypeOptions.configPaths
userConfig.configPaths
)
};
17 changes: 1 addition & 16 deletions packages/electrode-archetype-react-app-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,6 @@
"jest": "^22.4.3",
"json-loader": "^0.5.7",
"jsonfile": "^2.2.2",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.1.1",
"karma-coverage": "^1.1.1",
"karma-firefox-launcher": "^1.0.0",
"karma-ie-launcher": "^1.0.0",
"karma-intl-shim": "^1.0.3",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.2",
"karma-phantomjs-launcher": "^1.0.2",
"karma-phantomjs-shim": "^1.4.0",
"karma-safari-launcher": "^1.0.0",
"karma-sonarqube-unit-reporter": "0.0.14",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.26",
"karma-webpack": "^2.0.2",
"loader-utils": "^1.1.0",
"lodash": "^4.13.1",
"mkdirp": "^0.5.1",
Expand All @@ -95,7 +80,6 @@
"optimize-css-assets-webpack-plugin": "^3.2.0",
"optional-require": "^1.0.0",
"penthouse": "^0.11.13",
"phantomjs-prebuilt": "^2.1.13",
"postcss-cssnext": "^2.7.0",
"postcss-import": "^9.1.0",
"postcss-loader": "^0.11.1",
Expand Down Expand Up @@ -129,6 +113,7 @@
},
"optionalDependencies": {
"electrode-archetype-opt-inferno": "^0.2.0",
"electrode-archetype-opt-karma": "../electrode-archetype-opt-karma",
"electrode-archetype-opt-react": "^1.0.0"
},
"devDependencies": {
Expand Down
77 changes: 48 additions & 29 deletions packages/electrode-archetype-react-app/arch-clap.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ function startAppServer(options) {
*
*/

// quick tips name naming and invoking tasks:
// - task name without . or - are primary tasks
// - task name starts with . are hidden in help output
// - when invoking tasks in [], starting name with ? means optional (ie: won't fail if task not found)

function makeTasks() {
const checkFrontendCov = minimum => {
if (typeof minimum === "string") {
Expand Down Expand Up @@ -671,52 +676,33 @@ Individual .babelrc files were generated for you in src/client and src/server
"test-cov": [
".build.test.client.babelrc",
".build.test.server.babelrc",
"karma-test-frontend-cov",
"jest-test-frontend-cov",
"?.karma.test-frontend-cov",
".jest.test-frontend-cov",
"test-server-cov"
],
].filter(x => x),
"test-dev": ["test-frontend-dev", "test-server-dev"],

"test-watch": () =>
exec(`pgrep -fl "webpack-dev-server.*${archetype.webpack.testPort}"`)
.then(() => `test-frontend-dev-watch`)
.catch(() => `test-watch-all`),

"test-frontend": mkCmd(`karma start`, quote(karmaConfig("karma.conf.js")), `--colors`),

"test-frontend-ci": mkCmd(
`karma start`,
quote(karmaConfig("karma.conf.coverage.js")),
`--colors`
),
"test-frontend": ["?.karma.test-frontend"],
"test-frontend-ci": ["?.karma.test-frontend-ci"],

"karma-test-frontend-cov": () => {
if (shell.test("-d", "test")) {
logger.info("\nRunning Karma unit tests:\n");
return mkCmd(`~$karma start`, quote(karmaConfig("karma.conf.coverage.js")), `--colors`);
}
return undefined;
},

"jest-test-frontend-cov": () => {
".jest.test-frontend-cov": () => {
const srcJestFiles = glob.sync(`${Path.resolve(AppMode.src.dir)}/**/\*.{test,spec}.{js,jsx}`);

if (shell.test("-d", "_test_") || srcJestFiles.length > 0) {
logger.info("Running jest unit tests");
return mkCmd(`~$jest`, `--config ${archetype.config.jest}/jest.config.js`);
}
return undefined;
},

"test-frontend-dev": () =>
exec(`pgrep -fl "webpack-dev-server.*${archetype.webpack.testPort}"`)
.then(() => exec(`karma start`, quote(karmaConfig("karma.conf.dev.js")), `--colors`))
.catch(() => `test-frontend`),
"test-frontend-dev": ["?.karma.test-frontend-dev"],

"test-frontend-dev-watch": mkCmd(
`karma start`,
quote(karmaConfig("karma.conf.watch.js")),
`--colors --browsers Chrome --no-single-run --auto-watch`
),
"test-frontend-dev-watch": ["?.karma.test-frontend-dev-watch"],

"test-server-cov": () => {
if (shell.test("-d", "test/server")) {
Expand Down Expand Up @@ -803,7 +789,7 @@ Individual .babelrc files were generated for you in src/client and src/server
}

if (Fs.existsSync(Path.resolve(AppMode.src.client, "dll.config.js"))) {
tasks = Object.assign(tasks, {
Object.assign(tasks, {
"build-dist-dll": {
dep: [".mk-dll-dir", ".mk-dist-dir", ".production-env"],
task: () =>
Expand All @@ -813,6 +799,39 @@ Individual .babelrc files were generated for you in src/client and src/server
});
}

if (archetype.options.karma !== false) {
Object.assign(tasks, {
".karma.test-frontend": mkCmd(`karma start`, quote(karmaConfig("karma.conf.js")), `--colors`),

".karma.test-frontend-ci": mkCmd(
`karma start`,
quote(karmaConfig("karma.conf.coverage.js")),
`--colors`
),

".karma.test-frontend-cov": () => {
if (shell.test("-d", "test")) {
logger.info("\nRunning Karma unit tests:\n");
return mkCmd(`~$karma start`, quote(karmaConfig("karma.conf.coverage.js")), `--colors`);
}
return undefined;
},

".karma.test-frontend-dev": () =>
exec(`pgrep -fl "webpack-dev-server.*${archetype.webpack.testPort}"`)
.then(() => exec(`karma start`, quote(karmaConfig("karma.conf.dev.js")), `--colors`))
.catch(() => `test-frontend`),

".karma.test-frontend-dev-watch": mkCmd(
`karma start`,
quote(karmaConfig("karma.conf.watch.js")),
`--colors --browsers Chrome --no-single-run --auto-watch`
)
});
} else {
logger.info("Disabling karma test tasks since archetype config options.karma === false");
}

return tasks;
}

Expand Down
14 changes: 8 additions & 6 deletions packages/electrode-archetype-react-app/config/archetype.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ const optionalRequire = require("optional-require")(require);
const constants = require("./constants");
const utils = require("../lib/utils");
const makeAppMode = require("../lib/app-mode");
const userConfig = optionalRequire(Path.resolve("archetype/config"), {
default: {
options: { reactLib: "react" }
}
});
const userConfig = Object.assign(
{
options: { reactLib: "react", karma: true }
},
optionalRequire(Path.resolve("archetype/config"))
);

module.exports = {
dir: Path.resolve(__dirname, ".."),
pkg,
AppMode: makeAppMode(constants.PROD_DIR, userConfig.options && userConfig.options.reactLib),
options: userConfig.options,
AppMode: makeAppMode(constants.PROD_DIR, userConfig.options.reactLib),
prodDir: constants.PROD_DIR,
eTmpDir: constants.ETMP_DIR,
prodModulesDir: Path.join(constants.PROD_DIR, "modules"),
Expand Down

0 comments on commit 2a716a3

Please sign in to comment.