From 96cfe7ea453946847ef76b41b978cd6c0042400f Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Tue, 7 Jul 2020 12:31:48 +1200 Subject: [PATCH] Get app data dir from process.env, update webpack (#2352) --- packages/insomnia-inso/__jest__/setup.js | 2 ++ packages/insomnia-inso/package-lock.json | 36 +++++++++++++++++++ packages/insomnia-inso/package.json | 9 ++--- .../insomnia-inso/src/__tests__/util.test.js | 28 ++++++++++++++- packages/insomnia-inso/src/db/index.js | 4 +-- packages/insomnia-inso/src/util.js | 10 ++++++ .../webpack.config.base.js} | 6 +--- .../webpack/webpack.config.development.js | 13 +++++++ .../webpack/webpack.config.production.js | 15 ++++++++ 9 files changed, 111 insertions(+), 12 deletions(-) rename packages/insomnia-inso/{webpack.config.js => webpack/webpack.config.base.js} (82%) create mode 100644 packages/insomnia-inso/webpack/webpack.config.development.js create mode 100644 packages/insomnia-inso/webpack/webpack.config.production.js diff --git a/packages/insomnia-inso/__jest__/setup.js b/packages/insomnia-inso/__jest__/setup.js index dce3310f7b..24470da68f 100644 --- a/packages/insomnia-inso/__jest__/setup.js +++ b/packages/insomnia-inso/__jest__/setup.js @@ -1 +1,3 @@ jest.mock('node-libcurl'); + +process.env.DEFAULT_APP_DATA_DIR = process.env.DEFAULT_APP_DATA_DIR || 'insomnia-app'; diff --git a/packages/insomnia-inso/package-lock.json b/packages/insomnia-inso/package-lock.json index ce2795baf8..f9c68433ef 100644 --- a/packages/insomnia-inso/package-lock.json +++ b/packages/insomnia-inso/package-lock.json @@ -2898,6 +2898,17 @@ "wrap-ansi": "^6.2.0" } }, + "clone-deep": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/clone-deep/-/clone-deep-4.0.1.tgz", + "integrity": "sha512-neHB9xuzh/wk0dIHweyAXv2aPGZIVk3pLMe+/RNzINf17fe0OG96QroktYAUm7SM1PBnzTabaLboqqxDyMU+SQ==", + "dev": true, + "requires": { + "is-plain-object": "^2.0.4", + "kind-of": "^6.0.2", + "shallow-clone": "^3.0.0" + } + }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -7508,6 +7519,15 @@ "safe-buffer": "^5.0.1" } }, + "shallow-clone": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/shallow-clone/-/shallow-clone-3.0.1.tgz", + "integrity": "sha512-/6KqX+GVUdqPuPPd2LxDDxzX6CAbjJehAAOKlNpqqUpAqPM6HeL8f+o3a+JsyGjn2lv0WY8UsTgUJjU9Ok55NA==", + "dev": true, + "requires": { + "kind-of": "^6.0.2" + } + }, "shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -9013,6 +9033,16 @@ } } }, + "webpack-merge": { + "version": "5.0.7", + "resolved": "https://registry.npmjs.org/webpack-merge/-/webpack-merge-5.0.7.tgz", + "integrity": "sha512-IxTERB8mNelEUN7YLSBhKMA0t/AySs0GGcUczNWWZTsNMKEFst4MM1t4aHX4ANAqVHO02A2zPPVXcdfv5Qpslw==", + "dev": true, + "requires": { + "clone-deep": "^4.0.1", + "wildcard": "^2.0.0" + } + }, "webpack-node-externals": { "version": "1.7.2", "resolved": "https://registry.npmjs.org/webpack-node-externals/-/webpack-node-externals-1.7.2.tgz", @@ -9077,6 +9107,12 @@ "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=" }, + "wildcard": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/wildcard/-/wildcard-2.0.0.tgz", + "integrity": "sha512-JcKqAHLPxcdb9KM49dufGXn2x3ssnfjbcaQdLlfZsL9rH9wgDQjUtDxbo8NE0F6SFvydeu1VhZe7hZuHsB2/pw==", + "dev": true + }, "wolfy87-eventemitter": { "version": "5.2.9", "resolved": "https://registry.npmjs.org/wolfy87-eventemitter/-/wolfy87-eventemitter-5.2.9.tgz", diff --git a/packages/insomnia-inso/package.json b/packages/insomnia-inso/package.json index 6b992cfc0e..8af3d71a6c 100644 --- a/packages/insomnia-inso/package.json +++ b/packages/insomnia-inso/package.json @@ -12,11 +12,11 @@ "test": "jest", "test:watch": "jest --watch", "test:snapshots": "npm run build && jest -u", - "start": "npm run build:watch", - "build": "webpack --config webpack.config.js --display errors-only", - "build:watch": "npm run build -- --watch", + "build": "webpack --config webpack/webpack.config.development.js --display errors-only", + "build:production": "webpack --config webpack/webpack.config.production.js --display errors-only", + "start": "npm run build -- --watch", "bootstrap": "npm run build", - "prepublish": "npm run build" + "prepare": "npm run build:production" }, "jest": { "setupFiles": [ @@ -41,6 +41,7 @@ "rimraf": "^3.0.2", "webpack": "^4.43.0", "webpack-cli": "^3.3.12", + "webpack-merge": "^5.0.7", "webpack-node-externals": "^1.7.2" }, "dependencies": { diff --git a/packages/insomnia-inso/src/__tests__/util.test.js b/packages/insomnia-inso/src/__tests__/util.test.js index d74ff11646..98c5ad73f8 100644 --- a/packages/insomnia-inso/src/__tests__/util.test.js +++ b/packages/insomnia-inso/src/__tests__/util.test.js @@ -1,6 +1,6 @@ // @flow import commander from 'commander'; -import { getAllOptions, exit, logErrorExit1 } from '../util'; +import { getAllOptions, exit, logErrorExit1, getDefaultAppDataDir } from '../util'; describe('getAllOptions()', () => { it('should combine options from all commands into one object', () => { @@ -63,3 +63,29 @@ describe('logErrorExit1()', () => { expect(exitSpy).toHaveBeenCalledWith(1); }); }); + +describe('getDefaultAppDataDir()', () => { + const OLD_ENV = process.env; + + beforeEach(() => { + process.env = { ...OLD_ENV }; // make a copy + }); + + afterAll(() => { + process.env = OLD_ENV; // restore old env + }); + + it('should return value if set', () => { + const value = 'dir'; + process.env.DEFAULT_APP_DATA_DIR = value; + expect(getDefaultAppDataDir()).toBe(value); + }); + + it('should throw error if not set', () => { + process.env.DEFAULT_APP_DATA_DIR = ''; + + expect(getDefaultAppDataDir).toThrowError( + 'Environment variable DEFAULT_APP_DATA_DIR is not set.', + ); + }); +}); diff --git a/packages/insomnia-inso/src/db/index.js b/packages/insomnia-inso/src/db/index.js index 44679407dc..90cf8bc2d3 100644 --- a/packages/insomnia-inso/src/db/index.js +++ b/packages/insomnia-inso/src/db/index.js @@ -3,6 +3,7 @@ import type { ApiSpec, BaseModel, Environment, UnitTest, UnitTestSuite, Workspac import envPaths from 'env-paths'; import gitAdapter from './adapters/git-adapter'; import neDbAdapter from './adapters/ne-db-adapter'; +import { getDefaultAppDataDir } from '../util'; export type Database = {| ApiSpec: Array, @@ -49,9 +50,8 @@ export const loadDb = async ({ // try load from nedb if (!db) { - // TODO: Note, unit tests will also try to access the Insomnia Designer app data directory. We should configure this depending on development or production. db = await neDbAdapter( - appDataDir || envPaths('Insomnia Designer', { suffix: '' }).data, + appDataDir || envPaths(getDefaultAppDataDir(), { suffix: '' }).data, filterTypes, ); } diff --git a/packages/insomnia-inso/src/util.js b/packages/insomnia-inso/src/util.js index 2c1d82d34c..a64800b9aa 100644 --- a/packages/insomnia-inso/src/util.js +++ b/packages/insomnia-inso/src/util.js @@ -44,3 +44,13 @@ export function logErrorExit1(err: Error) { export async function exit(result: Promise): Promise { return result.then(r => process.exit(r ? 0 : 1)).catch(logErrorExit1); } + +export function getDefaultAppDataDir(): string { + const dir = process.env.DEFAULT_APP_DATA_DIR; + + if (!dir) { + throw new Error('Environment variable DEFAULT_APP_DATA_DIR is not set.'); + } + + return dir; +} diff --git a/packages/insomnia-inso/webpack.config.js b/packages/insomnia-inso/webpack/webpack.config.base.js similarity index 82% rename from packages/insomnia-inso/webpack.config.js rename to packages/insomnia-inso/webpack/webpack.config.base.js index b238085634..bf493bbf45 100644 --- a/packages/insomnia-inso/webpack.config.js +++ b/packages/insomnia-inso/webpack/webpack.config.base.js @@ -4,13 +4,9 @@ const nodeExternals = require('webpack-node-externals'); module.exports = { entry: { index: './src/cli.js' }, target: 'node', - mode: 'production', devtool: 'source-map', - optimization: { - minimize: false, - }, output: { - path: path.resolve(__dirname, 'dist'), + path: path.resolve(__dirname, '..', 'dist'), filename: '[name].js', library: 'insomniacli', libraryTarget: 'commonjs2', diff --git a/packages/insomnia-inso/webpack/webpack.config.development.js b/packages/insomnia-inso/webpack/webpack.config.development.js new file mode 100644 index 0000000000..29852480da --- /dev/null +++ b/packages/insomnia-inso/webpack/webpack.config.development.js @@ -0,0 +1,13 @@ +const webpack = require('webpack'); +const {merge} = require('webpack-merge'); +const base = require('./webpack.config.base'); + +module.exports = merge(base, { + mode: 'development', + plugins: [ + new webpack.DefinePlugin({ + __DEV__: true, + 'process.env.DEFAULT_APP_DATA_DIR': JSON.stringify('insomnia-app'), + }), + ], +}); diff --git a/packages/insomnia-inso/webpack/webpack.config.production.js b/packages/insomnia-inso/webpack/webpack.config.production.js new file mode 100644 index 0000000000..6f5e5a96ac --- /dev/null +++ b/packages/insomnia-inso/webpack/webpack.config.production.js @@ -0,0 +1,15 @@ +const webpack = require('webpack'); +const {merge} = require('webpack-merge'); +const base = require('./webpack.config.base'); + +module.exports = merge(base, { + mode: 'production', + optimization: { + minimize: true, + }, + plugins: [ + new webpack.DefinePlugin({ + 'process.env.DEFAULT_APP_DATA_DIR': JSON.stringify('Insomnia Designer'), + }), + ], +});