Skip to content

Commit

Permalink
feat: 新增打包脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 15, 2023
1 parent 4192aa1 commit fc49b74
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["./node_modules/@terwer/eslint-config-custom/typescript/index.cjs"],
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ yarn-error.log*
# doc
reports
.vercel
dist
dist
build
11 changes: 11 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# platform

# Ignore artifacts:
dist
node_modules

# Ignore all dts files:
*.d.ts

# lib
/pnpm-lock.yaml
30 changes: 30 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

module.exports = {
semi: false,
singleQuote: false,
printWidth: 120
}
2 changes: 1 addition & 1 deletion libs/publisher-bridge/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@terwer/publisher-bridge",
"name": "publisher-bridge",
"version": "0.0.1",
"type": "module",
"description": "a bridge between publisher plugin and widget",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@terwer/changelog-parser": "^1.1.0",
"@terwer/commit-config-custom": "latest",
"@terwer/eslint-config-custom": "latest",
"archiver": "^5.3.1",
"execa": "^7.1.1",
"turbo": "^1.9.4"
},
Expand Down
159 changes: 152 additions & 7 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

59 changes: 57 additions & 2 deletions tools/packageApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,66 @@
* questions.
*/

import minimist from "minimist"
import { execa } from "execa"
import FileUtils from "./utils/fileUtils"

/**
* 插件打包
*
* @author terwer
* @version 1.0.0
* @since 1.0.0
*/
class PackageApp {
public async packagePlugin(isTest: boolean, skipBuild: boolean) {
if (skipBuild == false) {
// build bridge
await this.runCommand("pnpm", ["build", "-F", "publisher-bridge"])

}
// build plugin
await this.runCommand("pnpm", ["build", "-F", "publisher-main"])
}

// copy to root dist
await FileUtils.cp("./plugins/publisher-main/dist", "./dist/publish-tool", true, true)
await FileUtils.cp("./libs/publisher-bridge/dist", "./dist/publish-tool/lib/bridge", true, true)

// zip to build/package.zip etc.
const zipTo = "./build/package.zip"
await FileUtils.makeZip("./dist/publish-tool", zipTo)
console.log(`plugin packaged to ${zipTo}`)

// 开发测试阶段,拷贝到插件目录
if (isTest) {
// copy to local plugin folder
await FileUtils.cp(
"./dist/publish-tool",
"/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/plugins/publish-tool",
true,
true
)
}
}

private async runCommand(cmd: string, args: string[]) {
const { stdout } = await execa(cmd, args).pipeStdout(process.stdout)
console.log(stdout)
}
}

// 本地生产测试
// pnpm package -t
//
// 生产环境打包
// pnpm package
;(async () => {
const args = minimist(process.argv.slice(2))
const isTest = args.test || args.t || false
const skipBuild = args.test || args.s || false

const packageApp = new PackageApp()
// plugin
await packageApp.packagePlugin(isTest, skipBuild)
console.log("app packaged.")
})()
})()
123 changes: 123 additions & 0 deletions tools/utils/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import fs from "fs"
import path from "path"
import glob from "glob"
import archiver from "archiver"

class FileUtils {
static async cp(sourcePath, destinationPath, r = false, f = false) {
if (!fs.existsSync(sourcePath)) {
throw `文件或目录不存在:${sourcePath}`
}

if (fs.statSync(sourcePath).isFile()) {
throw `${sourcePath} 不是一个目录`
}

if (!fs.existsSync(destinationPath)) {
fs.mkdirSync(destinationPath, { recursive: true })
}

let pattern = `${sourcePath}/*`
if (r) {
pattern = `${sourcePath}/**/*`
}

let count = 0
const files = glob.sync(pattern, { nodir: true })
for (const file of files) {
const dest = path.join(destinationPath, file.replace(sourcePath, ""))
if (!fs.existsSync(path.dirname(dest))) {
fs.mkdirSync(path.dirname(dest), { recursive: true })
}

if (!f && fs.existsSync(dest)) {
throw `目标文件或目录已经存在: ${dest}`
}

try {
fs.copyFileSync(file, dest)
++count
} catch (err) {
throw `复制文件时出错:${file} => ${dest}`
}
}

console.log(`从 ${sourcePath} 复制 ${count} 个文件到 ${destinationPath}`)
}

public static async makeZip(source: string, destination: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
const archive = archiver("zip", { zlib: { level: 9 } })
const output = fs.createWriteStream(destination)

output.on("close", () => {
resolve(destination)
})

archive.on("warning", (err: archiver.ArchiverError) => {
if (err.code === "ENOENT") {
console.warn(err)
} else {
reject(err)
}
})

archive.on("error", (err: archiver.ArchiverError) => {
reject(err)
})

archive.pipe(output)
archive.directory(source, "/")
archive.finalize()
}).then((destination: string) => {
return new Promise<string>((resolve, reject) => {
const targetDirectory = path.dirname(destination)

fs.promises
.mkdir(targetDirectory, { recursive: true })
.then(() => {
resolve(destination)
})
.catch((err) => {
reject(err)
})
})
})
}
}

// 示例用法
// ;(async function () {
// try {
// await FileUtils.cp("./test", "./test-copy", true, true)
// } catch (error) {
// console.error(`执行命令出错: ${error}`)
// }
// })()

export default FileUtils

0 comments on commit fc49b74

Please sign in to comment.