@ventum-digital/iiq-plugin-project-generator
Version:
A npm tool to set-up the project structure for developing an IIQ Plugin.
104 lines (87 loc) • 3.28 kB
JavaScript
const fs = require('fs');
const path = require('path');
const https = require('https');
// Function to get the private token from ~/.npmrc
function getPrivateToken() {
const npmrcPath = path.resolve(process.env.HOME || process.env.USERPROFILE, '.npmrc');
if (!fs.existsSync(npmrcPath)) {
throw new Error('~/.npmrc file not found. Please ensure it exists and contains the PRIVATE-TOKEN.');
}
const npmrcContent = fs.readFileSync(npmrcPath, 'utf-8');
const tokenLine = npmrcContent.split('\n').find((line) => line.startsWith('//git.ventum.com/api/v4/projects/436/packages/npm/:_authToken='));
if (!tokenLine) {
throw new Error('//git.ventum.com/api/v4/projects/436/packages/npm/:_authToken= not found in ~/.npmrc.');
}
return tokenLine.split('=')[1].trim();
}
// Helper function to upload a file
function uploadFile(filePath, remotePath, privateToken, callback) {
if (!fs.existsSync(filePath)) {
console.error(`File not found: ${filePath}`);
callback(new Error(`File not found: ${filePath}`));
return;
}
const fileStream = fs.createReadStream(filePath);
const options = {
method: 'PUT',
headers: {
'PRIVATE-TOKEN': privateToken,
'Content-Type': 'application/octet-stream',
},
};
const req = https.request(remotePath, options, (res) => {
if (res.statusCode >= 200 && res.statusCode < 300) {
console.log(`Successfully uploaded: ${filePath}`);
callback(null);
} else {
console.error(`Failed to upload: ${filePath}`);
console.error(`Status: ${res.statusCode}`);
res.on('data', (data) => console.error(data.toString()));
callback(new Error(`Failed to upload: ${filePath}`));
}
});
req.on('error', (err) => {
console.error(`Request error: ${err.message}`);
callback(err);
});
fileStream.pipe(req);
}
// Main function to upload files
function main() {
try {
const privateToken = getPrivateToken();
if (process.argv.length < 3) {
console.error('Error: Missing root folder path argument.');
console.error('Usage: node upload.js <root-folder-path>');
process.exit(1);
}
const rootFolderPath = path.resolve(process.argv[2]);
const jarName = `${path.basename(rootFolderPath)}.jar`;
const pomName = `${path.basename(rootFolderPath)}.pom`;
const jarPath = path.join(rootFolderPath, jarName);
const pomPath = path.join(rootFolderPath, pomName);
const baseUrl = 'https://git.ventum.com/api/v4/projects/436/packages/maven';
const remoteFolder = `/${path.basename(rootFolderPath)}`;
const jarRemotePath = `${baseUrl}${remoteFolder}/${jarName}`;
const pomRemotePath = `${baseUrl}${remoteFolder}/${pomName}`;
// Upload the JAR file
uploadFile(jarPath, jarRemotePath, privateToken, (jarErr) => {
if (jarErr) {
console.error('Error uploading the JAR file.');
return;
}
// Upload the POM file
uploadFile(pomPath, pomRemotePath, privateToken, (pomErr) => {
if (pomErr) {
console.error('Error uploading the POM file.');
return;
}
console.log('Both files uploaded successfully.');
});
});
} catch (err) {
console.error(`Error: ${err.message}`);
}
}
// Run the script
main();