Skip to content

Commit c21495b

Browse files
merge master
2 parents e8e4e1d + 4e145c6 commit c21495b

File tree

93 files changed

+2744
-3248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2744
-3248
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@types/errorhandler": "1.5.0",
5454
"@types/express": "4.17.13",
5555
"@types/fs-extra": "9.0.13",
56+
"@types/got": "^9.6.12",
5657
"@types/livereload": "0.9.1",
5758
"@types/lodash": "4.14.177",
5859
"@types/morgan": "1.9.3",
@@ -78,6 +79,7 @@
7879
"minimist": "1.2.5",
7980
"mocha": "9.1.3",
8081
"node-emoji": "1.11.0",
82+
"p-limit": "^3.1.0",
8183
"prettier": "2.5.0",
8284
"rimraf": "3.0.2",
8385
"semver-sort": "0.0.4",
@@ -98,9 +100,11 @@
98100
"dependency-graph": "0.11.0",
99101
"errorhandler": "1.5.1",
100102
"express": "4.17.1",
103+
"express-promise-router": "^4.1.0",
101104
"form-data": "4.0.0",
102105
"fs-extra": "10.0.0",
103106
"getport": "0.1.0",
107+
"got": "^11.8.2",
104108
"livereload": "0.9.3",
105109
"lodash": "4.17.21",
106110
"minimal-request": "3.0.0",
@@ -129,6 +133,7 @@
129133
"serialize-error": "8.1.0",
130134
"targz": "1.0.1",
131135
"try-require": "1.2.1",
136+
"universalify": "^2.0.0",
132137
"yargs": "17.3.0"
133138
}
134139
}

src/cli/domain/clean.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
1-
import async from 'async';
21
import fs from 'fs-extra';
32
import makeGetComponentsByDir from './get-components-by-dir';
43
import path from 'path';
54

65
const getComponentsByDir = makeGetComponentsByDir();
76

