organize-pkg
Advanced tools
Comparing version 1.2.0 to 1.2.1
16
index.js
@@ -9,15 +9,3 @@ #!/usr/bin/env node | ||
*/ | ||
const utils=require("./utils");const params=utils.getExtraArgs();const cliName="organize-pkg";function help(msg=`${cliName} usage`){console.log(` | ||
${msg} | ||
${cliName} -keys "<key>,<key>,..." -out <output json path> | ||
`);} | ||
function isInvalidValue(value,msg){if(value===void 0||value.length===0){help(msg);return 1;} | ||
return 0} | ||
function organizePackageJson(){const rootPkgJsonPath="./package.json";let pkgJson;try{pkgJson=utils.readJson(rootPkgJsonPath);}catch(e){console.error(e);return;} | ||
if(pkgJson){const keys=params.keys||[];const isProd=params.prod;if(!isProd&&isInvalidValue(keys,`ERROR: "-keys" parameter must be required`)){return;} | ||
let dest=params.out;if(isInvalidValue(dest,`ERROR: "-out" parameter must be required`)){return;} | ||
const needless=isProd?["scripts","devDependencies"]:[];const actulaKeys=Array.isArray(keys)?keys:[keys];if(isProd){Object.keys(pkgJson).forEach(key=>{/^\/\//.test(key)&&delete pkgJson[key];});} | ||
actulaKeys.concat(needless).forEach(key=>{delete pkgJson[key];});if(!/\.json$/.test(dest)){const last=[...dest].pop();dest=(last==="/"||last==="\\")?dest+"package.json":`${dest}/package.json`} | ||
utils.writeTextUTF8(JSON.stringify(pkgJson,null,2),dest);}else{help(`ERROR: package.json not found`);}} | ||
organizePackageJson(); | ||
const utils=require("./utils"),params=utils.getExtraArgs(),cliName="organize-pkg";function help(e=cliName+" usage"){console.log(`\n${e}\n\n ${cliName} -keys "<key>,<key>,..." -out <output json path>\n`)}function isInvalidValue(e,a){return void 0===e||0===e.length?(help(a),1):0}function organizePackageJson(){let e;try{e=utils.readJson("./package.json")}catch(e){return void console.error(e)}if(e){const a=params.k||params.keys||[],s=params.p||params.prod;if(!s&&isInvalidValue(a,"ERROR: \"-keys\" parameter must be required"))return;let t=params.o||params.out;if(isInvalidValue(t,"ERROR: \"-out\" parameter must be required"))return;const r=s?["scripts","devDependencies"]:[],n=Array.isArray(a)?a:[a];if(s&&Object.keys(e).forEach(a=>{/^\/\//.test(a)&&delete e[a]}),n.concat(r).forEach(a=>{ | ||
delete e[a]}),!/\.json$/.test(t)){const e=[...t].pop();t="/"===e||"\\"===e?t+"package.json":t+"/package.json"}utils.writeTextUTF8(JSON.stringify(e,null,2),t)}else help("ERROR: package.json not found")}organizePackageJson(); |
{ | ||
"name": "organize-pkg", | ||
"description": "very simple package.json organizer", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"bin": "index.js", | ||
@@ -6,0 +6,0 @@ "files": [ |
@@ -17,8 +17,15 @@ # organize package json (organize-pkg) ![MIT](https://img.shields.io/badge/license-MIT-brightgreen) | ||
* Since version `1.2.1`, __shorthand option__ is available. | ||
+ `-p` same as `-prod` | ||
+ `-k` same as `-keys` | ||
+ `-o` same as `-out` | ||
```shell | ||
# file name is output as "./dist/package.json" | ||
>organize-pkg -keys "defs,//" -out ./dist | ||
>organize-pkg -k "defs,//" -o ./dist | ||
# file name is output as "./dist/output.json" | ||
>organize-pkg -keys "defs,//" -out ./dist/output.json | ||
>organize-pkg -k "defs,//" -o ./dist/output.json | ||
``` | ||
@@ -48,2 +55,3 @@ | ||
organize-pkg -prod -out ./dist [-keys "defs"] | ||
organize-pkg -p -o ./dist [-k "defs"] | ||
``` | ||
@@ -50,0 +58,0 @@ |
19
utils.js
@@ -8,17 +8,4 @@ /*! | ||
*/ | ||
const fs=require("fs");const path=require("path");const ArgsConfig={startIndex:2,prefix:"-",};function gea(args,args_config=ArgsConfig){const varIndex=args_config.prefix.length;const re=new RegExp(`^${args_config.prefix}\\w+`);const params={};for(let index=0;index<args.length;){const opt=args[index++];if(opt&&re.test(opt)){let value=args[index];if(value===void 0||value.startsWith(args_config.prefix)){value=true;}else{if(/\[.+\]/.test(value)||/^\/[^/]+\/[gimuy]{0,5}/.test(value)){value=eval(value);}else if(/,/.test(value)){value=value.split(",");} | ||
index++;} | ||
params[opt.substring(varIndex)]=value;}} | ||
return params;} | ||
function getExtraArgs(args_config,debug=false){debug&&console.log("process.argv: ",process.argv);args_config=args_config||{};args_config=Object.assign(ArgsConfig,args_config);if(process.argv.length>args_config.startIndex){return gea(process.argv.slice(args_config.startIndex),args_config);} | ||
return{};} | ||
function checkParentDirectory(dest){const parent=path.dirname(dest);if(!fs.existsSync(parent)){fs.mkdirSync(parent,{recursive:true});}} | ||
function writeTextUTF8(content,dest,callback=null){checkParentDirectory(dest);const ws=fs.createWriteStream(dest);ws.on("error",function(err){console.log("WriteStream.error evnet!",arguments);}).on("close",function(){console.log("[close] %s, stream closed",dest);callback&&callback();});if(content instanceof Buffer){content=content.toString();} | ||
if(typeof content==="string"){const success=ws.write(content);console.log("writeTextUTF8: write: %s,",dest,success);if(!success){ws.once("drain",function(){console.log("[drain] file written: %s,",dest,ws.bytesWritten);ws.end();});} | ||
else{callback&&callback();}} | ||
else if("readable"in content){content.pipe(ws);}} | ||
function readTextUTF8(from,callback){if(typeof callback==="function"){fs.readFile(from,"utf8",callback);} | ||
else{return fs.readFileSync(from,"utf8");}} | ||
function readJson(path,callback){if(typeof callback==="function"){readTextUTF8(path,(err,data)=>{callback(err,JSON.parse(data));});} | ||
else{const data=readTextUTF8(path);return JSON.parse(data);}} | ||
module.exports={getExtraArgs,writeTextUTF8,readTextUTF8,readJson,}; | ||
const fs=require("fs"),path=require("path"),ArgsConfig={startIndex:2,prefix:"-"};function gea(args,args_config=ArgsConfig){const varIndex=args_config.prefix.length,re=new RegExp(`^${args_config.prefix}\\w+`),params={};for(let index=0;index<args.length;){const opt=args[index++];if(opt&&re.test(opt)){let value=args[index];void 0===value||value.startsWith(args_config.prefix)?value=!0:(/\[.+\]/.test(value)||/^\/[^/]+\/[gimuy]{0,5}/.test(value)?value=eval(value):/,/.test(value)&&(value=value.split(",")),index++),params[opt.substring(varIndex)]=value}}return params}function getExtraArgs(e,r=!1){return r&&console.log("process.argv: ",process.argv),e=e||{},e=Object.assign(ArgsConfig,e),process.argv.length>e.startIndex?gea(process.argv.slice(e.startIndex),e):{}}function checkParentDirectory(e){ | ||
const r=path.dirname(e);fs.existsSync(r)||fs.mkdirSync(r,{recursive:!0})}function writeTextUTF8(e,r,t=null){checkParentDirectory(r);const n=fs.createWriteStream(r);if(n.on("error",(function(e){console.log("WriteStream.error evnet!",arguments)})).on("close",(function(){console.log("[close] %s, stream closed",r),t&&t()})),e instanceof Buffer&&(e=e.toString()),"string"==typeof e){const s=n.write(e);console.log("writeTextUTF8: write: %s,",r,s),s?t&&t():n.once("drain",(function(){console.log("[drain] file written: %s,",r,n.bytesWritten),n.end()}))}else"readable"in e&&e.pipe(n)}function readTextUTF8(e,r){if("function"!=typeof r)return fs.readFileSync(e,"utf8");fs.readFile(e,"utf8",r)}function readJson(e,r){if("function"!=typeof r){const r=readTextUTF8(e);return JSON.parse(r)} | ||
readTextUTF8(e,(e,t)=>{r(e,JSON.parse(t))})}module.exports={getExtraArgs,writeTextUTF8,readTextUTF8,readJson}; |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
62
6160
25
3