Coder Social home page Coder Social logo

xgqfrms / set-process-env Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 58 KB

set-process-env by linux bash command without need git ignore the .env file

Home Page: https://www.npmjs.com/package/set-process-env

License: MIT License

JavaScript 100.00%
env nodejs port-env cross-env shelljs linux bash npm set-process-env

set-process-env's Introduction

set-process-env

set-process-env by linux bash command without need git ignore the .env file

https://www.npmjs.com/package/set-process-env

install

$ yarn global add set-process-env
# OR
$ npm i -g set-process-env

usage

# PORT_ENV for webpack & npm script dev
$ spe PORT_ENV=8090

demo

// package.json

// ⚠️ precondition ([email protected], &  npm scripts has dev command)
// ⚠️ 前置条件 ([email protected] & npm scripts has dev command)

{
  // ...
  "scripts": {
    "dev": "webpack-dev-server --inline",
    // "dev": "cross-env NODE_OPTIONS='--max-old-space-size=8192' cross-env NODE_ENV=development webpack-dev-server --inline",
    // ...
  },
  // ...
}
// webpack.config.js
const PORT = process.env.PORT_ENV || 8080;

console.log('PORT_ENV =', process.env.PORT_ENV);
// console.log('\n process.env.PORT_ENV =', process.env.PORT_ENV);
// console.log('\n process.env.NODE_ENV =', process.env.NODE_ENV);
// console.log('\n process.env.NODE_OPTIONS =', process.env.NODE_OPTIONS);
// console.log('\n ✅ ');

// const ip = require('ip');
// const hostIp = ip.address();
const config = {
  // ...
  devServer: {
    // ...
    // host: hostIp,
    port: PORT_ENV || 8080,
    proxy: [
      {
        context: ['/web/api/'],
        // dev
        target: 'https://web-dev.xgqfrms.xyz',
        // prod
        // target: 'https://web-prod.xgqfrms.xyz',
      },
    ],
  },
};

module.exports = config;

image

multi env

$ spe PORT_ENV=8090 PROXY_ENV=pre

image

image

Travis CI

https://travis-ci.org/xgqfrms/set-process-env/builds

https://www.cnblogs.com/xgqfrms/p/12851281.html

refs

https://www.npmjs.com/package/app-node-env

set-process-env's People

Contributors

xgqfrms avatar

Stargazers

 avatar

Watchers

 avatar  avatar

set-process-env's Issues

require("cross-env") All In One ❌

require("cross-env") All In One

#!/usr/bin/env node

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms 2021.10.14
 *
 * @description PORT_ENV Generator
 * @augments
 * @example spe PORT_ENV=8090
 *
 */

// ❌  not support 
const crossEnv = require("cross-env");

// const shell = require("shelljs");
// const fs = require("fs");

const {
  version,
  argv,
  platform,
  // versions,
  // arch,
  // env,
} = process;

let command = ``;

// old version cross-env (dist)
if (isPureNumber(env)) {
  // npm / yarn
  // 单个 process 直接执行 ✅
  command = `crossEnv ${key}=${env} npm run dev`;
  // command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}=${env} npm run dev`;
  // command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}=${env} yarn dev`;
} else {
  // 单个 process 直接执行 ✅
  command = `crossEnv ${key}='${env}' npm run dev`;
  // command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}='${env}' npm run dev`;
  // command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}='${env}' yarn dev`;
}

shell.exec bug

shell.exec bug

shell.exec not work

shelljs/shelljs#1048

Node version

v12.18.0

ShellJS version

  "dependencies": {
    "shelljs": "^0.8.4"
  }

Operating system:

macOS 10.15.7

Description of the bug:

wanted

$ ./spe.js  PORT_ENV=666 && node ./test.js
#
# test command = export PORT_ENV=666
# PORT_ENV = 666
# PORT_ENV =  666

actually

$ ./spe.js  PORT_ENV=666 && node ./test.js
#
# test command = export PORT_ENV=666
# PORT_ENV = undefined
# PORT_ENV = undefined

Example ShellJS command to reproduce the error:

spe.js

#!/usr/bin/env node

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms 2021.10.14
 *
 * @description Pset-process-env
 *
 */

 const shell = require("shelljs");

const fs = require("fs");

const {
  version,
  argv,
  platform,
} = process;

const isMac = (platform === 'darwin');

const args = argv.slice(2) || [];

const isPureNumber = (str = '') => {
  const numberDict = [...''.padEnd(10, 'x')].map((item, i) => `${i}`);
  const arr = `${str}`.split('');
  return arr.every(item => numberDict.includes(item));
}

let key = "";
let env = "";
if (!args[0]) {
  args[0] = "";
  key = 'PORT_ENV';
  env = 8080;
} else {
  const tempArr = args[0].split('=');
  key = tempArr[0] || 'PORT_ENV';
  env = isPureNumber(tempArr[1]) ? parseInt(tempArr[1]) : tempArr[1];
}

let command = ``;

  if(isMac) {
    command = `export ${key}=${env}`;
  } else {
    command = `set ${key}=${env}`;
  }


shell.exec(command);

console.log('\n test command =', command);
console.log('\n PORT_ENV =', process.env.PORT_ENV);

test.js

// console.log('process.env =', process.env);
console.log('PORT_ENV =', process.env.PORT_ENV);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.