Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new @xarc/opt- packages to replace the electrode-archetype-opt- ones #1725

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions packages/opt-archetype-check/xarc-opt-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
"use strict";

/**
*
* NOTE: this file is actually maintained centrally in a directory in our repo.
*
* There are many duplicate copies of it in each opt- package, but not as a
* npm module, because the opt- package needs this upfront for the npm script
* prepare. They each copy it from the central one.
*/

const Path = require("path");
const Fs = require("fs");
const assert = require("assert");

const isSameMajorVersion = (verA, verB) => {
// check for simple semver like x.x.x, ~x.x.x, or ^x.x.x only
let majorA = verA.match(/[\~\^]{0,1}(\d+)\.(\d+)\.(\d+)/);
if (majorA) {
majorA = majorA.slice(1, 4);
const majorB = verB.split(".");
if (majorB[0] !== majorA[0] || (majorB[0] === "0" && majorB[1] !== majorA[1])) {
return false;
}
}

return true;
};

function lookupAppDirByInitCwd() {
//
// env INIT_CWD is set by npm to the dir where it started running.
// Note that it's not strictly where package.json is, because npm
// would search up the directories for the first package.json found,
// which is why the we need to do the same search up lookup below.
//
let lookupDir = process.env.INIT_CWD;

if (!lookupDir) return undefined;

let count = 0;

while (count < 100) {
const pkgFile = Path.join(lookupDir, "package.json");

try {
require(pkgFile);
return lookupDir;
} catch (err) {
//
}

const upDir = Path.join(lookupDir, "..");
if (upDir === lookupDir) return undefined;
lookupDir = upDir;
count++;
}

return undefined;
}

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

//
// Trying to find the app's dir by checking for the first
// node_modules in the path string
// For example, /home/userid/myapp/node_modules/electrode-archetype-opt-react
// would yield app dir as /home/userid/myapp
//
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;
}

function checkAppPackage(appDir) {
try {
return JSON.parse(Fs.readFileSync(Path.join(appDir, "./package.json")).toString());
} catch (e) {
return {};
}
}

function xarcOptCheck() {
//
// Find the app's dir by using npm's INIT_CWD and then fallback to
// looking for node_modules in the path
//
const appDir = lookupAppDirByInitCwd() || findAppDir();
const appPkg = checkAppPackage(appDir);
const myPkg = JSON.parse(Fs.readFileSync(Path.join(__dirname, "./package.json")).toString());
const myName = myPkg.name;
const optParams = Object.assign({}, myPkg.xarcOptCheck);

const done = (pass, message) => {
return Object.assign({ pass, message }, optParams);
};

//
// just the package itself
//
if (cwd === appDir && myName === appPkg.name) {
return done(true, "self");
}

assert(
optParams.hasOwnProperty("optionalTagName"),
`opt archetype ${myName}: package.json missing xarcOptCheck.optionalTagName`
);

const optionalTagName = optParams.optionalTagName;

let foundOOO = [];

//
// If a workspace detected, then we don't know how dependencies are setup, so
// skip checking.
//
if (!appPkg.workspaces && optParams.onlyOneOf) {
// first, user's package.json cannot have multiple packages from onlyOneOf list
["dependencies", "devDependencies", "optionalDependencies"].forEach(x => {
if (appPkg[x]) {
foundOOO = foundOOO.concat(optParams.onlyOneOf.filter(n => appPkg[x].hasOwnProperty(n)));
}
});

if (foundOOO.length > 1) {
return done(
false,
`
ERROR
ERROR: you can have *only* ONE of these packages in your package.json dependencies/devDependencies/optionalDependencies.
ERROR: ${foundOOO.join(", ")}
ERROR
`
);
}

// If found a mutually excluding package but it's not this one, then skip installing this.
if (foundOOO.length > 0 && foundOOO.indexOf(myName) < 0) {
return done(
false,
`Found ${foundOOO[0]} in your package.json and \
it excludes this package ${myName} - skip installing`
);
}
}

return done(true);
}

module.exports = xarcOptCheck;

if (require.main === module) {
const r = xarcOptCheck();

if (r.pass) {
if (r.message) {
console.log(r.message);
}
} else {
console.error(r.message);
}

process.exit(r.pass ? 0 : 1);
}

module.exports.isSameMajorVersion = isSameMajorVersion;
13 changes: 4 additions & 9 deletions packages/xarc-app-dev/src/config/babel/babelrc-client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable @typescript-eslint/no-var-requires, no-console, @typescript-eslint/ban-ts-ignore */
export {};

const ck = require("chalker");
const requireAt = require("require-at");
const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");
import { getPluginFrom } from "./common";

const {
enableTypeScript,
Expand All @@ -19,12 +18,6 @@ const {
} = archetype.babel;

const addFlowPlugin = Boolean(enableFlow && optFlow);
//
// Resolve full path of a plugin that's the dependency of host npm package
//
function getPluginFrom(host, pluginName) {
return requireAt(require.resolve(`${host}/package.json`)).resolve(pluginName);
}

const basePlugins = [
...(enableDynamicImport
Expand Down Expand Up @@ -127,7 +120,9 @@ const plugins = basePlugins.concat(
}
]
],
enableKarmaCov && [getPluginFrom("electrode-archetype-opt-karma", "babel-plugin-istanbul")]
enableKarmaCov && [
getPluginFrom(["@xarc/opt-karma", "electrode-archetype-opt-karma"], "babel-plugin-istanbul")
]
);

