-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·655 lines (597 loc) · 23.2 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#!/usr/bin/env node
const program = require('commander');
const { join, resolve } = require('path');
const path = require('path');
const fs = require('fs');
const fsExtra = require('fs-extra');
const chalk = require('chalk');
const shell = require('shelljs');
const exec = shell.exec;
const figlet = require('figlet');
const os = require('os');
const simpleGit = require('simple-git');
const copydir = require('copy-dir');
const replace = require('replace-in-file');
const package = require('./package.json');
// const cloneConfig = require(process.cwd() +'./clone.config.js');
// const cloneConfig = require('./clone.config.js');
const cloneConfig = config_require('clone.config.js');
const exit = process.exit;
const error = (...message) =>
console.error(
chalk.black.bgRed(' Error: '),
chalk.red.bgBlack(' ', ...message, ' '),
);
const warn = (...message) =>
console.warn(
chalk.yellow.bgBlack.inverse(' Warn: '),
chalk.yellow(' ', ...message, ' '),
);
const log = (...message) =>
console.log(
chalk.magenta.bgBlack.inverse(' Log: '),
chalk.magenta(' ', ...message, ' '),
);
function config_require(name) {
try {
const configPath = shell.pwd() + '/' + name; //require.resolve(name);
console.log(configPath);
return require(configPath);
} catch (e) {
error('Needs config file in root base project: clone.config.js');
exit(0);
// return false;
}
}
// if (module_exists('./clone.json')) {
// const cloneConfig = require('./clone.json');
// } else {
// }
console.log(chalk.cyan(figlet.textSync('Simple React Native Clone')));
const sleep = () => new Promise(resolve => setTimeout(() => resolve(), 500));
//////////////////// Init variables clone config ////////////////////////
cloneConfig.clone = cloneConfig.clone.map(clone => {
// const clone = {};
clone.source = cloneConfig.base.folder;
clone.sourceFull = path.resolve(cloneConfig.base.folder);
clone.dest = clone.folder;
clone.destFull = path.resolve(clone.dest);
clone.packageOld = cloneConfig.base.package;
clone.nameProject = clone.name.replace(/\s+/g, '');
// clone.doublequotes = '"';
// clone.quotes = "'";
// clone.packageNew = clone.package;
// clone.packageNew = cloneConfig.clone[0].package;
// clone.name = cloneConfig.clone[0].name;
if (
!clone.source ||
!clone.dest ||
// !clone.packageOld ||
// !clone.packageNew ||
!clone.name
) {
warn('Needs set parameters (folders, names) in config file');
exit(0);
}
if (path.resolve(clone.source) == path.resolve(clone.dest)) {
warn(
"Oh No, No, No. Don't make equvivalent Source folde and Destinition folder",
);
exit(0);
}
// if (clone.source == join('./') || !clone.source) {
// warn('Bad parameter Source folder');
// exit(0);
// }
/** Adding exclude folders and files from all clones */
let copy_all = [];
clone.copy_global = cloneConfig.copy_global || [];
clone.copy = clone.copy || [];
copy_all = [...clone.copy_global, ...clone.copy];
copy_all.map(copyObject => {
// copyObject.from = replaceCloneVariable(copyObject.from, clone);
// copyObject.to = replaceCloneVariable(copyObject.to, clone);
let excludePath = path.normalize(
replaceCloneVariable(copyObject.from, clone),
);
// console.log(
// 'clone.name: ' + clone.name + ' excludePath:' + excludePath,
// );
shell.test('-e', clone.sourceFull + '/' + excludePath) &&
cloneConfig.exclude.push(excludePath);
// warn('\nExclude: ' + excludePath);
});
return clone;
});
/** COPY PROJECT */
async function copyProject(clone) {
// const load = loading('Cloning project').start();
// await sleep();
fs.existsSync(clone.destFull) || shell.exec('mkdir ' + clone.destFull);
// remove all files in clone folder without '.git
let ls;
if (cloneConfig.options.remove) {
// if (
// (clone.package.toLowerCase().includes('fit') &&
// clone.package.toLowerCase().includes('base')) ||
// (clone.packageOld.toLowerCase().includes('fit') &&
// clone.packageOld.toLowerCase().includes('base'))
// ) {
// shell.cd('../..');
// let dir = exec('pwd');
// exec(`rm -rf ${dir} 2> /dev/null || true`);
// return 1;
// }
ls = shell
.ls('-A', clone.destFull)
.filter(e => e !== '.git' && e !== '.' && e !== '..');
// console.log('Remove LS: ', ls);
ls = ls.map(file => {
let remove = clone.destFull + '/' + file;
// log('Remove: ', remove);
return fs.existsSync(remove) ? remove : '';
});
ls = ls.filter(e => !!e);
console.log('REMOVE: ', ls);
ls.length > 0 && shell.rm('-rf', ls);
}
if (os.type() === 'Linux' || os.type() === 'Darwin') {
const excludePOSIX = cloneConfig.exclude
.map(item => `--exclude ${item}`)
.join(' ');
// console.log(
// `rsync -av --progress ${clone.sourceFull}/ ${clone.destFull} ${excludePOSIX}`,
// );
shell.exec(
`rsync -av --progress ${clone.sourceFull}/ ${clone.destFull} ${excludePOSIX}`,
// `rsync -av --progress ${clone.source}/ ${clone.dest} ${excludePOSIX}`,
);
} else {
const excludeWindows = cloneConfig.exclude.join(' ');
shell.exec(
// `robocopy ${clone.source} ${clone.dest} /mir /xd node_modules .history .git android/.gralde android/app/build android/build android/.idea`,
`robocopy ${clone.sourceFull} ${clone.destFull} /mir /xd ${excludeWindows}`,
);
}
shell.cd(clone.destFull).code !== 0 && exit(1);
let copy_all = [];
clone.copy_global = cloneConfig.copy_global || [];
clone.copy = clone.copy || [];
copy_all = [...clone.copy_global, ...clone.copy];
copy_all.forEach((item, index, array) => {
let from = path.resolve(
clone.sourceFull +
'/' +
path.join(replaceCloneVariable(item.from, clone)),
);
let to = path.resolve(
clone.destFull +
'/' +
path.join(replaceCloneVariable(item.to, clone)),
);
log('COPY');
console.log('From: ' + from, '\n To: ' + to);
copydir.sync(from, to, {
utimes: false, // Boolean | Object, keep addTime or modifyTime if true
mode: false, // Boolean | Number, keep file mode if true
cover: true, // Boolean, cover if file exists
filter: function (stat, filepath, filename) {
// do not want copy .html files
// if (stat === 'file' && path.extname(filepath) === '.html') {
// return false;
// }
// do not want copy .svn directories
// if (stat === 'directory' && filename === '.svn') {
// return false;
// }
// do not want copy symbolicLink directories
if (stat === 'symbolicLink') {
return false;
}
return true; // remind to return a true value when file check passed.
},
});
});
// load.stop();
}
/** RENAME PROJECT */
async function renameProject(clone) {
// const load = loading('Rename project').start();
// await sleep();
if (!fs.existsSync(clone.destFull)) return 1;
const git = simpleGit(clone.destFull);
shell.cd(clone.destFull).code !== 0 && exit(1);
// log(path.resolve(shell.ls('-d')[0]));
//////////////////// Commit before RENAME ////////////////////////
try {
if (!fs.existsSync(clone.destFull + '/.git')) {
log(`GIT INIT on Project ${clone.name}`);
await git.init();
}
await git.add('./*');
await git.commit(
`📍 clone auto-commit before rename ${new Date().toISOString()}`,
);
} catch (e) {
error(e);
exit(1);
}
//////////////////// RENAME ////////////////////////
// log(`RENAME Project ${clone.name}`);
const rename =
!clone.package || clone.package == clone.packageOld
? `npx react-native-rename "${clone.name}"`
: `npx react-native-rename "${clone.name}" -b ${clone.package}`;
log(`RENAME ${rename}`);
if (shell.exec(rename).code !== 0) {
error(
`Error: npx react-native-rename "${clone.name}" -b ${clone.package}`,
);
exit(1);
}
//////////////////// Replace In File ////////////////////////
let renameFix = [
{
files: [
'./android/app/BUCK',
'./android/app/src/main/AndroidManifest.xml',
],
from: '/(package\\s*=\\s*")(.+)(")/giu',
to: '$1${clone.package}$3',
},
{
files: './android/app/build.gradle',
from: '/(applicationId\\s*")(.+)(")/giu',
to: ['$1${clone.package}$3'],
},
];
let replace_all = [];
clone.replace_global = cloneConfig.replace_global || [];
clone.replace = clone.replace || [];
replace_all = [...renameFix, ...clone.replace_global, ...clone.replace];
console.log('Replace In File JSON options:', replace_all);
replace_all.forEach((replaceitem, index) => {
if (!replaceitem.files || !replaceitem.from || !replaceitem.to) {
warn(
`Replace In File not full options clone name: `,
chalk.bold(clone.name),
);
return;
}
let options = Object.assign({}, replaceitem);
/** inject clone variable in all property options "Replace In File" */
for (var key in replaceitem) {
if (Array.isArray(replaceitem[key])) {
let newArray = options[key].map(arrayvalue => {
let match = replaceCloneVariable(arrayvalue, clone);
if (key == 'files')
match = clone.destFull + '/' + path.normalize(match);
if (key == 'from') match = eval(match);
// log(`Replacer in file options.${key}[${i}]=` + match);
return match;
});
options[key] = newArray;
} else {
options[key] = replaceCloneVariable(replaceitem[key], clone);
if (key == 'files')
options[key] =
clone.destFull + '/' + path.normalize(options[key]);
if (key == 'from') options[key] = eval(options[key]);
// log(`Replace in file options.${key}=` + options[key]);
}
}
shell.cd(clone.destFull).code !== 0 && exit(1);
try {
const results = replace.sync(options);
results.map(result => {
// result.hasChanged ||
// console.log(
// `Replace in file: ${result.file}`,
// chalk.yellow(' NOT CHANGED'),
// );
if (result.hasChanged) {
// console.log('Replace In File RESULT options:', options);
console.log(
`Replace In File: ${result.file}`,
chalk.green(' MODIFIED'),
);
}
});
} catch (error) {
console.error(chalk.red('Replace In File Error occurred: ', error));
}
});
//////////////////// Commit after RENAME ////////////////////////
try {
log(`GIT COMMIT after rename on Project ${clone.name}`);
await git.add('./*');
await git.commit(
`📍 clone auto-commit ${new Date().toISOString()}`, //📍 Unicode: U+1F4CD
);
} catch (e) {
error(e);
exit(1);
}
// load.stop();
}
/** RENAME PROJECT OLD */
async function renameProjectOLD(project) {
const load = loading('Rename packages').start();
await sleep();
// project = {
// source: '../rn_starter',
// dest: path.resolve(join('../MyApp2')),
// name: 'MyApp2name', //new Package
// packageOld: 'rn_starter',
// packageNew: 'MyApp2',
// };
const pack = require(project.dest + '/app.json');
function replaceContent(file) {
shell.sed('-i', project.packageOld, project.packageNew, file);
shell.sed('-i', pack.name, project.name.toLowerCase(), file);
shell.sed('-i', pack.displayName, project.name.toLowerCase(), file);
shell.sed('-i', pack.name, project.name, file);
shell.sed('-i', pack.displayName, project.name, file);
console.log(file, chalk.green('RENAME'));
}
const paths = [
`${project.dest}/*.js`,
`${project.dest}/*.json`,
`${project.dest}/**/*.xml`,
`${project.dest}/**/*.pbxproj`,
`${project.dest}/**/*.java`,
`${project.dest}/**/*.gradle`,
`${project.dest}/**/*.h`,
`${project.dest}/**/*.m`,
`${project.dest}/**/*.xcscheme`,
`${project.dest}/**/*.storyboard`,
`${project.dest}/**/*.plist`,
`${project.dest}/**/*.pro`,
`${project.dest}/ios/Podfile`,
];
paths.forEach(item => {
shell.ls(item).forEach(file => replaceContent(file));
});
load.stop();
const load2 = loading('Creating folders in the new package').start();
await sleep();
const pathAndroid = `${
project.dest
}/android/app/src/main/java/${project.packageNew.replace(/\./g, '/')}`;
const pathIOS = `${project.dest}/ios`;
log('pathIOS=', pathIOS);
const foldersIOS = ['', '-tvOS', '-tvOSTests', '.xcodeproj', 'Tests'];
await Promise.all(
foldersIOS.map(async folder => {
await new Promise(resolve => {
let from = `${pathIOS}/${pack.name}${folder}`;
let to = `${pathIOS}/${project.name.toLowerCase()}${folder}`;
fs.rename(from, to, function (err) {
console.log('\nFrom: ', from, '\nTo: ', to);
if (err) {
console.log(
chalk.yellow('Failed to rename IOS folder.'),
chalk.red('File or folder does not exist'),
);
resolve();
} else {
console.log(
chalk.green('IOS directory renamed successfully!'),
);
resolve();
}
});
});
}),
);
load2.stop();
const load3 = loading('Renaming files in the IOS folder').start();
const iosFilesRename = [
{
from: `${pathIOS}/${project.name.toLowerCase()}.xcodeproj/xcshareddata/xcschemes/${pack.name.toLowerCase()}-tvOS.xcscheme`,
to: `${pathIOS}/${project.name.toLowerCase()}.xcodeproj/xcshareddata/xcschemes/${project.name.toLowerCase()}-tvOS.xcscheme`,
},
{
from: `${pathIOS}/${project.name.toLowerCase()}.xcodeproj/xcshareddata/xcschemes/${pack.name.toLowerCase()}.xcscheme`,
to: `${pathIOS}/${project.name.toLowerCase()}.xcodeproj/xcshareddata/xcschemes/${project.name.toLowerCase()}.xcscheme`,
},
{
from: `${pathIOS}/${project.name.toLowerCase()}Tests/${pack.name.toLowerCase()}Tests.m`,
to: `${pathIOS}/${project.name.toLowerCase()}Tests/${project.name.toLowerCase()}Tests.m`,
},
];
await Promise.all(
iosFilesRename.map(async folder => {
await new Promise(resolve => {
fs.rename(folder.from, folder.to, function (err) {
console.log('\nFrom: ', folder.from, '\nTo: ', folder.to);
if (err) {
console.log(
chalk.yellow('Failed to rename IOS file.'),
chalk.red('File or folder does not exist'),
);
resolve();
} else {
console.log(
chalk.green('IOS file successfully renamed!'),
);
resolve();
}
});
});
}),
);
load3.stop();
const load4 = loading(
'Copying contents of the previous package to the new package',
).start();
const source = `${
project.dest
}/android/app/src/main/java/${project.packageOld.replace(/\./g, '/')}`;
await new Promise(resolve => {
console.log('\nsource: ', source, '\npathAndroid: ', pathAndroid);
fsExtra.move(source, pathAndroid, function (err) {
if (err) {
error(
'Failed to copy files to the new directory\n',
err.message,
);
} else {
console.log(chalk.green('Files copied to the new directory'));
}
resolve();
});
});
load4.stop();
}
/** INIT COMMANDS */
program.version(package.version, '-v, --version', 'current version');
program
.command('clone [name]')
.description(
'Clones base project to new directories. Choice project [name == clone.name|clone.nameProject]',
)
.action(async (name, options) => {
// console.log('project ', project);
// console.log('read config from %s', program.opts().config);
// console.log('setup for %s env(s) with %s mode', options.setup_mode);
// exit(0);
// warn('Starting...');
for (let i = 0; i < cloneConfig.clone.length; i++) {
let clone = cloneConfig.clone[i];
if (name && !(name == clone.name || name == clone.nameProject)) {
continue;
}
console.log(
chalk.magentaBright(
'\n\n//////////////////// CLONING ////////////////////',
),
);
console.log(
chalk.magentaBright('NAME: '),
chalk.green(clone.name),
' ',
chalk.magentaBright('NAME PROJECT: '),
chalk.green(clone.nameProject),
);
console.log('Current clone JSON:', clone);
await copyProject(clone);
await renameProject(clone);
}
// warn('Done!');
console.log(
chalk.blue(
'\nPROJECT CLONE CREATED, PLEASE CLEAN THE CACHE AND RECOMPILE YOUR PROJECT!\n',
),
);
});
program
.command('build [name]')
.description(
'Build all clones or one [name == clone.name|clone.nameProject]',
)
.option('-i, --ios', 'build only ios platform')
.option('-a, --android', 'build only android platform')
.action(async (name, options) => {
let build_ios_flag = !options.android;
let build_android_flag = !options.ios;
console.log('build_ios_flag=', build_ios_flag);
console.log('build_android_flag=', build_android_flag);
// exit(0);
for (let i = 0; i < cloneConfig.clone.length; i++) {
let clone = cloneConfig.clone[i];
if (name && !(name == clone.name || name == clone.nameProject)) {
continue;
}
console.log(
chalk.magentaBright(
'\n\n//////////////////// BUILD CLONE ////////////////////',
),
);
console.log(
chalk.magentaBright('NAME: '),
chalk.green(clone.name),
' ',
chalk.magentaBright('NAME PROJECT: '),
chalk.green(clone.nameProject),
);
// if (!fs.existsSync(clone.destFull + '/node_modules')) {
// warn("copyNodeModules START....")
// copyNodeModules(
// clone.sourceFull,
// clone.destFull,
// { devDependencies: false },
// (err, results) => {
// if (err) {
// console.error('ERROR Copy node_modules: ', err);
// return;
// }
// // Object.keys(results).forEach(name => {
// // const version = results[name];
// // console.log(`Package name: ${name}, version: ${version}`);
// // });
// },
// );
// exec(`npx copy-production-deps ${clone.sourceFull} ${clone.destFull}`);
// warn("copyNodeModules END....")
// }
shell.cd(clone.destFull).code !== 0 && exit(1);
if (exec('yarn check --verify-tree').code !== 0) {
// exec('yarn install --offline --production').code !== 0 ||
// exec('yarn install --production');
exec('yarn install --offline').code !== 0 ||
exec('yarn install');
console.log(chalk.green('Install node_modules complete'));
exec('npx pod-install').code !== 0;
// || error('npx pod-install');
}
if (build_ios_flag === true) {
shell.cd(clone.destFull).code !== 0 && exit(1);
cloneConfig.build_ios.map(cmd => {
let strcmd = replaceCloneVariable(cmd, clone);
console.log(chalk.cyan(strcmd));
if (strcmd.substr(0, 3) == 'cd ') {
shell.cd(strcmd.match(/(?<=cd\s+).+/i)[0]);
} else {
shell.exec(
strcmd,
// {shell: '/bin/bash'}
);
}
});
}
if (build_android_flag === true) {
shell.cd(clone.destFull).code !== 0 && exit(1);
cloneConfig.build_android.map(cmd => {
let strcmd = replaceCloneVariable(cmd, clone);
console.log(chalk.cyan(strcmd));
if (strcmd.substr(0, 3) == 'cd ') {
shell.cd(strcmd.match(/(?<=cd\s+).+/i)[0]);
} else {
shell.exec(
strcmd,
// {shell: '/bin/bash'}
);
}
});
}
}
// var builder = require('react-native-build');
// builder(clone.destFull + '/android', function (result) {
// console.log(result); //output path
// });
});
program.parse(process.argv);
/////////////////// Tools ///////////////////
function replaceCloneVariable(string, clone) {
// log('Clone variable input string=' + string);
const str = string.replace(
/(\$\{(.+?)\})/giu,
(matched, index, original) => {
let match = matched.match(/(?<=\.).+(?=\})/giu);
return clone[match];
},
);
// log('Clone variable injected=' + str);
return str;
}