@sekrab/bin 0.0.0-stage → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +32 -4
- package/readme.md +84 -0
- package/tasks/angular/angular.js +295 -0
- package/tasks/angular/angulardmart/component.dialog.template +24 -0
- package/tasks/angular/angulardmart/component.form.template +57 -0
- package/tasks/angular/angulardmart/component.partial.template +21 -0
- package/tasks/angular/angulardmart/component.template +21 -0
- package/tasks/angular/angulardmart/config.template +7 -0
- package/tasks/angular/angulardmart/directive.standalone.template +19 -0
- package/tasks/angular/angulardmart/model.template +61 -0
- package/tasks/angular/angulardmart/module.template +17 -0
- package/tasks/angular/angulardmart/pipe.standalone.template +11 -0
- package/tasks/angular/angulardmart/route.standalone.template +6 -0
- package/tasks/angular/angulardmart/route.template +4 -0
- package/tasks/angular/angulardmart/routeModule.template +21 -0
- package/tasks/angular/angulardmart/service.template +66 -0
- package/tasks/angular/angulardmart/state.template +10 -0
- package/tasks/angular/angulardmart/view.form.template +48 -0
- package/tasks/angular/angulardmart/view.partial.template +1 -0
- package/tasks/angular/angulardmart/view.template +5 -0
- package/tasks/angular/angulartemplates/component.dialog.template +24 -0
- package/tasks/angular/angulartemplates/component.form.template +56 -0
- package/tasks/angular/angulartemplates/component.partial.template +21 -0
- package/tasks/angular/angulartemplates/component.template +21 -0
- package/tasks/angular/angulartemplates/config.template +7 -0
- package/tasks/angular/angulartemplates/directive.standalone.template +19 -0
- package/tasks/angular/angulartemplates/model.template +39 -0
- package/tasks/angular/angulartemplates/module.template +17 -0
- package/tasks/angular/angulartemplates/pipe.standalone.template +11 -0
- package/tasks/angular/angulartemplates/route.standalone.template +6 -0
- package/tasks/angular/angulartemplates/route.template +4 -0
- package/tasks/angular/angulartemplates/routeModule.template +21 -0
- package/tasks/angular/angulartemplates/service.template +80 -0
- package/tasks/angular/angulartemplates/state.template +10 -0
- package/tasks/angular/angulartemplates/view.form.template +48 -0
- package/tasks/angular/angulartemplates/view.partial.template +1 -0
- package/tasks/angular/angulartemplates/view.template +5 -0
- package/tasks/angular/cli.js +27 -0
- package/tasks/angular/extract.js +115 -0
- package/tasks/cli.js +27 -0
- package/tasks/config.json +42 -0
- package/tasks/files.js +22 -0
- package/tasks/index.js +19 -0
- package/tasks/shut/cli.js +12 -0
- package/tasks/shut/critical.js +28 -0
- package/tasks/shut/shut.icons.js +78 -0
- package/tasks/shut/shut.js +87 -0
- package/tasks/svelte/translate.js +51 -0
- package/README.md +0 -3
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Extract translation terms into the locale files. Plain node, no gulp.
|
|
2
|
+
// Usage from a client project (see angular/cli.js):
|
|
3
|
+
// npx sekrab extract
|
|
4
|
+
// or in code:
|
|
5
|
+
// await require('@sekrab/bin/tasks/angular/extract.js')(config)();
|
|
6
|
+
//
|
|
7
|
+
// Steps:
|
|
8
|
+
// 1. for every language without a locale file (prefix-lang.js), copy the default language file,
|
|
9
|
+
// switching its locale id and language name
|
|
10
|
+
// 2. scan the ts and html files for "'value' | translate:'key'" and, for every key not yet in
|
|
11
|
+
// a locale file, add "key: 'value'," before "// inject:translations" in that file
|
|
12
|
+
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
const { findFiles } = require('../files');
|
|
16
|
+
|
|
17
|
+
const _translateReg = /\s*["']([\w\d?.,!\s\(\)]+)["']\s*\|\s*translate:['"]([\w]+)['"]\s*/gim;
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
nice to have
|
|
21
|
+
catch count
|
|
22
|
+
['"]?([\w\d?.,!\s\(\)]+)['"]?\s*\|\s*translate:['"]([\w]+)['"]:(\d+)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
catch select
|
|
26
|
+
["']?([\w\d?.,!\s\(\)]+)["']?\s*\|\s*translate:['"]([\w]+)['"]:null:["']([\w\d?.,!\s\(\)]+)["']
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
catch normal
|
|
30
|
+
['"]?([\w\d?.,!\s\(\)]+)['"]?\s*\|\s*translate:['"]([\w]+)['"][^:]
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
const startTag = '// inject:translations';
|
|
34
|
+
const endTag = '// endinject';
|
|
35
|
+
|
|
36
|
+
module.exports = (options) => {
|
|
37
|
+
const { prefix, languages } = options;
|
|
38
|
+
const { scan, destination } = options.extract;
|
|
39
|
+
|
|
40
|
+
const localeFile = (lang) => path.join(destination, `${prefix}-${lang.name}.js`);
|
|
41
|
+
|
|
42
|
+
// 1. copy the default language file for a language that has none
|
|
43
|
+
const createMissing = (lang) => {
|
|
44
|
+
const file = localeFile(lang);
|
|
45
|
+
if (fs.existsSync(file)) return;
|
|
46
|
+
|
|
47
|
+
const defaultLanguage = languages.find((x) => x.isDefault);
|
|
48
|
+
// replace 'ar-JO' with 'fr-CA'
|
|
49
|
+
const content = fs.readFileSync(localeFile(defaultLanguage), 'utf8')
|
|
50
|
+
.replace(`'${defaultLanguage.localeId}'`, `'${lang.localeId}'`)
|
|
51
|
+
.replace(`'${defaultLanguage.name}'`, `'${lang.name}'`);
|
|
52
|
+
|
|
53
|
+
fs.writeFileSync(file, content);
|
|
54
|
+
console.log('created', file);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// 2. add the new keys found in the sources to one locale file
|
|
58
|
+
const extractTo = (lang, sources) => {
|
|
59
|
+
const file = localeFile(lang);
|
|
60
|
+
const destinationText = fs.readFileSync(file, 'utf8');
|
|
61
|
+
|
|
62
|
+
const start = destinationText.indexOf(startTag);
|
|
63
|
+
const end = destinationText.indexOf(endTag, start);
|
|
64
|
+
if (start < 0 || end < 0) {
|
|
65
|
+
console.log(`no "${startTag}" ... "${endTag}" in ${file}, skipped`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// one group of lines per source file that has new keys
|
|
70
|
+
const allKeys = [];
|
|
71
|
+
const groups = [];
|
|
72
|
+
for (const source of sources) {
|
|
73
|
+
const content = fs.readFileSync(source, 'utf8');
|
|
74
|
+
const keys = [];
|
|
75
|
+
let match;
|
|
76
|
+
while ((match = _translateReg.exec(content))) {
|
|
77
|
+
const key = match[2];
|
|
78
|
+
// skip keys already in the locale file, or already found in another source
|
|
79
|
+
if (destinationText.indexOf(key + ':') < 0 && allKeys.indexOf(key) < 0) {
|
|
80
|
+
allKeys.push(key);
|
|
81
|
+
keys.push(`${key}: '${match[1]}',`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (keys.length) groups.push(keys.join('\n'));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!groups.length) {
|
|
88
|
+
console.log('no new keys', file);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// the new keys replace whatever was between the tags, then the tags move after them,
|
|
93
|
+
// so the keys stay in the file and the next run adds after them
|
|
94
|
+
const eol = destinationText.includes('\r\n') ? '\r\n' : '\n';
|
|
95
|
+
const lineStart = destinationText.lastIndexOf('\n', start) + 1;
|
|
96
|
+
const indent = destinationText.substring(lineStart, start).match(/^\s*/)[0];
|
|
97
|
+
|
|
98
|
+
const injected =
|
|
99
|
+
eol + indent + groups.join(eol + indent) +
|
|
100
|
+
eol + indent + startTag + ' \n ' + endTag;
|
|
101
|
+
|
|
102
|
+
fs.writeFileSync(
|
|
103
|
+
file,
|
|
104
|
+
destinationText.substring(0, start) + injected + destinationText.substring(end + endTag.length)
|
|
105
|
+
);
|
|
106
|
+
console.log(`added ${allKeys.length} keys to`, file);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// first create missing files, then add the keys
|
|
110
|
+
return async function extract() {
|
|
111
|
+
languages.forEach(createMissing);
|
|
112
|
+
const sources = findFiles(scan, ['.ts', '.html']);
|
|
113
|
+
languages.forEach((lang) => extractTo(lang, sources));
|
|
114
|
+
};
|
|
115
|
+
};
|
package/tasks/cli.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Shared by the package commands (shut/cli.js and angular/cli.js).
|
|
2
|
+
// loadConfig: the package defaults, merged with config.json in the current folder.
|
|
3
|
+
// run: call the task named by the first command line argument.
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
|
|
8
|
+
const loadConfig = () => {
|
|
9
|
+
const defaults = require('./config.json');
|
|
10
|
+
const localFile = path.resolve('config.json');
|
|
11
|
+
const local = fs.existsSync(localFile) ? require(localFile) : {};
|
|
12
|
+
return { ...defaults, ...local };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const run = (command, tasks) => {
|
|
16
|
+
const task = process.argv[2];
|
|
17
|
+
if (!tasks[task]) {
|
|
18
|
+
console.log(`usage: ${command} <${Object.keys(tasks).join('|')}>`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
Promise.resolve(tasks[task]()).catch((err) => {
|
|
22
|
+
console.error(err.message || err);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
module.exports = { loadConfig, run };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"prefix": "sh",
|
|
3
|
+
"assets": {
|
|
4
|
+
"shutPath": "./node_modules/shut/src/",
|
|
5
|
+
"srcPath": "./raw/",
|
|
6
|
+
"distPath": "./src/assets/",
|
|
7
|
+
"iconfontsPath": "./fontfiles/",
|
|
8
|
+
"cssFileName": "sh",
|
|
9
|
+
"minifyPath": "./public/",
|
|
10
|
+
"browserslist": [
|
|
11
|
+
"> 6%"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"ng": {
|
|
15
|
+
"appPath": "./src/app/",
|
|
16
|
+
"templates": "/angulartemplates/"
|
|
17
|
+
},
|
|
18
|
+
"extract": {
|
|
19
|
+
"scan": "./src/app/components",
|
|
20
|
+
"destination": "./src/locale/"
|
|
21
|
+
},
|
|
22
|
+
"locales": {
|
|
23
|
+
"source": "./host/client/placeholder.html",
|
|
24
|
+
"destination": "./host/index/",
|
|
25
|
+
"fileName": "index.html",
|
|
26
|
+
"isUrlBased": false,
|
|
27
|
+
"withFolders": false
|
|
28
|
+
},
|
|
29
|
+
"languages": [
|
|
30
|
+
{
|
|
31
|
+
"name": "en",
|
|
32
|
+
"isRtl": false,
|
|
33
|
+
"localeId": "en"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"name": "ar",
|
|
37
|
+
"isRtl": true,
|
|
38
|
+
"localeId": "ar-JO",
|
|
39
|
+
"isDefault": true
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
package/tasks/files.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Shared file helpers for the plain node tasks.
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
// all files under a folder with one of the extensions, sorted, grouped in extension order
|
|
7
|
+
// e.g. findFiles('src', ['.ts', '.html']) gives all ts files first, then all html files
|
|
8
|
+
const findFiles = (dir, extensions) => {
|
|
9
|
+
const all = [];
|
|
10
|
+
const walk = (folder) => {
|
|
11
|
+
for (const entry of fs.readdirSync(folder, { withFileTypes: true })) {
|
|
12
|
+
const full = path.join(folder, entry.name);
|
|
13
|
+
if (entry.isDirectory()) walk(full);
|
|
14
|
+
else all.push(full);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
walk(dir);
|
|
18
|
+
all.sort();
|
|
19
|
+
return extensions.flatMap((ext) => all.filter((f) => f.endsWith(ext)));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports = { findFiles };
|
package/tasks/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
// old gulp tasks, no longer exported
|
|
3
|
+
// const ng = require('./angular/ng');
|
|
4
|
+
// const locales = require('./angular/locales');
|
|
5
|
+
// const assets = require('./shut/assets');
|
|
6
|
+
// const icons = require('./shut/icons');
|
|
7
|
+
|
|
8
|
+
// new, without gulp
|
|
9
|
+
const shut = require('./shut/shut');
|
|
10
|
+
const shuticons = require('./shut/shut.icons');
|
|
11
|
+
const angular = require('./angular/angular');
|
|
12
|
+
const extract = require('./angular/extract');
|
|
13
|
+
const svelteTranslate = require('./svelte/translate');
|
|
14
|
+
|
|
15
|
+
const defaultConfig = require('./config.json');
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
defaultConfig, shut, shuticons, angular, extract, svelteTranslate
|
|
19
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// shut command line, installed as the "shut" bin of @sekrab/bin.
|
|
3
|
+
// Run from a client folder that has a config.json:
|
|
4
|
+
// npx shut <rawless|buildcss|critical|watch|icons>
|
|
5
|
+
|
|
6
|
+
const { loadConfig, run } = require('../cli');
|
|
7
|
+
const config = loadConfig();
|
|
8
|
+
|
|
9
|
+
run('shut', {
|
|
10
|
+
...require('./shut')(config),
|
|
11
|
+
icons: require('./shut.icons')(config),
|
|
12
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
|
|
2
|
+
exports.CriticalText = function(txt, iscritical) {
|
|
3
|
+
// find /* CRITICAL BEGIN */ and /* CRITICAL END */ and save their index location
|
|
4
|
+
|
|
5
|
+
var re = /\/\* CRITICAL BEGIN \*\/[\s\S]*?\/\* CRITICAL END \*\//gim;
|
|
6
|
+
var s;
|
|
7
|
+
var matches = [];
|
|
8
|
+
while (s = re.exec(txt)) {
|
|
9
|
+
var rtl = s[0];
|
|
10
|
+
matches.push(rtl);
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
// remove from css
|
|
14
|
+
txt = txt.replace(re, '');
|
|
15
|
+
|
|
16
|
+
if (!iscritical) {
|
|
17
|
+
return txt;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// then create another text with the critical content
|
|
21
|
+
let critical = '';
|
|
22
|
+
for (var i = 0; i < matches.length; i++) {
|
|
23
|
+
critical += matches[i] + '\n';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return critical;
|
|
27
|
+
}
|
|
28
|
+
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Shut icons: prepare icon font files generated by the icomoon tool. Plain node, no gulp.
|
|
2
|
+
// Usage from a client project (see cli.js):
|
|
3
|
+
// npx shut icons
|
|
4
|
+
// or in code:
|
|
5
|
+
// await require('@sekrab/bin/tasks/shut/shut.icons.js')(config)();
|
|
6
|
+
//
|
|
7
|
+
// Steps:
|
|
8
|
+
// 1. copy variables.less and selection.json from the icomoon folder into srcPath/dummy
|
|
9
|
+
// 2. copy the icomoon fonts into distPath/fonts
|
|
10
|
+
// 3. in less/ui.icons.less, between "// inject:icons" and "// endinject", write each
|
|
11
|
+
// icon variable followed by its mixin call: @icon-x: "\e900"; .icon(icon-x,@icon-x);
|
|
12
|
+
// 4. in dummy/iconset.html, between "<!-- inject:icons -->" and "<!-- endinject -->",
|
|
13
|
+
// write a preview <li> per icon with its ligature name
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
|
|
18
|
+
module.exports = function (config) {
|
|
19
|
+
const { iconfontsPath, srcPath, distPath } = config.assets;
|
|
20
|
+
|
|
21
|
+
const dummyDir = path.join(srcPath, 'dummy');
|
|
22
|
+
const lessFile = path.join(srcPath, 'less', 'ui.icons.less');
|
|
23
|
+
const htmlFile = path.join(dummyDir, 'iconset.html');
|
|
24
|
+
|
|
25
|
+
// replace whatever sits between the start tag line and the end tag,
|
|
26
|
+
// indent the lines like the end tag, and keep the file's line endings
|
|
27
|
+
const inject = (text, startTag, endTag, lines) => {
|
|
28
|
+
const eol = text.includes('\r\n') ? '\r\n' : '\n';
|
|
29
|
+
const re = new RegExp(`(${startTag}[^\\n]*\\n)[\\s\\S]*?([ \\t]*)(${endTag})`);
|
|
30
|
+
return text.replace(re, (match, start, indent, end) =>
|
|
31
|
+
start + lines.map((line) => indent + line + eol).join('') + indent + end);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const write = (file, content) => {
|
|
35
|
+
fs.writeFileSync(file, content);
|
|
36
|
+
console.log('wrote', file);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return async function icons() {
|
|
40
|
+
// 1 and 2: copy files
|
|
41
|
+
fs.mkdirSync(dummyDir, { recursive: true });
|
|
42
|
+
for (const name of ['variables.less', 'selection.json']) {
|
|
43
|
+
fs.copyFileSync(path.join(iconfontsPath, name), path.join(dummyDir, name));
|
|
44
|
+
}
|
|
45
|
+
fs.cpSync(path.join(iconfontsPath, 'fonts'), path.join(distPath, 'fonts'), { recursive: true });
|
|
46
|
+
console.log('copied icon files and fonts');
|
|
47
|
+
|
|
48
|
+
// icon name => ligature, from selection.json, e.g. { 'arrow-back': 'backward' }
|
|
49
|
+
const selection = JSON.parse(fs.readFileSync(path.join(dummyDir, 'selection.json'), 'utf8'));
|
|
50
|
+
const ligatures = {};
|
|
51
|
+
for (const icon of selection.icons) {
|
|
52
|
+
ligatures[icon.properties.name] = icon.properties.ligatures;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// lines like: @icon-arrow-back: "\e5c4";
|
|
56
|
+
const lines = fs.readFileSync(path.join(dummyDir, 'variables.less'), 'utf8')
|
|
57
|
+
.split(/\r?\n/)
|
|
58
|
+
.filter((line) => line.includes('@icon-'));
|
|
59
|
+
|
|
60
|
+
const lessLines = [];
|
|
61
|
+
let htmlItems = '';
|
|
62
|
+
for (const line of lines) {
|
|
63
|
+
const variable = line.split(':')[0].trim(); // @icon-arrow-back
|
|
64
|
+
const cssName = variable.substring(1); // icon-arrow-back
|
|
65
|
+
const iconName = variable.substring(6); // arrow-back
|
|
66
|
+
|
|
67
|
+
lessLines.push(`${line}\t.icon(${cssName},${variable});`);
|
|
68
|
+
htmlItems += `<li><span class="symbol ${cssName}">${variable}</span> <i>${ligatures[iconName]}</i></li>`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 3 and 4: inject into less and html
|
|
72
|
+
const less = fs.readFileSync(lessFile, 'utf8');
|
|
73
|
+
write(lessFile, inject(less, '// inject:icons', '// endinject', lessLines));
|
|
74
|
+
|
|
75
|
+
const html = fs.readFileSync(htmlFile, 'utf8');
|
|
76
|
+
write(htmlFile, inject(html, '<!-- inject:icons -->', '<!-- endinject -->', [htmlItems]));
|
|
77
|
+
};
|
|
78
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Shut assets: build css from the client's less files + the shut library less files.
|
|
2
|
+
// Plain node, no gulp. Usage from a client project (see cli.js):
|
|
3
|
+
// npx shut <rawless|buildcss|critical|watch>
|
|
4
|
+
// or in code:
|
|
5
|
+
// const shut = require('@sekrab/bin/tasks/shut/shut.js')(config);
|
|
6
|
+
// await shut.buildcss();
|
|
7
|
+
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const less = require('less');
|
|
11
|
+
const CleanCSS = require('clean-css');
|
|
12
|
+
const { CriticalText } = require('./critical.js');
|
|
13
|
+
|
|
14
|
+
module.exports = function (config) {
|
|
15
|
+
const { srcPath, shutPath, distPath, minifyPath, cssFileName } = config.assets;
|
|
16
|
+
|
|
17
|
+
const lessDir = path.join(srcPath, 'less');
|
|
18
|
+
const cssDir = path.join(distPath, 'css');
|
|
19
|
+
|
|
20
|
+
// list less files in the client less folder that start with a prefix, e.g. "ui."
|
|
21
|
+
const listLess = (prefix) =>
|
|
22
|
+
fs.readdirSync(lessDir)
|
|
23
|
+
.filter((f) => f.startsWith(prefix) && f.endsWith('.less'))
|
|
24
|
+
.sort();
|
|
25
|
+
|
|
26
|
+
// replace everything between "// inject:name" and "// endinject" with @import lines
|
|
27
|
+
const inject = (text, name, files) => {
|
|
28
|
+
const imports = files.map((f) => `@import "${f}";`).join('\n');
|
|
29
|
+
const re = new RegExp(`(// inject:${name}\\s*\\n)[\\s\\S]*?(// endinject)`);
|
|
30
|
+
return text.replace(re, `$1${imports}\n$2`);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const write = (file, content) => {
|
|
34
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
35
|
+
fs.writeFileSync(file, content);
|
|
36
|
+
console.log('wrote', file);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// 1. sh.imports.less + ui.*.less + media.*.less => less/all.less
|
|
40
|
+
// 2. compile all.less (with shut library less folder on the path) => css/sh.css
|
|
41
|
+
const rawless = async () => {
|
|
42
|
+
let text = fs.readFileSync(path.join(lessDir, 'sh.imports.less'), 'utf8');
|
|
43
|
+
text = inject(text, 'uiless', listLess('ui.'));
|
|
44
|
+
text = inject(text, 'medialess', listLess('media.'));
|
|
45
|
+
|
|
46
|
+
const allLess = path.join(lessDir, 'all.less');
|
|
47
|
+
write(allLess, text);
|
|
48
|
+
|
|
49
|
+
const output = await less.render(text, {
|
|
50
|
+
filename: path.resolve(allLess),
|
|
51
|
+
paths: [path.join(shutPath, 'less')],
|
|
52
|
+
});
|
|
53
|
+
write(path.join(cssDir, `${cssFileName}.css`), output.css);
|
|
54
|
+
return output.css;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// rawless, then minify into the public folder
|
|
58
|
+
const buildcss = async () => {
|
|
59
|
+
const css = await rawless();
|
|
60
|
+
const min = new CleanCSS().minify(css);
|
|
61
|
+
if (min.errors.length) throw new Error(min.errors.join('\n'));
|
|
62
|
+
write(path.join(minifyPath, 'css', `${cssFileName}.min.css`), min.styles);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// rawless, then split into sh.general.css and sh.critical.css
|
|
66
|
+
// (critical = blocks wrapped in /* CRITICAL BEGIN */ ... /* CRITICAL END */)
|
|
67
|
+
const critical = async () => {
|
|
68
|
+
const css = await rawless();
|
|
69
|
+
write(path.join(cssDir, `${cssFileName}.general.css`), CriticalText(css, false));
|
|
70
|
+
write(path.join(cssDir, `${cssFileName}.critical.css`), CriticalText(css, true));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// rebuild on change of sh.*, ui.*, media.* less files (all.less is ignored, we write it)
|
|
74
|
+
const watch = () => {
|
|
75
|
+
const run = () => rawless().catch((err) => console.error(err.message || err));
|
|
76
|
+
let timer;
|
|
77
|
+
run();
|
|
78
|
+
fs.watch(lessDir, (event, file) => {
|
|
79
|
+
if (!/^(sh|ui|media)\..*\.less$/.test(file || '')) return;
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
timer = setTimeout(run, 100);
|
|
82
|
+
});
|
|
83
|
+
console.log('watching', lessDir);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
return { rawless, buildcss, critical, watch };
|
|
87
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Svelte translate: collect translate('value', 'key') calls into keys.lang.json files.
|
|
2
|
+
// Plain node, no gulp. Usage from a client project (see angular/cli.js):
|
|
3
|
+
// npx sekrab svelteTranslate
|
|
4
|
+
// or in code:
|
|
5
|
+
// await require('@sekrab/bin/tasks/svelte/translate.js')(config)();
|
|
6
|
+
//
|
|
7
|
+
// For every language, keys already in extract.destination/keys.lang.json keep their values;
|
|
8
|
+
// new keys found in the ts and svelte files under extract.scan are added with the found value.
|
|
9
|
+
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
const { findFiles } = require('../files');
|
|
13
|
+
|
|
14
|
+
// match all translate('text', 'key',);
|
|
15
|
+
const _translateReg = /translate\(\s*'([^']*)'\s*,\s*'([^']*)'[\s,]*\)/gim;
|
|
16
|
+
// VER_NEXT: do toast meessages and Res.Get
|
|
17
|
+
// const _toastReg = /Toast.Show\(Error|Warning|Success)\('([^']*)'/gim;
|
|
18
|
+
|
|
19
|
+
module.exports = (options) => {
|
|
20
|
+
const { languages } = options;
|
|
21
|
+
const { scan, destination } = options.extract;
|
|
22
|
+
|
|
23
|
+
return async function svelteTranslate() {
|
|
24
|
+
// key => value found in the sources; the first source with a key wins
|
|
25
|
+
const found = {};
|
|
26
|
+
for (const source of findFiles(scan, ['.ts', '.svelte'])) {
|
|
27
|
+
const content = fs.readFileSync(source, 'utf8');
|
|
28
|
+
let match;
|
|
29
|
+
while ((match = _translateReg.exec(content)) !== null) {
|
|
30
|
+
if (!(match[2] in found)) found[match[2]] = match[1];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const lang of languages) {
|
|
35
|
+
const jsonPath = path.join(destination, `keys.${lang.name}.json`);
|
|
36
|
+
const keys = fs.existsSync(jsonPath) ? JSON.parse(fs.readFileSync(jsonPath, 'utf8')) : {};
|
|
37
|
+
|
|
38
|
+
let added = 0;
|
|
39
|
+
for (const key in found) {
|
|
40
|
+
if (!(key in keys)) {
|
|
41
|
+
keys[key] = found[key];
|
|
42
|
+
added++;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fs.mkdirSync(path.dirname(jsonPath), { recursive: true });
|
|
47
|
+
fs.writeFileSync(jsonPath, JSON.stringify(keys, null, 4));
|
|
48
|
+
console.log(`added ${added} keys to`, jsonPath);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
package/README.md
DELETED