Skip to content

Commit 7e52f2c

Browse files
Merge pull request #1227 from opencomponents/domain-validators-ts
Refactor some validators and domain files to TS
2 parents 4d6fe25 + 0a235b4 commit 7e52f2c

File tree

13 files changed

+173
-107
lines changed

13 files changed

+173
-107
lines changed

src/cli/domain/init-template/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const _ = require('lodash');
77

88
const installTemplate = require('./install-template');
99
const npm = require('../../../utils/npm-utils');
10-
const scaffold = require('./scaffold');
10+
const scaffold = require('./scaffold').default;
1111

1212
module.exports = function(options, callback) {
1313
const { compiler, componentPath } = options;
@@ -17,8 +17,10 @@ module.exports = function(options, callback) {
1717
async.series(
1818
[
1919
cb => fs.ensureDir(componentPath, cb),
20+
// @ts-ignore
2021
cb => npm.init(npmOptions, cb),
2122
cb => installTemplate(options, cb),
23+
// @ts-ignore
2224
cb => scaffold(_.extend(options, { compilerPath }), cb)
2325
],
2426
callback

src/cli/domain/init-template/scaffold.js renamed to src/cli/domain/init-template/scaffold.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
'use strict';
1+
import fs from 'fs-extra';
2+
import path from 'path';
23

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

6-
const strings = require('../../../resources').default;
6+
interface ScaffoldOptions {
7+
compiler: string;
8+
compilerPath: string;
9+
componentName: string;
10+
componentPath: string;
11+
templateType: string;
12+
}
713

8-
module.exports = function scaffold(options, callback) {
14+
export default function scaffold(
15+
options: ScaffoldOptions,
16+
callback: Callback<{ ok: true }, string>
17+
) {
918
const {
1019
compiler,
1120
compilerPath,
@@ -37,6 +46,6 @@ module.exports = function scaffold(options, callback) {
3746
const url =
3847
(compilerPackage.bugs && compilerPackage.bugs.url) ||
3948
`the ${templateType} repo`;
40-
return callback(strings.errors.cli.scaffoldError(url, error));
49+
return (callback as any)(strings.errors.cli.scaffoldError(url, error));
4150
}
42-
};
51+
}

src/registry/domain/extract-package.js renamed to src/registry/domain/extract-package.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
'use strict';
1+
import path from 'path';
2+
import targz from 'targz';
23

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

6-
const getPackageJsonFromTempDir = require('./get-package-json-from-temp-dir')
7-
.default;
8-
9-
module.exports = function(files, callback) {
6+
export default function extractPackage(
7+
files: Express.Multer.File[],
8+
callback: Callback<{
9+
outputFolder: string;
10+
packageJson: any;
11+
}>
12+
) {
1013
const packageFile = files[0],
1114
packagePath = path.resolve(packageFile.path),
1215
packageUntarOutput = path.resolve(
@@ -23,7 +26,7 @@ module.exports = function(files, callback) {
2326
},
2427
err => {
2528
if (err) {
26-
return callback(err);
29+
return (callback as any)(err);
2730
}
2831

2932
getPackageJsonFromTempDir(packageOutput, (err, packageJson) => {
@@ -34,4 +37,4 @@ module.exports = function(files, callback) {
3437
});
3538
}
3639
);
37-
};
40+
}

src/registry/domain/validators/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
const semver = require('semver');
44

5-
const ocCliVersionValidator = require('./oc-cli-version');
5+
const ocCliVersionValidator = require('./oc-cli-version').default;
66
const componentParametersValidator = require('./component-parameters');
77
const packageJsonValidator = require('./package-json-validator');
88
const pluginsRequirementsValidator = require('./plugins-requirements');
99
const registryConfigurationValidator = require('./registry-configuration');
1010
const uploadedPackageValidator = require('./uploaded-package');
11-
const nodeVersionValidator = require('./node-version');
11+
const nodeVersionValidator = require('./node-version').default;
1212

1313
module.exports = {
1414
validateComponentName: function(componentName) {

src/registry/domain/validators/node-version.js

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import semver from 'semver';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
5+
const packageInfo = fs.readJSONSync(
6+
path.join(__dirname, '..', '..', '..', '..', 'package.json')
7+
);
8+
9+
type OkResult = { isValid: true };
10+
type ErrorResult = {
11+
isValid: false;
12+
error: {
13+
suggestedVersion: string;
14+
registryNodeVersion: string;
15+
cliNodeVersion: string;
16+
code: string;
17+
};
18+
};
19+
type Result = OkResult | ErrorResult;
20+
21+
export default function nodeVersion(
22+
userAgent: string,
23+
nodeVersion: string
24+
): Result {
25+
const baseError = (
26+
opts: Partial<ErrorResult['error']> = {}
27+
): ErrorResult => ({
28+
isValid: false,
29+
error: {
30+
suggestedVersion: packageInfo.engines.node || '*',
31+
registryNodeVersion: nodeVersion,
32+
cliNodeVersion: '',
33+
code: '',
34+
...opts
35+
}
36+
});
37+
38+
if (!userAgent) {
39+
return baseError({ code: 'empty' });
40+
}
41+
42+
const matchVersion = /.*\/v([\w|.]+)-.*/.exec(userAgent);
43+
if (!matchVersion) {
44+
return baseError({
45+
code: 'not_valid',
46+
cliNodeVersion: 'not-valid'
47+
});
48+
}
49+
50+
const cliNodeVersion = matchVersion[1];
51+
if (!semver.satisfies(cliNodeVersion, packageInfo.engines.node)) {
52+
return baseError({
53+
code: 'not_matching',
54+
cliNodeVersion
55+
});
56+
}
57+
58+
return { isValid: true };
59+
}

src/registry/domain/validators/oc-cli-version.js

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import semver from 'semver';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
5+
const packageInfo = fs.readJSONSync(
6+
path.join(__dirname, '..', '..', '..', '..', 'package.json')
7+
);
8+
9+
type OkResult = { isValid: true };
10+
type ErrorResult = {
11+
isValid: false;
12+
error: {
13+
suggestedVersion: string;
14+
registryVersion: string;
15+
cliVersion: string;
16+
code: string;
17+
};
18+
};
19+
type Result = OkResult | ErrorResult;
20+
21+
export default function ocCliVersion(userAgent: string): Result {
22+
const baseError = (
23+
opts: Partial<ErrorResult['error']> = {}
24+
): ErrorResult => ({
25+
isValid: false,
26+
error: {
27+
suggestedVersion: `${semver.major(packageInfo.version)}.${semver.minor(
28+
packageInfo.version
29+
)}.X`,
30+
registryVersion: packageInfo.version,
31+
cliVersion: '',
32+
code: '',
33+
...opts
34+
}
35+
});
36+
37+
if (!userAgent) {
38+
return baseError({ code: 'empty' });
39+
}
40+
41+
const matchVersion = /oc-cli-([\w|.]+).*/.exec(userAgent);
42+
if (!matchVersion) {
43+
return baseError({ code: 'not_valid', cliVersion: 'not_valid' });
44+
}
45+
46+
const cliVersion = matchVersion[1];
47+
if (semver.lt(cliVersion, packageInfo.version)) {
48+
return baseError({ code: 'old_version', cliVersion });
49+
}
50+
51+
return { isValid: true };
52+
}

src/registry/routes/publish.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

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

@@ -31,13 +31,16 @@ module.exports = function(repository) {
3131
});
3232
}
3333

34+
// @ts-ignore
3435
validationResult = validator.validateNodeVersion(
3536
req.headers['user-agent'],
3637
process.version
3738
);
3839
if (!validationResult.isValid) {
3940
res.errorDetails = strings.errors.registry.NODE_CLI_VERSION_IS_NOT_VALID(
41+
// @ts-ignore
4042
validationResult.error.registryNodeVersion,
43+
// @ts-ignore
4144
validationResult.error.cliNodeVersion
4245
);
4346
return res.status(409).json({

tasks/build.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const ocClientPackageInfo = require(`${clientComponentDir}package.json`);
1313
log['start']('Building client');
1414

1515
fs.emptyDirSync(path.join(__dirname, clientComponentDir, 'src'));
16+
fs.copyFileSync(
17+
path.join(__dirname, clientComponentDir, 'package.json'),
18+
path.join(
19+
__dirname,
20+
clientComponentDir.replace('src', 'dist'),
21+
'package.json'
22+
)
23+
);
1624

1725
ocClientBrowser.getLib((err, libContent) => {
1826
if (err) {

0 commit comments

Comments
 (0)