Skip to content

Commit 5e55f8e

Browse files
Merge branch 'master' into more-registry-domain-ts
2 parents 66fd0b5 + dd3a78c commit 5e55f8e

36 files changed

+554
-417
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
'use strict';
1+
import strings from '../../../resources';
22

3-
const strings = require('../../../resources').default;
4-
5-
module.exports = (options, cb) => {
3+
export default function ensureCompilerIsDeclaredAsDevDependency(
4+
options: {
5+
componentPath: string;
6+
pkg: { devDependencies: Dictionary<string> };
7+
template: string;
8+
},
9+
cb: Callback<string, string>
10+
): void {
611
const { componentPath, pkg, template } = options;
712
const compilerDep = `${template}-compiler`;
813
const isOk = pkg.devDependencies[compilerDep];
@@ -12,4 +17,4 @@ module.exports = (options, cb) => {
1217
: strings.errors.cli.TEMPLATE_DEP_MISSING(template, componentPath);
1318

1419
cb(err, compilerDep);
15-
};
20+
}

src/cli/domain/handle-dependencies/get-compiler.js renamed to src/cli/domain/handle-dependencies/get-compiler.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
'use strict';
1+
import path from 'path';
22

3-
const path = require('path');
3+
import cleanRequire from '../../../utils/clean-require';
4+
import { Logger } from '../../logger';
5+
import installCompiler from './install-compiler';
46

5-
const cleanRequire = require('../../../utils/clean-require').default;
6-
const installCompiler = require('./install-compiler');
7-
8-
module.exports = (options, cb) => {
7+
export default function getCompiler(
8+
options: {
9+
compilerDep: string;
10+
componentPath: string;
11+
logger: Logger;
12+
pkg: { name: string; devDependencies: Dictionary<string> };
13+
},
14+
cb: Callback<string, string | number>
15+
): void {
916
const { compilerDep, componentPath, logger, pkg } = options;
1017
const compilerPath = path.join(componentPath, 'node_modules', compilerDep);
1118
const compiler = cleanRequire(compilerPath, { justTry: true });
@@ -28,4 +35,4 @@ module.exports = (options, cb) => {
2835
};
2936

3037
installCompiler(installOptions, cb);
31-
};
38+
}

src/cli/domain/handle-dependencies/get-missing-dependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import moduleExists from '../../../utils/module-exists';
33

44
export default function getMissingDependencies(
55
dependencies: Record<string, string> = {}
6-
) {
6+
): string[] {
77
const missing: string[] = [];
88

99
Object.entries(dependencies).forEach(([dependency, version]) => {

src/cli/domain/handle-dependencies/index.js

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import async from 'async';
2+
import coreModules from 'builtin-modules';
3+
import fs from 'fs-extra';
4+
import path from 'path';
5+
import _ from 'lodash';
6+
7+
import ensureCompilerIsDeclaredAsDevDependency from './ensure-compiler-is-declared-as-devDependency';
8+
import getCompiler from './get-compiler';
9+
import installMissingDependencies from './install-missing-dependencies';
10+
import linkMissingDependencies from './link-missing-dependencies';
11+
import isTemplateLegacy from '../../../utils/is-template-legacy';
12+
import strings from '../../../resources';
13+
import { Logger } from '../../logger';
14+
import { Component } from '../../../types';
15+
16+
const getComponentPackageJson = (componentPath: string, cb: Callback<any>) =>
17+
fs.readJson(path.join(componentPath, 'package.json'), cb);
18+
19+
export default function handleDependencies(
20+
options: {
21+
components: string[];
22+
logger: Logger;
23+
useComponentDependencies?: boolean;
24+
},
25+
callback: Callback<
26+
{
27+
modules: string[];
28+
templates: Array<(...args: unknown[]) => unknown>;
29+
},
30+
string
31+
>
32+
): void {
33+
const { components, logger, useComponentDependencies } = options;
34+
35+
const dependencies: Dictionary<string> = {};
36+
const addDependencies = (componentDependencies: Dictionary<string>) =>
37+
_.each(componentDependencies || {}, (version, dependency) => {
38+
dependencies[dependency] = version;
39+
});
40+
41+
const templates: Dictionary<(...args: unknown[]) => unknown> = {};
42+
const addTemplate = (
43+
templateName: string,
44+
template: (...args: unknown[]) => unknown
45+
) => {
46+
templates[templateName] = template;
47+
};
48+
49+
const setupComponentDependencies = (
50+
componentPath: string,
51+
done: (err?: unknown) => void
52+
) =>
53+
async.waterfall(
54+
[
55+
(cb: Callback<Component>) => getComponentPackageJson(componentPath, cb),
56+
(
57+
pkg: Component,
58+
cb: Callback<{
59+
componentPath: string;
60+
logger: Logger;
61+
pkg: Component;
62+
template: string;
63+
}>
64+
) => {
65+
addDependencies(pkg.dependencies);
66+
67+
const template = pkg.oc.files.template.type;
68+
if (isTemplateLegacy(template)) {
69+
return done();
70+
}
71+
72+
cb(null, { componentPath, logger, pkg, template });
73+
},
74+
75+
(
76+
options: {
77+
componentPath: string;
78+
logger: Logger;
79+
pkg: Component;
80+
template: string;
81+
},
82+
cb: any
83+
) =>
84+
ensureCompilerIsDeclaredAsDevDependency(options, (err, compilerDep) =>
85+
cb(err, _.extend(options, { compilerDep }))
86+
),
87+
88+
(
89+
options: {
90+
componentPath: string;
91+
logger: Logger;
92+
pkg: Component;
93+
template: string;
94+
compilerDep: string;
95+
},
96+
cb: any
97+
) =>
98+
getCompiler(options, (err, compiler) =>
99+
cb(err, _.extend(options, { compiler }))
100+
),
101+
102+
(
103+
options: {
104+
compiler: (...args: unknown[]) => unknown;
105+
template: string;
106+
},
107+
cb: any
108+
) => {
109+
const { compiler, template } = options;
110+
addTemplate(template, compiler);
111+
cb();
112+
}
113+
],
114+
done
115+
);
116+
117+
logger.warn(strings.messages.cli.CHECKING_DEPENDENCIES);
118+
async.eachSeries(components, setupComponentDependencies, err => {
119+
if (err) {
120+
return callback(err as any, undefined as any);
121+
}
122+
123+
const result = {
124+
modules: _.union(coreModules, Object.keys(dependencies)).sort(),
125+
templates: Object.values(templates)
126+
};
127+
const options = { dependencies, logger };
128+
if (useComponentDependencies) {
129+
// @ts-ignore
130+
options.componentPath = components[0];
131+
return linkMissingDependencies(options as any, err =>
132+
callback(err, result)
133+
);
134+
}
135+
installMissingDependencies(options, err => callback(err, result));
136+
});
137+
}

src/cli/domain/handle-dependencies/install-compiler.js renamed to src/cli/domain/handle-dependencies/install-compiler.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
'use strict';
1+
import cleanRequire from '../../../utils/clean-require';
2+
import isTemplateValid from '../../../utils/is-template-valid';
3+
import * as npm from '../../../utils/npm-utils';
4+
import strings from '../../../resources/index';
5+
import { Logger } from '../../logger';
26

3-
const cleanRequire = require('../../../utils/clean-require').default;
4-
const isTemplateValid = require('../../../utils/is-template-valid').default;
5-
const npm = require('../../../utils/npm-utils');
6-
const strings = require('../../../resources/index').default;
7-
8-
module.exports = (options, cb) => {
7+
export default function installCompiler(
8+
options: {
9+
compilerPath: string;
10+
componentPath: string;
11+
dependency: string;
12+
logger: Logger;
13+
},
14+
cb: Callback<string, string | number>
15+
): void {
916
const { compilerPath, componentPath, dependency, logger } = options;
1017

1118
logger.warn(strings.messages.cli.INSTALLING_DEPS(dependency), true);
@@ -24,4 +31,4 @@ module.exports = (options, cb) => {
2431
const errorMsg = 'There was a problem while installing the compiler';
2532
cb(!err && isOk ? null : errorMsg, compiler);
2633
});
27-
};
34+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
'use strict';
1+
import path from 'path';
22

3-
const path = require('path');
4-
const _ = require('lodash');
3+
import getMissingDependencies from './get-missing-dependencies';
4+
import * as npm from '../../../utils/npm-utils';
5+
import strings from '../../../resources/index';
6+
import { Logger } from '../../logger';
57

6-
const getMissingDependencies = require('./get-missing-dependencies').default;
7-
const npm = require('../../../utils/npm-utils');
8-
const strings = require('../../../resources/index').default;
9-
10-
module.exports = (options, callback) => {
8+
export default function installMissingDependencies(
9+
options: { dependencies: Dictionary<string>; logger: Logger },
10+
callback: (err: string | null) => void
11+
): void {
1112
const { dependencies, logger } = options;
1213

1314
const missing = getMissingDependencies(dependencies);
1415

15-
if (_.isEmpty(missing)) {
16+
if (!missing.length) {
1617
return callback(null);
1718
}
1819

@@ -26,11 +27,12 @@ module.exports = (options, callback) => {
2627
};
2728

2829
npm.installDependencies(npmOptions, err => {
29-
if (err || !_.isEmpty(getMissingDependencies(dependencies))) {
30+
if (err || getMissingDependencies(dependencies).length) {
3031
logger.err('FAIL');
3132
return callback(strings.errors.cli.DEPENDENCIES_INSTALL_FAIL);
3233
}
34+
3335
logger.ok('OK');
3436
callback(null);
3537
});
36-
};
38+
}

0 commit comments

Comments
 (0)