UNPKG

sdin

Version:

JavaScript package builder.

2 lines 2.5 kB
import{resolve as e}from"path";import{pathExists as t,pathExistsSync as a}from"fs-extra";import{readExportsSync as r}from"./read";import{printTask as o,green as i,red as c,yellow as s}from"./print";import{isEmail as u,isPackageName as m}from"./check";import{isPlainObject as l}from"lodash";import{execute as h}from"./execute";import{NpmError as f}from"./errors";let n={name:"unknown",version:"1.0.0",description:"unknown",author:"unknown <unknown@unknown.domain>",authorName:"unknown",authorEmail:"unknown@unknown.domain"},_="package.json",E=/^(.+) <(.+)>$/,d=/[\/\?]+/;function p(o){let n=o;for(let o=0;n&&o<10;o++){if(a(e(n,_)))return n;n=e(n,"../")}throw new f(f.NOT_HAS_ROOT_PATH,"Cannot find root path.")}function w(o,n){var o=e(o,_),t=r(o,n);if(l(t)){if(!(t.name&&t.version&&t.description&&t.author))throw new f(f.PACKAGE_JSON_MISSING_REQUIRED_FIELD,"".concat(_,' must contain "name", "version", "description", "author" fields'));if(!m(t.name))throw new f(f.PACKAGE_NAME_IS_NOT_KEBAB_CASE,"Please change package name to kebab-case");var a=t.author;if("object"==typeof a)t.authorName=a.name||"",t.authorEmail=a.email||"",t.authorEmail?t.author="".concat(t.authorName," <").concat(t.authorEmail,">"):t.author=t.authorName;else{a=E.exec(a);if(!(a&&3<=a.length))throw new f(f.PACKAGE_AUTHOR_MARK_FORMAT_ERROR,'Please change author to "name <xxx@xxx.xxx>" in '.concat(_));t.authorName=a[1],t.authorEmail=a[2]}if(u(t.authorEmail))return t;throw new f(f.PACKAGE_AUTHOR_EMAIL_FORMAT_ERROR,"Author email format error in ".concat(_))}if(n)throw new f(f.PACKAGE_JSON_CONTENT_IS_NOT_PLAIN_OBJECT,"File ".concat(o," exports is not ").concat(_," content."))}function A(o){var n=o.split(d);return 1===n.length?o:"@"===n[0][0]?n[0]+"/"+n[1]:n[0]}function O(o,n){return o.dependencies[n]||o.devDependencies[n]||""}async function x(n){return o({exitCode:f.DOWNLOAD_MODULES_FAILED,task:async o=>{o=o.loading;if(!await t(n))throw new f(f.DOWNLOAD_MODULES_ROOT_IS_NOT_EXIST,"Folder ".concat(n," is not exist."));await h("cd ".concat(n," && npm i"),o)},loading:o=>{if(o){if(o.toString)return o.toString();if("string"==typeof o)return o}return"Downloading modules to ".concat(s(n),".")},resolve:()=>"Successfully downloaded modules to ".concat(i(n),"."),reject:()=>"Failed to download modules to ".concat(c(n),".")})}export{n as PACKAGE_INFO,_ as PACKAGE_INFO_FILE_NAME,p as getPackageRootPath,w as readPackageInfo,A as getDependenceName,O as getDependenceVersion,x as downloadModules}; //# sourceMappingURL=npm.js.map