From e476a320b35b5d72f0766b6139143c514f77220d Mon Sep 17 00:00:00 2001 From: onvia <272493431@qq.com> Date: Sat, 22 Jul 2023 15:43:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8C=85=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ccc-tnt-psd2ui-v3.4.+/dist/main.js | 306 +++++++++--------- .../dist/panels/default/index.js | 242 +++++++------- ccc-tnt-psd2ui-v3.4.+/src/main.ts | 35 +- package-lock.json | 190 +++++++++++ package.json | 15 + src/index.js | 130 ++++++++ 6 files changed, 646 insertions(+), 272 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/index.js diff --git a/ccc-tnt-psd2ui-v3.4.+/dist/main.js b/ccc-tnt-psd2ui-v3.4.+/dist/main.js index 5545d7a..4d7f59e 100644 --- a/ccc-tnt-psd2ui-v3.4.+/dist/main.js +++ b/ccc-tnt-psd2ui-v3.4.+/dist/main.js @@ -1,144 +1,162 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.unload = exports.load = exports.methods = void 0; -//@ts-ignore -const package_json_1 = __importDefault(require("../package.json")); -const fs_extra_1 = __importDefault(require("fs-extra")); -const path_1 = __importDefault(require("path")); -const child_process_1 = __importDefault(require("child_process")); -let exec = child_process_1.default.exec; -const ENGINE_VER = "v342"; // -const projectAssets = path_1.default.join(Editor.Project.path, "assets"); -const cacheFile = path_1.default.join(Editor.Project.path, "local", "psd-to-prefab-cache.json"); -const commandBat = path_1.default.join(Editor.Project.path, `extensions\\${package_json_1.default.name}\\libs\\psd2ui\\command.bat`); -const configFile = path_1.default.join(Editor.Project.path, `extensions\\${package_json_1.default.name}\\config\\psd.config.json`); -let uuid2md5 = new Map(); -let cacheFileJson = {}; -/** - * @en - * @zh 为扩展的主进程的注册方法 - */ -exports.methods = { - openPanel() { - Editor.Panel.open(package_json_1.default.name); - }, - onClickPsd2UICache() { - console.log(`main-> onClickPsd2UICache111 `); - return new Promise((resolve, reject) => { - console.log(`main-> onClickPsd2UICache`); - let options = { - "project-assets": projectAssets, - "cache": cacheFile, - "init": true, - "engine-version": ENGINE_VER - }; - Promise.all(_exec(options, [])).then(() => { - console.log("[psd2prefab] 执行缓存结束"); - resolve(); - }); - }); - }, - async onPsd2UIDropFiles(param) { - let files = param.files; - let isForceImg = param.isForceImg; - let isImgOnly = param.isImgOnly; - let output = param.output; - let options = { - "project-assets": projectAssets, - "cache": cacheFile, - "engine-version": ENGINE_VER, - }; - let tasks = []; - for (let i = 0; i < files.length; i++) { - const file = files[i]; - let stat = fs_extra_1.default.statSync(file); - if (stat.isFile()) { - let ext = path_1.default.extname(file); - if (ext != '.psd') { - continue; - } - } - let args = JSON.parse(JSON.stringify(options)); - args["input"] = file; - if (output) { - args["output"] = output; - } - if (isImgOnly) { - // 只导出图片 - args["img-only"] = true; - } - else { - // 强制导出图片 - if (isForceImg) { - args["force-img"] = true; - } - args["config"] = configFile; - } - _exec(args, tasks); - } - await Promise.all(tasks); - genUUID2MD5Mapping(); - console.log("[ccc-tnt-psd2ui] psd 导出完成,输出位置为:", output ? output : "psd 同级目录"); - }, -}; -function _exec(options, tasks) { - let jsonContent = JSON.stringify(options); - // console.log("[ccc-tnt-psd2ui] 批处理命令参数:" + jsonContent); - let base64 = Buffer.from(jsonContent).toString("base64"); - // console.log('[ccc-tnt-psd2ui] start ' + commandBat + ' ' + `--json ${base64}`); - tasks.push(new Promise((rs) => { - exec('start ' + commandBat + ' ' + `--json ${base64}`, { windowsHide: false }, (err, stdout, stderr) => { - rs(); - }); - })); - return tasks; -} -/** - * 资源删除的监听 - * - * @param {*} event - */ -function onAssetDeletedListener(event) { - if (uuid2md5.has(event)) { - let md5 = uuid2md5.get(event); - console.log(`[ccc-tnt-psd2ui] 删除资源 md5: ${md5}, uuid: ${event}`); - delete cacheFileJson[`${md5}`]; - fs_extra_1.default.writeFileSync(cacheFile, JSON.stringify(cacheFileJson, null, 2)); - } -} -/** - * 生成 uuid 转 MD5 的映射 - * - */ -function genUUID2MD5Mapping() { - if (!fs_extra_1.default.existsSync(cacheFile)) { - return; - } - let content = fs_extra_1.default.readFileSync(cacheFile, 'utf-8'); - let obj = JSON.parse(content); - cacheFileJson = obj; - for (const key in obj) { - const element = obj[key]; - uuid2md5.set(element.textureUuid, key); - } -} -/** - * @en Hooks triggered after extension loading is complete - * @zh 扩展加载完成后触发的钩子 - */ -const load = function () { - genUUID2MD5Mapping(); - Editor.Message.addBroadcastListener("asset-db:asset-delete", onAssetDeletedListener); -}; -exports.load = load; -/** - * @en Hooks triggered after extension uninstallation is complete - * @zh 扩展卸载完成后触发的钩子 - */ -const unload = function () { - Editor.Message.removeBroadcastListener("asset-db:asset-delete", onAssetDeletedListener); -}; -exports.unload = unload; +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.unload = exports.load = exports.methods = void 0; +//@ts-ignore +const package_json_1 = __importDefault(require("../package.json")); +const fs_extra_1 = __importDefault(require("fs-extra")); +const path_1 = __importDefault(require("path")); +const os_1 = __importDefault(require("os")); +const child_process_1 = __importDefault(require("child_process")); +let exec = child_process_1.default.exec; +const ENGINE_VER = "v342"; // +const packagePath = path_1.default.join(Editor.Project.path, "extensions", package_json_1.default.name); +const projectAssets = path_1.default.join(Editor.Project.path, "assets"); +const cacheFile = path_1.default.join(Editor.Project.path, "local", "psd-to-prefab-cache.json"); +const configFile = path_1.default.join(`${packagePath}/config/psd.config.json`); +const nodejsFile = path_1.default.join(packagePath, "bin", `node${os_1.default.platform() == 'darwin' ? "" : ".exe"}`); +const psd = path_1.default.join(packagePath, "libs", "psd2ui", "index.js"); +let uuid2md5 = new Map(); +let cacheFileJson = {}; +/** + * @en + * @zh 为扩展的主进程的注册方法 + */ +exports.methods = { + openPanel() { + Editor.Panel.open(package_json_1.default.name); + }, + onClickPsd2UICache() { + console.log(`main-> onClickPsd2UICache111 `); + return new Promise((resolve, reject) => { + console.log(`main-> onClickPsd2UICache`); + let options = { + "project-assets": projectAssets, + "cache": cacheFile, + "init": true, + "engine-version": ENGINE_VER + }; + Promise.all(_exec(options, [])).then(() => { + console.log("[psd2prefab] 执行缓存结束"); + resolve(); + }); + }); + }, + async onPsd2UIDropFiles(param) { + let files = param.files; + let isForceImg = param.isForceImg; + let isImgOnly = param.isImgOnly; + let output = param.output; + let options = { + "project-assets": projectAssets, + "cache": cacheFile, + "engine-version": ENGINE_VER, + }; + let tasks = []; + for (let i = 0; i < files.length; i++) { + const file = files[i]; + let stat = fs_extra_1.default.statSync(file); + if (stat.isFile()) { + let ext = path_1.default.extname(file); + if (ext != '.psd') { + continue; + } + } + let args = JSON.parse(JSON.stringify(options)); + args["input"] = file; + if (output) { + args["output"] = output; + } + if (isImgOnly) { + // 只导出图片 + args["img-only"] = true; + } + else { + // 强制导出图片 + if (isForceImg) { + args["force-img"] = true; + } + args["config"] = configFile; + } + _exec(args, tasks); + } + await Promise.all(tasks); + genUUID2MD5Mapping(); + console.log("[ccc-tnt-psd2ui] psd 导出完成,输出位置为:", output ? output : "psd 同级目录"); + }, +}; +function _exec(options, tasks) { + let jsonContent = JSON.stringify(options); + if (!fs_extra_1.default.existsSync(nodejsFile)) { + console.log(`main-> 没有批处理文件`, nodejsFile); + return tasks; + } + // 处理权限问题 + if (os_1.default.platform() === 'darwin') { + if (fs_extra_1.default.statSync(nodejsFile).mode != 33261) { + console.log(`[ccc-tnt-psd2ui] 设置权限`); + fs_extra_1.default.chmodSync(nodejsFile, 33261); + } + } + console.log("[ccc-tnt-psd2ui] 批处理命令参数:" + jsonContent); + let base64 = Buffer.from(jsonContent).toString("base64"); + tasks.push(new Promise((rs) => { + // console.log(`main-> `, `${nodejsFile} ${psd}` + ' ' + `--json ${base64}`); + exec(`${nodejsFile} ${psd}` + ' ' + `--json ${base64}`, { windowsHide: false }, (err, stdout, stderr) => { + console.log("[ccc-tnt-psd2ui]:\n", stdout); + if (stderr) { + console.log(stderr); + } + rs(); + }); + })); + return tasks; +} +/** + * 资源删除的监听 + * + * @param {*} event + */ +function onAssetDeletedListener(event) { + if (uuid2md5.has(event)) { + let md5 = uuid2md5.get(event); + console.log(`[ccc-tnt-psd2ui] 删除资源 md5: ${md5}, uuid: ${event}`); + delete cacheFileJson[`${md5}`]; + fs_extra_1.default.writeFileSync(cacheFile, JSON.stringify(cacheFileJson, null, 2)); + } +} +/** + * 生成 uuid 转 MD5 的映射 + * + */ +function genUUID2MD5Mapping() { + if (!fs_extra_1.default.existsSync(cacheFile)) { + return; + } + let content = fs_extra_1.default.readFileSync(cacheFile, 'utf-8'); + let obj = JSON.parse(content); + cacheFileJson = obj; + for (const key in obj) { + const element = obj[key]; + uuid2md5.set(element.textureUuid, key); + } +} +/** + * @en Hooks triggered after extension loading is complete + * @zh 扩展加载完成后触发的钩子 + */ +const load = function () { + genUUID2MD5Mapping(); + Editor.Message.addBroadcastListener("asset-db:asset-delete", onAssetDeletedListener); +}; +exports.load = load; +/** + * @en Hooks triggered after extension uninstallation is complete + * @zh 扩展卸载完成后触发的钩子 + */ +const unload = function () { + Editor.Message.removeBroadcastListener("asset-db:asset-delete", onAssetDeletedListener); +}; +exports.unload = unload; diff --git a/ccc-tnt-psd2ui-v3.4.+/dist/panels/default/index.js b/ccc-tnt-psd2ui-v3.4.+/dist/panels/default/index.js index b697fdb..b7b8d6f 100644 --- a/ccc-tnt-psd2ui-v3.4.+/dist/panels/default/index.js +++ b/ccc-tnt-psd2ui-v3.4.+/dist/panels/default/index.js @@ -1,121 +1,121 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const fs_extra_1 = require("fs-extra"); -const path_1 = require("path"); -const vue_1 = require("vue"); -const weakMap = new WeakMap(); -const AssetDir = `${Editor.Project.path}/assets`; -/** - * @zh 如果希望兼容 3.3 之前的版本可以使用下方的代码 - * @en You can add the code below if you want compatibility with versions prior to 3.3 - */ -// Editor.Panel.define = Editor.Panel.define || function(options: any) { return options } -module.exports = Editor.Panel.define({ - listeners: { - show() { }, - hide() { }, - }, - template: (0, fs_extra_1.readFileSync)((0, path_1.join)(__dirname, '../../../static/template/default/index.html'), 'utf-8'), - style: (0, fs_extra_1.readFileSync)((0, path_1.join)(__dirname, '../../../static/style/default/index.css'), 'utf-8'), - $: { - app: '#app', - }, - methods: {}, - ready() { - if (this.$.app) { - const app = (0, vue_1.createApp)({}); - app.config.compilerOptions.isCustomElement = (tag) => tag.startsWith('ui-'); - app.component('psd2ui', { - template: (0, fs_extra_1.readFileSync)((0, path_1.join)(__dirname, '../../../static/template/vue/psd2ui.html'), 'utf-8'), - data() { - return { - isImgOnly: false, - isForceImg: false, - isProcessing: false, - outputPath: "", - }; - }, - created() { - let str = localStorage.getItem(`${Editor.Project.name}_psd2ui_output`); - if (str) { - this.outputPath = str; - } - }, - beforeUnmount() { - localStorage.setItem(`${Editor.Project.name}_psd2ui_output`, this.outputPath); - }, - methods: { - async onClickCache() { - if (this.isProcessing) - return; - this.isProcessing = true; - await Editor.Message.request("ccc-tnt-psd2ui", "on-click-cache"); - this.isProcessing = false; - }, - onForceChanged(e) { - this.isForceImg = !this.isForceImg; - }, - onImgOnlyChanged() { - this.isImgOnly = !this.isImgOnly; - }, - async onClickDropArea(event) { - if (this.isProcessing) { - Editor.Dialog.warn("当前有正在处理的文件,请等待完成。\n如果已完成,请关闭 DOS 窗口。"); - return; - } - let result = await Editor.Dialog.select({ - 'multi': true, - 'type': "file", - 'filters': [ - { - 'extensions': ["psd"], - 'name': "请选择 PSD" - } - ] - }); - let files = result.filePaths; - this.processPsd(files); - }, - onDragEnter(event) { - event.stopPropagation(); - event.preventDefault(); - // event.target.add("drag-hovering") - }, - onDragLeave(event) { - event.stopPropagation(); - event.preventDefault(); - // event.target.remove("drag-hovering") - }, - async onDropFiles(event) { - let files = []; - [].forEach.call(event.dataTransfer.files, function (file) { - files.push(file.path); - }, false); - this.processPsd(files); - }, - async processPsd(files) { - if (!files.length) { - return; - } - if (this.isProcessing) { - Editor.Dialog.warn("当前有正在处理的文件,请等待完成。\n如果已完成,请关闭 DOS 窗口。"); - return; - } - this.isProcessing = true; - await Editor.Message.request("ccc-tnt-psd2ui", "on-drop-file", { output: this.outputPath, files, isForceImg: this.isForceImg, isImgOnly: this.isImgOnly }); - this.isProcessing = false; - } - }, - }); - app.mount(this.$.app); - weakMap.set(this, app); - } - }, - beforeClose() { }, - close() { - const app = weakMap.get(this); - if (app) { - app.unmount(); - } - }, -}); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs_extra_1 = require("fs-extra"); +const path_1 = require("path"); +const vue_1 = require("vue"); +const weakMap = new WeakMap(); +const AssetDir = `${Editor.Project.path}/assets`; +/** + * @zh 如果希望兼容 3.3 之前的版本可以使用下方的代码 + * @en You can add the code below if you want compatibility with versions prior to 3.3 + */ +// Editor.Panel.define = Editor.Panel.define || function(options: any) { return options } +module.exports = Editor.Panel.define({ + listeners: { + show() { }, + hide() { }, + }, + template: (0, fs_extra_1.readFileSync)((0, path_1.join)(__dirname, '../../../static/template/default/index.html'), 'utf-8'), + style: (0, fs_extra_1.readFileSync)((0, path_1.join)(__dirname, '../../../static/style/default/index.css'), 'utf-8'), + $: { + app: '#app', + }, + methods: {}, + ready() { + if (this.$.app) { + const app = (0, vue_1.createApp)({}); + app.config.compilerOptions.isCustomElement = (tag) => tag.startsWith('ui-'); + app.component('psd2ui', { + template: (0, fs_extra_1.readFileSync)((0, path_1.join)(__dirname, '../../../static/template/vue/psd2ui.html'), 'utf-8'), + data() { + return { + isImgOnly: false, + isForceImg: false, + isProcessing: false, + outputPath: "", + }; + }, + created() { + let str = localStorage.getItem(`${Editor.Project.name}_psd2ui_output`); + if (str) { + this.outputPath = str; + } + }, + beforeUnmount() { + localStorage.setItem(`${Editor.Project.name}_psd2ui_output`, this.outputPath); + }, + methods: { + async onClickCache() { + if (this.isProcessing) + return; + this.isProcessing = true; + await Editor.Message.request("ccc-tnt-psd2ui", "on-click-cache"); + this.isProcessing = false; + }, + onForceChanged(e) { + this.isForceImg = !this.isForceImg; + }, + onImgOnlyChanged() { + this.isImgOnly = !this.isImgOnly; + }, + async onClickDropArea(event) { + if (this.isProcessing) { + Editor.Dialog.warn("当前有正在处理的文件,请等待完成。\n如果已完成,请关闭 DOS 窗口。"); + return; + } + let result = await Editor.Dialog.select({ + 'multi': true, + 'type': "file", + 'filters': [ + { + 'extensions': ["psd"], + 'name': "请选择 PSD" + } + ] + }); + let files = result.filePaths; + this.processPsd(files); + }, + onDragEnter(event) { + event.stopPropagation(); + event.preventDefault(); + // event.target.add("drag-hovering") + }, + onDragLeave(event) { + event.stopPropagation(); + event.preventDefault(); + // event.target.remove("drag-hovering") + }, + async onDropFiles(event) { + let files = []; + [].forEach.call(event.dataTransfer.files, function (file) { + files.push(file.path); + }, false); + this.processPsd(files); + }, + async processPsd(files) { + if (!files.length) { + return; + } + if (this.isProcessing) { + Editor.Dialog.warn("当前有正在处理的文件,请等待完成。\n如果已完成,请关闭 DOS 窗口。"); + return; + } + this.isProcessing = true; + await Editor.Message.request("ccc-tnt-psd2ui", "on-drop-file", { output: this.outputPath, files, isForceImg: this.isForceImg, isImgOnly: this.isImgOnly }); + this.isProcessing = false; + } + }, + }); + app.mount(this.$.app); + weakMap.set(this, app); + } + }, + beforeClose() { }, + close() { + const app = weakMap.get(this); + if (app) { + app.unmount(); + } + }, +}); diff --git a/ccc-tnt-psd2ui-v3.4.+/src/main.ts b/ccc-tnt-psd2ui-v3.4.+/src/main.ts index d043cd2..e27bc2e 100644 --- a/ccc-tnt-psd2ui-v3.4.+/src/main.ts +++ b/ccc-tnt-psd2ui-v3.4.+/src/main.ts @@ -2,16 +2,21 @@ import packageJSON from '../package.json'; import fs from 'fs-extra'; import path from 'path'; +import Os from 'os'; import child_process from "child_process"; let exec = child_process.exec; const ENGINE_VER = "v342"; // +const packagePath = path.join(Editor.Project.path, "extensions", packageJSON.name); const projectAssets = path.join(Editor.Project.path, "assets"); const cacheFile = path.join(Editor.Project.path, "local", "psd-to-prefab-cache.json"); -const commandBat = path.join(Editor.Project.path, `extensions\\${packageJSON.name}\\libs\\psd2ui\\command.bat`); -const configFile = path.join(Editor.Project.path, `extensions\\${packageJSON.name}\\config\\psd.config.json`); +const configFile = path.join(`${packagePath}/config/psd.config.json`); + +const nodejsFile = path.join(packagePath, "bin", `node${Os.platform() == 'darwin' ? "" : ".exe"}`); +const psd = path.join(packagePath, "libs", "psd2ui", "index.js"); + let uuid2md5: Map = new Map(); let cacheFileJson: Record = {}; /** @@ -92,14 +97,30 @@ export const methods: { [key: string]: (...any: any) => any } = { console.log("[ccc-tnt-psd2ui] psd 导出完成,输出位置为:", output ? output : "psd 同级目录"); }, }; -function _exec(options: Record, tasks: Promise[]) { +function _exec(options: any, tasks: any) { let jsonContent = JSON.stringify(options); + if (!fs.existsSync(nodejsFile)) { + console.log(`main-> 没有批处理文件`, nodejsFile); - // console.log("[ccc-tnt-psd2ui] 批处理命令参数:" + jsonContent); + return tasks; + } + // 处理权限问题 + if (Os.platform() === 'darwin') { + if (fs.statSync(nodejsFile).mode != 33261) { + console.log(`[ccc-tnt-psd2ui] 设置权限`); + fs.chmodSync(nodejsFile, 33261); + } + } + + console.log("[ccc-tnt-psd2ui] 批处理命令参数:" + jsonContent); let base64 = Buffer.from(jsonContent).toString("base64"); - // console.log('[ccc-tnt-psd2ui] start ' + commandBat + ' ' + `--json ${base64}`); tasks.push(new Promise((rs) => { - exec('start ' + commandBat + ' ' + `--json ${base64}`, { windowsHide: false }, (err, stdout, stderr) => { + // console.log(`main-> `, `${nodejsFile} ${psd}` + ' ' + `--json ${base64}`); + exec(`${nodejsFile} ${psd}` + ' ' + `--json ${base64}`, { windowsHide: false }, (err, stdout, stderr) => { + console.log("[ccc-tnt-psd2ui]:\n",stdout); + if (stderr) { + console.log(stderr); + } rs(); }) })); @@ -116,7 +137,7 @@ function onAssetDeletedListener(event: any) { let md5 = uuid2md5.get(event); console.log(`[ccc-tnt-psd2ui] 删除资源 md5: ${md5}, uuid: ${event}`); delete cacheFileJson[`${md5}`]; - fs.writeFileSync(cacheFile,JSON.stringify(cacheFileJson,null,2)); + fs.writeFileSync(cacheFile, JSON.stringify(cacheFileJson, null, 2)); } } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..6b834b8 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,190 @@ +{ + "name": "psd2ui-release", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "psd2ui-release", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "jszip": "^3.10.1" + } + }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmmirror.com/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "node_modules/immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmmirror.com/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==" + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmmirror.com/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "node_modules/jszip": { + "version": "3.10.1", + "resolved": "https://registry.npmmirror.com/jszip/-/jszip-3.10.1.tgz", + "integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==", + "dependencies": { + "lie": "~3.3.0", + "pako": "~1.0.2", + "readable-stream": "~2.3.6", + "setimmediate": "^1.0.5" + } + }, + "node_modules/lie": { + "version": "3.3.0", + "resolved": "https://registry.npmmirror.com/lie/-/lie-3.3.0.tgz", + "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==", + "dependencies": { + "immediate": "~3.0.5" + } + }, + "node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmmirror.com/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==" + }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmmirror.com/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmmirror.com/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==" + }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + } + }, + "dependencies": { + "core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmmirror.com/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmmirror.com/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==" + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmmirror.com/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "jszip": { + "version": "3.10.1", + "resolved": "https://registry.npmmirror.com/jszip/-/jszip-3.10.1.tgz", + "integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==", + "requires": { + "lie": "~3.3.0", + "pako": "~1.0.2", + "readable-stream": "~2.3.6", + "setimmediate": "^1.0.5" + } + }, + "lie": { + "version": "3.3.0", + "resolved": "https://registry.npmmirror.com/lie/-/lie-3.3.0.tgz", + "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==", + "requires": { + "immediate": "~3.0.5" + } + }, + "pako": { + "version": "1.0.11", + "resolved": "https://registry.npmmirror.com/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==" + }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmmirror.com/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmmirror.com/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..00fb2f7 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "psd2ui-release", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "release24x": "", + "release34x": "" + }, + "author": "", + "license": "ISC", + "dependencies": { + "jszip": "^3.10.1" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..31e46db --- /dev/null +++ b/src/index.js @@ -0,0 +1,130 @@ +const fs = require('fs'); +const path = require('path'); +const JSZIP = require('jszip'); +const OS = require('os'); +const child_process = require('child_process'); + +const root = path.join(__dirname, "../"); +let tmpFolder = ""; +// 排除部分文件或文件夹 +const exclude = name => !['@types', 'node_modules'].includes(name) + +// zip 递归读取文件夹下的文件流 +function readDir(zip, nowPath) { + // 读取目录中的所有文件及文件夹(同步操作) + let files = fs.readdirSync(nowPath) + + //遍历检测目录中的文件 + files.filter(exclude).forEach((fileName, index) => { + // 打印当前读取的文件名 + console.log(fileName, index) + + // 当前文件的全路径 + let fillPath = path.join(nowPath, fileName) + + // 获取一个文件的属性 + let file = fs.statSync(fillPath) + + // 如果是目录的话,继续查询 + if (file.isDirectory()) { + // 压缩对象中生成该目录 + let dirlist = zip.folder(fileName) + // (递归)重新检索目录文件 + readDir(dirlist, fillPath) + } else { + // 压缩目录添加文件 + zip.file(fileName, fs.readFileSync(fillPath)) + } + }) +} + +// 开始压缩文件 +function zipFolder({ target = __dirname, output = __dirname + '/result.zip' }) { + // 创建 zip 实例 + const zip = new JSZIP() + + // zip 递归读取文件夹下的文件流 + readDir(zip, target) + + // 设置压缩格式,开始打包 + zip.generateAsync({ + // nodejs 专用 + type: 'nodebuffer', + // 压缩算法 + compression: 'DEFLATE', + // 压缩级别 + compressionOptions: { level: 9, }, + }).then(content => { + // 将打包的内容写入 当前目录下的 result.zip中 + fs.writeFileSync(output, content, 'utf-8') + }) +} + +// zipFolder({ +// // 目标文件夹 +// target: path.join(__dirname, 'test'), +// // 输出 zip +// output: __dirname + '/result.zip' +// }) + + +console.log(`index-> `, path.join(__dirname, "../", 'ccc-tnt-psd2ui-v3.4.+')); + + +function taskStart(name, callback) { + tmpFolder = path.join(root, `${name}_tmp`); + callback(name); +} + +function copyPlguin(name, callback) { + + fs.copyFile(path.join(root, name), tmpFolder, () => { + callback(name); + }); +} + +function cd2TmpFolder(name, callback) { + + child_process.exec(`cd ${tmpFolder}`, (err, stdout, stderr) => { + console.log(stdout); + console.log(stderr); + if (err) { + console.log(`child_process err-> `, err); + } else { + callback(name); + } + }); +} + +function unstallPackage(name, callback, packageNames) { + for (let i = 0; i < packageNames.length; i++) { + const element = packageNames[i]; + child_process.exec(`npm uninstall ${element}`, (err, stdout, stderr) => { + console.log(stdout); + console.log(stderr); + if (err) { + console.log(`child_process err-> `, err); + } else { + if (i == packageNames.length - 1) { + callback(name); + } + } + }); + } +} + +taskStart("ccc-tnt-psd2ui-v3.4.+", (name) => { + copyPlguin(name, (name) => { + cd2TmpFolder(name, (name) => { + // unstallPackage(name, (name) => { + + // }, [ + // "@types/fs-extra", + // "@types/node", + // "typescript" + // ]); + }); + }); +}); + +console.log(`index-> `, root);