node.js & read argv
node.js & read argv
https://nodejs.org/docs/latest/api/process.html
https://flaviocopes.com/node-cli-args/
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description APP_ENV Genarator
* @augments
* @example
*
*/
const fs = require("fs");
const args = process.argv.slice(2);
console.log(`process.argv =`, process.argv);
console.log(`args =`, args);
const filename = "./fs.open.js";
fs.open(filename, "r", (err, fd) => {
if (err) {
fs.writeFile(filename, `export const APP_ENV = "preview";`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
console.log("The file was created!");
let env = "preview";
console.log("The file was saved!", env);
});
} else {
// truncated 截断 === empty ???
console.log("The file exists!");
// console.log("The file isn't exists!");
fs.writeFile(filename, `// truncated 截断 === empty`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
console.log("The file was truncated!");
});
}
});
## js path
$ node env-generator.js key=value
OK
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description APP_ENV Genarator
* @augments
* @example
*
*/
const fs = require("fs");
const args = process.argv.slice(2);
console.log(`process.argv =`, process.argv);
console.log(`args =`, args);
console.log(`args.toString().substr(4) =`, args.toString().substr(4));
const filename = "./fs.open.js";
fs.open(filename, "r", (err, fd) => {
if (err) {
fs.writeFile(filename, `export const APP_ENV = "preview";`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
console.log("The file was created!");
let env = "preview";
console.log("The file was saved!", env);
});
} else {
// truncated 截断 === empty ???
console.log("The file exists!");
// console.log("The file isn't exists!");
fs.writeFile(filename, `// truncated 截断 === empty`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
console.log("The file was truncated!");
});
}
});
APP_ENV
const env={
testing:'',
dev:'',
local:'',
statging:'',
product:''
}
npm scripts
$ node env-generator env=testing
$ node env-generator env=dev
$ node env-generator env=local
$ node env-generator env=statging
$ node env-generator env=product
OR
$ node env-generator.js env=testing
$ node env-generator.js env=dev
$ node env-generator.js env=local
$ node env-generator.js env=statging
$ node env-generator.js env=product
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms 2019.05.12
*
* @description APP_ENV Genarator
* @augments
* @example node env-generator.js env=dev
*
*/
const fs = require("fs");
const env = process.argv.slice(2)[0].substr(4);
const filename = "./env.js";
fs.open(filename, "r", (err, fd) => {
console.log("APP_ENV =", env);
if (err) {
fs.writeFile(filename, `export const APP_ENV = "${env}";`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
});
} else {
fs.writeFile(filename, `export const APP_ENV = "${env}";`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
});
}
});
new version
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms 2019.05.12
*
* @description APP_ENV Genarator
* @augments
* @example node env-generator.js env=dev
*
*/
const fs = require("fs");
const env = process.argv.slice(2)[0].substr(4);
const filename = "./env.js";
fs.open(filename, "r", (err, fd) => {
console.log("APP_ENV =", env);
if (err) {
fs.writeFile(filename, `const APP_ENV = "${env}";\nexport default APP_ENV;`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
});
} else {
fs.writeFile(filename, `const APP_ENV = "${env}";\nexport default APP_ENV;`, (err, fd) => {
if(err) {
console.log(`writeFile err =`, err);
}
});
}
});
app.js
import APP_ENV from "./env";
console.log(`APP_ENV = `, APP_ENV, typeof(APP_ENV), APP_ENV.length);
// import * as APP_ENV from "./env";
// console.log(`APP_ENV = `, APP_ENV, typeof(APP_ENV), APP_ENV.length);
// APP_ENV = {APP_ENV: "dev", __esModule: true}
import * as ENV from "./env";
console.log(`ENV.APP_ENV = `, ENV.APP_ENV, typeof(ENV.APP_ENV), ENV.APP_ENV.length);
// ENV.APP_ENV = dev string 3
// import * as APP_ENV from "./env";
// console.log(`APP_ENV = `, APP_ENV, typeof(APP_ENV), APP_ENV.length);
// APP_ENV = {APP_ENV: "dev", __esModule: true}
// import * as ENV from "./env";
// console.log(`ENV.APP_ENV = `, ENV.APP_ENV, typeof(ENV.APP_ENV), ENV.APP_ENV.length);
// ENV.APP_ENV = dev string 3
import {APP_ENV} from "./env";
console.log(`APP_ENV = `, APP_ENV, typeof(APP_ENV), APP_ENV.length);
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10855749.html
未经授权禁止转载,违者必究!