Skip to content

Commit

Permalink
Merge pull request #1227 from opencomponents/domain-validators-ts
Browse files Browse the repository at this point in the history
Refactor some validators and domain files to TS
  • Loading branch information
ricardo-devis-agullo authored Sep 29, 2021
2 parents 4d6fe25 + 0a235b4 commit 7e52f2c
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 107 deletions.
4 changes: 3 additions & 1 deletion src/cli/domain/init-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const _ = require('lodash');

const installTemplate = require('./install-template');
const npm = require('../../../utils/npm-utils');
const scaffold = require('./scaffold');
const scaffold = require('./scaffold').default;

module.exports = function(options, callback) {
const { compiler, componentPath } = options;
Expand All @@ -17,8 +17,10 @@ module.exports = function(options, callback) {
async.series(
[
cb => fs.ensureDir(componentPath, cb),
// @ts-ignore
cb => npm.init(npmOptions, cb),
cb => installTemplate(options, cb),
// @ts-ignore
cb => scaffold(_.extend(options, { compilerPath }), cb)
],
callback
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
'use strict';
import fs from 'fs-extra';
import path from 'path';

const fs = require('fs-extra');
const path = require('path');
import strings from '../../../resources';

const strings = require('../../../resources').default;
interface ScaffoldOptions {
compiler: string;
compilerPath: string;
componentName: string;
componentPath: string;
templateType: string;
}

module.exports = function scaffold(options, callback) {
export default function scaffold(
options: ScaffoldOptions,
callback: Callback<{ ok: true }, string>
) {
const {
compiler,
compilerPath,
Expand Down Expand Up @@ -37,6 +46,6 @@ module.exports = function scaffold(options, callback) {
const url =
(compilerPackage.bugs && compilerPackage.bugs.url) ||
`the ${templateType} repo`;
return callback(strings.errors.cli.scaffoldError(url, error));
return (callback as any)(strings.errors.cli.scaffoldError(url, error));
}
};
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';
import path from 'path';
import targz from 'targz';

const path = require('path');
const targz = require('targz');
import getPackageJsonFromTempDir from './get-package-json-from-temp-dir';

const getPackageJsonFromTempDir = require('./get-package-json-from-temp-dir')
.default;

module.exports = function(files, callback) {
export default function extractPackage(
files: Express.Multer.File[],
callback: Callback<{
outputFolder: string;
packageJson: any;
}>
) {
const packageFile = files[0],
packagePath = path.resolve(packageFile.path),
packageUntarOutput = path.resolve(
Expand All @@ -23,7 +26,7 @@ module.exports = function(files, callback) {
},
err => {
if (err) {
return callback(err);
return (callback as any)(err);
}

getPackageJsonFromTempDir(packageOutput, (err, packageJson) => {
Expand All @@ -34,4 +37,4 @@ module.exports = function(files, callback) {
});
}
);
};
}
4 changes: 2 additions & 2 deletions src/registry/domain/validators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

const semver = require('semver');

const ocCliVersionValidator = require('./oc-cli-version');
const ocCliVersionValidator = require('./oc-cli-version').default;
const componentParametersValidator = require('./component-parameters');
const packageJsonValidator = require('./package-json-validator');
const pluginsRequirementsValidator = require('./plugins-requirements');
const registryConfigurationValidator = require('./registry-configuration');
const uploadedPackageValidator = require('./uploaded-package');
const nodeVersionValidator = require('./node-version');
const nodeVersionValidator = require('./node-version').default;

module.exports = {
validateComponentName: function(componentName) {
Expand Down
39 changes: 0 additions & 39 deletions src/registry/domain/validators/node-version.js

This file was deleted.

59 changes: 59 additions & 0 deletions src/registry/domain/validators/node-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import semver from 'semver';
import fs from 'fs-extra';
import path from 'path';

const packageInfo = fs.readJSONSync(
path.join(__dirname, '..', '..', '..', '..', 'package.json')
);

type OkResult = { isValid: true };
type ErrorResult = {
isValid: false;
error: {
suggestedVersion: string;
registryNodeVersion: string;
cliNodeVersion: string;
code: string;
};
};
type Result = OkResult | ErrorResult;

export default function nodeVersion(
userAgent: string,
nodeVersion: string
): Result {
const baseError = (
opts: Partial<ErrorResult['error']> = {}
): ErrorResult => ({
isValid: false,
error: {
suggestedVersion: packageInfo.engines.node || '*',
registryNodeVersion: nodeVersion,
cliNodeVersion: '',
code: '',
...opts
}
});

if (!userAgent) {
return baseError({ code: 'empty' });
}

const matchVersion = /.*\/v([\w|.]+)-.*/.exec(userAgent);
if (!matchVersion) {
return baseError({
code: 'not_valid',
cliNodeVersion: 'not-valid'
});
}

const cliNodeVersion = matchVersion[1];
if (!semver.satisfies(cliNodeVersion, packageInfo.engines.node)) {
return baseError({
code: 'not_matching',
cliNodeVersion
});
}

return { isValid: true };
}
41 changes: 0 additions & 41 deletions src/registry/domain/validators/oc-cli-version.js

This file was deleted.

52 changes: 52 additions & 0 deletions src/registry/domain/validators/oc-cli-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import semver from 'semver';
import fs from 'fs-extra';
import path from 'path';

const packageInfo = fs.readJSONSync(
path.join(__dirname, '..', '..', '..', '..', 'package.json')
);

type OkResult = { isValid: true };
type ErrorResult = {
isValid: false;
error: {
suggestedVersion: string;
registryVersion: string;
cliVersion: string;
code: string;
};
};
type Result = OkResult | ErrorResult;

export default function ocCliVersion(userAgent: string): Result {
const baseError = (
opts: Partial<ErrorResult['error']> = {}
): ErrorResult => ({
isValid: false,
error: {
suggestedVersion: `${semver.major(packageInfo.version)}.${semver.minor(
packageInfo.version
)}.X`,
registryVersion: packageInfo.version,
cliVersion: '',
code: '',
...opts
}
});

if (!userAgent) {
return baseError({ code: 'empty' });
}

const matchVersion = /oc-cli-([\w|.]+).*/.exec(userAgent);
if (!matchVersion) {
return baseError({ code: 'not_valid', cliVersion: 'not_valid' });
}

const cliVersion = matchVersion[1];
if (semver.lt(cliVersion, packageInfo.version)) {
return baseError({ code: 'old_version', cliVersion });
}

return { isValid: true };
}
5 changes: 4 additions & 1 deletion src/registry/routes/publish.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const extractPackage = require('../domain/extract-package');
const extractPackage = require('../domain/extract-package').default;
const strings = require('../../resources/index').default;
const validator = require('../domain/validators');

Expand Down Expand Up @@ -31,13 +31,16 @@ module.exports = function(repository) {
});
}

// @ts-ignore
validationResult = validator.validateNodeVersion(
req.headers['user-agent'],
process.version
);
if (!validationResult.isValid) {
res.errorDetails = strings.errors.registry.NODE_CLI_VERSION_IS_NOT_VALID(
// @ts-ignore
validationResult.error.registryNodeVersion,
// @ts-ignore
validationResult.error.cliNodeVersion
);
return res.status(409).json({
Expand Down
8 changes: 8 additions & 0 deletions tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const ocClientPackageInfo = require(`${clientComponentDir}package.json`);
log['start']('Building client');

fs.emptyDirSync(path.join(__dirname, clientComponentDir, 'src'));
fs.copyFileSync(
path.join(__dirname, clientComponentDir, 'package.json'),
path.join(
__dirname,
clientComponentDir.replace('src', 'dist'),
'package.json'
)
);

ocClientBrowser.getLib((err, libContent) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cli-domain-init-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('cli : domain : init-template', () => {
'../../../utils/npm-utils': {
init: stubs.npmStub
},
'./scaffold': stubs.scaffoldStub
'./scaffold': { default: stubs.scaffoldStub }
}
);

Expand Down
8 changes: 4 additions & 4 deletions test/unit/registry-domain-extract-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe('registry : domain : extract-package', () => {
{
targz: { decompress: decompressStub },
path: { resolve: pathResolveStub },
'./get-package-json-from-temp-dir': {
default: sinon.stub().yields(null, { package: 'hello' })
}
'./get-package-json-from-temp-dir': sinon
.stub()
.yields(null, { package: 'hello' })
}
);
).default;

describe('when successfully extracting package', () => {
let response;
Expand Down
14 changes: 12 additions & 2 deletions test/unit/registry-domain-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,12 @@ describe('registry : domain : validator', () => {
'./oc-cli-version': injectr(
'../../dist/registry/domain/validators/oc-cli-version.js',
{
'../../../../package.json': { version: '0.16.34' }
'fs-extra': {
readJSONSync: () => ({ version: '0.16.34' })
}
},
{
__dirname: '/'
}
)
}
Expand Down Expand Up @@ -1060,7 +1065,12 @@ describe('registry : domain : validator', () => {
'./node-version': injectr(
'../../dist/registry/domain/validators/node-version.js',
{
'../../../../package.json': { engines: { node: '>=0.10.35' } }
'fs-extra': {
readJSONSync: () => ({ engines: { node: '>=0.10.35' } })
}
},
{
__dirname: '/'
}
)
}
Expand Down

0 comments on commit 7e52f2c

Please sign in to comment.