mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
133 lines
3.7 KiB
JavaScript
133 lines
3.7 KiB
JavaScript
import { createHash } from 'node:crypto';
|
||
import { execFileSync } from 'node:child_process';
|
||
import {
|
||
copyFileSync,
|
||
existsSync,
|
||
mkdirSync,
|
||
readFileSync,
|
||
renameSync,
|
||
rmSync,
|
||
unlinkSync,
|
||
writeFileSync,
|
||
} from 'node:fs';
|
||
import { dirname, resolve } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import { build } from 'vite';
|
||
|
||
/**
|
||
* 构建功能脚本 → artifacts/features,打 zip 壳,并同步到后端 static。
|
||
*/
|
||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||
const root = resolve(__dirname, '..');
|
||
const artifactsRoot = resolve(root, 'artifacts');
|
||
const featuresOutDir = resolve(artifactsRoot, 'features');
|
||
const extensionOutDir = resolve(artifactsRoot, 'extension');
|
||
const zipName = 'ecbi-data-sync-extension.zip';
|
||
const zipLocalPath = resolve(artifactsRoot, zipName);
|
||
|
||
const backendFeaturesStatic = resolve(
|
||
root,
|
||
'../server/src/main/resources/static/extension-features',
|
||
);
|
||
const backendExtensionStatic = resolve(
|
||
root,
|
||
'../server/src/main/resources/static/extension',
|
||
);
|
||
|
||
function syncDirContents(srcDir, destDir) {
|
||
if (existsSync(destDir)) {
|
||
rmSync(destDir, { recursive: true, force: true });
|
||
}
|
||
mkdirSync(destDir, { recursive: true });
|
||
for (const name of ['manifest.json', 'pdd-ad-fee.js', 'pdd-ad-fee.js.map']) {
|
||
const src = resolve(srcDir, name);
|
||
if (existsSync(src)) {
|
||
copyFileSync(src, resolve(destDir, name));
|
||
}
|
||
}
|
||
}
|
||
|
||
function packExtensionZip() {
|
||
if (!existsSync(extensionOutDir)) {
|
||
throw new Error(
|
||
`扩展壳目录不存在: ${extensionOutDir}(请先执行 vite build)`,
|
||
);
|
||
}
|
||
if (existsSync(zipLocalPath)) {
|
||
unlinkSync(zipLocalPath);
|
||
}
|
||
|
||
// zip 内一层目录:ecbi-data-sync-extension/…,解压后直接「加载已解压的扩展」
|
||
const packageDirName = 'ecbi-data-sync-extension';
|
||
const packageDir = resolve(artifactsRoot, packageDirName);
|
||
if (existsSync(packageDir)) {
|
||
rmSync(packageDir, { recursive: true, force: true });
|
||
}
|
||
renameSync(extensionOutDir, packageDir);
|
||
try {
|
||
execFileSync('zip', ['-r', '-q', zipLocalPath, packageDirName], {
|
||
cwd: artifactsRoot,
|
||
});
|
||
} finally {
|
||
renameSync(packageDir, extensionOutDir);
|
||
}
|
||
|
||
mkdirSync(backendExtensionStatic, { recursive: true });
|
||
copyFileSync(zipLocalPath, resolve(backendExtensionStatic, zipName));
|
||
}
|
||
|
||
async function main() {
|
||
mkdirSync(artifactsRoot, { recursive: true });
|
||
|
||
await build({
|
||
configFile: resolve(root, 'vite.features.config.ts'),
|
||
});
|
||
|
||
const entryFile = 'pdd-ad-fee.js';
|
||
const jsPath = resolve(featuresOutDir, entryFile);
|
||
const code = readFileSync(jsPath, 'utf8');
|
||
const sha256 = createHash('sha256').update(code).digest('hex');
|
||
|
||
const manifest = {
|
||
schemaVersion: 1,
|
||
updatedAt: new Date().toISOString(),
|
||
features: [
|
||
{
|
||
id: 'pdd-ad-fee',
|
||
name: '拼多多商品推广费同步',
|
||
version: '0.3.10',
|
||
description:
|
||
'从推广总览读取推广费并同步到 BI(总花费为 0 不记录)',
|
||
matchUrlPatterns: [
|
||
'*://yingxiao.pinduoduo.com/*',
|
||
'https://yingxiao.pinduoduo.com/',
|
||
],
|
||
entry: entryFile,
|
||
sha256,
|
||
executionWorld: 'main',
|
||
},
|
||
],
|
||
};
|
||
|
||
writeFileSync(
|
||
resolve(featuresOutDir, 'manifest.json'),
|
||
`${JSON.stringify(manifest, null, 2)}\n`,
|
||
'utf8',
|
||
);
|
||
|
||
syncDirContents(featuresOutDir, backendFeaturesStatic);
|
||
packExtensionZip();
|
||
|
||
console.log(`[build-features] wrote ${featuresOutDir}`);
|
||
console.log(`[build-features] synced features → ${backendFeaturesStatic}`);
|
||
console.log(
|
||
`[build-features] packed zip → ${resolve(backendExtensionStatic, zipName)}`,
|
||
);
|
||
console.log(`[build-features] pdd-ad-fee sha256=${sha256}`);
|
||
}
|
||
|
||
main().catch((err) => {
|
||
console.error(err);
|
||
process.exit(1);
|
||
});
|