-
Notifications
You must be signed in to change notification settings - Fork 33
/
addXblock.js
75 lines (63 loc) · 2.14 KB
/
addXblock.js
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
// A script to create a new xblock editor in this repo.
/* eslint no-console: 0 */
// xblock name is is the third argument after node and fedx-scripts
const xblockName = process.argv[2];
// I. Create Editor Files
const fs = require('fs');
const filepath = `src/editors/containers/${xblockName}Editor/index.jsx`;
const contents = fs.readFileSync('./src/example.jsx');
fs.mkdir(`src/editors/containers/${xblockName}Editor/`, { recursive: true }, (err) => {
if (err) { throw err; }
});
fs.writeFile(filepath, contents, (err) => {
if (err) { throw err; } else {
console.log(`Editor is created successfully at ${filepath}`);
}
});
const openFileToArray = (filename) => {
const content = fs.readFileSync(filename, 'utf8');
return content.split('\n');
};
const WriteIntoFile = (path, target, addition) => {
const contentArray = openFileToArray(path);
contentArray.every((value, index) => {
if (value.includes(addition)) {
return true; // don't add the message in again.p
}
if (value.includes(target)) {
contentArray.splice(index, 0, `${addition}\n`);
return false;
}
return true;
});
const newContent = contentArray.join('\n');
fs.writeFileSync(path, newContent, (err) => {
if (err) { throw err; } else {
console.log(`Editor is created successfully at ${filepath}`);
}
});
};
// II. Update Constants
// Add a new line at line 5 src/frontend-lib-content-components/src/editors/data/constants/app.js
// with <name>: '<name>',
const tag = 'ADDED_EDITORS';
WriteIntoFile(
'src/editors/data/constants/app.js',
tag,
` ${xblockName}: '${xblockName}',`,
);
const importTag = 'ADDED_EDITOR_IMPORTS';
WriteIntoFile(
'src/editors/supportedEditors.js',
importTag,
`import ${xblockName}Editor from './containers/${xblockName}Editor'`,
);
const useTag = 'ADDED_EDITORS';
WriteIntoFile(
'src/editors/supportedEditors.js',
useTag,
` [blockTypes.${xblockName}]: ${xblockName}Editor,`,
);
// Add to src/frontend-lib-content-components/src/editors/supportedEditors.js
// at line 0 add: import <name>Editor from './containers/<name>Editor';
// add at 5th to last line: [blockTypes.<name>]: <name>Editor,