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

Start moving callback based functions to promises #1241

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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.175",
"@types/morgan": "1.9.3",
Expand Down Expand Up @@ -99,6 +100,7 @@
"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 @@ -127,7 +129,7 @@
"serialize-error": "8.1.0",
"targz": "1.0.1",
"try-require": "1.2.1",
"universalify": "2.0.0",
"universalify": "^2.0.0",
"yargs": "17.2.1"
}
}
20 changes: 9 additions & 11 deletions src/cli/domain/handle-dependencies/get-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import path from 'path';
import { Template } from '../../../types';

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: { name: string; devDependencies: Dictionary<string> };
},
cb: Callback<string, string | number>
): void {
export default function getCompiler(options: {
compilerDep: string;
componentPath: string;
logger: Logger;
pkg: { name: string; devDependencies: Dictionary<string> };
}): Promise<Template> {
const { compilerDep, componentPath, logger, pkg } = options;
const compilerPath = path.join(componentPath, 'node_modules', compilerDep);
const compiler = cleanRequire(compilerPath, { justTry: true });

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

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

installCompiler(installOptions, cb);
return installCompiler(installOptions);
}
7 changes: 5 additions & 2 deletions src/cli/domain/handle-dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import coreModules from 'builtin-modules';
import fs from 'fs-extra';
import path from 'path';
import _ from 'lodash';
import { fromPromise } from 'universalify';

import ensureCompilerIsDeclaredAsDevDependency from './ensure-compiler-is-declared-as-devDependency';
import getCompiler from './get-compiler';
Expand Down Expand Up @@ -95,7 +96,7 @@ export default function handleDependencies(
},
cb: any
) =>
getCompiler(options, (err, compiler) =>
fromPromise(getCompiler)(options, (err, compiler) =>
cb(err, _.extend(options, { compiler }))
),

Expand Down Expand Up @@ -132,6 +133,8 @@ export default function handleDependencies(
callback(err, result)
);
}
installMissingDependencies(options, err => callback(err, result));
fromPromise(installMissingDependencies)(options, err =>
callback(err as any, result)
);
});
}
38 changes: 23 additions & 15 deletions src/cli/domain/handle-dependencies/install-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import isTemplateValid from '../../../utils/is-template-valid';
import * as npm from '../../../utils/npm-utils';
import strings from '../../../resources/index';
import { Logger } from '../../logger';
import { Template } from '../../../types';

