Skip to content

Commit ebb3d60

Browse files
committed
Introduce debug option
1 parent e62eb55 commit ebb3d60

6 files changed

Lines changed: 64 additions & 22 deletions

File tree

‎packages/import-export/index.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ module.exports = {
2020
browser: true
2121
},
2222
preventUpdateAssets: false,
23-
importDraftsOnlyDefault: false
23+
importDraftsOnlyDefault: false,
24+
// When true (or when the `APOS_DEBUG_IMPORT_EXPORT='1'` env var is set),
25+
// verbose internal progress is logged.
26+
debug: false
2427
},
2528
init(self) {
2629
self.formats = {

‎packages/import-export/lib/handlers.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module.exports = self => {
7373
if (!ids.length) {
7474
return;
7575
}
76-
self.apos.util.debug(`Clearing ${ids.length} timer(s)`);
76+
self.debug(`Clearing ${ids.length} timer(s)`);
7777
for (const key of ids) {
7878
const entry = self.timeoutIds[key];
7979
delete self.timeoutIds[key];
@@ -85,7 +85,7 @@ module.exports = self => {
8585
await entry.handler();
8686
}
8787
}
88-
self.apos.util.debug('Timer(s) cleared');
88+
self.debug('Timer(s) cleared');
8989
}
9090
}
9191
};

