UNPKG

5.16 kBJavaScriptView Raw
1const chalk = require('chalk');
2const path = require('path');
3const fse = require('fs-extra');
4const copy = require('recursive-copy');
5const through = require('through2');
6const build = require('../build');
7const translate = require('../translate');
8
9const afterLastSlashRegex = new RegExp(`\\${path.sep}*([^\\${path.sep}]*)$`);
10/**
11 * Submodule's relative paths to specific files
12 * @type {String[]}
13 */
14const MARKET_ARRAY = [
15 {excludePrefix: '**', path: 'index.html'},
16 // {excludePrefix: '**', path: '__build.js'},
17 // {excludePrefix: '**', path: 'js/main.js'},
18 // {excludePrefix: '**', path: 'js/config/local-api-config.json'},
19 // {excludePrefix: '**', path: 'js/config/stage-api-config.json'},
20 // {excludePrefix: '**', path: 'js/config/state-routes.js'},
21];
22const MARKET_FILES = MARKET_ARRAY.map((item)=> item.path);
23const FILTER = [
24 '**/*',
25 '!**/.git/**',
26 '!**/.git*',
27 '!**/*.spec.js',
28 '!**/node_modules/**',
29 '!**/bower_components/**',
30 '!**/karma.conf.js',
31 '!**/main.karma.js',
32 '!**/*.md'
33];
34let replacer = (output)=> (match, p1) => p1 + output;
35
36let transformFiles = {
37 'main.js': (_str, src, output) => {
38 let pathRegex = new RegExp('(.*' + src + '.*:.*)(' + src + ')', 'g');
39 let appIdRegex = new RegExp('(.*applicationId.*)(' + src + ')', 'g');
40
41 return _str
42 .replace(pathRegex, replacer(output))
43 .replace(appIdRegex, replacer(output));
44 },
45 '__build.js': (_str, src, output) => {
46 let pathRegex = new RegExp('(:.*)(' + src + ')', 'g');
47
48 return _str
49 .replace(pathRegex, replacer(output));
50 },
51 'index.html': (_str, src, output) => {
52 let pathRegex = new RegExp('(.*' + src + '.*:.*)(' + src + ')("|\').*', 'g');
53 let appIdRegex = new RegExp('(.*applicationId.*)(' + src + ')', 'g');
54 let selfRegex = new RegExp(src, 'g');
55 let mainjsRegex = /(data-main=".*?)(\.min)?(")/;
56
57 let out = _str
58 .replace(pathRegex, (match, p1, p2, p3) => {
59 return p1 + output + p3 + ',\n' + match.replace(selfRegex, output)
60 })
61 .replace(appIdRegex, replacer(output))
62 .replace(mainjsRegex, '$1.min$3');
63
64 return out;
65 }
66};
67
68module.exports = function buildApp(_args, _outputPath, _configPath, _market = '', _regexp = [], _verbose = false) {
69 const IS_MARKET = _market !== '' && _market;
70 let apps = [];
71 let filters = FILTER.concat(_regexp);
72
73 if (typeof _configPath !== 'undefined') {
74 let configPath = path.resolve(_configPath);
75 let config = require(configPath);
76 if (_args.length > 0) {
77 apps = config.apps.filter(element => _args.some(e => e === element.name));
78 } else {
79 apps = config.apps;
80 }
81
82 apps = apps.map(element => {
83 element.src = path.resolve(element.src);
84 element.output = path.resolve(element.output);
85 return element;
86 });
87 } else if (typeof _outputPath !== 'undefined' && _args.length > 0) {
88 let outputPath = path.resolve(_outputPath);
89 let srcPath = path.resolve(_args[0]);
90
91 apps.push({
92 src: srcPath,
93 output: outputPath
94 });
95 }
96
97 apps.forEach((element) => {
98 let market = element.market || _market;
99 const HAS_MARKET = market || IS_MARKET;
100 let srcPath = path.normalize(element.src);
101 let outputPath = path.normalize(element.output);
102 let srcDirname = srcPath.match(afterLastSlashRegex)[1];
103 let outputDirname = outputPath.match(afterLastSlashRegex)[1];
104 let filter = filters;
105 let replace = `-${market}.`;
106 let getCleanName = (fileName)=> fileName.replace(replace, '.');
107
108 if (HAS_MARKET) {
109 filter = filters.concat(MARKET_ARRAY.map((item)=>
110 path.join(`!${item.excludePrefix}`, item.path)));
111 }
112
113 fse.emptyDirSync(outputPath);
114 copy(srcPath, outputPath, {
115 overwrite: true,
116 dot: true,
117 filter: filter,
118 rename: function(filePath) {
119 let fileName = path.basename(filePath);
120 let cleanName = getCleanName(fileName);
121 let dirName = path.dirname(filePath);
122 let clearPath = path.join(dirName, cleanName);
123
124 if (!(HAS_MARKET && fileName.includes(replace) &&
125 (MARKET_FILES.includes(clearPath) || MARKET_FILES.includes(cleanName)
126 ))) {
127 return filePath;
128 }
129
130 return clearPath;
131 },
132 transform: (_src, _dest, _stats) => {
133 let fileName = path.basename(_src);
134 let cleanName = getCleanName(fileName);
135
136 if (typeof transformFiles[cleanName] !== 'function') {
137 return null;
138 }
139 return through((chunk, enc, done) => {
140 let output = chunk.toString();
141 output = transformFiles[cleanName](output, srcDirname, outputDirname);
142 done(null, output);
143 });
144 }
145 }).then((_results) => {
146 console.info(chalk.magenta(_results.length + ' file(s) copied to ',
147 outputPath));
148
149 translate
150 .replaceText([outputPath])
151 .then(() => build([outputPath], true, market, _verbose))
152 .catch(() => {
153 console.log(chalk.red(outputPath + ': translation failed'));
154 build([outputPath], true, market);
155 });
156 /** @todo: translate */
157 }).catch(_err => {
158 console.error(_err);
159 throw _err;
160 });
161 });
162};
163