-
Notifications
You must be signed in to change notification settings - Fork 17
/
init.php
81 lines (62 loc) · 2.57 KB
/
init.php
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
<?php
fwrite(\STDOUT, "Enter an extension name:\n");
$fh = fopen('php://stdin', 'r');
$name = trim(fgets($fh));
fclose($fh);
$nameLabel = $name;
$name = ucfirst($name);
$name = str_replace(' ', '', ucwords(preg_replace('/^a-z0-9]+/', ' ', $name)));
$nameHyphen = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $name));
fwrite(\STDOUT, "Enter a description text:\n");
$fh = fopen('php://stdin', 'r');
$description = trim(fgets($fh));
fclose($fh);
if (substr($description, -1) !== '.') $description .= '.';
fwrite(\STDOUT, "Enter an author name:\n");
$fh = fopen('php://stdin', 'r');
$author = trim(fgets($fh));
fclose($fh);
fwrite(\STDOUT, "Do you want to use ES6 modules in frontend? [y/n]\n");
$fh = fopen('php://stdin', 'r');
$es6 = trim(fgets($fh)) === 'y';
$bundled = $es6 ? "true" : "false";
$jsTranspiled = $es6 ? "true" : "false";
fclose($fh);
$replacePlaceholders = function (string $file) use ($name, $nameHyphen, $nameLabel, $description, $author, $bundled, $jsTranspiled)
{
$content = file_get_contents($file);
$content = str_replace('{@name}', $name, $content);
$content = str_replace('{@nameHyphen}', $nameHyphen, $content);
$content = str_replace('{@nameLabel}', $nameLabel, $content);
$content = str_replace('{@description}', $description, $content);
$content = str_replace('{@author}', $author, $content);
$content = str_replace('{@bundled}', $bundled, $content);
$content = str_replace('{@jsTranspiled}', $jsTranspiled, $content);
file_put_contents($file, $content);
};
$replacePlaceholders('package.json');
$replacePlaceholders('extension.json');
$replacePlaceholders('jsconfig.json');
$replacePlaceholders('config-default.json');
$replacePlaceholders('README.md');
$replacePlaceholders('src/files/custom/Espo/Modules/MyModuleName/Resources/module.json');
if ($es6) {
$content = <<<CLIENT_JSON
{
"scriptList": [
"__APPEND__",
"client/custom/modules/{@nameHyphen}/lib/init.js"
]
}
CLIENT_JSON;
$path = 'src/files/custom/Espo/Modules/MyModuleName/Resources/metadata/app/';
mkdir($path, 0755, true);
$path .= "client.json";
file_put_contents($path, $content);
$replacePlaceholders($path);
}
rename('src/files/custom/Espo/Modules/MyModuleName', 'src/files/custom/Espo/Modules/'. $name);
rename('src/files/client/custom/modules/my-module-name', 'src/files/client/custom/modules/'. $nameHyphen);
rename('tests/unit/Espo/Modules/MyModuleName', 'tests/unit/Espo/Modules/'. $name);
rename('tests/integration/Espo/Modules/MyModuleName', 'tests/integration/Espo/Modules/'. $name);
echo "Ready. Now you need to run 'npm install'.\n";