forked from data-driven-forms/react-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-mapper.js
More file actions
94 lines (82 loc) · 3.2 KB
/
Copy pathgenerate-mapper.js
File metadata and controls
94 lines (82 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* eslint-disable no-console */
const inquirer = require('inquirer');
const replace = require('replace-in-file');
const ncp = require('ncp').ncp;
const path = require('path');
const QUESTIONS = [
{
name: 'componentmapper',
type: 'input',
message: 'Project name (i.e.: pf4, pf3, mui, blueprint, etc.):',
validate(input) {
if (/^([A-Za-z\d_-])+$/.test(input)) {
return true;
} else {
return 'Project name may only include letters or numbers.';
}
}
},
{
name: 'typescript',
type: 'confirm',
message: 'Do you want to attach TypeScript types?'
}
];
inquirer.prompt(QUESTIONS).then(async ({ componentmapper, typescript }) => {
const successmessage = `Next steps:
1. Update styles in "packages/${componentmapper}-component-mapper/demo/index.html"
2. Add dependencies in "packages/${componentmapper}-component-mapper/package.json",
3. (optional) Transform imports to allow threeshake (bundle size optimization) in "packages/common/babel.config.js"
4. Have a fun and make some magic! :-)
Please visit https://data-driven-forms.org for more information.
(After your mapper is done, consider adding it to the documentation page.)
`;
console.log('Creating ', componentmapper, '-component-mapper', typescript ? ' with TypeScript' : '');
console.log('Copying template');
await ncp('./templates/component-mapper', `./packages/${componentmapper}-component-mapper`, {}, async () => {
console.log('Replacing strings');
const options = {
files: [
path.resolve(__dirname, `../packages/${componentmapper}-component-mapper/README.md`),
path.resolve(__dirname, `../packages/${componentmapper}-component-mapper/package.json`),
path.resolve(__dirname, `../packages/${componentmapper}-component-mapper/demo/index.js`)
],
from: /\{\{componentmapper\}\}/g,
to: componentmapper
};
try {
await replace(options);
} catch (e) {
console.error('Error occurred:', e);
}
const optionTypeScriptPath = {
files: [path.resolve(__dirname, `../packages/${componentmapper}-component-mapper/package.json`)],
from: /\{\{typingspath\}\}/g,
to: typescript ? '\n "typings": "index.d.ts",' : ''
};
const optionTypeScriptCommand = {
files: [path.resolve(__dirname, `../packages/${componentmapper}-component-mapper/package.json`)],
from: /\{\{buildtypingscmd\}\}/g,
to: typescript ? ' && npm run build:typings' : ''
};
const optionTypeScriptScript = {
files: [path.resolve(__dirname, `../packages/${componentmapper}-component-mapper/package.json`)],
from: /\{\{buildtypingsscript\}\}/g,
to: typescript ? '\n "build:typings": "node ../../scripts/generate-typings.js",' : ''
};
try {
await replace(optionTypeScriptPath);
await replace(optionTypeScriptCommand);
await replace(optionTypeScriptScript);
} catch (e) {
console.error('Error occurred when replacing typescript variables:', e);
}
if (typescript) {
await ncp('./templates/typings', `./packages/${componentmapper}-component-mapper`, {}, () => {
console.log(successmessage);
});
} else {
console.log(successmessage);
}
});
});