8-
export function fetchList(
9-
dirPath: string,
10-
callback: (err: Error | null, data: string[]) => void
11-
): void {
12-
return getComponentsByDir(dirPath, (err, list) => {
13-
if (err) return (callback as any)(err);
14-
if (list.length === 0) return callback(null, []);
7+
export async function fetchList(dirPath: string): Promise<string[]> {
8+
const list = await getComponentsByDir(dirPath);
159

16-
const toRemove = list.map(folder => path.join(folder, 'node_modules'));
17-
const folderExists = (
18-
folder: string,
19-
cb: (err: Error | null, data: boolean) => void
20-
) => fs.exists(folder, exists => cb(null, exists));
10+
if (list.length === 0) return [];
2111

22-
async.filterSeries(toRemove, folderExists, callback as any);
23-
});
12+
const toRemove = list.map(folder => path.join(folder, 'node_modules'));
13+
14+
return toRemove.filter(fs.existsSync);
2415
}
2516

26-
export function remove(
27-
list: string[],
28-
callback: (err: Error | null, data: string) => void
29-
): void {
30-
return async.eachSeries(list, fs.remove, callback as any);
17+
export async function remove(list: string[]): Promise<void> {
18+
for (const item of list) {
19+
await fs.remove(item);
20+
}
3121
}

src/cli/domain/get-components-by-dir.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,11 @@ import path from 'path';
33
import { Component } from '../../types';
44

55
export default function getComponentsByDir() {
6-
return (
6+
return async (
77
componentsDir: string,
8-
componentsToRunOrCb:
9-
| string[]
10-
| ((err: Error | null, data: string[]) => void),
11-
callbackMaybe?: (err: Error | null, data: string[]) => void
12-
): void => {
13-
const componentsToRun =
14-
typeof componentsToRunOrCb === 'function'
15-
? undefined
16-
: componentsToRunOrCb;
17-
const callback =
18-
typeof componentsToRunOrCb === 'function'
19-
? componentsToRunOrCb
20-
: callbackMaybe!;
21-
22-
const isOcComponent = function (file: string) {
8+
componentsToRun?: string[]
9+
): Promise<string[]> => {
10+
const isOcComponent = (file: string) => {
2311
const filePath = path.resolve(componentsDir, file);
2412
const packagePath = path.join(filePath, 'package.json');
2513
let content: Component;
@@ -39,23 +27,21 @@ export default function getComponentsByDir() {
3927
return typeof packagedProperty === 'undefined';
4028
};
4129

42-
let dirContent: string[];
43-
4430
try {
45-
dirContent = fs.readdirSync(componentsDir);
31+
let dirContent = await fs.readdir(componentsDir);
4632
if (componentsToRun) {
4733
dirContent = dirContent.filter(content =>
4834
componentsToRun.includes(content)
4935
);
5036
}
51-
} catch (err) {
52-
return callback(null, []);
53-
}
5437

55-
const components = dirContent
56-
.filter(isOcComponent)
57-
.map(component => path.resolve(componentsDir, component));
38+
const components = dirContent
39+
.filter(isOcComponent)
40+
.map(component => path.resolve(componentsDir, component));
5841

59-
callback(null, components);
42+
return components;
43+
} catch (err) {
44+
return [];
45+
}
6046
};
6147
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import strings from '../../../resources';
22
import { Component } from '../../../types';
33

4-
export default function ensureCompilerIsDeclaredAsDevDependency(
5-
options: {
6-
componentPath: string;
7-
pkg: Component;
8-
template: string;
9-
},
10-
cb: (err: string | null, data: string) => void
11-
): void {
4+
export default function ensureCompilerIsDeclaredAsDevDependency(options: {
5+
componentPath: string;
6+
pkg: Component;
7+
template: string;
8+
}): string {
129
const { componentPath, pkg, template } = options;
1310
const compilerDep = `${template}-compiler`;
1411
const isOk = pkg.devDependencies?.[compilerDep];
1512

16-
const err = isOk
17-
? null
18-
: strings.errors.cli.TEMPLATE_DEP_MISSING(template, componentPath);
13+
if (!isOk)
14+
throw strings.errors.cli.TEMPLATE_DEP_MISSING(template, componentPath);
1915

20-
cb(err, compilerDep);
16+
return compilerDep;
2117
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ import cleanRequire from '../../../utils/clean-require';
55
import { Logger } from '../../logger';
66
import installCompiler from './install-compiler';
77

8-
export default function getCompiler(
9-
options: {
10-
compilerDep: string;
11-
componentPath: string;
12-
logger: Logger;
13-
pkg: { devDependencies: Record<string, string> };
14-
},
15-
cb: (err: string | number | null, data: Template) => void
16-
): void {
8+
export default function getCompiler(options: {
9+
compilerDep: string;
10+
componentPath: string;
11+
logger: Logger;
12+
pkg: { devDependencies: Record<string, string> };
13+
}): Promise<Template> {
1714
const { compilerDep, componentPath, logger, pkg } = options;
1815
const compilerPath = path.join(componentPath, 'node_modules', compilerDep);
1916
const compiler = cleanRequire<Template>(compilerPath, { justTry: true });
2017

2118
if (compiler) {
22-
return cb(null, compiler);
19+
return Promise.resolve(compiler);
2320
}
2421

2522
let dependency = compilerDep;
@@ -34,5 +31,5 @@ export default function getCompiler(
3431
logger
3532
};
3633

37-
installCompiler(installOptions, cb);
34+
return installCompiler(installOptions);
3835
}
Lines changed: 55 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import async from 'async';
21
import coreModules from 'builtin-modules';
32
import fs from 'fs-extra';
43
import path from 'path';
@@ -10,31 +9,23 @@ import linkMissingDependencies from './link-missing-dependencies';
109
import isTemplateLegacy from '../../../utils/is-template-legacy';
1110
import strings from '../../../resources';
1211
import { Logger } from '../../logger';
13-
import { Component } from '../../../types';
12+
import { Component, Template } from '../../../types';
1413

15-
const getComponentPackageJson = (
16-
componentPath: string,
17-
cb: (err: Error | null, data: Component) => void
18-
) => fs.readJson(path.join(componentPath, 'package.json'), cb);
14+
const getComponentPackageJson = (componentPath: string): Promise<Component> =>
15+
fs.readJson(path.join(componentPath, 'package.json'));
1916

2017
const union = (a: ReadonlyArray<string>, b: ReadonlyArray<string>) => [
2118
...new Set([...a, ...b])
2219
];
2320

24-
export default function handleDependencies(
25-
options: {
26-
components: string[];
27-
logger: Logger;
28-
useComponentDependencies?: boolean;
29-
},
30-
callback: (
31-
err: string | null,
32-
data: {
33-
modules: string[];
34-
templates: Array<(...args: unknown[]) => unknown>;
35-
}
36-
) => void
37-
): void {
21+
export default async function handleDependencies(options: {
22+
components: string[];
23+
logger: Logger;
24+
useComponentDependencies?: boolean;
25+
}): Promise<{
26+
modules: string[];
27+
templates: Array<Template>;
28+
}> {
3829
const { components, logger, useComponentDependencies } = options;
3930

4031
const dependencies: Record<string, string> = {};
@@ -45,103 +36,58 @@ export default function handleDependencies(
4536
}
4637
);
4738

48-
const templates: Record<string, (...args: unknown[]) => unknown> = {};
49-
const addTemplate = (
50-
templateName: string,
51-
template: (...args: unknown[]) => unknown
52-
) => {
39+
const templates: Record<string, Template> = {};
40+
const addTemplate = (templateName: string, template: Template) => {
5341
templates[templateName] = template;
5442
};
5543

56-
const setupComponentDependencies = (
57-
componentPath: string,
58-
done: (err?: unknown) => void
59-
) =>
60-
async.waterfall(
61-
[
62-
(cb: (err: Error | null, data: Component) => void) =>
63-
getComponentPackageJson(componentPath, cb),
64-
(
65-
pkg: Component,
66-
cb: (
67-
err: Error | null,
68-
data: {
69-
componentPath: string;
70-
logger: Logger;
71-
pkg: Component;
72-
template: string;
73-
}
74-
) => void
75-
) => {
76-
addDependencies(pkg.dependencies);
44+
const setupComponentDependencies = async (
45+
componentPath: string
46+
): Promise<void> => {
47+
const pkg = await getComponentPackageJson(componentPath);
48+
addDependencies(pkg.dependencies);
7749

78-
const template = pkg.oc.files.template.type;
79-
if (isTemplateLegacy(template)) {
80-
return done();
81-
}
50+
const template = pkg.oc.files.template.type;
51+
if (isTemplateLegacy(template)) {
52+
return;
53+
}
8254

83-
cb(null, { componentPath, logger, pkg, template });
84-
},
55+
const compilerDep = ensureCompilerIsDeclaredAsDevDependency({
56+
componentPath,
57+
pkg,
58+
template
59+
});
60+
Object.assign(options, { compilerDep });
8561

86-
(
87-
options: {
88-
componentPath: string;
89-
logger: Logger;
90-
pkg: Component;
91-
template: string;
92-
},
93-
cb: any
94-
) =>
95-
ensureCompilerIsDeclaredAsDevDependency(options, (err, compilerDep) =>
96-
cb(err, Object.assign(options, { compilerDep }))
97-
),
62+
const compiler = await getCompiler({
63+
compilerDep,
64+
componentPath,
65+
logger,
66+
pkg: pkg as { devDependencies: Record<string, string> }
67+
});
68+
Object.assign(options, { compiler });
69+
addTemplate(template, compiler);
70+
};
9871

99-
(
100-
options: {
101-
componentPath: string;
102-
logger: Logger;
103-
pkg: Component & { devDependencies: Record<string, string> };
104-
template: string;
105-
compilerDep: string;
106-
},
107-
cb: any
108-
) =>
109-
getCompiler(options, (err, compiler) =>
110-
cb(err, Object.assign(options, { compiler }))
111-
),
72+
logger.warn(strings.messages.cli.CHECKING_DEPENDENCIES);
11273

113-
(
114-
options: {
115-
compiler: (...args: unknown[]) => unknown;
116-
template: string;
117-
},
118-
cb: any
119-
) => {
120-
const { compiler, template } = options;
121-
addTemplate(template, compiler);
122-
cb();
123-
}
124-
],
125-
done
126-
);
74+
for (const component of components) {
75+
await setupComponentDependencies(component);
76+
}
12777

128-
logger.warn(strings.messages.cli.CHECKING_DEPENDENCIES);
129-
async.eachSeries(components, setupComponentDependencies, err => {
130-
if (err) {
131-
return callback(err as any, undefined as any);
132-
}
78+
const result = {
79+
modules: union(coreModules, Object.keys(dependencies)).sort(),
80+
templates: Object.values(templates)
81+
};
82+
if (useComponentDependencies) {
83+
linkMissingDependencies({
84+
componentPath: components[0],
85+
dependencies,
86+
logger
87+
});
88+
return result;
89+
}
13390

134-
const result = {
135-
modules: union(coreModules, Object.keys(dependencies)).sort(),
136-
templates: Object.values(templates)
137-
};
138-
const options = { dependencies, logger };
139-
if (useComponentDependencies) {
140-
return linkMissingDependencies(
141-
{ ...options, componentPath: components[0] },
142-
err => callback(err, result)
143-
);
144-
}
145-
installMissingDependencies(options, err => callback(err, result));
146-
});
91+
await installMissingDependencies({ dependencies, logger });
92+
return result;
14793
}

0 commit comments

Comments
 (0)