Skip to content

Commit

Permalink
[Minor] [Feature] allow user to change app's root directory (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
durrab authored Nov 24, 2020
1 parent c4d141d commit 9c7a324
Show file tree
Hide file tree
Showing 26 changed files with 167 additions and 92 deletions.
8 changes: 7 additions & 1 deletion packages/xarc-app-dev/src/config/archetype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ let cachedArchetype = null;
* @returns options - final options with defaults and env applied
*/
module.exports = function getDevOptions(user: XarcOptions = {}) {
//checking for cwd from xclap or from env
const cwd = user.cwd || process.env.XARC_CWD || process.cwd();
process.env.XARC_CWD = cwd;

if (cachedArchetype) {
// if cached is already runnig
cachedArchetype._fromCache = true;
// maintained for backwards compatibility
return cachedArchetype;
Expand All @@ -24,7 +29,7 @@ module.exports = function getDevOptions(user: XarcOptions = {}) {
// first get legacy configs
const legacy = getDevArchetypeLegacy();
// try to read xarc-options.json if it exist and merge it into legacy
const xarcOptions = loadXarcOptions();
const xarcOptions = loadXarcOptions(cwd, false);
user = _.merge({}, xarcOptions, user);
const proxy = getEnvProxy();

Expand All @@ -40,6 +45,7 @@ module.exports = function getDevOptions(user: XarcOptions = {}) {
// merge the rest into top level
_.merge(legacy, {
...user,
cwd,
webpackOptions: undefined,
babelOptions: undefined,
addOnFeatures: undefined
Expand Down
7 changes: 5 additions & 2 deletions packages/xarc-app-dev/src/config/get-dev-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ const Path = require("path");
const Fs = require("fs");

let cachedProxy = null;
import { loadXarcOptions } from "../lib/utils";

module.exports = function createDevProxy() {
const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;
if (cachedProxy) {
return cachedProxy;
}
Expand Down Expand Up @@ -65,8 +68,8 @@ module.exports = function createDevProxy() {

const searchDirs = ["", "config", "test", "src"];
for (const f of searchDirs) {
const key = Path.resolve(f, "dev-proxy.key");
const cert = Path.resolve(f, "dev-proxy.crt");
const key = Path.resolve(xarcCwd, f, "dev-proxy.key");
const cert = Path.resolve(xarcCwd, f, "dev-proxy.crt");
if (Fs.existsSync(key) && Fs.existsSync(cert)) {
return { key, cert };
}
Expand Down
3 changes: 2 additions & 1 deletion packages/xarc-app-dev/src/config/jest/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ const jestMajVersion = parseInt(jestPkg.version.split(".")[0], 10);
import { loadXarcOptions } from "../../lib/utils";

const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;

const { enableTypeScript } = xarcOptions.babel;

// https://jestjs.io/docs/en/configuration.html#testregex-string
const scrTypes = enableTypeScript ? "jt" : "j";
const testRegex = `(/_?_tests?_?_/.*|(\\.|\/)(test|spec))\\.[${scrTypes}]sx?$`;

const rootDir = process.cwd();
const rootDir = xarcCwd;

const jestDefaultConfig = {
rootDir,
Expand Down
8 changes: 6 additions & 2 deletions packages/xarc-app-dev/src/config/karma/karma.conf.coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ const webpackCovCfg = require(customCheck.getWebpackStartConfig(
"../webpack/webpack.config.coverage",
false
));
import { loadXarcOptions } from "../../lib/utils";

module.exports = function(config) {
const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;

karmaConf(config);
const settings = {
reporters: ["spec", "sonarqubeUnit", "coverage"],
Expand All @@ -28,12 +32,12 @@ module.exports = function(config) {
{ type: "lcov", subdir: "." },
{ type: "text", subdir: "." }
],
dir: Path.resolve("coverage", "client")
dir: Path.resolve(xarcCwd, "coverage", "client")
},
sonarQubeUnitReporter: {
sonarQubeVersion: "5.x",
outputFile: "gunit.xml",
outputDir: Path.resolve("coverage", "client"),
outputDir: Path.resolve(xarcCwd, "coverage", "client"),
useBrowserName: false
}
};
Expand Down
5 changes: 4 additions & 1 deletion packages/xarc-app-dev/src/config/karma/karma.conf.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ export {};
const loadUserConfig = require("./util/load-user-config");
const Path = require("path");
const browserSettings = require("./browser-settings");
import { loadXarcOptions } from "../../lib/utils";

module.exports = function(config) {
const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;
const settings = {
frameworks: ["mocha"],
reporters: ["spec"],
basePath: process.cwd(), // repository root.
basePath: xarcCwd, // repository root.
files: [
// Test bundle (must be created via `npm run dev|server-test`)
"http://127.0.0.1:3001/assets/bundle.js"
Expand Down
13 changes: 9 additions & 4 deletions packages/xarc-app-dev/src/config/karma/karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const customCheck = require("@xarc/webpack/lib/util/custom-check");
const loadUserConfig = require("./util/load-user-config");
const browserSettings = require("./browser-settings");
const loadElectrodeDll = require("./util/load-electrode-dll");
import { loadXarcOptions } from "../../lib/utils";

function getXarcOptPlugins() {
try {
Expand Down Expand Up @@ -54,7 +55,9 @@ function getArchetypeOptPlugins() {
let MAIN_PATH;

try {
MAIN_PATH = require.resolve(Path.resolve("test/karma-entry"));
const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;
MAIN_PATH = require.resolve(Path.resolve(xarcCwd, "test/karma-entry"));
} catch (err) {
if (getXarcOptPlugins()) {
MAIN_PATH = require.resolve("./entry-xarc.js");
Expand Down Expand Up @@ -89,8 +92,10 @@ module.exports = function(config) {
console.error("ERROR: @xarc/opt-karma not found - running karma tests is not possible");
plugins = [];
}
const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;
const settings = {
basePath: process.cwd(),
basePath: xarcCwd,
frameworks: ["mocha", "intl-shim"],
files: DLL_PATHS.concat(MAIN_PATH),
plugins,
Expand Down Expand Up @@ -123,12 +128,12 @@ module.exports = function(config) {
{ type: "lcov", subdir: "." },
{ type: "text", subdir: "." }
],
dir: Path.resolve("coverage", "client")
dir: Path.resolve(xarcCwd, "coverage", "client")
},
sonarQubeUnitReporter: {
sonarQubeVersion: "5.x",
outputFile: "gunit.xml",
outputDir: Path.resolve("coverage", "client"),
outputDir: Path.resolve(xarcCwd, "coverage", "client"),
useBrowserName: false
},
captureTimeout: 100000,
Expand Down
7 changes: 5 additions & 2 deletions packages/xarc-app-dev/src/config/load-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ export {};

const Path = require("path");
const optionalRequire = require("optional-require")(require);
import { loadXarcOptions } from "../lib/utils";

function checkTopDevArchetype(devArchName) {
const topPkg = optionalRequire(Path.resolve("package.json"));
const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;
const topPkg = optionalRequire(Path.resolve(xarcCwd, "package.json"));
if (topPkg && topPkg.name === devArchName) {
// In case @xarc/app is being used for test/dev in the -dev archetype
// resolve config/archetype in @xarc/app-dev's own dir
return optionalRequire(Path.resolve("config/archetype"));
return optionalRequire(Path.resolve(xarcCwd, "config/archetype"));
} else {
return optionalRequire(`${devArchName}/config/archetype`);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/xarc-app-dev/src/config/opt2/xarc-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ export type XarcOptions = {
* options related to babel transpiler
*/
babelOptions?: BabelOptions;

/**
* option for app directory if passed to loadXarcDevTasks using xclap.ts
* e.g: loadXarcDevTasks(null, { cwd: "/actual-dir-of-app" });
*/
cwd?: string;
};
6 changes: 5 additions & 1 deletion packages/xarc-app-dev/src/lib/app-dev-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
WEBPACK_EVENT_STATS
} from "./dev-admin/webpack-dev-relay";

import { loadXarcOptions } from "./utils";

class AppDevMiddleware {
webpackDev: any;

Expand Down Expand Up @@ -43,7 +45,9 @@ class AppDevMiddleware {
if (!_.isEmpty(data.refreshModules)) {
data.refreshModules.forEach(m => {
try {
const moduleFullPath = require.resolve(Path.resolve(m));
const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;
const moduleFullPath = require.resolve(Path.resolve(xarcCwd, m));
delete require.cache[moduleFullPath];
} catch (err) {
//
Expand Down
12 changes: 6 additions & 6 deletions packages/xarc-app-dev/src/lib/babel-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import { loadXarcOptions } from "../lib/utils";
const serverDir = process.argv[2] || "src/server";

let start;

const xarcOptions = loadXarcOptions();
const xarcCwd = xarcOptions.cwd;
try {
// Try to load user's dev.js under src/server
start = require(Path.resolve(serverDir, "dev.js"));
start = require(Path.resolve(xarcCwd, serverDir, "dev.js"));
} catch (e) {
const xarcOptions = loadXarcOptions();
const cwdNM = Path.resolve("node_modules");
const cwd = process.cwd();
const cwdNM = Path.resolve(xarcCwd, "node_modules");
const cwd = xarcCwd;

// fallback to default action that loads babel-register and then requires
// src/server, under which there should be an index.js file.
Expand All @@ -44,7 +44,7 @@ try {
cache: true
});

const fullServerDir = Path.resolve(serverDir);
const fullServerDir = Path.resolve(xarcCwd, serverDir);

start = require(fullServerDir);
}
Expand Down
Loading

0 comments on commit 9c7a324

Please sign in to comment.