Skip to content

Commit e6a22d9

Browse files
committed
Promises for installLibraries/installNpm
1 parent 7187cab commit e6a22d9

File tree

1 file changed

+47
-62
lines changed

1 file changed

+47
-62
lines changed

main.js

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ function main() {
10961096
context.logWithLineInfo.error = context.logWithLineInfo.bind(1, 'error');
10971097
context.logWithLineInfo.info = context.logWithLineInfo.bind(1, 'info');
10981098

1099-
installLibraries(() => {
1099+
installLibraries().then(() => {
11001100

11011101
// Load the TS declarations for Node.js and all 3rd party modules
11021102
loadTypeScriptDeclarations();
@@ -1294,7 +1294,6 @@ function stopAllScripts(cb) {
12941294
setTimeout(() => cb(), 0);
12951295
}
12961296

1297-
const attempts = {};
12981297
let globalScript = '';
12991298
/** Generated declarations for global TypeScripts */
13001299
let globalDeclarations = '';
@@ -1748,90 +1747,76 @@ function getName(id) {
17481747
return null;
17491748
}
17501749

1751-
function installNpm(npmLib, callback) {
1752-
const path = __dirname;
1753-
if (typeof npmLib === 'function') {
1754-
callback = npmLib;
1755-
npmLib = undefined;
1756-
}
1750+
async function installNpm(npmLib) {
1751+
return new Promise((resolve, reject) => {
1752+
const path = __dirname;
17571753

1758-
// Also, set the working directory (cwd) of the process instead of using --prefix
1759-
// because that has ugly bugs on Windows
1760-
const cmd = `npm install ${npmLib} --omit=dev`;
1761-
adapter.log.info(`Installing ${npmLib} into ${__dirname} - cmd: ${cmd}`);
1754+
// Also, set the working directory (cwd) of the process instead of using --prefix
1755+
// because that has ugly bugs on Windows
1756+
const cmd = `npm install ${npmLib} --omit=dev`;
1757+
adapter.log.info(`Installing ${npmLib} into ${__dirname} - cmd: ${cmd}`);
17621758

1763-
// System call used for update of js-controller itself,
1764-
// because during installation npm packet will be deleted too, but some files must be loaded even during the installation process.
1765-
const child = mods['child_process'].exec(cmd, {
1766-
windowsHide: true,
1767-
cwd: path,
1768-
});
1759+
// System call used for update of js-controller itself,
1760+
// because during installation npm packet will be deleted too, but some files must be loaded even during the installation process.
1761+
const child = mods['child_process'].exec(cmd, {
1762+
windowsHide: true,
1763+
cwd: path,
1764+
});
17691765

1770-
child.stdout && child.stdout.on('data', buf =>
1771-
adapter.log.info(buf.toString('utf8')));
1766+
child.stdout && child.stdout.on('data', buf =>
1767+
adapter.log.info(buf.toString('utf8')));
17721768

1773-
child.stderr && child.stderr.on('data', buf =>
1774-
adapter.log.error(buf.toString('utf8')));
1769+
child.stderr && child.stderr.on('data', buf =>
1770+
adapter.log.error(buf.toString('utf8')));
17751771

1776-
child.on('err', err => {
1777-
adapter.log.error(`Cannot install ${npmLib}: ${err}`);
1778-
typeof callback === 'function' && callback(npmLib);
1779-
callback = null;
1780-
});
1781-
child.on('error', err => {
1782-
adapter.log.error(`Cannot install ${npmLib}: ${err}`);
1783-
typeof callback === 'function' && callback(npmLib);
1784-
callback = null;
1785-
});
1772+
child.on('err', err => {
1773+
adapter.log.error(`Cannot install ${npmLib}: ${err}`);
1774+
reject(npmLib);
1775+
});
1776+
child.on('error', err => {
1777+
adapter.log.error(`Cannot install ${npmLib}: ${err}`);
1778+
reject(npmLib);
1779+
});
17861780

1787-
child.on('exit', (code /* , signal */) => {
1788-
if (code) {
1789-
adapter.log.error(`Cannot install ${npmLib}: ${code}`);
1790-
}
1791-
// command succeeded
1792-
if (typeof callback === 'function') callback(npmLib);
1793-
callback = null;
1781+
child.on('exit', (code /* , signal */) => {
1782+
if (code) {
1783+
adapter.log.error(`Cannot install ${npmLib}: ${code}`);
1784+
reject(code);
1785+
}
1786+
// command succeeded
1787+
resolve(code);
1788+
});
17941789
});
17951790
}
17961791

1797-
function installLibraries(callback) {
1798-
let allInstalled = true;
1792+
async function installLibraries() {
17991793
if (adapter.config && adapter.config.libraries) {
18001794
const libraries = adapter.config.libraries.split(/[,;\s]+/);
18011795

18021796
for (let lib = 0; lib < libraries.length; lib++) {
18031797
if (libraries[lib] && libraries[lib].trim()) {
18041798
libraries[lib] = libraries[lib].trim();
1805-
let libName = libraries[lib];
1799+
let depName = libraries[lib];
1800+
let version = 'latest';
18061801

1807-
const versionChunkPos = libName.indexOf('@', 1);
1808-
if (versionChunkPos > -1) {
1809-
libName = libName.slice(0, versionChunkPos);
1802+
if (depName.includes('@')) {
1803+
[ depName, version ] = depName.split('@', 2);
18101804
}
18111805

1812-
adapter.log.debug(`Found custom dependency in config: "${libraries[lib]}" (${libName})`);
1813-
1814-
if (!nodeFS.existsSync(`${__dirname}/node_modules/${libName}/package.json`)) {
1815-
if (!attempts[libraries[lib]]) {
1816-
attempts[libraries[lib]] = 1;
1817-
} else {
1818-
attempts[libraries[lib]]++;
1819-
}
1820-
if (attempts[libraries[lib]] > 3) {
1821-
adapter.log.error(`Cannot install npm packet: ${libraries[lib]}`);
1822-
continue;
1823-
}
1806+
adapter.log.debug(`Found custom dependency in config: "${libraries[lib]}" (${depName}@${version})`);
18241807

1825-
installNpm(libraries[lib], () =>
1826-
installLibraries(callback));
1808+
if (!nodeFS.existsSync(`${__dirname}/node_modules/${depName}/package.json`)) {
1809+
adapter.log.info(`Installing custom dependency: "${libraries[lib]}" (${depName}@${version})`);
18271810

1828-
allInstalled = false;
1829-
break;
1811+
try {
1812+
await installNpm(libraries[lib]);
1813+
} catch (err) {
1814+
adapter.log.warn(`Cannot install npm package ${libraries[lib]}: ${err}`);
1815+
}
18301816
}
18311817
}
18321818
}
18331819
}
1834-
allInstalled && callback();
18351820
}
18361821

18371822
function createVM(source, name) {

0 commit comments

Comments
 (0)