-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexus-npm.js
91 lines (74 loc) · 2.89 KB
/
nexus-npm.js
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
'use strict';
const fs = require('fs');
const path = require('path');
const { exec } = require("child_process");
const dirsAndFilesOps = require('./dirs-and-files-ops');
const shared = require('./shared');
const logger = require('./logger');
const createTar = (tarsDir, tarFilename, folder, dirName) => new Promise((res, rej) => {
exec(`tar --exclude=node_modules -czf "${path.join(tarsDir, tarFilename)}" -C .. "${dirName}"`, {
cwd: folder
}, (error, stdout, stderr) => {
if (error) {
rej(`error: ${error.message}`);
return;
}
if (stderr) {
rej(`stderr: ${stderr}`);
return;
}
res(`${tarFilename}`);
});
});
const isRealPackage = dir => {
if (dirsAndFilesOps.isPathExist(dir, 'package.json')) {
try {
const fullPathName = path.join(dir, 'package.json');
const componentFileContent = require(fullPathName);
return !!componentFileContent.name && !!componentFileContent.version;
} catch (ex) {
logger('Corrrupted pacakge?', dir, ex)
return false;
}
}
return false;
}
const mapPackageDetails = dir => {
const fullPathName = path.join(dir, 'package.json');
const componentFileContent = require(fullPathName);
let componentName = componentFileContent.name;
let componentBrand;
if (componentFileContent.name.indexOf('@') === 0) {
[componentName, componentBrand] = componentFileContent.name.split('/')[1];
}
const componentVersion = componentFileContent.version;
const componentDirectory = path.basename(path.dirname(fullPathName));
// TODO: Save under brand directory
const fileName = `${componentName}-${componentVersion}.tar.gz`;
return {
dir, componentName,
componentBrand, componentVersion,
componentDirectory, fileName
}
}
async function createTars(WORKING_DIR, tarsDir) {
return dirsAndFilesOps.getDirectoriesRecursive(WORKING_DIR).then(dirs => dirs.filter(isRealPackage))
.then(dirs => dirs.map(mapPackageDetails))
.then(components => dirsAndFilesOps.resolveDirectoryContent(tarsDir, false)
.then(existings => components.filter(c => existings.indexOf(c.fileName) === -1))).then(components => {
logger('Total', components.length, 'tars');
return Promise.all(components.map(c => createTar(tarsDir,
c.fileName,
c.dir,
c.componentDirectory)))
});
}
const upload = async (tarsDir, repository) => dirsAndFilesOps.resolveDirectoryContent(tarsDir, true)
.then(tarFiles => Promise.all(tarFiles.map(tf => shared.uploadToNexus(repository, [{
formKey: 'npm.asset',
formValue: fs.createReadStream(tf),
filepath: tf
}]))));
const performBulkUpload = (basePath, nexusRepository, args) => createTars(basePath, args.tarsDir)
.then(() => upload(args.tarsDir, nexusRepository));
module.exports = performBulkUpload;