Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback to promises #1286

Merged
merged 101 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
b127c01
setup libraries to make transition
ricardo-devis-agullo Oct 12, 2021
742ccb0
Start moving callback based functions to promises
ricardo-devis-agullo Oct 12, 2021
301c288
move more files to promises
ricardo-devis-agullo Oct 12, 2021
ae21c48
fix type issue
ricardo-devis-agullo Oct 12, 2021
80f688a
Merge branch 'callback-to-promises' into start-transition-to-promises
ricardo-devis-agullo Oct 12, 2021
a967ba4
Merge branch 'callback-to-promises' into more-cbs-to-promises
ricardo-devis-agullo Oct 12, 2021
1f8114e
Merge pull request #1241 from opencomponents/start-transition-to-prom…
ricardo-devis-agullo Oct 13, 2021
27e019f
Merge branch 'callback-to-promises' into more-cbs-to-promises
ricardo-devis-agullo Oct 13, 2021
96332c5
merge cb
ricardo-devis-agullo Oct 13, 2021
934ce63
Merge pull request #1242 from opencomponents/more-cbs-to-promises
ricardo-devis-agullo Oct 13, 2021
faa8a75
move local functions to promises
ricardo-devis-agullo Oct 13, 2021
eca8226
move some methods from repository to promises
ricardo-devis-agullo Oct 13, 2021
9cf854e
move registry cli to promises
ricardo-devis-agullo Oct 13, 2021
c5eb084
merge master
ricardo-devis-agullo Oct 13, 2021
52b6169
merge cb branch
ricardo-devis-agullo Oct 13, 2021
8b2cd5b
Merge branch 'callback-to-promises' into registry-cli-to-promises
ricardo-devis-agullo Oct 13, 2021
d82ae93
Merge branch 'callback-to-promises' into some-repository-methods-to-p…
ricardo-devis-agullo Oct 13, 2021
d2bdb91
Merge pull request #1245 from opencomponents/local-to-promises
ricardo-devis-agullo Oct 14, 2021
0534057
merge cb main
ricardo-devis-agullo Oct 14, 2021
35ae822
add additional throw?
ricardo-devis-agullo Oct 14, 2021
197d6c5
Merge branch 'callback-to-promises' into some-repository-methods-to-p…
ricardo-devis-agullo Oct 14, 2021
f165461
merge master
ricardo-devis-agullo Oct 15, 2021
2252602
merge cb branch
ricardo-devis-agullo Oct 15, 2021
a5734ec
Merge pull request #1246 from opencomponents/registry-cli-to-promises
ricardo-devis-agullo Oct 15, 2021
ffdcfb6
merge cb branch
ricardo-devis-agullo Oct 15, 2021
f191d4d
Merge pull request #1247 from opencomponents/some-repository-methods-…
ricardo-devis-agullo Oct 15, 2021
de2640c
move the rest of facade to promises
ricardo-devis-agullo Oct 15, 2021
b0e2728
merge master
ricardo-devis-agullo Oct 15, 2021
c85943c
merge cb branch
ricardo-devis-agullo Oct 15, 2021
87e54d5
move components cache to promises
ricardo-devis-agullo Oct 16, 2021
f832774
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Oct 18, 2021
87e4b9c
Merge branch 'callback-to-promises' into components-cache-to-promise
ricardo-devis-agullo Oct 18, 2021
fa3fb7c
Merge branch 'callback-to-promises' into facade-to-promises
ricardo-devis-agullo Oct 18, 2021
21ac4c9
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Oct 20, 2021
344402d
Merge branch 'callback-to-promises' into facade-to-promises
ricardo-devis-agullo Oct 20, 2021
2d7ef15
Merge branch 'callback-to-promises' into components-cache-to-promise
ricardo-devis-agullo Oct 20, 2021
9a79ad1
merge master
ricardo-devis-agullo Oct 21, 2021
9a9f9e3
Merge branch 'callback-to-promises' into facade-to-promises
ricardo-devis-agullo Oct 21, 2021
5f889df
Merge branch 'callback-to-promises' into components-cache-to-promise
ricardo-devis-agullo Oct 21, 2021
4083a27
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Oct 22, 2021
d64c9c9
Merge branch 'callback-to-promises' into facade-to-promises
ricardo-devis-agullo Oct 22, 2021
716ea1d
Merge branch 'callback-to-promises' into components-cache-to-promise
ricardo-devis-agullo Oct 22, 2021
96dc331
add await to removeFolders
ricardo-devis-agullo Oct 22, 2021
b7f77da
remove nested ternary
ricardo-devis-agullo Oct 22, 2021
b1dd63b
Merge pull request #1257 from opencomponents/facade-to-promises
ricardo-devis-agullo Oct 22, 2021
7c69113
Merge branch 'callback-to-promises' into components-cache-to-promise
ricardo-devis-agullo Oct 22, 2021
b74ce3a
Merge pull request #1258 from opencomponents/components-cache-to-promise
ricardo-devis-agullo Oct 22, 2021
7151783
merge master
ricardo-devis-agullo Oct 22, 2021
76bcb3b
update lock
ricardo-devis-agullo Oct 22, 2021
73136c6
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Oct 22, 2021
3123b41
merge master
ricardo-devis-agullo Oct 22, 2021
a59631c
merge master
ricardo-devis-agullo Oct 22, 2021
9533d7e
merge master
ricardo-devis-agullo Oct 22, 2021
608c188
merge master
ricardo-devis-agullo Oct 22, 2021
a70965c
move components-details and repository to promises
ricardo-devis-agullo Oct 23, 2021
ed3e5fb
upgrade storage adapters and parse legacy ones
ricardo-devis-agullo Oct 23, 2021
582b8c4
add express-promise-router to allow promises on routes
ricardo-devis-agullo Oct 23, 2021
6382f70
move plugins initialiser to promises
ricardo-devis-agullo Oct 24, 2021
5b82539
remove minimal-request from acceptance-tests
ricardo-devis-agullo Oct 25, 2021
b95b835
merge conflicts
ricardo-devis-agullo Oct 26, 2021
6bfb6f3
Merge branch 'callback-to-promises' into components-details-promise
ricardo-devis-agullo Oct 26, 2021
1a7ce23
merge
ricardo-devis-agullo Oct 26, 2021
4a0ab02
Merge branch 'callback-to-promises' into add-express-promise-router
ricardo-devis-agullo Oct 26, 2021
8f2b51e
Merge branch 'callback-to-promises' into plugins-initialiser-promise
ricardo-devis-agullo Oct 26, 2021
e244225
Merge pull request #1265 from opencomponents/components-details-promise
ricardo-devis-agullo Oct 28, 2021
2e3f61d
merge changes
ricardo-devis-agullo Oct 28, 2021
c71f414
Merge branch 'callback-to-promises' into add-express-promise-router
ricardo-devis-agullo Oct 28, 2021
dc5c365
merge cb branch
ricardo-devis-agullo Oct 28, 2021
5c81bba
rename getAdapter/parseAdapter to getPromiseBasedAdapter
ricardo-devis-agullo Oct 28, 2021
88a1403
rename version to firstPromiseBasedVersion
ricardo-devis-agullo Oct 28, 2021
a134c55
Merge pull request #1266 from opencomponents/upgrade-storage-adapters
ricardo-devis-agullo Oct 28, 2021
53dfd69
Merge branch 'callback-to-promises' into add-express-promise-router
ricardo-devis-agullo Oct 28, 2021
49ba344
Merge branch 'callback-to-promises' into plugins-initialiser-promise
ricardo-devis-agullo Oct 28, 2021
c7e1732
Merge pull request #1267 from opencomponents/add-express-promise-router
ricardo-devis-agullo Oct 28, 2021
792d922
Merge branch 'callback-to-promises' into plugins-initialiser-promise
ricardo-devis-agullo Oct 28, 2021
5232254
add comment back
ricardo-devis-agullo Oct 28, 2021
7cb407f
merge master
ricardo-devis-agullo Oct 28, 2021
9b80f34
Merge branch 'callback-to-promises' into plugins-initialiser-promise
ricardo-devis-agullo Oct 28, 2021
1c15773
add +1 to plugin index fails
ricardo-devis-agullo Oct 28, 2021
d47154f
Merge pull request #1269 from opencomponents/plugins-initialiser-promise
ricardo-devis-agullo Oct 29, 2021
6e6396f
promisify registry
ricardo-devis-agullo Oct 29, 2021
5e15775
save msg on catch
ricardo-devis-agullo Oct 29, 2021
6f2efc3
move nested renderer to promises
ricardo-devis-agullo Oct 31, 2021
900bcc7
assign plugins in same line
ricardo-devis-agullo Nov 15, 2021
8f364b6
Merge pull request #1272 from opencomponents/promisify-registry
ricardo-devis-agullo Nov 15, 2021
4c32dfc
Merge branch 'callback-to-promises' into nested-renderer-to-promise
ricardo-devis-agullo Nov 15, 2021
586f729
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Nov 15, 2021
17a1797
merge master
ricardo-devis-agullo Nov 15, 2021
9b411a0
merge branch
ricardo-devis-agullo Nov 15, 2021
98711c8
rename rendererCb to rendererWithCallback
ricardo-devis-agullo Nov 16, 2021
a131d90
Remove console.logs on tests
ricardo-devis-agullo Nov 16, 2021
2cde37d
Merge pull request #1273 from opencomponents/nested-renderer-to-promise
ricardo-devis-agullo Nov 16, 2021
a3f3751
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Nov 16, 2021
e079a57
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Nov 16, 2021
a4fe8bf
merge master
ricardo-devis-agullo Nov 16, 2021
e516eca
merge master
ricardo-devis-agullo Nov 16, 2021
1cd9359
Merge branch 'master' into callback-to-promises
ricardo-devis-agullo Nov 16, 2021
4964302
promisify publish route
ricardo-devis-agullo Jan 4, 2022
55de4f3
Merge pull request #1285 from opencomponents/promisify-publish-route
ricardo-devis-agullo Jan 5, 2022
aeff82e
improve type of fallback
ricardo-devis-agullo Jan 14, 2022
593c653
merge master
ricardo-devis-agullo Jan 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@types/errorhandler": "1.5.0",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/got": "^9.6.12",
"@types/livereload": "0.9.1",
"@types/lodash": "4.14.177",
"@types/morgan": "1.9.3",
Expand All @@ -78,6 +79,7 @@
"minimist": "1.2.5",
"mocha": "9.1.3",
"node-emoji": "1.11.0",
"p-limit": "^3.1.0",
"prettier": "2.5.0",
"rimraf": "3.0.2",
"semver-sort": "0.0.4",
Expand All @@ -98,9 +100,11 @@
"dependency-graph": "0.11.0",
"errorhandler": "1.5.1",
"express": "4.17.1",
"express-promise-router": "^4.1.0",
"form-data": "4.0.0",
"fs-extra": "10.0.0",
"getport": "0.1.0",
"got": "^11.8.2",
"livereload": "0.9.3",
"lodash": "4.17.21",
"minimal-request": "3.0.0",
Expand Down Expand Up @@ -129,6 +133,7 @@
"serialize-error": "8.1.0",
"targz": "1.0.1",
"try-require": "1.2.1",
"universalify": "^2.0.0",
"yargs": "17.3.0"
}
}
30 changes: 10 additions & 20 deletions src/cli/domain/clean.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import async from 'async';
import fs from 'fs-extra';
import makeGetComponentsByDir from './get-components-by-dir';
import path from 'path';

