xgqfrms / 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
MIT License
1 stars 0 forks source link

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

Open xgqfrms opened 2 years ago

xgqfrms commented 2 years ago

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`;
}
xgqfrms commented 2 years ago

cross-env & cross-env-shell

https://github.com/kentcdodds/cross-env#cross-env-vs-cross-env-shell

image

.npmrc

https://github.com/kentcdodds/cross-env/issues/192#issuecomment-513341729

cmd => powershell

# in .npmrc
script-shell = "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"

# using the CLI
$ npm config set script-shell = "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
xgqfrms commented 2 years ago

https://github.com/kentcdodds/cross-env/issues/257

xgqfrms commented 2 years ago

JSON.stringify(process.env.NODE_ENV)

https://stackoverflow.com/questions/65844864/cross-env-not-changing-env-variable-when-running-yarn

webpack.config.js

{
  // ...
  plugins: [
    // ...
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
  ],
}