xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

node.js & read argv

node.js & read argv

https://nodejs.org/docs/latest/api/process.html

https://flaviocopes.com/node-cli-args/

https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments-to-a-node-js-program


"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);


posted @ 2019-05-13 11:36  xgqfrms  阅读(287)  评论(4编辑  收藏  举报