This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
util.js
159 lines (132 loc) · 3.74 KB
/
util.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* © 2017 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
import parseDataURL from 'data-urls';
import fs from 'fs-extra';
import path from 'path';
import readJsonSync from 'read-json-sync';
import semver from 'semver';
import {getPackageTargetDir} from 'liferay-npm-build-tools-common/lib/packages';
import PkgDesc from 'liferay-npm-build-tools-common/lib/pkg-desc';
import report from './report';
/**
* Iterate through the elements of an array applying an async process serially
* to each one of them.
* @param {Array} values array of values to be iterated
* @param {function} asyncProcess the async process (that returns a Promise) to
* be executed on each value
* @return {Promise} a Promise that is resolved as soon as the iteration
* finishes
*/
export function iterateSerially(values, asyncProcess) {
return new Promise(resolve => {
if (values.length == 0) {
resolve();
return;
}
const val = values[0];
const p = asyncProcess(val);
p.then(() => {
iterateSerially(values.slice(1), asyncProcess).then(() => {
resolve();
});
});
});
}
/**
* Load the source map of a transpiled JS file.
* @param {string} filePath the path to the transpiled JS file
* @return {Object|null} the source map object or null if not present
*/
export function loadSourceMap(filePath) {
const fileContent = fs.readFileSync(filePath);
const offset1 = fileContent.lastIndexOf('//# sourceMappingURL=');
const offset2 = fileContent.lastIndexOf('/*# sourceMappingURL=');
const offset = Math.max(offset1, offset2);
const annotation = fileContent.toString().substring(offset);
let matches = annotation.match(/\/\/# sourceMappingURL=(.*)/);
if (!matches) {
matches = annotation.match(/\/\*# sourceMappingURL=(.*) \*\//);
if (!matches) {
return null;
}
}
const url = matches[1];
if (url.indexOf('data:') == 0) {
const parsedData = parseDataURL(url);
if (parsedData) {
const {body, mimeType} = parsedData;
if (mimeType.toString() === 'application/json') {
return JSON.parse(body.toString());
}
}
} else {
const sourceMapFile = path.normalize(
path.join(path.dirname(filePath), url)
);
try {
return readJsonSync(sourceMapFile);
} catch (err) {
// Swallow.
}
}
return null;
}
/**
* Rename a package folder if package.json doesn't match original package name
* or version.
* @param {PkgDesc} pkg the package descriptor
* @return {Promise} a Promise that returns the modified PkgDesc
*/
export function renamePkgDirIfPkgJsonChanged(pkg) {
const pkgJson = readJsonSync(path.join(pkg.dir, 'package.json'));
const outputDir = path.dirname(pkg.dir);
if (pkgJson.name !== pkg.name || pkgJson.version !== pkg.version) {
const newDir = path.join(
outputDir,
getPackageTargetDir(pkgJson.name, pkgJson.version)
);
return fs
.move(pkg.dir, newDir)
.then(
() =>
new PkgDesc(
pkgJson.name,
pkgJson.version,
newDir,
pkg.isRoot
)
);
}
return Promise.resolve(pkg);
}
/**
* Report linked dependencies of a given package.json
* @param {Object} pkgJson pacakge.json file contents
* @return {void}
*/
export function reportLinkedDependencies(pkgJson) {
['dependencies', 'devDependencies'].forEach(scope => {
if (pkgJson[scope] != null) {
Object.keys(pkgJson[scope]).forEach(depName => {
const depVersion = pkgJson[scope][depName];
if (semver.validRange(depVersion) == null) {
const depPkgJsonPath = path.join(
'node_modules',
depName,
'package.json'
);
const depPkgJson = readJsonSync(depPkgJsonPath);
pkgJson[scope][depName] = depPkgJson.version;
report.linkedDependency(
depName,
depVersion,
depPkgJson.version
);
}
});
}
});
}