-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
888 lines (785 loc) · 33.3 KB
/
utils.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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
const path = require("path");
const { exec, execSync } = require("child_process");
const fs = require("fs");
const colors = require("colors");
const tail = require("tail");
const mysql = require("mysql2/promise");
const inquirer = require("inquirer");
const spinners = require("cli-spinners");
colors.enable();
require("dotenv").config({ path: path.resolve(process.cwd(), ".env") });
const {
LTD,
logsDirectory,
siteDirectory,
LOCAL_WOO_PATH,
LOCAL_DUMMY_PATH,
} = require("./constants.js");
const { selectPHP } = require("./php-controller.js");
const { reloadNginx } = require("./nginx.js");
// ======================================
// Generic helpers.
// ======================================
/**
* Wraps a callback with a loader.
*
* @param {string} text
* @param {function} callback
* @returns {Promise}
*/
async function wrapWithLoader(text, callback) {
const spinner = {
frames: spinners.dots4.frames,
interval: spinners.dots4.interval,
text,
frame: 0,
};
// Create a new spinner instance and start it
process.stdout.write(`\n`);
const loader = setInterval(() => {
process.stdout.write(
`\r${spinner.text} ${spinner.frames[spinner.frame]}`.cyan
);
spinner.frame = (spinner.frame + 1) % spinner.frames.length;
}, spinner.interval);
const response = await callback();
// Stop the loader and clear the line
clearInterval(loader);
process.stdout.write("\r\x1b[K");
return response;
}
exports.wrapWithLoader = wrapWithLoader;
/**
* Fetches the projects from the .woa_projects.json file.
*
* @returns {array}
*/
function fetchProjects() {
const PROJECTS_CONFIG_FILE_NAME = ".woa_projects.json";
const PROJECTS_WOA_DEV_FILE = path.join(
process.env.HOME,
PROJECTS_CONFIG_FILE_NAME
);
const tasks = [];
if (fs.existsSync(PROJECTS_WOA_DEV_FILE)) {
try {
const projects = JSON.parse(
fs.readFileSync(PROJECTS_WOA_DEV_FILE, "utf8")
);
if (!projects || !projects.length) {
console.error(
`- Projects are empty in ${PROJECTS_WOA_DEV_FILE} file.`.grey
);
}
projects.forEach((project) => {
tasks.push(project);
});
} catch (error) {
console.error(
`There is an error in your ${PROJECTS_WOA_DEV_FILE} file. Please ensure that no trailing commas exist in the configuration.`
.red
);
console.error(error);
process.exit(1);
}
}
return tasks;
}
exports.fetchProjects = fetchProjects;
/**
* Writes an entry to the hosts file.
*
* @param {string} siteName
*
* @returns boolean
*/
function writeHostsFile(siteName) {
// Add the site to /etc/hosts if it doesn't exist
const hostsFilePath = "/etc/hosts";
const hostsFile = fs.readFileSync(hostsFilePath, "utf-8");
// Define the block pattern
const blockStartPattern = `# Lamma: ${siteName} START`;
const blockEndPattern = `# Lamma: ${siteName} END`;
// Check if the block already exists
if (
!hostsFile.includes(`${blockStartPattern}\n`) ||
!hostsFile.includes(`\n${blockEndPattern}`)
) {
const tempHostsFile = path.join(__dirname, "temp_hosts");
// If the block doesn't exist, add the updated format
const siteHostsBlock = `# Lamma: ${siteName} START\n::1 ${siteName}${LTD}\n127.0.0.1 ${siteName}${LTD}\n# Lamma: ${siteName} END\n`;
fs.writeFileSync(tempHostsFile, `${hostsFile}\n${siteHostsBlock}`);
execSync(`sudo sh -c 'cat "${tempHostsFile}" > "${hostsFilePath}"'`);
fs.unlinkSync(tempHostsFile);
console.log(`- Added ${siteName}${LTD} to /etc/hosts`.grey);
return true;
} else {
console.log(
`- ${siteName}${LTD} is already included in the hosts file`.grey
);
return false;
}
}
exports.writeHostsFile = writeHostsFile;
async function handleWatchLog(siteName) {
// Create the log file.
if (!fs.existsSync(`${logsDirectory}/${siteName}.php.log`)) {
console.log(`Creating log file for ${siteName}...`.blue);
fs.writeFileSync(`${logsDirectory}/${siteName}.php.log`, "");
}
console.log(`Watching log file for ${siteName}...`.blue);
const tailStream = new tail.Tail(
path.join(logsDirectory, siteName + ".php.log"),
{ fromBeginning: false, nLines: 100 }
);
tailStream.on("line", (line) => {
const match = line.match(/^\[(.*)\] (.*)/);
if (match) {
const timestamp = colors.gray(match[1]);
const message = match[2].replace(/(\r\n|\n|\r)/gm, "");
console.log(`${timestamp.cyan}` + ` ${message.white}`);
} else {
console.log(line.white);
}
});
}
exports.handleWatchLog = handleWatchLog;
/**
* Creates a new database for the given site name.
*
* @param {string} siteName
*
* @returns void
*/
async function createDatabase(siteName) {
const connection = await mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: "",
});
try {
await connection.query(`CREATE DATABASE IF NOT EXISTS ${siteName}`);
console.log(`- Database '${siteName}' created (or already exists)`.grey);
} catch (error) {
console.error(`Error creating database: ${error.stack}`.red);
} finally {
await connection.end();
}
}
exports.createDatabase = createDatabase;
// Generic helper.
function createSymlinks(sitePath, symlinks = []) {
symlinks = symlinks.length ? symlinks : fetchDefaultSymlinks(sitePath);
if (!symlinks.length) {
console.log(`No default symlinks found. Moving on...`.grey);
}
try {
symlinks.forEach((symlink) => {
fs.symlinkSync(symlink.source, symlink.target, "dir");
console.log(
`- Symlink from "${symlink.source}" to "${symlink.target}"`.grey
);
});
} catch (error) {
console.error(`Error creating symlinks: ${error.stack}`.red);
}
}
exports.createSymlinks = createSymlinks;
// ======================================
// WordPress helpers.
// ======================================
/**
* Installs WP CLI If it doesn't exists.
*
* @returns void
*/
function installWPCLI() {
try {
const wpPath = execSync("which wp").toString().trim();
if (wpPath) {
console.log(`- WP-CLI is already installed at '${wpPath}'`.grey);
return;
}
execSync(
"curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod +x wp-cli.phar && sudo mv wp-cli.phar /usr/local/bin/wp"
);
console.log("- WP-CLI installed successfully".grey);
} catch (error) {
console.error(`Error installing WP-CLI: ${error.stack}`.red);
}
}
exports.installWPCLI = installWPCLI;
/**
* Runs a WP command.
*
* @param {string} command
* @param {object} options - Options to pass to the exec function.
*
* @returns {Promise}
*/
function runWPCommand(command, options = {}) {
return new Promise((resolve, reject) => {
const { cwd = process.cwd(), ...otherOptions } = options;
// Check if the command already starts with 'wp '.
const wpCommand = command.startsWith("wp ") ? command : `wp ${command}`;
const execOptions = { cwd, ...otherOptions };
exec(wpCommand, execOptions, (error, stdout, stderr) => {
if (error) {
console.log(`Error running WP command: ${error.stack}`.red);
reject(error);
} else {
resolve(stdout);
}
});
});
}
exports.runWPCommand = runWPCommand;
// Generic helper.
function fetchDefaultSymlinks(sitePath) {
if (undefined === LOCAL_WOO_PATH || !LOCAL_WOO_PATH) {
return [];
}
const symlinks = [
{
source: LOCAL_WOO_PATH,
target: `${sitePath}/wp-content/plugins/woocommerce`,
},
{
source: LOCAL_DUMMY_PATH,
target: `${sitePath}/wp-content/plugins/woocommerce-gateway-dummy`,
},
];
return symlinks;
}
exports.fetchDefaultSymlinks = fetchDefaultSymlinks;
function copyFilesWithExclusions(srcDir, destDir, exclusions) {
const files = fs.readdirSync(srcDir);
for (const file of files) {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
// Check if the current file matches any exclusion pattern
const shouldExclude = exclusions.some((exclude) => {
return file === exclude || srcPath.includes(path.join(exclude, "/"));
});
if (!shouldExclude) {
if (fs.statSync(srcPath).isDirectory()) {
// Recursively copy directories
fs.mkdirSync(destPath, { recursive: true });
copyFilesWithExclusions(srcPath, destPath, exclusions);
} else {
// Copy files
fs.copyFileSync(srcPath, destPath);
}
}
}
}
/**
* Returns an object with two arrays of commands to run before and after WordPress installation.
*
* @param {string} title
* @param {string} siteName
*
* @returns {Object}
*/
function fetchProvisionCommands(title, siteName) {
const installCommands = [
`core download --locale=en_US --force`,
`core config --dbname=${siteName} --dbuser=${process.env.DB_USER} --dbpass=${process.env.DB_PASSWORD} --dbhost=${process.env.DB_HOST} --dbprefix=${process.env.DB_PREFIX} --skip-check`,
`core install --url=${siteName}${LTD} --title="${
title ? title : "My Awesome Site"
}" --admin_user=${process.env.WP_ADMIN_USER} --admin_password=${
process.env.WP_ADMIN_PASSWORD
} --admin_email=${process.env.WP_ADMIN_EMAIL} --skip-email`,
`option update siteurl ${siteName}${LTD}`,
`rewrite structure '/%postname%/'`,
];
const postInstallCommands = [
`plugin activate woocommerce`,
`plugin activate woocommerce-gateway-dummy`,
`wc customer update 1 --user=1 --billing='{"first_name":"John","last_name":"Doe","company":"Automattic","country":"US","address_1":"addr 1","address_2":"addr 2","city":"San Francisco","state":"CA","postcode":"94107","phone":"123456789"}' --shipping='{"first_name":"John","last_name":"Doe","company":"Automattic","country":"US","address_1":"addr 1","address_2":"addr 2","city":"San Francisco","state":"CA","postcode":"94107","phone":"123456789"}'`,
`option update woocommerce_store_address '60 29th Street #343'`,
`option update woocommerce_store_city 'San Francisco'`,
`option update woocommerce_store_postcode 94110`,
`option update woocommerce_default_country 'US:CA'`,
`option update woocommerce_default_customer_address 'geolocation'`,
`option update woocommerce_currency 'USD'`,
`option update woocommerce_currency_pos 'left'`,
`option update woocommerce_onboarding_profile --format=json '{"skipped":true}'`,
`wp option set --format=json woocommerce_dummy_settings '{"enabled":"yes","title":"Dummy Payment","description":"The goods are yours. No money needed.","result":"success"}'`,
`post create --post_type=page --post_status=publish --post_title='Blocks cart' --post_name='blocks-cart' --post_content='<!-- wp:woocommerce/cart {"align":"wide"} --><div class="wp-block-woocommerce-cart alignwide is-loading"><!-- wp:woocommerce/filled-cart-block --><div class="wp-block-woocommerce-filled-cart-block"><!-- wp:woocommerce/cart-items-block --><div class="wp-block-woocommerce-cart-items-block"><!-- wp:woocommerce/cart-line-items-block --><div class="wp-block-woocommerce-cart-line-items-block"></div><!-- /wp:woocommerce/cart-line-items-block --></div><!-- /wp:woocommerce/cart-items-block --><!-- wp:woocommerce/cart-totals-block --><div class="wp-block-woocommerce-cart-totals-block"><!-- wp:woocommerce/cart-order-summary-block --><div class="wp-block-woocommerce-cart-order-summary-block"></div><!-- /wp:woocommerce/cart-order-summary-block --><!-- wp:woocommerce/cart-express-payment-block --><div class="wp-block-woocommerce-cart-express-payment-block"></div><!-- /wp:woocommerce/cart-express-payment-block --><!-- wp:woocommerce/proceed-to-checkout-block --><div class="wp-block-woocommerce-proceed-to-checkout-block"></div><!-- /wp:woocommerce/proceed-to-checkout-block --><!-- wp:woocommerce/cart-accepted-payment-methods-block --><div class="wp-block-woocommerce-cart-accepted-payment-methods-block"></div><!-- /wp:woocommerce/cart-accepted-payment-methods-block --></div><!-- /wp:woocommerce/cart-totals-block --></div><!-- /wp:woocommerce/filled-cart-block --><!-- wp:woocommerce/empty-cart-block --><div class="wp-block-woocommerce-empty-cart-block"><!-- wp:image {"align":"center","sizeSlug":"small"} --><div class="wp-block-image"><figure class="aligncenter size-small"><img src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzgiIGhlaWdodD0iMzgiIHZpZXdCb3g9IjAgMCAzOCAzOCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTE5IDBDOC41MDQwMyAwIDAgOC41MDQwMyAwIDE5QzAgMjkuNDk2IDguNTA0MDMgMzggMTkgMzhDMjkuNDk2IDM4IDM4IDI5LjQ5NiAzOCAxOUMzOCA4LjUwNDAzIDI5LjQ5NiAwIDE5IDBaTTI1LjEyOSAxMi44NzFDMjYuNDg1MSAxMi44NzEgMjcuNTgwNiAxMy45NjY1IDI3LjU4MDYgMTUuMzIyNkMyNy41ODA2IDE2LjY3ODYgMjYuNDg1MSAxNy43NzQyIDI1LjEyOSAxNy43NzQyQzIzLjc3MyAxNy43NzQyIDIyLjY3NzQgMTYuNjc4NiAyMi42Nzc0IDE1LjMyMjZDMjIuNjc3NCAxMy45NjY1IDIzLjc3MyAxMi44NzEgMjUuMTI5IDEyLjg3MVpNMTEuNjQ1MiAzMS4yNTgxQzkuNjE0OTIgMzEuMjU4MSA3Ljk2Nzc0IDI5LjY0OTIgNy45Njc3NCAyNy42NTczQzcuOTY3NzQgMjYuMTI1IDEwLjE1MTIgMjMuMDI5OCAxMS4xNTQ4IDIxLjY5NjhDMTEuNCAyMS4zNjczIDExLjg5MDMgMjEuMzY3MyAxMi4xMzU1IDIxLjY5NjhDMTMuMTM5MSAyMy4wMjk4IDE1LjMyMjYgMjYuMTI1IDE1LjMyMjYgMjcuNjU3M0MxNS4zMjI2IDI5LjY0OTIgMTMuNjc1NCAzMS4yNTgxIDExLjY0NTIgMzEuMjU4MVpNMTIuODcxIDE3Ljc3NDJDMTEuNTE0OSAxNy43NzQyIDEwLjQxOTQgMTYuNjc4NiAxMC40MTk0IDE1LjMyMjZDMTAuNDE5NCAxMy45NjY1IDExLjUxNDkgMTIuODcxIDEyLjg3MSAxMi44NzFDMTQuMjI3IDEyLjg3MSAxNS4zMjI2IDEzLjk2NjUgMTUuMzIyNiAxNS4zMjI2QzE1LjMyMjYgMTYuNjc4NiAxNC4yMjcgMTcuNzc0MiAxMi44NzEgMTcuNzc0MlpNMjUuOTEwNSAyOS41ODc5QzI0LjE5NDQgMjcuNTM0NyAyMS42NzM4IDI2LjM1NDggMTkgMjYuMzU0OEMxNy4zNzU4IDI2LjM1NDggMTcuMzc1OCAyMy45MDMyIDE5IDIzLjkwMzJDMjIuNDAxNiAyMy45MDMyIDI1LjYxMTcgMjUuNDA0OCAyNy43ODc1IDI4LjAyNUMyOC44NDQ4IDI5LjI4MTUgMjYuOTI5NCAzMC44MjE0IDI1LjkxMDUgMjkuNTg3OVoiIGZpbGw9ImJsYWNrIi8+Cjwvc3ZnPgo=" alt=""/></figure></div><!-- /wp:image --><!-- wp:heading {"textAlign":"center","className":"wc-block-cart__empty-cart__title"} --><h2 class="has-text-align-center wc-block-cart__empty-cart__title">Your cart is currently empty!</h2><!-- /wp:heading --><!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center"><a href="http://localhost:8889/shop/">Browse store</a>.</p><!-- /wp:paragraph --><!-- wp:separator {"className":"is-style-dots"} --><hr class="wp-block-separator is-style-dots"/><!-- /wp:separator --><!-- wp:heading {"textAlign":"center"} --><h2 class="has-text-align-center">New in store</h2><!-- /wp:heading --><!-- wp:woocommerce/product-new {"rows":1} /--></div><!-- /wp:woocommerce/empty-cart-block --></div><!-- /wp:woocommerce/cart -->'`,
`post create --post_type=page --post_status=publish --post_title='Blocks checkout' --post_name='blocks-checkout' --post_content='<!-- wp:woocommerce/checkout {"align":"wide"} --><div class="wp-block-woocommerce-checkout wc-block-checkout alignwide is-loading"><!-- wp:woocommerce/checkout-fields-block --><div class="wp-block-woocommerce-checkout-fields-block"><!-- wp:woocommerce/checkout-express-payment-block --><div class="wp-block-woocommerce-checkout-express-payment-block"></div><!-- /wp:woocommerce/checkout-express-payment-block --><!-- wp:woocommerce/checkout-contact-information-block --><div class="wp-block-woocommerce-checkout-contact-information-block"></div><!-- /wp:woocommerce/checkout-contact-information-block --><!-- wp:woocommerce/checkout-shipping-address-block --><div class="wp-block-woocommerce-checkout-shipping-address-block"></div><!-- /wp:woocommerce/checkout-shipping-address-block --><!-- wp:woocommerce/checkout-billing-address-block --><div class="wp-block-woocommerce-checkout-billing-address-block"></div><!-- /wp:woocommerce/checkout-billing-address-block --><!-- wp:woocommerce/checkout-shipping-methods-block --><div class="wp-block-woocommerce-checkout-shipping-methods-block"></div><!-- /wp:woocommerce/checkout-shipping-methods-block --><!-- wp:woocommerce/checkout-payment-block --><div class="wp-block-woocommerce-checkout-payment-block"></div><!-- /wp:woocommerce/checkout-payment-block --><!-- wp:woocommerce/checkout-order-note-block --><div class="wp-block-woocommerce-checkout-order-note-block"></div><!-- /wp:woocommerce/checkout-order-note-block --><!-- wp:woocommerce/checkout-terms-block --><div class="wp-block-woocommerce-checkout-terms-block"></div><!-- /wp:woocommerce/checkout-terms-block --><!-- wp:woocommerce/checkout-actions-block --><div class="wp-block-woocommerce-checkout-actions-block"></div><!-- /wp:woocommerce/checkout-actions-block --></div><!-- /wp:woocommerce/checkout-fields-block --><!-- wp:woocommerce/checkout-totals-block --><div class="wp-block-woocommerce-checkout-totals-block"><!-- wp:woocommerce/checkout-order-summary-block --><div class="wp-block-woocommerce-checkout-order-summary-block"></div><!-- /wp:woocommerce/checkout-order-summary-block --></div><!-- /wp:woocommerce/checkout-totals-block --></div><!-- /wp:woocommerce/checkout -->'`,
];
return {
install: installCommands,
postinstall: postInstallCommands,
};
}
exports.fetchProvisionCommands = fetchProvisionCommands;
/**
* Helper function to transform a value to a PHP value.
*
* @param {string|number|boolean} value
*
* @returns {string}
*/
function transformToPHPValue(value) {
if (typeof value === "string") {
return `'${value}'`;
} else if (typeof value === "boolean") {
return value ? "true" : "false";
} else if (typeof value === "number") {
return value.toString();
} else {
throw new Error(`Unsupported constant value type: ${typeof value}`);
}
}
exports.transformToPHPValue = transformToPHPValue;
/**
* Adds a constant to the wp-config.php file.
*
* @param {string} sitePath
* @param {string} constantName
* @param {string|number|boolean} constantValue
*/
function addConstantToConfig(sitePath, constantName, constantValue) {
const configFile = `${sitePath}/wp-config.php`;
try {
const configContents = fs.readFileSync(configFile, "utf8");
if (configContents.includes(constantName)) {
console.log(
`${constantName} constant already exists in ${configFile}.`.grey
);
} else {
const phpValue = transformToPHPValue(constantValue);
const constantDefinition = `define('${constantName}', ${phpValue});\n`;
const newConfigContents = configContents.replace(
/\n\/\*\s*That's all, stop editing! Happy publishing\.\s*\*\/\n/,
`${constantDefinition}$&`
);
fs.writeFileSync(configFile, newConfigContents, "utf8");
console.log(`- ${constantName} constant added to ${configFile}.`.grey);
}
} catch (error) {
console.error(
`Error adding ${constantName} constant to ${configFile}: ${error.stack}`
.red
);
}
}
exports.addConstantToConfig = addConstantToConfig;
async function activateTheme(sitePath, theme) {
try {
const projects = fetchProjects();
// If theme argument is empty or not set, create a radio list from all the project names that contain the "wp-content/themes" in the remoteDir
if (!theme) {
const themeProjects = projects.filter((project) =>
project.remoteDir.includes("wp-content/themes")
);
const choices = themeProjects.map((project) => ({
name: project.name,
value: project.value,
}));
choices.push(new inquirer.Separator());
choices.push({ name: "Custom", value: "custom" });
const answers = await inquirer.prompt([
{
type: "list",
name: "theme",
message: "Select a theme to activate:",
choices: choices,
},
]);
theme = answers.theme;
}
// If the user chose "Custom", prompt for the theme name
if (theme === "custom") {
const answer = await inquirer.prompt([
{
type: "input",
name: "themeName",
message: "Enter the name of the custom theme to activate:",
},
]);
theme = answer.themeName;
}
// Check if the theme is present in any of the projects and has a remoteDir under 'wp-content/themes'
const filteredProjects = projects.filter(
(project) =>
project.value === theme &&
project.remoteDir.includes("wp-content/themes")
);
if (filteredProjects.length === 1) {
// project found and matches the conditions
const project = filteredProjects[0];
// Find the index where "wp-content/" starts
const wpContentIndex = project.remoteDir.indexOf("wp-content/");
const symlinkTargetRel = project.remoteDir.substring(wpContentIndex);
const symlinkTarget = path
.join(sitePath, symlinkTargetRel)
.replace(/\/+$/, "");
try {
// Create the symlink
try {
fs.symlinkSync(project.localDir, symlinkTarget, "dir");
console.log(
`- Created symlink from '${project.localDir}' to '${symlinkTarget}'...`
.grey
);
} catch (error) {
console.log(`- ${error}`.grey);
}
await runWPCommand(`theme activate ${theme}`, { cwd: sitePath });
console.log(`- Activating theme '${theme}'...`.grey);
} catch (error) {
console.error(`Error activating theme ${theme}: ${error.message}`);
}
} else {
// Install and activate the theme
console.log(`- Installing and activating theme '${theme}'...`.grey);
const installCommand = `theme install ${theme} --activate`;
// execSync(installCommand, { cwd: sitePath, stdio: "inherit" });
await runWPCommand(installCommand, { cwd: sitePath });
}
console.log("Done.");
} catch (error) {
console.error(`Error activating theme: ${error.stack}`.red);
}
}
exports.activateTheme = activateTheme;
async function unmanagePlugins(siteName) {
const projects = fetchProjects();
const symlinkedPlugins = await getManagedPlugins(siteName);
const sitePath = `${siteDirectory}/${siteName}`;
const pluginsDir = `${sitePath}/wp-content/plugins`;
if (symlinkedPlugins.length === 0) {
console.log("No symlinked plugins found in wp-content/plugins.");
return;
}
// Prompt the user to select plugins
const selectedPlugins = await inquirer.prompt([
{
type: "checkbox",
message: "Select plugins to remove symlinks and copy files",
name: "selected",
choices: symlinkedPlugins.map((plugin) => {
const found = projects.filter((project) => {
return project.value === plugin.name;
});
if (!found.length) {
return false;
}
return {
name: found[0].name,
value: plugin.name,
};
}),
},
]);
// Process selected plugins
for (const pluginName of selectedPlugins.selected) {
const pluginPath = `${pluginsDir}/${pluginName}`;
// Find the project that matches the plugin name.
const filteredProjects = projects.filter(
(project) => project.value === pluginName
);
if (filteredProjects.length !== 1) {
console.error(`Error finding project for ${pluginName}`);
continue;
}
const project = filteredProjects[0];
// Remove symlink
try {
fs.unlinkSync(pluginPath);
console.log(`Removed symlink for ${pluginName}`);
} catch (err) {
console.error(`Error removing symlink for ${pluginName}: ${err.message}`);
}
// Copy files excluding specified directories
try {
const excludeArgs = [".git", ".cache", "node_modules/", "tests/"];
copyFilesWithExclusions(project.localDir, pluginPath, excludeArgs);
console.log(`Copied files for ${pluginName}`);
} catch (err) {
console.error(`Error copying files for ${pluginName}: ${err.message}`);
}
}
}
exports.unmanagePlugins = unmanagePlugins;
async function getManagedPlugins(siteName) {
const sitePath = `${siteDirectory}/${siteName}`;
try {
const pluginsDir = `${sitePath}/wp-content/plugins`;
const installedPlugins = await runWPCommand(
"wp plugin list --format=json",
{ cwd: sitePath }
);
const plugins = JSON.parse(installedPlugins);
// Filter only symlinks in wp-content/plugins
const symlinkedPlugins = plugins.filter((plugin) => {
const pluginPath = path.join(pluginsDir, plugin.name);
return (
fs.existsSync(pluginPath) && fs.lstatSync(pluginPath).isSymbolicLink()
);
});
return symlinkedPlugins;
} catch (err) {
console.error("Error getting list of installed plugins:", err);
return [];
}
}
async function activatePlugins(sitePath, plugins) {
try {
const projects = fetchProjects();
let pluginChoices = [];
// If plugins array argument is empty, create a multichoice list from all the plugin names that contain the "wp-content/plugins" in the remoteDir
if (!plugins || plugins.length === 0) {
const pluginProjects = projects.filter((project) =>
project.remoteDir.includes("wp-content/plugins")
);
pluginChoices = pluginProjects.map((project) => ({
name: project.name,
value: project.value,
}));
}
// Add a custom plugin option to the list of choices
if (pluginChoices.length) {
pluginChoices.push(new inquirer.Separator());
}
pluginChoices.push({
name: "Enter custom plugin name(s)",
value: "custom",
});
// Prompt user to select plugins to activate
const answers = await inquirer.prompt([
{
type: "checkbox",
name: "plugins",
message: "Select plugins to activate:",
choices: pluginChoices,
},
]);
// If user selected custom plugin name(s), prompt them to enter the plugin name(s) as a comma-separated list and add them to the plugins array
if (answers.plugins.includes("custom")) {
const customPlugins = await inquirer.prompt([
{
type: "input",
name: "plugins",
message: "Enter custom plugin name(s), separated by commas:",
},
]);
plugins = [
...plugins,
...answers.plugins.filter((plugin) => plugin !== "custom"),
...customPlugins.plugins.split(",").map((plugin) => plugin.trim()),
];
} else {
plugins = [...plugins, ...answers.plugins];
}
// Remove duplicate plugins from the plugins array
plugins = [...new Set(plugins)];
// Activate or install and activate the plugins based on the conditions
for (const plugin of plugins) {
const filteredProjects = projects.filter(
(project) =>
project.value === plugin &&
project.remoteDir.includes("wp-content/plugins")
);
if (filteredProjects.length === 1) {
// project found and matches the conditions
const project = filteredProjects[0];
// Find the index where "wp-content/" starts
const wpContentIndex = project.remoteDir.indexOf("wp-content/");
const symlinkTargetRel = project.remoteDir.substring(wpContentIndex);
const symlinkTarget = path
.join(sitePath, symlinkTargetRel)
.replace(/\/+$/, "");
try {
if (plugin !== "woocommerce") {
try {
fs.symlinkSync(project.localDir, symlinkTarget, "dir");
console.log(
`- Created symlink from '${project.localDir}' to '${symlinkTarget}'...`
.grey
);
} catch (error) {
console.log(`- ${error}`.grey);
}
}
await runWPCommand(`plugin activate ${plugin}`, { cwd: sitePath });
console.log(`- Activating plugin '${plugin}'...`.grey);
} catch (error) {
console.error(`Error activating plugin ${plugin}: ${error.message}`);
}
} else {
// Install and activate the plugin
console.log(`- Installing and activating plugin '${plugin}'...`.grey);
const installCommand = `plugin install ${plugin} --activate`;
await runWPCommand(installCommand, { cwd: sitePath });
}
}
console.log("Done.");
return plugins;
} catch (error) {
console.error(`Error activating plugins: ${error.stack}`.red);
}
return [];
}
exports.activatePlugins = activatePlugins;
async function runProvision(title, siteName, sitePath, theme, plugins) {
const pluginsArray =
plugins && plugins.length ? plugins.shift().split(",") : [];
process.chdir(sitePath);
try {
console.log(`Making the site accessible`.blue);
writeHostsFile(siteName);
addWordpressHtaccess(sitePath);
console.log(`Select PHP`.blue);
await selectPHP(siteName);
const commands = fetchProvisionCommands(title, siteName);
console.log(`Installing WordPress`.blue);
await createDatabase(siteName);
// Run the commands to install WordPress.
for (var i = 0; i < commands.install.length; i++) {
console.log(
`- Running command ${commands.install[i].substring(0, 150)}[..]`.grey
);
await runWPCommand(`${commands.install[i]}`, { cwd: sitePath });
}
console.log(`Creating symlinks`.blue);
createSymlinks(sitePath);
// Run the commands to postinstall WordPress.
console.log(`Configuring WordPress`.blue);
for (i = 0; i < commands.postinstall.length; i++) {
console.log(
`- Running command ${commands.postinstall[i].substring(0, 150)}[..]`
.grey
);
await runWPCommand(`${commands.postinstall[i]}`, { cwd: sitePath });
}
addConstantToConfig(sitePath, "JETPACK_AUTOLOAD_DEV", true);
// Setup theme and plugins.
console.log(`Setup plugins`.blue);
const pluginsChosen = await activatePlugins(sitePath, pluginsArray);
if (
pluginsChosen.includes("all-in-one-wp-migration") &&
pluginsChosen.includes("all-in-one-wp-migration-unlimited-extension")
) {
await restoreWordPressSite(siteName);
// We need to re-activate plugins after restoring.
console.log(`Re-Activating plugins`.blue);
for (const plugin of pluginsChosen) {
await runWPCommand(`plugin activate ${plugin}`, { cwd: sitePath });
console.log(`- Activating plugin '${plugin}'...`.grey);
}
}
console.log(`Setup theme`.blue);
await activateTheme(sitePath, theme);
// Clear the cache.
await runWPCommand(`rewrite flush`, { cwd: sitePath });
await reloadNginx();
} catch (error) {
console.error(`Error running script: ${error.stack}`.red);
}
}
exports.runProvision = runProvision;
/**
* Creates the htaccess file for a wordpress site.
*
* @param {string} siteName
*
* @returns boolean
*/
function addWordpressHtaccess(sitePath) {
console.log("Adding .htaccess file".blue);
try {
const htaccessContent = `# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
`;
const htaccessFilePath = path.join(sitePath, ".htaccess");
fs.writeFileSync(htaccessFilePath, htaccessContent);
console.log(`- Created ${htaccessFilePath}`.grey);
return true;
} catch (error) {
console.error(`Error creating .htaccess file: ${error.stack}`.red);
return false;
}
}
exports.addWordpressHtaccess = addWordpressHtaccess;
async function restoreWordPressSite(siteName) {
try {
const shouldUseWpress = await inquirer.prompt({
type: "confirm",
name: "useWpress",
message: "Do you want to use a wpress file for restoration?",
});
if (!shouldUseWpress.useWpress) {
console.log("Skipping WordPress restoration.");
return;
}
let wpressFilePath = "";
while (true) {
const wpressPrompt = await inquirer.prompt({
type: "input",
name: "filePath",
message: "Enter the file path containing the wpress file:",
});
wpressFilePath = wpressPrompt.filePath;
if (fs.existsSync(wpressFilePath)) {
break;
} else {
console.error(
"The specified wpress file does not exist. Please provide a valid file path."
);
}
}
const sitePath = `${siteDirectory}/${siteName}`;
const destinationPath = `${sitePath}/wp-content/ai1wm-backups/`;
const backupFileName = "backup.wpress";
const destinationFile = `${destinationPath}${backupFileName}`;
if (!fs.existsSync(destinationFile)) {
fs.mkdirSync(destinationPath, { recursive: true });
fs.copyFileSync(wpressFilePath, destinationFile);
console.log(`Copied wpress file to ${destinationFile}`);
} else {
console.log(
`Destination file ${destinationFile} already exists. Skipping copy.`
);
}
await wrapWithLoader(
"Restoring from wpress file. This may take a while...",
async () => {
await runWPCommand(`ai1wm restore ${backupFileName} --yes`, {
cwd: sitePath,
});
}
);
await wrapWithLoader("Regenerating thumbnails...", async () => {
await runWPCommand(`media regenerate --yes`, {
cwd: sitePath,
});
});
console.log(`\nSite restored successfully.`);
await runWPCommand(
`wp user create ${process.env.WP_ADMIN_USER} ${process.env.WP_ADMIN_EMAIL} --role=administrator --user_pass=${process.env.WP_ADMIN_PASSWORD}`,
{ cwd: sitePath }
);
console.log("\nTest user added successfully.");
console.log(`\nMigration Completed.`.blue);
} catch (error) {
console.error(`An error occurred: ${error.message}`);
}
}
exports.restoreWordPressSite = restoreWordPressSite;