‎packages/import-export/lib/methods/export.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ module.exports = self => {
510510
const downloadPath = path.join('/exports', filename);
511511
const downloadUrl = `${self.apos.attachment.uploadfs.getUrl()}${downloadPath}`;
512512
const copyIn = util.promisify(self.apos.attachment.uploadfs.copyIn);
513-
self.apos.util.debug(`[export] copying ${filepath} to ${self.apos.rootDir}/public/uploads${downloadPath}`);
513+
self.debug(`[export] copying ${filepath} to ${self.apos.rootDir}/public/uploads${downloadPath}`);
514514
try {
515515
await copyIn(filepath, downloadPath);
516516
} catch (error) {
@@ -565,7 +565,7 @@ module.exports = self => {
565565

566566
async function processAttachment([ name, url ]) {
567567
const temp = self.apos.attachment.uploadfs.getTempPath() + '/' + self.apos.util.generateId();
568-
self.apos.util.debug(`[export] processing attachment ${name} temporarily stored in ${temp}`);
568+
self.debug(`[export] processing attachment ${name} temporarily stored in ${temp}`);
569569
try {
570570
await copyOut(url, temp);
571571
const { size } = await fsp.stat(temp);
@@ -593,9 +593,9 @@ module.exports = self => {
593593
removeFromUploadFs(downloadPath, expiration) {
594594
const ms = expiration || 1000 * 60 * 10;
595595
const id = uniqueId();
596-
self.apos.util.debug(`[export] removing ${self.apos.rootDir}/public/uploads${downloadPath} from uploadfs in ${ms / 1000 / 60} minutes`);
596+
self.debug(`[export] removing ${self.apos.rootDir}/public/uploads${downloadPath} from uploadfs in ${ms / 1000 / 60} minutes`);
597597
const handler = () => {
598-
self.apos.util.debug(`[export] removing ${self.apos.rootDir}/public/uploads${downloadPath} from uploadfs`);
598+
self.debug(`[export] removing ${self.apos.rootDir}/public/uploads${downloadPath} from uploadfs`);
599599
delete self.timeoutIds[id];
600600
return new Promise((resolve, _reject) => {
601601
self.apos.attachment.uploadfs.remove(downloadPath, error => {

‎packages/import-export/lib/methods/import.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ module.exports = self => {
12721272
},
12731273

12741274
async cleanExport(req) {
1275-
self.apos.util.debug('[import] cleaning export...');
1275+
self.debug('[import] cleaning export...');
12761276
const exportPath = await self.getExportPath(
12771277
self.apos.launder.string(req.body.exportId)
12781278
);

‎packages/import-export/lib/methods/index.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@ const fsp = require('node:fs/promises');
22
const importMethods = require('./import.js');
33
const exportMethods = require('./export.js');
44

5+
const noop = () => {};
6+
57
module.exports = self => {
8+
const debugEnabled = self.options.debug === true ||
9+
process.env.APOS_DEBUG_IMPORT_EXPORT === '1';
10+
// Bind once at startup so each call site pays no per-call cost.
11+
// No-op unless the module's `debug` option is `true` or the
12+
// `APOS_DEBUG_IMPORT_EXPORT` env var is set.
13+
const debug = debugEnabled
14+
? (...args) => self.apos.util.debug(...args)
15+
: noop;
16+
617
return {
18+
debug,
19+
720
registerFormats(formats = {}) {
821
verifyFormats(formats);
922

@@ -56,7 +69,7 @@ module.exports = self => {
5669
} else {
5770
await fsp.unlink(filepath);
5871
}
59-
self.apos.util.debug(`removed: ${filepath}`);
72+
self.debug(`removed: ${filepath}`);
6073
} catch (err) {
6174
console.trace(err);
6275
self.apos.util.error(

‎packages/import-export/test/import-page.js‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ describe('@apostrophecms/import-export:import-page', function () {
365365
titleSortified: 'level 2 page 1',
366366
type: 'test-page',
367367
updatedAt: importedDocs.at(0).updatedAt,
368-
...(importedDocs.at(0).importedAt && { importedAt: importedDocs.at(0).importedAt }),
368+
...(importedDocs.at(0).importedAt && {
369+
importedAt: importedDocs.at(0).importedAt
370+
}),
369371
updatedBy: {
370372
_id: null, // TODO: should be my user id
371373
title: 'System Task',
@@ -405,7 +407,9 @@ describe('@apostrophecms/import-export:import-page', function () {
405407
titleSortified: 'level 2 page 1',
406408
type: 'test-page',
407409
updatedAt: importedDocs.at(1).updatedAt,
408-
...(importedDocs.at(1).importedAt && { importedAt: importedDocs.at(1).importedAt }),
410+
...(importedDocs.at(1).importedAt && {
411+
importedAt: importedDocs.at(1).importedAt
412+
}),
409413
updatedBy: {
410414
_id: null, // TODO: should be my user id
411415
title: 'System Task',
@@ -445,7 +449,9 @@ describe('@apostrophecms/import-export:import-page', function () {
445449
titleSortified: 'level 4 page 1',
446450
type: 'test-page',
447451
updatedAt: importedDocs.at(2).updatedAt,
448-
...(importedDocs.at(2).importedAt && { importedAt: importedDocs.at(2).importedAt }),
452+
...(importedDocs.at(2).importedAt && {
453+
importedAt: importedDocs.at(2).importedAt
454+
}),
449455
updatedBy: {
450456
_id: null, // TODO: should be my user id
451457
title: 'System Task',
@@ -487,7 +493,9 @@ describe('@apostrophecms/import-export:import-page', function () {
487493
titleSortified: 'level 4 page 1',
488494
type: 'test-page',
489495
updatedAt: importedDocs.at(3).updatedAt,
490-
...(importedDocs.at(3).importedAt && { importedAt: importedDocs.at(3).importedAt }),
496+
...(importedDocs.at(3).importedAt && {
497+
importedAt: importedDocs.at(3).importedAt
498+
}),
491499
updatedBy: {
492500
_id: null, // TODO: should be my user id
493501
title: 'System Task',
@@ -618,7 +626,9 @@ describe('@apostrophecms/import-export:import-page', function () {
618626
titleSortified: 'level 2 page 1',
619627
type: 'test-page',
620628
updatedAt: importedDocs.at(0).updatedAt,
621-
...(importedDocs.at(0).importedAt && { importedAt: importedDocs.at(0).importedAt }),
629+
...(importedDocs.at(0).importedAt && {
630+
importedAt: importedDocs.at(0).importedAt
631+
}),
622632
updatedBy: {
623633
_id: null, // TODO: should be my user id
624634
title: 'System Task',
@@ -658,7 +668,9 @@ describe('@apostrophecms/import-export:import-page', function () {
658668
titleSortified: 'level 2 page 1',
659669
type: 'test-page',
660670
updatedAt: importedDocs.at(1).updatedAt,
661-
...(importedDocs.at(1).importedAt && { importedAt: importedDocs.at(1).importedAt }),
671+
...(importedDocs.at(1).importedAt && {
672+
importedAt: importedDocs.at(1).importedAt
673+
}),
662674
updatedBy: {
663675
_id: null, // TODO: should be my user id
664676
title: 'System Task',
@@ -698,7 +710,9 @@ describe('@apostrophecms/import-export:import-page', function () {
698710
titleSortified: 'level 4 page 1',
699711
type: 'test-page',
700712
updatedAt: importedDocs.at(2).updatedAt,
701-
...(importedDocs.at(2).importedAt && { importedAt: importedDocs.at(2).importedAt }),
713+
...(importedDocs.at(2).importedAt && {
714+
importedAt: importedDocs.at(2).importedAt
715+
}),
702716
updatedBy: {
703717
_id: null, // TODO: should be my user id
704718
title: 'System Task',
@@ -740,7 +754,9 @@ describe('@apostrophecms/import-export:import-page', function () {
740754
titleSortified: 'level 4 page 1',
741755
type: 'test-page',
742756
updatedAt: importedDocs.at(3).updatedAt,
743-
...(importedDocs.at(3).importedAt && { importedAt: importedDocs.at(3).importedAt }),
757+
...(importedDocs.at(3).importedAt && {
758+
importedAt: importedDocs.at(3).importedAt
759+
}),
744760
updatedBy: {
745761
_id: null, // TODO: should be my user id
746762
title: 'System Task',
@@ -993,7 +1009,9 @@ describe('@apostrophecms/import-export:import-page', function () {
9931009
titleSortified: 'custom',
9941010
type: 'custom-page',
9951011
updatedAt: importedDocs.at(0).updatedAt,
996-
...(importedDocs.at(0).importedAt && { importedAt: importedDocs.at(0).importedAt }),
1012+
...(importedDocs.at(0).importedAt && {
1013+
importedAt: importedDocs.at(0).importedAt
1014+
}),
9971015
updatedBy: {
9981016
_id: null, // TODO: should be my user id
9991017
title: 'System Task',
@@ -1033,7 +1051,9 @@ describe('@apostrophecms/import-export:import-page', function () {
10331051
titleSortified: 'custom',
10341052
type: 'custom-page',
10351053
updatedAt: importedDocs.at(1).updatedAt,
1036-
...(importedDocs.at(1).importedAt && { importedAt: importedDocs.at(1).importedAt }),
1054+
...(importedDocs.at(1).importedAt && {
1055+
importedAt: importedDocs.at(1).importedAt
1056+
}),
10371057
updatedBy: {
10381058
_id: null, // TODO: should be my user id
10391059
title: 'System Task',
@@ -1073,7 +1093,9 @@ describe('@apostrophecms/import-export:import-page', function () {
10731093
titleSortified: 'custom',
10741094
type: 'custom-page',
10751095
updatedAt: importedDocs.at(2).updatedAt,
1076-
...(importedDocs.at(2).importedAt && { importedAt: importedDocs.at(2).importedAt }),
1096+
...(importedDocs.at(2).importedAt && {
1097+
importedAt: importedDocs.at(2).importedAt
1098+
}),
10771099
updatedBy: {
10781100
_id: null, // TODO: should be my user id
10791101
title: 'System Task',
@@ -1113,7 +1135,9 @@ describe('@apostrophecms/import-export:import-page', function () {
11131135
titleSortified: 'custom level 3 page 1',
11141136
type: 'test-page',
11151137
updatedAt: importedDocs.at(3).updatedAt,
1116-
...(importedDocs.at(3).importedAt && { importedAt: importedDocs.at(3).importedAt }),
1138+
...(importedDocs.at(3).importedAt && {
1139+
importedAt: importedDocs.at(3).importedAt
1140+
}),
11171141
updatedBy: {
11181142
_id: null, // TODO: should be my user id
11191143
title: 'System Task',
@@ -1155,7 +1179,9 @@ describe('@apostrophecms/import-export:import-page', function () {
11551179
titleSortified: 'custom level 3 page 1',
11561180
type: 'test-page',
11571181
updatedAt: importedDocs.at(4).updatedAt,
1158-
...(importedDocs.at(4).importedAt && { importedAt: importedDocs.at(4).importedAt }),
1182+
...(importedDocs.at(4).importedAt && {
1183+
importedAt: importedDocs.at(4).importedAt
1184+
}),
11591185
updatedBy: {
11601186
_id: null, // TODO: should be my user id
11611187
title: 'System Task',

0 commit comments

Comments
 (0)