-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmobiscroll.js
executable file
·815 lines (732 loc) · 29.3 KB
/
mobiscroll.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#!/usr/bin/env node
const program = require('commander');
const inquirer = require('inquirer');
const fs = require('fs');
const utils = require('./src/utils.js');
const configIonic = require('./src/configIonic.js').configIonic;
const configAngular = require('./src/configAngular.js').configAngular;
const chalk = require('chalk');
const path = require('path');
const helperMessages = require('./src/helperMessages.js');
const ncp = require('ncp').ncp;
const figlet = require('figlet');
const os = require('os');
const semver = require('semver');
var isNpmSource = true;
var isTrial = false;
var isLite = false;
var isLazy = false;
var legacyPeerFlag = false;
var run = utils.run;
var printFeedback = utils.printFeedback;
//var printError = utils.printError;
var printWarning = utils.printWarning;
var localCliVersion = require('./package.json').version;
var mobiscrollVersion = null;
var useGlobalNpmrc = false;
var proxyUrl = '';
var useScss = undefined;
var startIoncVesrion = '';
process.env.HOME = process.env.HOME || ''; // fix npm-cli-login plugin on windows
function checkUpdate(proxy) {
let proxyParam = '';
if (proxy) {
proxyParam = ' --proxy ' + proxy;
}
return new Promise((resolve) => {
run('npm show @mobiscroll/cli version' + proxyParam, true, true, true).then((npmCliVersion) => {
// get the mobiscroll cli version from npm
if (npmCliVersion) {
npmCliVersion = npmCliVersion.trim();
if (localCliVersion != npmCliVersion) {
// if the two versions are not equal ask for update
inquirer
.prompt({
type: 'input',
name: 'update',
message: `The Mobiscroll CLI has an update available (${localCliVersion} => ${npmCliVersion})! Would you like to install it? (Y/n)`,
default: 'y',
})
.then((answer) => {
if (answer.update.toLowerCase() == 'y') {
run('npm install -g @mobiscroll/cli@latest' + proxyParam).then(() => {
printFeedback(`Updated Mobiscroll CLI to ${npmCliVersion}! \n\nPlease re-run your command!\n`);
process.exit();
});
} else {
console.log(`Skipping the installation of the latest Mobiscroll CLI version.`);
resolve();
}
});
} else {
// No CLI update found continuing the installation...
resolve();
}
} else {
utils.printWarning(
`It looks like the CLI couldn't determine the installed cli version. This problem might indicate npm problems on your system. You can update it manually by running the ${chalk.gray(
'npm install npm@latest -g'
)} `
);
console.log(`${chalk.magenta('\nIf the problem persists get in touch at [email protected].')}`);
resolve();
}
});
});
}
function notLoggedInFeedback() {
printFeedback(`You are not logged in to the Mobiscroll npm registry ${useGlobalNpmrc ? 'globally' : 'locally'}!\n`);
if (!useGlobalNpmrc) {
console.log(
`${chalk.yellow(
'Please Note:'
)} If you are logged in globally you will have to use the -g/--global flag with this command.\n\nUsage: ${chalk.gray(
'mobiscroll logout -g'
)}`
);
}
}
function removeTokenFromNpmrc(currDir) {
const npmrcPath = path.resolve(currDir, '.npmrc');
const yarnRcPath = path.resolve(currDir, '.yarnrc.yml');
if (fs.existsSync(npmrcPath)) {
let content = fs.readFileSync(npmrcPath).toString();
if (content.length > 5) {
// there is difference in the format of the auth token
const regexNpmFormat = /\/\/npm\.mobiscroll\.com\/:_authToken=.+\s\S+npm.mobiscroll.com\/?/gim;
const regexCliFormat = /@mobiscroll:registry=https:\/\/npm\.mobiscroll\.com(\/?)\s+(\S+)$/gim;
content = content.replace(regexCliFormat, '').replace(regexNpmFormat, '');
utils.writeToFile(npmrcPath, content, () => {
printFeedback('Successful logout!\n');
});
} else {
notLoggedInFeedback();
}
if (fs.existsSync(yarnRcPath)) {
utils.updateAuthTokenInYarnrc(currDir, true);
}
} else {
console.log(`No${useGlobalNpmrc ? ' global' : ' local'} .npmrc file found.`);
notLoggedInFeedback();
}
}
function handleScss() {
useScss = true;
}
function handleCss() {
useScss = false;
}
function handleProxy(url) {
proxyUrl = url;
}
function handleTrial() {
isTrial = true;
}
function handleLite() {
isLite = true;
}
function handleNpmInstall() {
isNpmSource = false;
}
function handleLegacyPeerFlags() {
legacyPeerFlag = true;
}
function handleMobiscrollVersion(vers) {
mobiscrollVersion = vers;
}
function handleStartIonicVersion(version) {
startIoncVesrion = version;
}
function handleGlobalInstall() {
useGlobalNpmrc = true;
}
function config(settings, callback) {
var packageJson = settings.packageJson;
if (!packageJson) {
try {
packageJson = require(settings.packageJsonLocation);
settings.packageJson = packageJson;
} catch (err) {
utils.printError('Could not open package.json file.\n\n' + err);
}
}
let projectType = settings.projectType;
utils.checkMeteor(packageJson, settings.currDir, projectType);
switch (projectType) {
case 'angular':
settings.isIonicApp = false;
configAngular(settings, callback);
break;
case 'angularjs':
break;
case 'ionic':
settings.isLazy = isLazy;
configIonic(settings, callback);
break;
case 'react':
helperMessages.reactHelp(settings.apiKey, settings.isLite, settings.isNpmSource, settings.useScss, settings.mobiscrollVersion);
if (callback) {
callback();
}
break;
case 'vue':
helperMessages.vueHelp(settings.isNpmSource, settings.useScss, settings.mobiscrollVersion);
if (callback) {
callback();
}
break;
case 'jquery':
helperMessages.jsHelp('jquery');
if (callback) {
callback();
}
break;
case 'javascript':
helperMessages.jsHelp('javascript');
if (callback) {
callback();
}
break;
}
}
function cloneProject(url, type, framework, name, newAppLocation, gitOptions, callback) {
utils.printLog(`Cloning ${type} starter app from git: ${url}`);
utils
.run('git clone ' + url + ' ' + name, true)
.then(() => {
utils.printLog(`Repository cloned successfully.`);
process.chdir(newAppLocation); // change directory to node modules folder
console.log(`Installing dependencies may take several minutes:\n`);
utils.run('npm install', true).then(() => {
utils.checkMbscNpmLogin(isTrial, useGlobalNpmrc, proxyUrl, framework, mobiscrollVersion, (userName, useTrial) => {
let configObject = {
projectType: type,
currDir: newAppLocation,
userName,
useTrial,
mobiscrollVersion: 4, // force v4 installation until v5 version of starters available
proxyUrl,
};
utils.installMobiscroll(configObject, () => {
if (callback) {
callback();
}
});
});
});
})
.catch((err) => {
utils.printError('Could not clone starter project from git.\n\n' + err);
});
}
function askStyleSheetType(version, useScss, config, callback) {
var skipQuestion = false;
var localScss = undefined;
var isIonic = config.projectType === 'ionic' && config.framework !== 'react';
var globalScssPath = path.resolve(config.currDir, 'src', isIonic ? 'global.scss' : 'styles.scss');
var isGlobalScss = fs.existsSync(globalScssPath);
if (useScss === undefined && config.packageJson) {
var isOldIonic =
config.packageJson.dependencies && config.packageJson.dependencies['ionic-angular'] !== undefined; /* ionic version 2/3*/
// ionic && angular cli
if ((config.projectType === 'angular' || config.projectType === 'ionic') && !isOldIonic) {
let checkStyleLoaded = isGlobalScss && fs.readFileSync(globalScssPath, 'utf8').toString();
if (isGlobalScss && checkStyleLoaded && checkStyleLoaded.indexOf('mobiscroll') !== -1) {
skipQuestion = true;
localScss = true;
} else {
skipQuestion = !isGlobalScss; // only ask if the angular project was set up with scss
localScss = false;
if (isIonic && fs.existsSync(path.resolve(config.currDir, 'angular.json'))) {
checkStyleLoaded = fs.readFileSync(path.resolve(config.currDir, 'angular.json'), 'utf8').toString();
skipQuestion = checkStyleLoaded && checkStyleLoaded.indexOf('mobiscroll') !== -1;
localScss = false;
}
}
}
}
if (config.projectType === 'jquery' || config.projectType === 'javascript') {
skipQuestion = true;
}
// only ask the scss install if the version is larger then 4.7.0
if (semver.gte(version, '4.7.0') && useScss === undefined && !skipQuestion) {
let choices = ['CSS', 'SCSS'];
console.log('\n');
inquirer
.prompt({
type: 'list',
name: 'stylesheet',
message: `Which stylesheet format would you like to use?`,
default: isIonic ? 'SCSS' : 'CSS',
choices: isIonic ? choices.reverse() : choices,
})
.then((answer) => {
if (callback) {
callback(answer.stylesheet === 'SCSS');
}
});
} else {
if (callback) {
callback(localScss || useScss);
}
}
}
function startProject(url, type, framework, name, gitOptions, callback) {
printFeedback('Mobiscroll start command started.');
var currDir = process.cwd(), // get the directory where the mobiscroll command was executed
newAppLocation = path.resolve(currDir, name);
if (fs.existsSync(newAppLocation)) {
inquirer
.prompt({
type: 'input',
name: 'confirm',
message: `The directory .\\${chalk.gray(name)} exists. Would you like to overwrite the directory with this new project?(y/N)`,
default: 'N',
})
.then((answer) => {
if (answer.confirm.toLowerCase() == 'y') {
utils.deleteFolder(newAppLocation); // delete the app with the same name
cloneProject(url, type, framework, name, newAppLocation, gitOptions, callback);
} else {
console.log(`Not erasing existing project in .\\${name}`);
return;
}
});
//return;
} else {
cloneProject(url, type, framework, name, newAppLocation, gitOptions, callback);
}
}
function createProject(type, name) {
switch (type) {
case 'angular':
startProject('https://github.com/acidb/angular-starter', type, 'angular', name, {}, () => {
utils.testInstalledCLI('ng -v', 'npm install -g @angular/cli', 'ng serve -o', name, type);
});
break;
case 'ionic-angular':
if (startIoncVesrion && +startIoncVesrion === 4) {
startProject(
'https://github.com/acidb/ionic-angular-starter',
'ionic',
'angular',
name,
{
checkout: 'v4',
},
() => {
utils.testInstalledCLI('ionic -v', 'npm install -g ionic', 'ionic serve', name, type);
}
);
} else {
startProject('https://github.com/acidb/ionic-angular-starter', 'ionic', 'angular', name, {}, () => {
utils.testInstalledCLI('ionic -v', 'npm install -g @ionic/cli', 'ionic serve', name, type);
});
}
break;
case 'ionic-react':
startProject('https://github.com/acidb/ionic-react-starter', 'react', 'react', name, {}, () => {
utils.testInstalledCLI('ionic -v', 'npm install -g @ionic/cli', 'ionic serve', name, 'react');
});
break;
case 'ionic':
startProject('https://github.com/acidb/ionic-starter', type, 'angular', name, {}, () => {
utils.testInstalledCLI('ionic -v', 'npm install -g ionic', 'ionic serve', name, type);
});
break;
case 'react':
startProject('https://github.com/acidb/react-starter', type, 'react', name, {}, () => {
utils.testInstalledCLI('create-react-app --version', 'npm install -g create-react-app', 'npm start', name, type);
});
break;
// case 'vue':
// startProject('', type, name, () => {
// utils.testInstalledCLI('vue -V', 'npm install -g @vue/cli', 'npm run serve', name, type);
// });
// break;
default:
printWarning(
'No valid project type was specified. Currently the following project types are supported: [ angular, ionic, ionic-angular, ionic-react, react ]'
);
break;
}
}
function handleStart(type, name) {
if (!name) {
inquirer
.prompt([
{
type: 'input',
name: 'projectName',
message: 'What would you like to name your project:',
validate: function validateProjectName(name) {
return name !== '';
},
},
])
.then((answer) => {
createProject(type, answer.projectName);
});
} else {
createProject(type, name);
}
}
function handleConfig(projectType) {
if (!projectType) {
printWarning(
'Please specify the project type. [ionic, angular, javascript, jquery, react, vue] \n\nFor more information please run the ' +
chalk.gray('mobiscroll config --help') +
' command.'
);
return;
}
checkUpdate(proxyUrl).then(() => {
var framework = projectType,
jsFileName = `@mobiscroll/angular${isLite ? '-lite' : ''}`,
cssFileName = `../node_modules/@mobiscroll/angular${isLite ? '-lite' : ''}/dist/css/mobiscroll.min.css`,
currDir = process.cwd(), // get the directory where the mobiscroll command was executed
packageJsonLocation = path.resolve(currDir, 'package.json'),
packageJson = '';
// check if package.json is in the current directory
if (!fs.existsSync(packageJsonLocation)) {
printWarning("There is no package.json in this directory.\nPlease run this command in the project's root directory!");
process.exit();
}
if (packageJsonLocation) {
try {
packageJson = require(packageJsonLocation);
} finally {
const dependencies = packageJson.dependencies;
framework =
projectType == 'ionic' ? (dependencies['@ionic/react'] ? 'react' : dependencies['@ionic/vue'] ? 'vue' : 'angular') : projectType;
}
}
printFeedback('Mobiscroll configuration started.');
if (isLite) {
utils.installMobiscrollLite(projectType, mobiscrollVersion, function () {
let configObject = {
projectType,
currDir,
packageJsonLocation,
jsFileName,
cssFileName,
isNpmSource,
apiKey: '',
isLite,
useScss,
};
config(configObject);
});
} else if (isNpmSource) {
utils.checkMbscNpmLogin(isTrial, useGlobalNpmrc, proxyUrl, framework, mobiscrollVersion, (userName, useTrial, data) => {
utils.removeUnusedPackages(projectType, packageJsonLocation, useTrial, false, () => {
let configObject = {
projectType,
currDir,
packageJsonLocation,
packageJson,
jsFileName,
cssFileName,
isNpmSource,
apiKey: useTrial ? data.TrialCode : '',
isLite,
useScss,
userName,
useTrial,
mobiscrollVersion,
proxyUrl,
framework,
legacyPeerFlag,
package: data.Package,
};
// Install mobiscroll npm package
utils.installMobiscroll(configObject, (version) => {
configObject.mobiscrollVersion = version;
askStyleSheetType(version, useScss, configObject, (isScssSelected) => {
configObject.useScss = isScssSelected;
config(configObject);
});
});
});
});
} else {
// if no-npm flag is set
var files,
localCssFileName = [],
localJsFileName = [],
esmBundleAvailable = false,
// framework = projectType == 'ionic' ? ( packageJson.dependencies['@ionic/react'] ? 'react' : 'angular') : projectType,
mbscFolderLocation = path.resolve(currDir, 'src', 'lib', 'mobiscroll'),
jsFileLocation = path.resolve(mbscFolderLocation, 'js'),
esmFileLocation = path.resolve(mbscFolderLocation, 'esm5'),
cssFileLocation = path.resolve(mbscFolderLocation, 'css');
utils.removeUnusedPackages(
projectType,
packageJsonLocation,
false,
false,
() => {
// check if mobiscroll js files are copied to the specific location and get the js file name
if (fs.existsSync(jsFileLocation)) {
files = fs.readdirSync(jsFileLocation);
localJsFileName = files.filter(function (item) {
return item.match(/^mobiscroll\..*\.js$/);
});
}
// check if there's an esm bundle
esmBundleAvailable = fs.existsSync(esmFileLocation);
// check if css files are copied to the specific location and get the css file name
if (fs.existsSync(cssFileLocation)) {
files = fs.readdirSync(cssFileLocation);
localCssFileName = files.filter(function (item) {
return item.match(/^mobiscroll\..*\.css$/);
});
}
if (!localJsFileName.length || !localCssFileName.length) {
printWarning(
`No mobiscroll js/css files were found in your project's src/lib/mobiscroll folder. \n\nPlease make sure to extract the downloaded Mobiscroll package, then grab the ${framework == 'angular'
? 'lib folder and copy it into src folder of your app!'
: 'js and css folders and copy it into src/lib/mobiscroll folder of your app. If there is no such folder available, you can create it.'
}`
);
return;
}
var jsFileContent = fs.readFileSync(path.resolve(jsFileLocation, localJsFileName.toString()).toString()).toString();
let versionResult = /\{version:\s?['"]([a-z0-9.-]+)['"]\}/gim.exec(jsFileContent); // regex for version 5
if (versionResult === null) {
// it's version 4
versionResult = /version:\s?['"]([a-z0-9.-]+)['"]/gim.exec(jsFileContent);
}
const version = versionResult[1];
let packageFolder = path.resolve(currDir, 'src', 'lib', 'mobiscroll-package');
let distFolder = path.resolve(packageFolder, 'dist');
mobiscrollVersion = version;
// create new folders
if (!fs.existsSync(packageFolder)) {
fs.mkdirSync(packageFolder, 0o777);
fs.mkdirSync(distFolder, 0o777);
}
let configObject = {
localCssFileName: localCssFileName[0],
localJsFileName: localJsFileName[0],
projectType,
currDir,
packageJsonLocation,
packageJson,
jsFileName,
isNpmSource,
apiKey: '',
isLite,
useScss,
framework,
mobiscrollVersion,
};
askStyleSheetType(version, useScss, configObject, (isScssSelected) => {
const isAngular = framework == 'angular';
configObject.useScss = isScssSelected;
if (configObject.useScss) {
let scssFileLocation = path.resolve(cssFileLocation, `mobiscroll.${framework}.scss`);
let fileData = fs.readFileSync(scssFileLocation).toString();
if (fileData && isAngular) {
fileData = fileData.replace("$mbsc-font-path: '' !default;", "$mbsc-font-path: '~@mobiscroll/angular/dist/css/' !default;");
utils.writeToFile(scssFileLocation, fileData);
}
}
// copy the mobiscroll resources to another folder for packing
ncp(mbscFolderLocation, path.resolve(distFolder), (err) => {
if (err) {
utils.printError('Could not copy Mobiscroll resources.\n\n' + err);
return;
}
var noNpmPackageJson = '';
try {
noNpmPackageJson = require(path.resolve(__dirname, 'resources', framework, 'pckg.json'));
} catch (err) {
utils.printError('Could not open package.json file.\n\n' + err);
}
console.log(`\n${chalk.green('>')} Mobiscroll resources were copied successfully.`);
// create the package.json file for the
noNpmPackageJson.version = version;
if (noNpmPackageJson.main) {
noNpmPackageJson.main = noNpmPackageJson.main + localJsFileName[0];
}
if (noNpmPackageJson.module) {
noNpmPackageJson.module = noNpmPackageJson.module + localJsFileName[0];
}
if (semver.gte(version, '5.0.0-beta6') && !isAngular && esmBundleAvailable) {
noNpmPackageJson.module = 'dist/esm5/' + localJsFileName[0];
if (noNpmPackageJson.files && noNpmPackageJson.files.length !== undefined) {
noNpmPackageJson.files.push('dist/esm5/**');
}
}
if (!esmBundleAvailable ||
isAngular && packageJson && packageJson.dependencies && utils.getMainAngularVersion(packageJson) < 16) {
delete noNpmPackageJson.exports;
}
if (noNpmPackageJson.types) {
noNpmPackageJson.types = noNpmPackageJson.types + localJsFileName[0].replace('js', 'd.ts');
}
if (noNpmPackageJson.style) {
noNpmPackageJson.style = noNpmPackageJson.style + localCssFileName[0];
}
if (semver.gte(version, '5.0.0-beta') && !isAngular) {
// typing location is different in v5 except angular
noNpmPackageJson.typings = 'dist/src/' + framework + '/bundle.d.ts';
}
let useYarn = utils.testYarn(currDir);
// remove previously installed mobiscroll package (fix npm caching the local package)
utils.run(`${useYarn ? 'yarn remove' : 'npm uninstall'} @mobiscroll/${framework} --save`, true, true, true).then(() => {
utils.printLog('Removing old Mobiscroll version.');
// write the new package.json
utils.writeToFile(path.resolve(packageFolder, 'package.json'), JSON.stringify(noNpmPackageJson, null, 2), () => {
function pack() {
// pack with npm pack
utils.packMobiscroll(packageFolder, currDir, framework, useYarn, mobiscrollVersion, (packageName) => {
var packageJson = '';
try {
packageJson = require(packageJsonLocation);
} catch (err) {
utils.printError('Could not open package.json file.\n\n' + err);
}
if (useYarn) {
// workaround delete yarn cache tmp folder manually. Issue (https://github.com/yarnpkg/yarn/issues/5357)
let yarnCacheLocation = utils.runSync('yarn cache dir');
utils.deleteFolder(path.resolve(yarnCacheLocation.trim(), '.tmp'));
utils.runSync(`yarn cache clean @mobiscroll/${framework}`);
} else {
packageJson.dependencies[`@mobiscroll/${framework}`] = 'file:./src/lib/mobiscroll-package/' + packageName;
}
utils.writeToFile(packageJsonLocation, JSON.stringify(packageJson, null, 4), () => {
console.log(
`${chalk.green('>') + chalk.grey(' package.json')
} modified to load mobiscroll from the generated package file. \n`
);
// run npm install
const npmInstallCmd = 'npm install' + (legacyPeerFlag ? ' --legacy-peer-deps' : '');
utils.run(useYarn ? 'yarn add file:./src/lib/mobiscroll-package/' + packageName : npmInstallCmd, true).then(() => {
cssFileName =
(projectType == 'ionic'
? packageJson.dependencies['@ionic/angular']
? `./node_modules/@mobiscroll/${framework}/dist/css/`
: 'lib/mobiscroll/css/'
: `../node_modules/@mobiscroll/${framework}/dist/css/`) + localCssFileName;
configObject.cssFileName = cssFileName;
//config(projectType, currDir, packageJsonLocation, jsFileName, cssFileName, isNpmSource, false, false, () => {
config(configObject, () => {
console.log(`\n${chalk.green('>')} Removing unused mobiscroll files.`);
fs.unlinkSync(path.resolve(packageFolder, 'package.json')); // delete the package.json in dist
utils.deleteFolder(mbscFolderLocation); // delete source folder
utils.deleteFolder(distFolder); // delete created dist
});
});
});
});
}
if (esmBundleAvailable) {
utils.writeToFile(path.resolve(packageFolder, 'dist/esm5/package.json'), '{ "type": "module" }', pack);
} else {
pack();
}
});
});
});
});
},
true
);
}
});
}
function handleLogin() {
utils.login(useGlobalNpmrc, proxyUrl);
}
function handleLogout() {
removeTokenFromNpmrc(path.resolve(useGlobalNpmrc ? os.homedir() : process.cwd()));
}
// options
program
.version(localCliVersion, '-v')
.option(
'-g, --global',
"Modify the Mobiscroll npm login credentials save/use location to the global .npmrc file. Starting from cli v0.6.0 by default it is saved in the application's directory.",
handleGlobalInstall
)
.usage('[commands] [options]');
// commands
// config command
program
.command('config [types]')
.description(
`Configures your current project with the Mobiscroll resources and dependencies. For more information run the ${chalk.gray(
'mobiscroll config --help'
)} command.\n`
)
.action(handleConfig)
.on('--help', helperMessages.configHelp)
//.option('-l, --lazy', 'Skipping MbscModule injection from app.module.ts in case of Ionic lazy loading project.\n', handleLazy)
.option('-t, --trial', 'The project will be tuned up with trial configuration.\n', handleTrial)
.option('-i, --lite', 'The project will be tuned up with lite configuration.\n', handleLite)
.option(
'-n, --no-npm',
"Mobiscroll resources won't be installed from npm. In this case the Mobiscroll resources must be copied manually to the src/lib folder.\n",
handleNpmInstall
)
.option(
'--version [version]',
`Pass the Mobiscroll version which you want to install. Supports the following formats: ${chalk.gray(
'--version=4.7.1'
)} or ${chalk.gray('--version=5')}\n`,
handleMobiscrollVersion
)
.option(
'--proxy [proxy]',
`Pass a proxy URL which will be passed to the internal requests. Supports the following formats: ${chalk.gray(
'{protocol}://{host}:{port} example: --proxy=http://myproxy.com:1122'
)} or with auth details ${chalk.gray(
'{protocol}://{user}:{password}@{host}:{port} example: --proxy=http://myuser:[email protected]:1122'
)}`,
handleProxy
)
.option('--scss', 'The project will be configured with scss styles instead of css.', handleScss)
.option('--css', 'The project will be configured with css styles instead of scss.', handleCss)
.option(
'--legacy-peer-deps',
`Pass the ${chalk.gray('--legacy-peer-deps')} flag to internally executed npm install command.`,
handleLegacyPeerFlags
);
program
.command('login')
.description('Logs you in to the Mobiscroll npm registry. (Use the --global flag if you want to login globally). \n')
.action(handleLogin);
program
.command('logout')
.description('Logs you out from the Mobiscroll npm registry. (Use the --global flag if you want to log out globally).\n')
.action(handleLogout);
program
.command('start [types] [name]')
.on('--help', helperMessages.startHelp)
.option('-t, --trial', 'The project will be tuned up with trial configuration.\n', handleTrial)
.option(
'--ionic-version [version]',
`Pass the ionic-angular version of the starter app which you want to start. E.g ${chalk.gray(
'mobiscroll start ionic-angular --ionic-version=4'
)}`,
handleStartIonicVersion
)
.description(`Creates a new Mobiscroll starter project and installs the Mobiscroll resources from npm.`)
.action(handleStart);
program.on('command:*', function () {
// warning on unknown commands
printWarning(`Invalid command: ${chalk.green(program.args.join(' '))}`);
console.error('\nSee --help for a list of available commands.');
process.exit(1);
});
program.parse(process.argv);
// print help if no commands or options was passed
if (!program.args.length) {
console.log(figlet.textSync('Mobiscroll CLI'));
console.log(' Version: ' + localCliVersion);
if (program.rawArgs.indexOf('-v') == -1) {
program.help();
}
}