/
npm-packages-exists.ts
67 lines (62 loc) · 2.44 KB
/
npm-packages-exists.ts
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
import { resolve } from 'path';
import { existsSync } from 'fs';
import { red, bold } from 'colorette';
import { npmExists } from './npm-exists';
import { isLocalPath } from './path-utils';
import { resolvePackages } from './resolve-packages';
import { getPathToGlobalPackages } from './global-packages-path';
const WEBPACK_SCAFFOLD_PREFIX = 'webpack-scaffold';
/**
*
* Loops through an array and checks if a package is registered
* on npm and throws an error if it is not.
*
* @param {String[]} pkg - Array of packages to check existence of
* @returns {Array} resolvePackages - Returns a process to install the packages
*/
export function npmPackagesExists(pkg: string[]): void {
const acceptedPackages: string[] = [];
function resolvePackagesIfReady(): void | Function {
if (acceptedPackages.length === pkg.length) {
return resolvePackages(acceptedPackages);
}
}
pkg.forEach((scaffold: string): void => {
if (isLocalPath(scaffold)) {
// If the scaffold is a path to a local folder, no name validation is necessary.
acceptedPackages.push(scaffold);
resolvePackagesIfReady();
return;
}
if (existsSync(resolve(getPathToGlobalPackages(), scaffold))) {
// If scaffold is already installed or is a linked package
acceptedPackages.push(scaffold);
resolvePackagesIfReady();
return;
}
// The scaffold is on npm; validate name and existence
if (
scaffold.length <= WEBPACK_SCAFFOLD_PREFIX.length ||
scaffold.slice(0, WEBPACK_SCAFFOLD_PREFIX.length) !== WEBPACK_SCAFFOLD_PREFIX
) {
throw new TypeError(
bold(`${scaffold} isn't a valid name.\n`) +
red(`\nIt should be prefixed with '${WEBPACK_SCAFFOLD_PREFIX}', but have different suffix.\n`),
);
}
npmExists(scaffold)
.then((moduleExists: boolean): void => {
if (moduleExists) {
acceptedPackages.push(scaffold);
} else {
Error.stackTraceLimit = 0;
throw new TypeError(`Cannot resolve location of package ${scaffold}.`);
}
})
.catch((err: Error): void => {
console.error(err.stack || err);
process.exit(2);
})
.then(resolvePackagesIfReady);
});
}