-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
113 lines (92 loc) · 3.98 KB
/
build.js
File metadata and controls
113 lines (92 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* OJS Plugin Build Script
*
* Automates the creation of a release-ready .tar.gz archive.
* Reads the version from version.xml and excludes unnecessary files.
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const pluginDir = 'metadataNormalizer';
const versionXmlPath = path.join(__dirname, 'version.xml');
// List of files and directories to exclude from the release archive
const exclusions = [
'.git',
'.github',
'.gitignore',
'test',
'build.js',
'package.json',
'package-lock.json',
'node_modules',
'*.md', // Exclude markdown files like README.md, CONTRIBUTING.md, or plan artifacts if generated locally
];
// Ensure we keep the main plugin directory structure intact inside the tarball
// OJS expects a plugin to be inside a directory mirroring its name (e.g., metadataNormalizer/)
console.log('📦 Starting OJS Plugin Build Process...');
// 1. Read the version from version.xml
let version = 'unknown';
try {
const versionXml = fs.readFileSync(versionXmlPath, 'utf8');
const versionMatch = versionXml.match(/<release>(.*?)<\/release>/);
if (versionMatch && versionMatch[1]) {
version = versionMatch[1];
console.log(`✅ Detected version: ${version}`);
} else {
console.warn('⚠️ Could not find <release> tag in version.xml. Defaulting to "unknown".');
}
} catch (error) {
console.error('❌ Error reading version.xml:', error.message);
process.exit(1);
}
// 2. Define the output filename
const outputFilename = `${pluginDir}-${version}.tar.gz`;
// 3. Build the tar command
// In Windows, if tar is available (Windows 10+), we can use it directly.
// We need to compress the *current* directory but put it inside a folder named 'metadataNormalizer' in the archive.
// An easier approach for cross-platform is creating a temp directory, copying files, then compressing.
const os = require('os');
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ojs-build-'));
const tempPluginDir = path.join(tmpDir, pluginDir);
try {
// Create staging directory
fs.mkdirSync(tempPluginDir);
// Prepare exclusion filter for file copying
console.log('⏳ Staging files for release...');
// Simple recursive copy function with exclusions
function copyRecursiveSync(src, dest) {
const exists = fs.existsSync(src);
const stats = exists && fs.statSync(src);
const isDirectory = exists && stats.isDirectory();
// Check exclusions based on base name or glob pattern
const baseName = path.basename(src);
if (exclusions.includes(baseName)) return;
if (exclusions.includes('*.md') && path.extname(src) === '.md') return;
if (isDirectory) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function(childItemName) {
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
}
// Read current directory and copy non-excluded files
fs.readdirSync(__dirname).forEach(item => {
// Skip the output file if it already exists from a previous aborted run
if (item === outputFilename) return;
copyRecursiveSync(path.join(__dirname, item), path.join(tempPluginDir, item));
});
console.log('⏳ Compressing archive...');
// Execute tar command on the staged directory
// -z: gzip, -c: create, -f: file, -C: change directory
const command = `tar -zcf ${outputFilename} -C ${tmpDir} ${pluginDir}`;
execSync(command, { cwd: __dirname, stdio: 'inherit' });
console.log(`\n🎉 Success! Release created at: ${outputFilename}`);
} catch (error) {
console.error('\n❌ Build failed:', error.message);
} finally {
console.log('🧹 Cleaning up temporary files...');
// Clean up temporary directory (Node 14+ specific method)
fs.rmSync(tmpDir, { recursive: true, force: true });
}