xhs-mp-project 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ import { Project } from "..";
2
+ export declare class Checker {
3
+ project: Project;
4
+ constructor(project: any);
5
+ }
@@ -0,0 +1,9 @@
1
+ export declare const ProjectEvents: {
2
+ readonly CHANGE_EXT_JSON_PATH: "baba:changeExtJsonPath";
3
+ readonly CHANGE_EXT_JSON: "baba:changeExtJson";
4
+ };
5
+ export declare const MAIN_PKG_ROOT = "xhs-main";
6
+ export declare const MiniMode: {
7
+ miniprogram: string;
8
+ minigame: string;
9
+ };
@@ -0,0 +1,12 @@
1
+ import { ProjectConfig } from "./projectConfig";
2
+ import { Checker } from './checker';
3
+ export interface IProjectProps {
4
+ projectPath: string;
5
+ watch?: boolean;
6
+ }
7
+ export declare class Project {
8
+ projectPath: string;
9
+ projectConfig: ProjectConfig;
10
+ checker: Checker;
11
+ constructor(props: IProjectProps);
12
+ }
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var t=require("path"),e=require("events"),n=require("fs-extra"),o=require("lodash");function s(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,o.get?o:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var i=s(t),r=s(n);require("crypto"),require("net");const a=(t,e=100)=>{let n=null;return function(...o){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,o))}},h="baba:changeExtJsonPath",c="baba:changeExtJson",p="miniprogram",l="minigame",u=require("chokidar"),d={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class g extends e.EventEmitter{constructor(t){super(),this._projectJson="project.config.json",this._appJson="app.json",this._extJson="ext.json",this._projectPath="",this._extJsonDir="",this._canUploadExtJson=!1,this._appMode=p,this._isLazyload=!1,this._customEntry="",this.transformAppJsonHandlers={},this.onProjectConfigJsonChange=a((()=>{var t,e,n,s;const{projectConfig:i}=this.snapshot,a=r.readJSONSync(this.projectJsonPath);this._projectJsonContent=a,this.snapshot.projectConfig=o.cloneDeep(a),this.emit("project-config-json-changed",{oldData:i,newData:a}),(null==i?void 0:i.libVersion)!==(null==a?void 0:a.libVersion)&&this.emit("lib-version-changed",{oldData:null==i?void 0:i.libVersion,newData:null==a?void 0:a.libVersion}),(null===(t=null==i?void 0:i.setting)||void 0===t?void 0:t.enableSimulatorV2)!==(null===(e=null==a?void 0:a.setting)||void 0===e?void 0:e.enableSimulatorV2)&&this.emit("simulator-mode-changed",{oldData:null===(n=null==i?void 0:i.setting)||void 0===n?void 0:n.enableSimulatorV2,newData:null===(s=null==a?void 0:a.setting)||void 0===s?void 0:s.enableSimulatorV2})}),100),this.onProjectPrivateJsonChange=a((()=>{const{customEntry:t}=this.snapshot;this.updateCustomEntry();const e=this._customEntry;this.snapshot.customEntry=e,this.emit("project-private-json-changed"),this.emit("custom-entry-changed",{oldData:t,newData:e})}),100),this.onAppJsonChange=a((()=>{const{appJson:t}=this.snapshot,e=r.readJSONSync(this.entryJsonPath);this._appJsonContent=e,this.snapshot.appJson=o.cloneDeep(e),this.emit("app-json-changed",{oldData:t,newData:e})}),100),this.onExtJsonChange=a((()=>{const{extJson:t}=this.snapshot,e=r.readJSONSync(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=o.cloneDeep(e),this.emit(c,{oldData:t,newData:e})}),100),this.getCustomEntry=t=>{var e,n,o,s;const i={pathName:"",query:"",entry:"",item:{}};try{if(!r.existsSync(t))return i;const a=r.readJSONSync(t),h=(null===(n=null===(e=null==a?void 0:a.condition)||void 0===e?void 0:e.miniprogram)||void 0===n?void 0:n.current)||-1,c=null===(s=null===(o=null==a?void 0:a.condition)||void 0===o?void 0:o.miniprogram)||void 0===s?void 0:s.list;if(-1===h)return i;const p=c.filter((t=>t.id===h))[0];return p?(i.pathName=p.pathName,i.query=p.query,"halfPageNativeFunctionalized"===p.launchMode&&(i.query=p.query+="&runtime_mode=2"),i.entry=i.query?`${i.pathName}?${i.query}`:`${i.pathName}`,i):i}catch(t){return console.log("getCustomEntry error: ",t),i}},this.getCompileEntryPage=()=>{var t,e,n,o,s;const i=this.projectPrivateConfigJsonPath,a={path:"",query:"",launchMode:"default"};if(!r.existsSync(i))return a;const h=r.readJsonSync(i),c=null===(e=null===(t=null==h?void 0:h.condition)||void 0===t?void 0:t.miniprogram)||void 0===e?void 0:e.current,p=null===(s=null===(o=null===(n=null==h?void 0:h.condition)||void 0===n?void 0:n.miniprogram)||void 0===o?void 0:o.list)||void 0===s?void 0:s.filter((t=>t.id===c))[0];return p?{path:p.pathName,query:p.query.trim(),launchMode:p.launchMode}:a};const{projectPath:e,watch:n}=t;this.checkFileExist(),this._projectPath=e,this._extJsonDir=r.existsSync(this.extJsonPath)?this.projectPath:"",this._appMode=r.existsSync(i.join(this._projectPath,"game.json"))?l:p,this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),n&&this.watchProject()}get projectPath(){return this._projectPath}get projectJsonPath(){return i.resolve(this._projectPath,this._projectJson)}get miniprogramRoot(){return this.projectJsonContent.miniprogramRoot||""}get miniprogramDir(){const t=i.join(this.projectPath,this.miniprogramRoot);return t.endsWith("/")?t.slice(0,-1):t}get appMode(){return this._appMode}get lazyload(){return this._isLazyload}get projectJsonContent(){return r.pathExistsSync(this.projectJsonPath)?(this._projectJsonContent||(this._projectJsonContent=r.readJSONSync(this.projectJsonPath)),this._projectJsonContent):{}}set projectJsonContent(t){this._projectJsonContent=t,r.writeJSONSync(this.projectJsonPath,t,{spaces:2})}get projectPrivateConfigJsonPath(){return i.resolve(this._projectPath,"project.private.config.json")}get entryJson(){return this.appMode===p?"app.json":"game.json"}get entryJsonPath(){if(!r.existsSync(this.projectJsonPath))return"";const t=r.readJsonSync(this.projectJsonPath);let e=".";return t.miniprogramRoot&&(e=t.miniprogramRoot),i.resolve(this._projectPath,e,this.entryJson)}get appJsonContent(){if(!this.entryJsonPath||!r.existsSync(this.entryJsonPath))throw new Error(`打开项目失败: ${this.entryJson}不存在`);return this._appJsonContent||(this._appJsonContent=r.readJSONSync(this.entryJsonPath)),this._appJsonContent}get extJsonPath(){return i.resolve(this._projectPath,this._extJson)}get extJsonContent(){return this.extJsonPath&&r.existsSync(this.extJsonPath)?(this._extJsonContent||(this._extJsonContent=r.readJSONSync(this.extJsonPath).ext||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(h))}get canUploadExtJson(){return this._canUploadExtJson}get customEntry(){return this._customEntry}get projectAppId(){let t="";try{t=this.projectJsonContent.appid}catch(t){console.log("get projectAppId error:",t)}if(!t)throw new Error("appid can not be empty!!!");return t}watchProject(){this.snapshot=o.cloneDeep({projectConfig:this.projectJsonContent,appJson:this.appJsonContent,extJson:this.extJsonContent,customEntry:this._customEntry});const t=u.watch(this.projectPath,{persistent:!0,ignoreInitial:!0,ignored:/node_modules/});return t.on("change",(t=>{this.onFileChange("change",t)})).on("add",(t=>{this.onFileChange("add",t)})).on("unlink",(t=>{this.onFileChange("unlink",t)})),t}onFileChange(t,e){const n=i.relative(this.projectPath,e);"change"===t&&("project.config.json"===n?this.onProjectConfigJsonChange():"project.private.config.json"===n?this.onProjectPrivateJsonChange():"ext.json"===n?this.onExtJsonChange():"app.json"===n&&this.onAppJsonChange()),this.emit("onFileChange",{eventName:t,filePath:e,relativePath:n})}checkFileExist(){if(!this._projectPath)return;const t=this.projectJsonPath;r.existsSync(t)||r.writeJSONSync(this.projectJsonPath,d,{spaces:2})}updateCustomEntry(){const t=this.getCustomEntry(this.projectPrivateConfigJsonPath),e=(null==t?void 0:t.entry)||"";this._customEntry=e}get settings(){var t;return{minified:!0,urlCheck:!0,useNewCompiler:!1,useLiteCompiler:!0,enableV2:!1,...(null===(t=this.projectJsonContent)||void 0===t?void 0:t.setting)||{}}}changeCompileSetting(t){const e=this.projectJsonContent;e.setting?e.setting={...e.setting,...t}:e.setting=t,this.projectJsonContent=e}createDefaultProjectConfig(t){const e={description:"项目配置文件",setting:{urlCheck:!0,minified:!0},compileType:"miniprogram",libVersion:"latest",appid:"wx93e095ba089dbfb5",projectname:i.basename(t)},n=i.resolve(t,this._projectJson);r.writeJSONSync(n,e,{spaces:2})}changeExtJsonPermission(t){this._canUploadExtJson=t,t&&r.existsSync(this.extJsonPath)?this.extJsonDir=this.projectPath:this.extJsonDir=""}getSubPackages(){return this.appJsonContent.subPackages||[]}isFileInMainPkg(t){return this.getSubPackages().every((e=>!t.startsWith(`${e}/`)))}isFileInSubpackage(t){return this.getSubPackages().some((e=>t.startsWith(`${e}/`)))}isPkgPage(t,e){let n=[];n="xhs-main"===t?this.appJsonContent.pages||[]:t.pages||[];const o=i.extname(e);return n.some((t=>`${t}${o}`===e))}formatGameAppJSON(t){return t.map((t=>{let e=((t="")=>t.split(i.sep).join(i.posix.sep))(`${t.root}`).replace(/^\/+|\/+$/g,"");if(/\.(ts|js)$/.test(e)){const t=e.split("/").filter(Boolean);e=t.slice(0,t.length-1).join("/")}return{root:e}}))}}class J{constructor(t){this.project=t}}exports.Project=class{constructor(t){const{projectPath:e}=t;this.projectPath=e,this.projectConfig=new g(t),this.checker=new J(this)}};
@@ -0,0 +1,73 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ interface IProjectProps {
4
+ projectPath: string;
5
+ watch?: boolean;
6
+ }
7
+ declare class ProjectConfig extends EventEmitter {
8
+ private _projectJson;
9
+ private _appJson;
10
+ private _extJson;
11
+ private _projectPath;
12
+ private _extJsonDir;
13
+ private _canUploadExtJson;
14
+ private _appMode;
15
+ private _isLazyload;
16
+ private _customEntry;
17
+ private snapshot;
18
+ private transformAppJsonHandlers;
19
+ private _projectJsonContent;
20
+ private _appJsonContent;
21
+ private _extJsonContent;
22
+ constructor(props: IProjectProps);
23
+ get projectPath(): string;
24
+ get projectJsonPath(): string;
25
+ get miniprogramRoot(): any;
26
+ get miniprogramDir(): string;
27
+ get appMode(): string;
28
+ get lazyload(): boolean;
29
+ get projectJsonContent(): any;
30
+ set projectJsonContent(content: any);
31
+ get projectPrivateConfigJsonPath(): string;
32
+ get entryJson(): "app.json" | "game.json";
33
+ get entryJsonPath(): string;
34
+ get appJsonContent(): any;
35
+ get extJsonPath(): string;
36
+ get extJsonContent(): any;
37
+ get extJsonDir(): string;
38
+ set extJsonDir(value: string);
39
+ get canUploadExtJson(): boolean;
40
+ get customEntry(): string;
41
+ get projectAppId(): string;
42
+ watchProject(): any;
43
+ onFileChange(eventName: any, filePath: any): void;
44
+ onProjectConfigJsonChange: (...args: any[]) => void;
45
+ onProjectPrivateJsonChange: (...args: any[]) => void;
46
+ onAppJsonChange: (...args: any[]) => void;
47
+ onExtJsonChange: (...args: any[]) => void;
48
+ private checkFileExist;
49
+ getCustomEntry: (fsPath: string) => {
50
+ pathName: string;
51
+ query: string;
52
+ entry: string;
53
+ item: {};
54
+ };
55
+ updateCustomEntry(): void;
56
+ getCompileEntryPage: () => {
57
+ path: any;
58
+ query: any;
59
+ launchMode: any;
60
+ };
61
+ get settings(): any;
62
+ changeCompileSetting(setting: any): void;
63
+ createDefaultProjectConfig(projectPath: string): void;
64
+ changeExtJsonPermission(permission: boolean): void;
65
+ getSubPackages(): any;
66
+ isFileInMainPkg(filePath: any): any;
67
+ isFileInSubpackage(filePath: any): any;
68
+ isPkgPage(pkg: any, filePath: any): boolean;
69
+ formatGameAppJSON(list: any[]): {
70
+ root: string;
71
+ }[];
72
+ }
73
+ export { ProjectConfig };
@@ -0,0 +1,11 @@
1
+ export declare const throttle: (fn: any, wait?: number) => (...args: any[]) => void;
2
+ export declare const debounce: (func: any, wait?: number) => (...args: any[]) => void;
3
+ export declare const fileSize: (size: number) => string;
4
+ export declare function getMd5(str: any): any;
5
+ export declare function getType(object: any): string;
6
+ export declare function normalizePath(str: any): any;
7
+ export declare const compareVersion: (v1Str: string, v2Str: string) => number;
8
+ export declare const formatPath: (pathStr?: string) => string;
9
+ export declare const toUnixPath: (pathStr?: string) => string;
10
+ export declare const unixJoin: (...paths: string[]) => string;
11
+ export declare const unixResolve: (...paths: string[]) => string;
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "xhs-mp-project",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "publishConfig": {
8
+ "registry": "https://registry.npmjs.org/",
9
+ "access": "public"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "dependencies": {
17
+ "xhs-mp-cli": "^1.0.0",
18
+ "xhs-mp-compiler": "^1.0.0",
19
+ "xhs-mp-core": "^1.0.0",
20
+ "xhs-mp-utils": "^1.0.0",
21
+ "chokidar": "^3.6.0",
22
+ "fs-extra": "^11.2.0",
23
+ "lodash": "^4.17.21",
24
+ "query-string": "^8.1.0"
25
+ },
26
+ "devDependencies": {
27
+ "@rollup/plugin-commonjs": "^25.0.7",
28
+ "@types/node": "14",
29
+ "rimraf": "^5.0.5",
30
+ "rollup": "^4.9.6",
31
+ "rollup-plugin-terser": "^7.0.2",
32
+ "rollup-plugin-typescript2": "^0.36.0",
33
+ "typescript": "^5.4.3"
34
+ },
35
+ "scripts": {
36
+ "clean": "rimraf -rf ./dist",
37
+ "watch": "npm run clean && rollup -c -w --sourcemap true",
38
+ "build": "npm run clean && rollup -c",
39
+ "lint": "eslint --cache --ext .js,.jsx,.ts,.tsx,.vue,svelte ./src",
40
+ "lint:fix": "npm run lint -- --quiet --fix",
41
+ "test": "echo \"todo\"",
42
+ "version:beta": "npm version prerelease --no-git-tag-version"
43
+ }
44
+ }