export default function installCompiler(
options: {
compilerPath: string;
componentPath: string;
dependency: string;
logger: Logger;
},
cb: Callback<string, string | number>
): void {
export default async function installCompiler(options: {
compilerPath: string;
componentPath: string;
dependency: string;
logger: Logger;
}): Promise<Template> {
const { compilerPath, componentPath, dependency, logger } = options;

logger.warn(strings.messages.cli.INSTALLING_DEPS(dependency), true);
Expand All @@ -23,12 +21,22 @@ export default function installCompiler(
save: false,
silent: true
};
const errorMsg = 'There was a problem while installing the compiler';

try {
await npm.installDependency(npmOptions);
logger.ok('OK');

npm.installDependency(npmOptions, err => {
err ? logger.err('FAIL') : logger.ok('OK');
const compiler = cleanRequire(compilerPath, { justTry: true });
const isOk = isTemplateValid(compiler);
const errorMsg = 'There was a problem while installing the compiler';
cb(!err && isOk ? null : errorMsg, compiler);
});

if (!isTemplateValid(compiler)) {
throw errorMsg;
}

return compiler;
} catch (err) {
logger.err('FAIL');

throw errorMsg;
}
}
24 changes: 14 additions & 10 deletions src/cli/domain/handle-dependencies/install-missing-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import * as npm from '../../../utils/npm-utils';
import strings from '../../../resources/index';
import { Logger } from '../../logger';

export default function installMissingDependencies(
options: { dependencies: Dictionary<string>; logger: Logger },
callback: (err: string | null) => void
): void {
export default async function installMissingDependencies(options: {
dependencies: Dictionary<string>;
logger: Logger;
}): Promise<void> {
const { dependencies, logger } = options;

const missing = getMissingDependencies(dependencies);

if (!missing.length) {
return callback(null);
return;
}

logger.warn(strings.messages.cli.INSTALLING_DEPS(missing.join(', ')), true);
Expand All @@ -26,13 +26,17 @@ export default function installMissingDependencies(
silent: true
};

npm.installDependencies(npmOptions, err => {
if (err || getMissingDependencies(dependencies).length) {
try {
await npm.installDependencies(npmOptions);

if (getMissingDependencies(dependencies).length) {
logger.err('FAIL');
return callback(strings.errors.cli.DEPENDENCIES_INSTALL_FAIL);
throw strings.errors.cli.DEPENDENCIES_INSTALL_FAIL;
}

logger.ok('OK');
callback(null);
});
} catch (err) {
logger.err('FAIL');
throw strings.errors.cli.DEPENDENCIES_INSTALL_FAIL;
}
}
33 changes: 13 additions & 20 deletions src/cli/domain/init-template/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import async from 'async';
import fs from 'fs-extra';
import path from 'path';

Expand All @@ -7,27 +6,21 @@ import * as npm from '../../../utils/npm-utils';
import scaffold from './scaffold';
import { Logger } from '../../logger';

export default function initTemplate(
options: {
componentPath: string;
templateType: string;
componentName: string;
compiler: string;
logger: Logger;
},
callback: Callback<{ ok: true }, string>
): void {
export default async function initTemplate(options: {
componentPath: string;
templateType: string;
componentName: string;
compiler: string;
logger: Logger;
}): Promise<{ ok: true }> {
const { compiler, componentPath } = options;
const compilerPath = path.join(componentPath, 'node_modules', compiler);
const npmOptions = { initPath: componentPath, silent: true };

async.series(
[
cb => fs.ensureDir(componentPath, cb),
cb => npm.init(npmOptions, cb as any),
cb => installTemplate(options, cb as any),
cb => scaffold(Object.assign(options, { compilerPath }), cb as any)
],
callback as any
);
await fs.ensureDir(componentPath);
await npm.init(npmOptions);
await installTemplate(options);
await scaffold(Object.assign(options, { compilerPath }));

return { ok: true };
}
25 changes: 11 additions & 14 deletions src/cli/domain/init-template/install-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ interface Options {
logger: Logger;
}

export default function installTemplate(
options: Options,
callback: Callback<{ ok: true }, string>
): void {
export default async function installTemplate(
options: Options
): Promise<{ ok: true }> {
const { compiler, componentPath, logger, templateType } = options;
const errorMessage = 'template type not valid';

const npmOptions = {
dependency: compiler,
Expand All @@ -27,18 +27,13 @@ export default function installTemplate(

logger.log(strings.messages.cli.installCompiler(compiler));

npm.installDependency(npmOptions, (err, result) => {
const errorMessage = 'template type not valid';
if (err) {
// @ts-ignore
return callback(errorMessage);
}
try {
const result = await npm.installDependency(npmOptions);

const installedCompiler = tryRequire(result.dest);

if (!isTemplateValid(installedCompiler, { compiler: true })) {
// @ts-ignore
return callback(errorMessage);
throw errorMessage;
}
const version = installedCompiler.getInfo().version;
logger.log(
Expand All @@ -49,6 +44,8 @@ export default function installTemplate(
)
);

return callback(null, { ok: true });
});
return { ok: true };
} catch (err) {
throw errorMessage;
}
}
21 changes: 9 additions & 12 deletions src/cli/domain/init-template/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,35 @@ interface ScaffoldOptions {
templateType: string;
}

export default function scaffold(
options: ScaffoldOptions,
callback: Callback<{ ok: true }, string>
): void {
export default async function scaffold(
options: ScaffoldOptions
): Promise<{ ok: true }> {
const { compiler, compilerPath, componentName, componentPath, templateType } =
options;

const baseComponentPath = path.join(compilerPath, 'scaffold');
const baseComponentFiles = path.join(baseComponentPath, 'src');
const compilerPackage = fs.readJsonSync(
const compilerPackage = await fs.readJson(
path.join(compilerPath, 'package.json')
);

try {
fs.copySync(baseComponentFiles, componentPath);
await fs.copy(baseComponentFiles, componentPath);

const componentPackage = fs.readJsonSync(
const componentPackage = await fs.readJson(
path.join(componentPath, 'package.json')
);
componentPackage.name = componentName;
componentPackage.devDependencies[compiler] = compilerPackage.version;
fs.writeJsonSync(componentPath + '/package.json', componentPackage, {
await fs.writeJson(componentPath + '/package.json', componentPackage, {
spaces: 2
});

return callback(null, { ok: true });
return { ok: true };
} catch (error) {
const url =
(compilerPackage.bugs && compilerPackage.bugs.url) ||
`the ${templateType} repo`;
return (callback as any)(
strings.errors.cli.scaffoldError(url, String(error))
);
throw strings.errors.cli.scaffoldError(url, String(error));
}
}
3 changes: 2 additions & 1 deletion src/cli/domain/local.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs-extra';
import targz from 'targz';
import { fromPromise } from 'universalify';

import * as clean from './clean';
import getComponentsByDir from './get-components-by-dir';
Expand Down Expand Up @@ -56,7 +57,7 @@ export default function local(): Local {
);
}
try {
initTemplate(
fromPromise(initTemplate)(
Object.assign(options, {
templateType,
compiler: `${templateType}-compiler`
Expand Down
Loading