const targets = archetype.babel.envTargets[archetype.babel.target];
Expand Down
12 changes: 4 additions & 8 deletions packages/xarc-app-dev/src/config/babel/babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export {};
* and this file will set preset-env targets accordingly.
*/
const ck = require("chalker");
const requireAt = require("require-at");
const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");
import { getPluginFrom } from "./common";

const isJest = Boolean(process.env.JEST_WORKER_ID);

Expand All @@ -26,12 +26,6 @@ const {
} = archetype.babel;

const addFlowPlugin = Boolean(enableFlow && optFlow);
//
// Resolve full path of a plugin that's the dependency of host npm package
//
function getPluginFrom(host, pluginName) {
return requireAt(require.resolve(`${host}/package.json`)).resolve(pluginName);
}

const fileId = "xarc-app-dev:babelrc.js";

Expand Down Expand Up @@ -138,7 +132,9 @@ const plugins = basePlugins.concat(
]
],
!isNodeTarget &&
enableKarmaCov && [getPluginFrom("electrode-archetype-opt-karma", "babel-plugin-istanbul")]
enableKarmaCov && [
getPluginFrom(["@xarc/opt-karma", "electrode-archetype-opt-karma"], "babel-plugin-istanbul")
]
);

const target = isNodeTarget ? "node" : archetype.babel.target;
Expand Down
18 changes: 18 additions & 0 deletions packages/xarc-app-dev/src/config/babel/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as requireAt from "require-at";

//
// Resolve full path of a plugin that's the dependency of host npm package
//
export function getPluginFrom(host, pluginName) {
let err;
for (const pkg of [].concat(host)) {
try {
return requireAt(require.resolve(`${pkg}/package.json`)).resolve(pluginName);
} catch (e) {
if (!err) {
err = e;
}
}
}
throw err;
}
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/eslint/.eslintrc-base
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
extends:
- "walmart"
- "./js/base.js"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
extends:
- "walmart/configurations/es5-node"
- "./js/es5-node.js"
globals:
expect: false
sandbox: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
extends:
- "walmart/configurations/es6-node"
- "./js/es6-node.js"
globals:
expect: false
sandbox: false
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/eslint/.eslintrc-node
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
extends:
- "walmart/configurations/es6-node"
- "./js/es6-node.js"
- plugin:react/recommended
parserOptions:
# this is not in the Walmart default configuration, for fairly good reason:
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/eslint/.eslintrc-react
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
extends:
- "walmart/configurations/es6-react"
- "./js/es6-react.js"
globals:
window: false
document: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
extends:
- "walmart/configurations/es6-test"
- "./js/es6-test.js"
- "./.eslintrc-react"
globals:
document: false
Expand Down
4 changes: 4 additions & 0 deletions packages/xarc-app-dev/src/config/eslint/js/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

const optEslintRequire = require("./opt-eslint-require");
module.exports = optEslintRequire("eslint-config-walmart");
3 changes: 3 additions & 0 deletions packages/xarc-app-dev/src/config/eslint/js/es5-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";
const optEslintRequire = require("./opt-eslint-require");
module.exports = optEslintRequire("eslint-config-walmart/configurations/es5-node");
3 changes: 3 additions & 0 deletions packages/xarc-app-dev/src/config/eslint/js/es6-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";
const optEslintRequire = require("./opt-eslint-require");
module.exports = optEslintRequire("eslint-config-walmart/configurations/es6-node");
3 changes: 3 additions & 0 deletions packages/xarc-app-dev/src/config/eslint/js/es6-react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";
const optEslintRequire = require("./opt-eslint-require");
module.exports = optEslintRequire("eslint-config-walmart/configurations/es6-react");
3 changes: 3 additions & 0 deletions packages/xarc-app-dev/src/config/eslint/js/es6-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";
const optEslintRequire = require("./opt-eslint-require");
module.exports = optEslintRequire("eslint-config-walmart/configurations/es6-test");
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

try {
module.exports = require("require-at")(require.resolve("@xarc/opt-eslint/package.json"));
} catch {
module.exports = require("require-at")(
require.resolve("electrode-archetype-opt-eslint/package.json")
);
}
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/jest/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require("lodash");
const fileMock = Path.join(__dirname, "__mocks__", "file-mock.js");
const frameworkMock = Path.join(__dirname, "__mocks__", "framework-mock.js");
const { getOptArchetypeRequire } = require("../../lib/utils");
const optRequire = getOptArchetypeRequire("electrode-archetype-opt-jest");
const optRequire = getOptArchetypeRequire(["@xarc/opt-jest", "electrode-archetype-opt-jest"]);

const jestPkg = optRequire("jest/package.json");
const jestMajVersion = parseInt(jestPkg.version.split(".")[0], 10);
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require("../typedef");

function checkOptArchetypeInAppDep(dependencies, isDev = undefined) {
const options = dependencies
.filter(x => x.startsWith("electrode-archetype-opt-"))
.filter(x => x.startsWith("electrode-archetype-opt-") || x.startsWith("@xarc/opt-"))
.reduce((acc, name) => {
//
// In dev mode, when all dev deps are installed, we can safely load
Expand Down
19 changes: 19 additions & 0 deletions packages/xarc-opt-eslint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Electrode eslint Addon

This is a feature package that instructs [Electrode](https://github.com/electrode-io/electrode) to enable eslint support.

Just install it as your `devDependencies` and Electrode will detect it and enable eslint support.

## Installation

```
npm i --save-dev @xarc/opt-eslint
```

> Note: **conflicts** with the old electrode-archetype-opt-eslint package.

## License

Copyright (c) 2016-present, WalmartLabs

Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).
Loading