const getComponentsByDir = makeGetComponentsByDir();

export function fetchList(
dirPath: string,
callback: (err: Error | null, data: string[]) => void
): void {
return getComponentsByDir(dirPath, (err, list) => {
if (err) return (callback as any)(err);
if (list.length === 0) return callback(null, []);
export async function fetchList(dirPath: string): Promise<string[]> {
const list = await getComponentsByDir(dirPath);

const toRemove = list.map(folder => path.join(folder, 'node_modules'));
const folderExists = (
folder: string,
cb: (err: Error | null, data: boolean) => void
) => fs.exists(folder, exists => cb(null, exists));
if (list.length === 0) return [];

async.filterSeries(toRemove, folderExists, callback as any);
});
const toRemove = list.map(folder => path.join(folder, 'node_modules'));

return toRemove.filter(fs.existsSync);
}

export function remove(
list: string[],
callback: (err: Error | null, data: string) => void
): void {
return async.eachSeries(list, fs.remove, callback as any);
export async function remove(list: string[]): Promise<void> {
for (const item of list) {
await fs.remove(item);
}
}
38 changes: 12 additions & 26 deletions src/cli/domain/get-components-by-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@ import path from 'path';
import { Component } from '../../types';

export default function getComponentsByDir() {
return (
return async (
componentsDir: string,
componentsToRunOrCb:
| string[]
| ((err: Error | null, data: string[]) => void),
callbackMaybe?: (err: Error | null, data: string[]) => void
): void => {
const componentsToRun =
typeof componentsToRunOrCb === 'function'
? undefined
: componentsToRunOrCb;
const callback =
typeof componentsToRunOrCb === 'function'
? componentsToRunOrCb
: callbackMaybe!;

const isOcComponent = function (file: string) {
componentsToRun?: string[]
): Promise<string[]> => {
const isOcComponent = (file: string) => {
const filePath = path.resolve(componentsDir, file);
const packagePath = path.join(filePath, 'package.json');
let content: Component;
Expand All @@ -39,23 +27,21 @@ export default function getComponentsByDir() {
return typeof packagedProperty === 'undefined';
};

let dirContent: string[];

try {
dirContent = fs.readdirSync(componentsDir);
let dirContent = await fs.readdir(componentsDir);
if (componentsToRun) {
dirContent = dirContent.filter(content =>
componentsToRun.includes(content)
);
}
} catch (err) {
return callback(null, []);
}

const components = dirContent
.filter(isOcComponent)
.map(component => path.resolve(componentsDir, component));
const components = dirContent
.filter(isOcComponent)
.map(component => path.resolve(componentsDir, component));

callback(null, components);
return components;
} catch (err) {
return [];
}
};
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import strings from '../../../resources';
import { Component } from '../../../types';

export default function ensureCompilerIsDeclaredAsDevDependency(
options: {
componentPath: string;
pkg: Component;
template: string;
},
cb: (err: string | null, data: string) => void
): void {
export default function ensureCompilerIsDeclaredAsDevDependency(options: {
componentPath: string;
pkg: Component;
template: string;
}): string {
const { componentPath, pkg, template } = options;
const compilerDep = `${template}-compiler`;
const isOk = pkg.devDependencies?.[compilerDep];

const err = isOk
? null
: strings.errors.cli.TEMPLATE_DEP_MISSING(template, componentPath);
if (!isOk)
throw strings.errors.cli.TEMPLATE_DEP_MISSING(template, componentPath);

cb(err, compilerDep);
return compilerDep;
}
19 changes: 8 additions & 11 deletions src/cli/domain/handle-dependencies/get-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import cleanRequire from '../../../utils/clean-require';
import { Logger } from '../../logger';
import installCompiler from './install-compiler';

export default function getCompiler(
options: {
compilerDep: string;
componentPath: string;
logger: Logger;
pkg: { devDependencies: Record<string, string> };
},
cb: (err: string | number | null, data: Template) => void
): void {
export default function getCompiler(options: {
compilerDep: string;
componentPath: string;
logger: Logger;
pkg: { devDependencies: Record<string, string> };
}): Promise<Template> {
const { compilerDep, componentPath, logger, pkg } = options;
const compilerPath = path.join(componentPath, 'node_modules', compilerDep);
const compiler = cleanRequire<Template>(compilerPath, { justTry: true });

if (compiler) {
return cb(null, compiler);
return Promise.resolve(compiler);
}

let dependency = compilerDep;
Expand All @@ -34,5 +31,5 @@ export default function getCompiler(
logger
};

installCompiler(installOptions, cb);
return installCompiler(installOptions);
}
164 changes: 55 additions & 109 deletions src/cli/domain/handle-dependencies/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import async from 'async';
import coreModules from 'builtin-modules';
import fs from 'fs-extra';
import path from 'path';
Expand All @@ -10,31 +9,23 @@ import linkMissingDependencies from './link-missing-dependencies';
import isTemplateLegacy from '../../../utils/is-template-legacy';
import strings from '../../../resources';
import { Logger } from '../../logger';
import { Component } from '../../../types';
import { Component, Template } from '../../../types';

const getComponentPackageJson = (
componentPath: string,
cb: (err: Error | null, data: Component) => void
) => fs.readJson(path.join(componentPath, 'package.json'), cb);
const getComponentPackageJson = (componentPath: string): Promise<Component> =>
fs.readJson(path.join(componentPath, 'package.json'));

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

export default function handleDependencies(
options: {
components: string[];
logger: Logger;
useComponentDependencies?: boolean;
},
callback: (
err: string | null,
data: {
modules: string[];
templates: Array<(...args: unknown[]) => unknown>;
}
) => void
): void {
export default async function handleDependencies(options: {
components: string[];
logger: Logger;
useComponentDependencies?: boolean;
}): Promise<{
modules: string[];
templates: Array<Template>;
}> {
const { components, logger, useComponentDependencies } = options;

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

const templates: Record<string, (...args: unknown[]) => unknown> = {};
const addTemplate = (
templateName: string,
template: (...args: unknown[]) => unknown
) => {
const templates: Record<string, Template> = {};
const addTemplate = (templateName: string, template: Template) => {
templates[templateName] = template;
};

const setupComponentDependencies = (
componentPath: string,
done: (err?: unknown) => void
) =>
async.waterfall(
[
(cb: (err: Error | null, data: Component) => void) =>
getComponentPackageJson(componentPath, cb),
(
pkg: Component,
cb: (
err: Error | null,
data: {
componentPath: string;
logger: Logger;
pkg: Component;
template: string;
}
) => void
) => {
addDependencies(pkg.dependencies);
const setupComponentDependencies = async (
componentPath: string
): Promise<void> => {
const pkg = await getComponentPackageJson(componentPath);
addDependencies(pkg.dependencies);

const template = pkg.oc.files.template.type;
if (isTemplateLegacy(template)) {
return done();
}
const template = pkg.oc.files.template.type;
if (isTemplateLegacy(template)) {
return;
}

cb(null, { componentPath, logger, pkg, template });
},
const compilerDep = ensureCompilerIsDeclaredAsDevDependency({
componentPath,
pkg,
template
});
Object.assign(options, { compilerDep });

(
options: {
componentPath: string;
logger: Logger;
pkg: Component;
template: string;
},
cb: any
) =>
ensureCompilerIsDeclaredAsDevDependency(options, (err, compilerDep) =>
cb(err, Object.assign(options, { compilerDep }))
),
const compiler = await getCompiler({
compilerDep,
componentPath,
logger,
pkg: pkg as { devDependencies: Record<string, string> }
});
Object.assign(options, { compiler });
addTemplate(template, compiler);
};

(
options: {
componentPath: string;
logger: Logger;
pkg: Component & { devDependencies: Record<string, string> };
template: string;
compilerDep: string;
},
cb: any
) =>
getCompiler(options, (err, compiler) =>
cb(err, Object.assign(options, { compiler }))
),
logger.warn(strings.messages.cli.CHECKING_DEPENDENCIES);

(
options: {
compiler: (...args: unknown[]) => unknown;
template: string;
},
cb: any
) => {
const { compiler, template } = options;
addTemplate(template, compiler);
cb();
}
],
done
);
for (const component of components) {
await setupComponentDependencies(component);
}

logger.warn(strings.messages.cli.CHECKING_DEPENDENCIES);
async.eachSeries(components, setupComponentDependencies, err => {
if (err) {
return callback(err as any, undefined as any);
}
const result = {
modules: union(coreModules, Object.keys(dependencies)).sort(),
templates: Object.values(templates)
};
if (useComponentDependencies) {
linkMissingDependencies({
componentPath: components[0],
dependencies,
logger
});
return result;
}

const result = {
modules: union(coreModules, Object.keys(dependencies)).sort(),
templates: Object.values(templates)
};
const options = { dependencies, logger };
if (useComponentDependencies) {
return linkMissingDependencies(
{ ...options, componentPath: components[0] },
err => callback(err, result)
);
}
installMissingDependencies(options, err => callback(err, result));
});
await installMissingDependencies({ dependencies, logger });
return result;
}
Loading