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

Local to promises #1245

Merged
merged 2 commits into from
Oct 14, 2021
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
42 changes: 19 additions & 23 deletions src/cli/domain/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs-extra';
import targz from 'targz';
import { fromPromise } from 'universalify';
import { promisify } from 'util';

import * as clean from './clean';
import getComponentsByDir from './get-components-by-dir';
Expand All @@ -15,31 +15,28 @@ import { Local } from '../../types';
export default function local(): Local {
return {
clean,
cleanup(compressedPackagePath: string, callback) {
return fs.unlink(compressedPackagePath, callback);
cleanup(compressedPackagePath: string) {
return fs.unlink(compressedPackagePath);
},
compress(input, output, callback) {
return targz.compress(
{
src: input,
dest: output,
tar: {
map: function (file) {
return Object.assign(file, {
name: `_package/${file.name}`
});
}
compress(input, output) {
return promisify(targz.compress)({
src: input,
dest: output,
tar: {
map: function (file) {
return Object.assign(file, {
name: `_package/${file.name}`
});
}
},
callback
);
}
});
},
getComponentsByDir: getComponentsByDir(),
init(options, callback) {
async init(options) {
const { componentName, logger } = options;
let { templateType } = options;
if (!validator.validateComponentName(componentName)) {
return callback('name not valid', undefined as any);
throw 'name not valid';
}

// LEGACY TEMPLATES WARNING
Expand All @@ -57,15 +54,14 @@ export default function local(): Local {
);
}
try {
fromPromise(initTemplate)(
await initTemplate(
Object.assign(options, {
templateType,
compiler: `${templateType}-compiler`
}),
callback as any
})
);
} catch (e) {
return callback('template type not valid', undefined as any);
throw 'template type not valid';
}
},
mock: mock(),
Expand Down
36 changes: 14 additions & 22 deletions src/cli/domain/package-components.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs-extra';
import { promisify } from 'util';
import path from 'path';

import requireTemplate from './handle-dependencies/require-template';
Expand All @@ -14,7 +15,7 @@ interface PackageOptions {

const packageComponents =
() =>
(options: PackageOptions, callback: Callback<Component>): void => {
async (options: PackageOptions): Promise<Component> => {
const production = options.production;
const componentPath = options.componentPath;
const minify = options.minify === true;
Expand All @@ -24,24 +25,18 @@ const packageComponents =
const ocPackagePath = path.join(__dirname, '../../../package.json');

if (!fs.existsSync(componentPackagePath)) {
return callback(
new Error('component does not contain package.json'),
undefined as any
);
throw new Error('component does not contain package.json');
} else if (!fs.existsSync(ocPackagePath)) {
return callback(
new Error('error resolving oc internal dependencies'),
undefined as any
);
throw new Error('error resolving oc internal dependencies');
}

fs.emptyDirSync(publishPath);
await fs.emptyDir(publishPath);

const componentPackage: Component = fs.readJsonSync(componentPackagePath);
const ocPackage = fs.readJsonSync(ocPackagePath);
const componentPackage: Component = await fs.readJson(componentPackagePath);
const ocPackage: Component = await fs.readJson(ocPackagePath);

if (!validator.validateComponentName(componentPackage.name)) {
return callback(new Error('name not valid'), undefined as any);
throw new Error('name not valid');
}

const type = componentPackage.oc.files.template.type;
Expand All @@ -55,15 +50,12 @@ const packageComponents =
production
};

try {
const ocTemplate = requireTemplate(type, {
compiler: true,
componentPath
});
ocTemplate.compile!(compileOptions, callback);
} catch (err) {
return callback(err as any, undefined as any);
}
const ocTemplate = requireTemplate(type, {
compiler: true,
componentPath
});
const compile = promisify(ocTemplate.compile!);
return compile(compileOptions);
};

export default packageComponents;
162 changes: 83 additions & 79 deletions src/cli/facade/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import strings from '../../resources/index';
import watch from '../domain/watch';
import { Logger } from '../logger';
import { Local } from '../../types';
import { fromPromise } from 'universalify';

type Registry = ReturnType<typeof oc.Registry>;

Expand Down Expand Up @@ -89,7 +90,7 @@ const dev =
production: opts.production
};

local.package(packageOptions, err => {
fromPromise(local.package)(packageOptions, (err: any) => {
if (!err) i++;
cb(err);
});
Expand Down Expand Up @@ -141,96 +142,99 @@ const dev =
};

logger.warn(cliMessages.SCANNING_COMPONENTS, true);
local.getComponentsByDir(componentsDir, (err, components) => {
if (_.isEmpty(components)) {
err = cliErrors.DEV_FAIL(cliErrors.COMPONENTS_NOT_FOUND) as any;
callback(err, undefined as any);
return logger.err(String(err));
}

logger.ok('OK');
_.forEach(components, component =>
logger.log(colors.green('├── ') + component)
);

handleDependencies({ components, logger }, (err, dependencies) => {
if (err) {
logger.err(err);
fromPromise(local.getComponentsByDir)(
componentsDir,
(err: any, components) => {
if (_.isEmpty(components)) {
err = cliErrors.DEV_FAIL(cliErrors.COMPONENTS_NOT_FOUND) as any;
logger.err(String(err));
return callback(err, undefined as any);
}
packageComponents(components, () => {
async.waterfall(
[
(next: any) => {
if (hotReloading) {
getPort(port + 1, (error, otherPort) => {
if (error) {
return next(error);
}
const liveReloadServer = livereload.createServer({
port: otherPort

logger.ok('OK');
_.forEach(components, component =>
logger.log(colors.green('├── ') + component)
);

handleDependencies({ components, logger }, (err, dependencies) => {
if (err) {
logger.err(err);
return callback(err, undefined as any);
}
packageComponents(components, () => {
async.waterfall(
[
(next: any) => {
if (hotReloading) {
getPort(port + 1, (error, otherPort) => {
if (error) {
return next(error);
}
const liveReloadServer = livereload.createServer({
port: otherPort
});
const refresher = () => liveReloadServer.refresh('/');
next(null, { refresher, port: otherPort });
});
const refresher = () => liveReloadServer.refresh('/');
next(null, { refresher, port: otherPort });
});
} else {
next(null, { refresher: _.noop, port: null });
} else {
next(null, { refresher: _.noop, port: null });
}
}
}
],
(err, liveReload: any) => {
if (err) {
logger.err(String(err));
return callback(err, undefined as any);
}

const registry = oc.Registry({
baseUrl,
prefix: opts.prefix || '',
dependencies: dependencies.modules,
discovery: true,
env: { name: 'local' },
fallbackRegistryUrl,
hotReloading,
liveReloadPort: liveReload.port,
local: true,
path: path.resolve(componentsDir),
port,
templates: dependencies.templates,
verbosity: 1
});

registerPlugins(registry);

logger.warn(cliMessages.REGISTRY_STARTING(baseUrl));
if (liveReload.port) {
logger.warn(
cliMessages.REGISTRY_LIVERELOAD_STARTING(liveReload.port)
);
}
registry.start(err => {
],
(err, liveReload: any) => {
if (err) {
if ((err as any).code === 'EADDRINUSE') {
err = cliErrors.PORT_IS_BUSY(port) as any;
}

logger.err(String(err));
return callback(err, undefined as any);
}

if (optWatch) {
watchForChanges(
{ components, refreshLiveReload: liveReload.refresher },
packageComponents
const registry = oc.Registry({
baseUrl,
prefix: opts.prefix || '',
dependencies: dependencies.modules,
discovery: true,
env: { name: 'local' },
fallbackRegistryUrl,
hotReloading,
liveReloadPort: liveReload.port,
local: true,
path: path.resolve(componentsDir),
port,
templates: dependencies.templates,
verbosity: 1
});

registerPlugins(registry);

logger.warn(cliMessages.REGISTRY_STARTING(baseUrl));
if (liveReload.port) {
logger.warn(
cliMessages.REGISTRY_LIVERELOAD_STARTING(liveReload.port)
);
}
callback(null, registry);
});
}
);
registry.start(err => {
if (err) {
if ((err as any).code === 'EADDRINUSE') {
err = cliErrors.PORT_IS_BUSY(port) as any;
}

logger.err(String(err));
return callback(err, undefined as any);
}

if (optWatch) {
watchForChanges(
{ components, refreshLiveReload: liveReload.refresher },
packageComponents
);
}
callback(null, registry);
});
}
);
});
});
});
});
}
);
};

export default dev;
Expand Down
5 changes: 3 additions & 2 deletions src/cli/facade/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import { fromPromise } from 'universalify';

import strings from '../../resources/index';
import { Local } from '../../types';
Expand All @@ -23,14 +24,14 @@ const init =
const componentPath = path.join(process.cwd(), opts.componentPath);
const componentName = path.basename(componentPath);

local.init(
fromPromise(local.init)(
{
componentName,
componentPath,
templateType,
logger
},
err => {
(err: any) => {
if (err) {
if (err === 'name not valid') {
err = errors.NAME_NOT_VALID;
Expand Down
Loading