1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-27 04:03:00 +02:00

Feature/put serve behind feature flag (#455)

* Add build-time feature flag capabilities

* Toggle `bw serve` command with `serve` flag

* Run linter and prettier
This commit is contained in:
Matt Gibson 2022-01-28 09:29:04 -05:00 committed by GitHub
parent 1b409653a2
commit 210e0502ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 117 additions and 18 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
node_modules
build
dist
config/local.json

30
config/config.js Normal file
View File

@ -0,0 +1,30 @@
function load(envName) {
return {
...loadConfig(envName),
...loadConfig("local"),
};
}
function log(configObj) {
const repeatNum = 50;
console.log(`${"=".repeat(repeatNum)}\nenvConfig`);
console.log(JSON.stringify(configObj, null, 2));
console.log(`${"=".repeat(repeatNum)}`);
}
function loadConfig(configName) {
try {
return require(`./${configName}.json`);
} catch (e) {
if (e instanceof Error && e.code === "MODULE_NOT_FOUND") {
return {};
} else {
throw e;
}
}
}
module.exports = {
load,
log,
};

5
config/development.json Normal file
View File

@ -0,0 +1,5 @@
{
"flags": {
"serve": true
}
}

5
config/production.json Normal file
View File

@ -0,0 +1,5 @@
{
"flags": {
"serve": false
}
}

27
spec/utils.spec.ts Normal file
View File

@ -0,0 +1,27 @@
import { FlagName } from "../src/flags";
import { CliUtils } from "../src/utils";
describe("flagEnabled", () => {
it("is true if flag is null", () => {
process.env.FLAGS = JSON.stringify({ test: null });
expect(CliUtils.flagEnabled("test" as FlagName)).toBe(true);
});
it("is true if flag is undefined", () => {
process.env.FLAGS = JSON.stringify({});
expect(CliUtils.flagEnabled("test" as FlagName)).toBe(true);
});
it("is true if flag is true", () => {
process.env.FLAGS = JSON.stringify({ test: true });
expect(CliUtils.flagEnabled("test" as FlagName)).toBe(true);
});
it("is false if flag is false", () => {
process.env.FLAGS = JSON.stringify({ test: false });
expect(CliUtils.flagEnabled("test" as FlagName)).toBe(false);
});
});

5
src/flags.ts Normal file
View File

@ -0,0 +1,5 @@
export type Flags = {
serve?: boolean;
};
export type FlagName = keyof Flags;

View File

@ -468,22 +468,24 @@ export class Program extends BaseProgram {
this.processResponse(response);
});
program
.command("serve")
.description("Start a RESTful API webserver.")
.option("--port <port>", "The port to run your API webserver on. Default port is 8087.")
.on("--help", () => {
writeLn("\n Examples:");
writeLn("");
writeLn(" bw serve");
writeLn(" bw serve --port 8080");
writeLn("", true);
})
.action(async (cmd) => {
await this.exitIfNotAuthed();
const command = new ServeCommand(this.main);
await command.run(cmd);
});
if (CliUtils.flagEnabled("serve")) {
program
.command("serve")
.description("Start a RESTful API webserver.")
.option("--port <port>", "The port to run your API webserver on. Default port is 8087.")
.on("--help", () => {
writeLn("\n Examples:");
writeLn("");
writeLn(" bw serve");
writeLn(" bw serve --port 8080");
writeLn("", true);
})
.action(async (cmd) => {
await this.exitIfNotAuthed();
const command = new ServeCommand(this.main);
await command.run(cmd);
});
}
}
protected processResponse(response: Response, exitImmediately = false) {

View File

@ -9,6 +9,7 @@ import { CollectionView } from "jslib-common/models/view/collectionView";
import { FolderView } from "jslib-common/models/view/folderView";
import { NodeUtils } from "jslib-common/misc/nodeUtils";
import { FlagName, Flags } from "./flags";
export class CliUtils {
static writeLn(s: string, finalLine: boolean = false, error: boolean = false) {
@ -174,4 +175,18 @@ export class CliUtils {
static convertBooleanOption(optionValue: any) {
return optionValue || optionValue === "" ? true : false;
}
static flagEnabled(flag: FlagName) {
return this.flags[flag] == null || this.flags[flag];
}
private static get flags(): Flags {
const envFlags = process.env.FLAGS;
if (typeof envFlags === "string") {
return JSON.parse(envFlags) as Flags;
} else {
return envFlags as Flags;
}
}
}

View File

@ -4,12 +4,16 @@ const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const config = require("./config/config");
if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = "development";
}
const ENV = (process.env.ENV = process.env.NODE_ENV);
const envConfig = config.load(ENV);
config.log(envConfig);
const moduleRules = [
{
test: /\.ts$/,
@ -39,9 +43,13 @@ const plugins = [
resourceRegExp: /^encoding$/,
contextRegExp: /node-fetch/,
}),
new webpack.EnvironmentPlugin({
BWCLI_ENV: ENV,
FLAGS: envConfig.flags,
}),
];
const config = {
const webpackConfig = {
mode: ENV,
target: "node",
devtool: ENV === "development" ? "eval-source-map" : "source-map",
@ -70,4 +78,4 @@ const config = {
externals: [nodeExternals()],
};
module.exports = config;
module.exports = webpackConfig;