Skip to content

Commit b4bf0ca

Browse files
committed
Add a .d.ts copy script
1 parent 21176ad commit b4bf0ca

File tree

8 files changed

+57
-5
lines changed

8 files changed

+57
-5
lines changed

packages/react-form-renderer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"scripts": {
1212
"start": "webpack-dev-server --env dev --config ./config/webpack.config.js --open --hot",
1313
"build": "rollup -c ./rollup.config.js",
14+
"build:typings": "node ../../scripts/copy-files.js",
1415
"release": "semantic-release"
1516
},
1617
"repository": "[email protected]:data-driven-forms/react-forms.git",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { DataTypeValidators } from '../validators';
1+
import { DataTypeValidators } from '../files/validators';
22

33
export default function convertType(dataType: DataTypeValidators, value: any): any;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { ValidatorFunction } from '../validators';
1+
import { ValidatorFunction } from './validators';
22

33
export default function(validators: ValidatorFunction[]): ValidatorFunction;

packages/react-form-renderer/src/files/field.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Validator } from "../validators";
2-
import { ConditionDefinition } from '../form-renderer/condition';
1+
import { Validator } from "./validators";
2+
import { ConditionDefinition } from './condition';
33
import { DataType } from "./data-types";
44

55
export type FieldAction = [string, ...any[]];

packages/react-form-renderer/src/validators/helpers.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ValidatorFunction } from ".";
1+
import { ValidatorFunction } from "../files/validators";
22
import { ReactNode } from "react";
33
import { ValidatorType } from "../files/use-field-api";
44
import { MessageTypes } from "./messages";

scripts/copy-files.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const path = require('path');
2+
const fse = require('fs-extra');
3+
const glob = require('glob');
4+
5+
const packagePath = process.cwd();
6+
const buildPath = path.join(packagePath, './dist');
7+
const srcPath = path.join(packagePath, './src/files');
8+
9+
const kebabToCamel = (str) =>
10+
str
11+
.split('-')
12+
.map((w) => w.replace(/^./, (char) => char.toUpperCase()))
13+
.join('')
14+
.split('.')
15+
.shift();
16+
17+
async function generateIndexTypes(from, to) {
18+
const files = glob.sync('**/*.d.ts', { cwd: from });
19+
const content = `${files.map(
20+
(file) => `export { default as ${kebabToCamel(file)} } from './${file.split('.').shift()}';
21+
export * from './${file.split('.').shift()}';
22+
`
23+
)}`.replace(/,/g, '');
24+
return Promise.all([fse.writeFile(path.resolve(to, 'cjs/index.d.ts'), content)]);
25+
}
26+
27+
async function copyTypesDefinitions(from, to) {
28+
if (!(await fse.exists(to))) {
29+
console.warn(`path ${to} does not exists`);
30+
return [];
31+
}
32+
33+
const files = glob.sync('**/*.d.ts', { cwd: from });
34+
const cmds = files.map((file) => {
35+
return fse.copy(path.resolve(from, file), path.resolve(to, `cjs/${file.split('/').pop()}`));
36+
});
37+
return Promise.all(cmds);
38+
}
39+
40+
async function copy() {
41+
try {
42+
// .d.ts files
43+
await copyTypesDefinitions(srcPath, buildPath);
44+
await generateIndexTypes(srcPath, buildPath);
45+
} catch (err) {
46+
console.error(err);
47+
process.exit(1);
48+
}
49+
}
50+
51+
copy();

0 commit comments

Comments
 (0)