@tenonhq/dovetail-core 0.0.132 → 0.0.133
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/appUtils.js +296 -51
- package/dist/commands.js +13 -1
- package/dist/createRecordCommand.js +19 -7
- package/dist/folderNaming.js +193 -0
- package/dist/gitIgnoreCheck.js +167 -0
- package/dist/tests/dovetailUtilsMSCollision.test.js +140 -0
- package/dist/tests/folderNaming.test.js +314 -0
- package/dist/tests/normalizeManifestKeys.test.js +83 -4
- package/dist/tests/refreshFailedScopes.test.js +2 -1
- package/dist/tests/refreshFolderCollisions.test.js +459 -0
- package/package.json +1 -1
- package/release-manifest.json +299 -342
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ See [`UPDATE_SET_COMMANDS.md`](UPDATE_SET_COMMANDS.md) for the full update-set C
|
|
|
54
54
|
| Code | Meaning |
|
|
55
55
|
| ---- | ------- |
|
|
56
56
|
| `0` | The command finished. |
|
|
57
|
-
| `1` | The command failed (any reason not listed below). |
|
|
57
|
+
| `1` | The command failed (any reason not listed below). For `refresh` this includes a scope that failed and a record that could not be put into the repository: its folder or a file could not be written, or git ignores the path. Each is listed by name. |
|
|
58
58
|
| `77` | ServiceNow rejected the credentials or refused the request. A write got HTTP 401 or 403, or a failing command got HTTP 401 on a read. |
|
|
59
59
|
|
|
60
60
|
A write is any request that changes the instance, whichever command sends it: `push`,
|
package/dist/appUtils.js
CHANGED
|
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.createAndAssignUpdateSet = exports.swapScope = exports.buildFiles = exports.summarizeRecord = exports.pushFiles = exports.getAppFileList = exports.groupAppFiles = exports.processMissingFiles = exports.refreshAllFiles = exports.findMissingFiles = exports.syncManifest = exports.narrowManifestToRecord = exports.narrowManifestToTables = exports.processManifest = exports.normalizeManifestKeys = exports.duplicateFolderName = exports.toSafeFolderName = exports.stampMetadataContent = exports.emptyMetadataFile = exports.EMPTY_METADATA_CONTENT = void 0;
|
|
39
|
+
exports.createAndAssignUpdateSet = exports.swapScope = exports.buildFiles = exports.summarizeRecord = exports.pushFiles = exports.getAppFileList = exports.groupAppFiles = exports.processMissingFiles = exports.refreshAllFiles = exports.findMissingFiles = exports.syncManifest = exports.narrowManifestToRecord = exports.narrowManifestToTables = exports.processManifest = exports.applyManifestFolderNames = exports.normalizeManifestKeys = exports.duplicateFolderName = exports.toSafeFolderName = exports.stampMetadataContent = exports.emptyMetadataFile = exports.EMPTY_METADATA_CONTENT = void 0;
|
|
40
40
|
const path_1 = __importDefault(require("path"));
|
|
41
41
|
const progress_1 = __importDefault(require("progress"));
|
|
42
42
|
const fUtils = __importStar(require("./FileUtils"));
|
|
@@ -50,6 +50,8 @@ const benchmark_1 = require("./benchmark");
|
|
|
50
50
|
const Logger_1 = require("./Logger");
|
|
51
51
|
const genericUtils_1 = require("./genericUtils");
|
|
52
52
|
const scopeId_1 = require("./scopeId");
|
|
53
|
+
const folderNaming_1 = require("./folderNaming");
|
|
54
|
+
const gitIgnoreCheck_1 = require("./gitIgnoreCheck");
|
|
53
55
|
// Delegate to the shared reader so the push hot-path and the
|
|
54
56
|
// createUpdateSet/switchUpdateSet writers stay on one parse contract.
|
|
55
57
|
const getUpdateSetConfig = () => (0, updateSetConfig_1.readUpdateSetConfig)();
|
|
@@ -291,8 +293,7 @@ const processTablesInManifest = async (tables, forceWrite, sourcePath, onRecordP
|
|
|
291
293
|
*/
|
|
292
294
|
const toSafeFolderName = (record) => {
|
|
293
295
|
var name = record.name || "";
|
|
294
|
-
|
|
295
|
-
return isUnsafe ? record.sys_id : name;
|
|
296
|
+
return (0, folderNaming_1.isSafeFolderName)(name) ? name : record.sys_id;
|
|
296
297
|
};
|
|
297
298
|
exports.toSafeFolderName = toSafeFolderName;
|
|
298
299
|
/**
|
|
@@ -305,13 +306,39 @@ const duplicateFolderName = (key, sysId) => {
|
|
|
305
306
|
return key + " (" + String(sysId || "").substring(0, 8) + ")";
|
|
306
307
|
};
|
|
307
308
|
exports.duplicateFolderName = duplicateFolderName;
|
|
309
|
+
// Whether a table's record names are built by the project's own configuration
|
|
310
|
+
// in a way that can end in the record's sys_id (differentiatorField: "sys_id",
|
|
311
|
+
// or a nameTemplate). Such a name must be kept as it arrives: see folderNaming.
|
|
312
|
+
// No loaded config (a manifest normalized outside a project) means no options.
|
|
313
|
+
const hasConfiguredRecordNames = (tableName) => {
|
|
314
|
+
var tableOptions;
|
|
315
|
+
try {
|
|
316
|
+
tableOptions = ConfigManager.getConfig().tableOptions;
|
|
317
|
+
}
|
|
318
|
+
catch (e) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
if (!tableOptions || !Object.prototype.hasOwnProperty.call(tableOptions, tableName))
|
|
322
|
+
return false;
|
|
323
|
+
var options = tableOptions[tableName];
|
|
324
|
+
if (!options || typeof options !== "object")
|
|
325
|
+
return false;
|
|
326
|
+
return (options.differentiatorField !== undefined ||
|
|
327
|
+
(typeof options.nameTemplate === "string" && options.nameTemplate !== ""));
|
|
328
|
+
};
|
|
308
329
|
/**
|
|
309
|
-
* Re-keys manifest records
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
330
|
+
* Re-keys manifest records to their folder names: one folder per record, the
|
|
331
|
+
* same on every disk. The rule (namesakes, letter-case twins, records that also
|
|
332
|
+
* share the first 8 id characters, names no disk can hold) lives in
|
|
333
|
+
* folderNaming.ts; this applies it table by table.
|
|
334
|
+
*
|
|
335
|
+
* Keeps the on-disk folder name (which every writer derives from `record.name`)
|
|
336
|
+
* equal to the manifest key that `dove push` looks records up by, and never
|
|
337
|
+
* leaves a manifest with fewer entries than records: two records that cannot be
|
|
338
|
+
* given separate folders stop the refresh of that application with an error
|
|
339
|
+
* that names them, instead of one overwriting the other.
|
|
340
|
+
*
|
|
341
|
+
* Idempotent, and independent of the order the records arrive in.
|
|
315
342
|
*/
|
|
316
343
|
const normalizeManifestKeys = (manifest) => {
|
|
317
344
|
var tables = manifest.tables || {};
|
|
@@ -320,26 +347,107 @@ const normalizeManifestKeys = (manifest) => {
|
|
|
320
347
|
var tableName = tableNames[i];
|
|
321
348
|
var records = tables[tableName].records || {};
|
|
322
349
|
var recordKeys = Object.keys(records);
|
|
323
|
-
var
|
|
350
|
+
var tableRecords = [];
|
|
324
351
|
for (var j = 0; j < recordKeys.length; j++) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
352
|
+
tableRecords.push(records[recordKeys[j]]);
|
|
353
|
+
}
|
|
354
|
+
var folderNames;
|
|
355
|
+
try {
|
|
356
|
+
folderNames = (0, folderNaming_1.assignFolderNames)(tableRecords, {
|
|
357
|
+
configuredNames: hasConfiguredRecordNames(tableName),
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
catch (e) {
|
|
361
|
+
if (e instanceof folderNaming_1.FolderNameCollisionError) {
|
|
362
|
+
throw new Error(tableName + ": " + e.message);
|
|
331
363
|
}
|
|
364
|
+
throw e;
|
|
365
|
+
}
|
|
366
|
+
var normalized = {};
|
|
367
|
+
for (var k = 0; k < tableRecords.length; k++) {
|
|
368
|
+
var record = tableRecords[k];
|
|
332
369
|
// Keep record.name === manifest key so all writers (which build the folder
|
|
333
370
|
// path from record.name) and push (which looks up by folder name) agree.
|
|
334
371
|
// The real display name lives in the server-provided metaData.json snapshot.
|
|
335
|
-
record.name =
|
|
336
|
-
normalized[
|
|
372
|
+
record.name = folderNames[k];
|
|
373
|
+
normalized[folderNames[k]] = record;
|
|
337
374
|
}
|
|
338
375
|
tables[tableName].records = normalized;
|
|
339
376
|
}
|
|
340
377
|
return manifest;
|
|
341
378
|
};
|
|
342
379
|
exports.normalizeManifestKeys = normalizeManifestKeys;
|
|
380
|
+
/**
|
|
381
|
+
* Gives downloaded records the folder names the manifest gave them.
|
|
382
|
+
*
|
|
383
|
+
* bulkDownload names the records it returns from the records in that one
|
|
384
|
+
* request, so its names are not the manifest's: a namesake downloaded without
|
|
385
|
+
* its twin comes back under the bare name, and an instance that names twins
|
|
386
|
+
* alike returns one name for two records. The manifest was named from the full
|
|
387
|
+
* set, so it is the one naming authority. Records are matched by sys_id, and
|
|
388
|
+
* the result is re-keyed so that key === record.name === folder for every
|
|
389
|
+
* writer downstream.
|
|
390
|
+
*
|
|
391
|
+
* A record the manifest does not list keeps a name derived from its own, and
|
|
392
|
+
* gets its full id when that name belongs to another record. Returns a new
|
|
393
|
+
* table map; the manifest is not modified.
|
|
394
|
+
*/
|
|
395
|
+
const applyManifestFolderNames = (downloaded, manifest) => {
|
|
396
|
+
var result = {};
|
|
397
|
+
var manifestTables = (manifest && manifest.tables) || {};
|
|
398
|
+
var tableNames = Object.keys(downloaded || {});
|
|
399
|
+
for (var t = 0; t < tableNames.length; t++) {
|
|
400
|
+
var tableName = tableNames[t];
|
|
401
|
+
var downloadedRecords = (downloaded[tableName] && downloaded[tableName].records) || {};
|
|
402
|
+
var listed = (manifestTables[tableName] && manifestTables[tableName].records) || {};
|
|
403
|
+
// sys_id -> manifest folder, and every folder the manifest has given out.
|
|
404
|
+
// Maps, not plain objects: a record may be named "constructor".
|
|
405
|
+
var folderOf = new Map();
|
|
406
|
+
var takenBy = new Map();
|
|
407
|
+
var listedKeys = Object.keys(listed);
|
|
408
|
+
for (var m = 0; m < listedKeys.length; m++) {
|
|
409
|
+
var listedRecord = listed[listedKeys[m]];
|
|
410
|
+
if (!listedRecord || typeof listedRecord.sys_id !== "string")
|
|
411
|
+
continue;
|
|
412
|
+
folderOf.set(listedRecord.sys_id, listedKeys[m]);
|
|
413
|
+
takenBy.set((0, folderNaming_1.foldFolderName)(listedKeys[m]), listedRecord.sys_id);
|
|
414
|
+
}
|
|
415
|
+
var renamed = {};
|
|
416
|
+
var downloadedKeys = Object.keys(downloadedRecords);
|
|
417
|
+
for (var d = 0; d < downloadedKeys.length; d++) {
|
|
418
|
+
var record = downloadedRecords[downloadedKeys[d]];
|
|
419
|
+
if (!record)
|
|
420
|
+
continue;
|
|
421
|
+
var sysId = typeof record.sys_id === "string" ? record.sys_id : "";
|
|
422
|
+
var listedFolder = sysId === "" ? undefined : folderOf.get(sysId);
|
|
423
|
+
var folder;
|
|
424
|
+
if (listedFolder !== undefined) {
|
|
425
|
+
folder = listedFolder;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
folder = (0, exports.toSafeFolderName)(record);
|
|
429
|
+
var holder = takenBy.get((0, folderNaming_1.foldFolderName)(folder));
|
|
430
|
+
if (holder !== undefined && holder !== sysId) {
|
|
431
|
+
var withId = (0, folderNaming_1.baseRecordName)(record.name || "", sysId) + " (" + sysId + ")";
|
|
432
|
+
folder = (0, folderNaming_1.isSafeFolderName)(withId) ? withId : sysId;
|
|
433
|
+
holder = takenBy.get((0, folderNaming_1.foldFolderName)(folder));
|
|
434
|
+
if (sysId === "" || (holder !== undefined && holder !== sysId)) {
|
|
435
|
+
throw new folderNaming_1.FolderNameCollisionError(folder, [
|
|
436
|
+
{ name: record.name || "", sys_id: sysId },
|
|
437
|
+
{ name: folder, sys_id: holder === undefined ? "" : holder },
|
|
438
|
+
]);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
takenBy.set((0, folderNaming_1.foldFolderName)(folder), sysId);
|
|
442
|
+
}
|
|
443
|
+
record.name = folder;
|
|
444
|
+
renamed[folder] = record;
|
|
445
|
+
}
|
|
446
|
+
result[tableName] = Object.assign({}, downloaded[tableName], { records: renamed });
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
449
|
+
};
|
|
450
|
+
exports.applyManifestFolderNames = applyManifestFolderNames;
|
|
343
451
|
const processManifest = async (manifest, forceWrite = false, sourcePath) => {
|
|
344
452
|
// Idempotent: guarantees filesystem-safe folder names + key ≡ record.name on
|
|
345
453
|
// every write path, including downloadCommand which does not normalize upstream.
|
|
@@ -426,6 +534,7 @@ const narrowManifestToRecord = (manifest, table, sysId) => {
|
|
|
426
534
|
exports.narrowManifestToRecord = narrowManifestToRecord;
|
|
427
535
|
const syncManifest = async (scope, options = {}) => {
|
|
428
536
|
var failedScopes = [];
|
|
537
|
+
var notWritten = [];
|
|
429
538
|
// Top-level entry owns the collector lifecycle. Recursive calls (all-scopes
|
|
430
539
|
// → per-scope) inherit the collector via options._benchmarkCollector.
|
|
431
540
|
var isBenchmarkOwner = false;
|
|
@@ -450,7 +559,7 @@ const syncManifest = async (scope, options = {}) => {
|
|
|
450
559
|
Logger_1.logger.warn("Skipping scope '" + scope + "' — not declared in dove.config.js `scopes`. " +
|
|
451
560
|
"Add it to config.scopes to sync, or remove its manifest file.");
|
|
452
561
|
FileLogger_1.fileLogger.debug("syncManifest: skipped undeclared scope '" + scope + "'");
|
|
453
|
-
return { failedScopes: failedScopes };
|
|
562
|
+
return { failedScopes: failedScopes, notWritten: notWritten };
|
|
454
563
|
}
|
|
455
564
|
Logger_1.logger.info("Refreshing scope: " + scope + "...");
|
|
456
565
|
const client = (0, snClient_1.defaultClient)();
|
|
@@ -533,11 +642,19 @@ const syncManifest = async (scope, options = {}) => {
|
|
|
533
642
|
}
|
|
534
643
|
if (collector)
|
|
535
644
|
collector.startScope(scope);
|
|
536
|
-
await (0, exports.refreshAllFiles)(refreshManifest, scopeSourcePath, {
|
|
645
|
+
var refreshResult = await (0, exports.refreshAllFiles)(refreshManifest, scopeSourcePath, {
|
|
537
646
|
force: options.force,
|
|
538
647
|
metadataOnly: options.metadataOnly,
|
|
539
648
|
benchmarkCollector: collector,
|
|
540
649
|
});
|
|
650
|
+
// Records this scope could not put into the repository. They do not fail
|
|
651
|
+
// the scope (everything else was written); the caller reports them by
|
|
652
|
+
// name and exits non-zero.
|
|
653
|
+
if (refreshResult && Array.isArray(refreshResult.notWritten)) {
|
|
654
|
+
for (var nw = 0; nw < refreshResult.notWritten.length; nw++) {
|
|
655
|
+
notWritten.push(Object.assign({}, refreshResult.notWritten[nw], { scope: scope }));
|
|
656
|
+
}
|
|
657
|
+
}
|
|
541
658
|
// Update the in-memory manifest for this scope. Skipped under
|
|
542
659
|
// metadataOnly: persisting the manifest would put record adds, drops and
|
|
543
660
|
// renames into a diff that is supposed to contain metaData.json only. A
|
|
@@ -576,6 +693,9 @@ const syncManifest = async (scope, options = {}) => {
|
|
|
576
693
|
if (childResult && Array.isArray(childResult.failedScopes)) {
|
|
577
694
|
failedScopes = failedScopes.concat(childResult.failedScopes);
|
|
578
695
|
}
|
|
696
|
+
if (childResult && Array.isArray(childResult.notWritten)) {
|
|
697
|
+
notWritten = notWritten.concat(childResult.notWritten);
|
|
698
|
+
}
|
|
579
699
|
}
|
|
580
700
|
}
|
|
581
701
|
}
|
|
@@ -594,7 +714,7 @@ const syncManifest = async (scope, options = {}) => {
|
|
|
594
714
|
Logger_1.logger.info(collector.formatSummary());
|
|
595
715
|
}
|
|
596
716
|
}
|
|
597
|
-
return { failedScopes: failedScopes };
|
|
717
|
+
return { failedScopes: failedScopes, notWritten: notWritten };
|
|
598
718
|
};
|
|
599
719
|
exports.syncManifest = syncManifest;
|
|
600
720
|
const markFileMissing = (missingObj) => (table) => (recordId) => (file) => {
|
|
@@ -701,6 +821,66 @@ const buildAllFilesMap = (manifest) => {
|
|
|
701
821
|
}
|
|
702
822
|
return result;
|
|
703
823
|
};
|
|
824
|
+
// A file system error as one line a person can act on: the code when there is
|
|
825
|
+
// one (EEXIST, ENAMETOOLONG, EACCES) plus the system's own message.
|
|
826
|
+
const describeError = (error) => {
|
|
827
|
+
if (error instanceof Error) {
|
|
828
|
+
var code = error.code;
|
|
829
|
+
return typeof code === "string" && error.message.indexOf(code) === -1
|
|
830
|
+
? code + ": " + error.message
|
|
831
|
+
: error.message;
|
|
832
|
+
}
|
|
833
|
+
return String(error);
|
|
834
|
+
};
|
|
835
|
+
const MAX_IGNORED_FILES_NAMED = 3;
|
|
836
|
+
/**
|
|
837
|
+
* Checks the files a refresh wrote against git's ignore rules and reports every
|
|
838
|
+
* record that has at least one ignored file, naming the rule and the files.
|
|
839
|
+
* Returns a note for the user when git could not be asked; never throws.
|
|
840
|
+
*/
|
|
841
|
+
const reportGitIgnored = async (targets, basePath, report) => {
|
|
842
|
+
if (targets.length === 0)
|
|
843
|
+
return undefined;
|
|
844
|
+
var ownerOf = new Map();
|
|
845
|
+
var allFiles = [];
|
|
846
|
+
for (var t = 0; t < targets.length; t++) {
|
|
847
|
+
for (var f = 0; f < targets[t].files.length; f++) {
|
|
848
|
+
ownerOf.set(targets[t].files[f], t);
|
|
849
|
+
allFiles.push(targets[t].files[f]);
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
var check = await (0, gitIgnoreCheck_1.findGitIgnoredPaths)(allFiles, basePath);
|
|
853
|
+
if (check.warning)
|
|
854
|
+
return check.warning;
|
|
855
|
+
if (!check.checked || check.ignored.length === 0)
|
|
856
|
+
return undefined;
|
|
857
|
+
// target index -> the ignored files of that record, in the order git gave them.
|
|
858
|
+
var byTarget = {};
|
|
859
|
+
var order = [];
|
|
860
|
+
for (var i = 0; i < check.ignored.length; i++) {
|
|
861
|
+
var hit = check.ignored[i];
|
|
862
|
+
var owner = ownerOf.get(hit.path);
|
|
863
|
+
if (owner === undefined)
|
|
864
|
+
continue;
|
|
865
|
+
if (!byTarget[owner]) {
|
|
866
|
+
byTarget[owner] = [];
|
|
867
|
+
order.push(owner);
|
|
868
|
+
}
|
|
869
|
+
byTarget[owner].push({
|
|
870
|
+
file: path_1.default.basename(hit.path),
|
|
871
|
+
rule: hit.source + ":" + hit.line + " " + hit.pattern,
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
for (var o = 0; o < order.length; o++) {
|
|
875
|
+
var hits = byTarget[order[o]];
|
|
876
|
+
var named = hits.slice(0, MAX_IGNORED_FILES_NAMED).map(function (h) { return h.file; });
|
|
877
|
+
var more = hits.length - named.length;
|
|
878
|
+
report(targets[order[o]].record, "ignored by git (" + hits[0].rule + "): " + named.join(", ") +
|
|
879
|
+
(more > 0 ? " and " + more + " more" : "") +
|
|
880
|
+
". The files are on disk; git will not add them until the rule changes.");
|
|
881
|
+
}
|
|
882
|
+
return undefined;
|
|
883
|
+
};
|
|
704
884
|
/**
|
|
705
885
|
* Refreshes local files against the ServiceNow instance for every file in the
|
|
706
886
|
* given manifest. Unlike `processMissingFiles` (which only writes files absent
|
|
@@ -716,19 +896,26 @@ const buildAllFilesMap = (manifest) => {
|
|
|
716
896
|
* orphan directory holding a lone metaData.json and no fields). Use to settle a
|
|
717
897
|
* mirror after a change to the metadata writer, without the source churn
|
|
718
898
|
* `--force` produces.
|
|
899
|
+
*
|
|
900
|
+
* Folder names come from `newManifest` (records are matched by sys_id), never
|
|
901
|
+
* from the names in the download. Returns the records it could not put into the
|
|
902
|
+
* repository: a folder or file that could not be written, or a path git
|
|
903
|
+
* ignores. One such record no longer stops the rest of the application.
|
|
719
904
|
*/
|
|
720
905
|
const refreshAllFiles = async (newManifest, sourcePath, options = {}) => {
|
|
906
|
+
// Records reached but not put into the repository: see NotWrittenRecord.
|
|
907
|
+
var notWritten = [];
|
|
721
908
|
try {
|
|
722
909
|
const allFiles = buildAllFilesMap(newManifest);
|
|
723
910
|
const tableNames = Object.keys(allFiles);
|
|
724
911
|
if (tableNames.length === 0)
|
|
725
|
-
return;
|
|
912
|
+
return { notWritten: notWritten };
|
|
726
913
|
FileLogger_1.fileLogger.debug("Refreshing file content for " + tableNames.length + " tables (force=" + !!options.force +
|
|
727
914
|
", metadataOnly=" + !!options.metadataOnly + ")");
|
|
728
915
|
const { tableOptions = {} } = ConfigManager.getConfig();
|
|
729
916
|
const client = (0, snClient_1.defaultClient)();
|
|
730
917
|
const totalChunks = Math.ceil(tableNames.length / BULK_DOWNLOAD_TABLE_CHUNK_SIZE);
|
|
731
|
-
const
|
|
918
|
+
const downloaded = {};
|
|
732
919
|
for (var i = 0; i < tableNames.length; i += BULK_DOWNLOAD_TABLE_CHUNK_SIZE) {
|
|
733
920
|
const chunkTableNames = tableNames.slice(i, i + BULK_DOWNLOAD_TABLE_CHUNK_SIZE);
|
|
734
921
|
const chunkMissing = {};
|
|
@@ -740,10 +927,20 @@ const refreshAllFiles = async (newManifest, sourcePath, options = {}) => {
|
|
|
740
927
|
" (" + chunkTableNames.length + " tables): " + chunkTableNames.join(", "));
|
|
741
928
|
const chunkResult = await (0, snClient_1.unwrapSNResponse)(client.getMissingFiles(chunkMissing, tableOptions));
|
|
742
929
|
for (var tableName in chunkResult) {
|
|
743
|
-
|
|
930
|
+
downloaded[tableName] = chunkResult[tableName];
|
|
744
931
|
}
|
|
745
932
|
}
|
|
933
|
+
// The manifest names the folders, not the download: bulkDownload names
|
|
934
|
+
// records from the ones in each request, so its names can differ from the
|
|
935
|
+
// manifest's and can repeat. See applyManifestFolderNames.
|
|
936
|
+
const filesToProcess = (0, exports.applyManifestFolderNames)(downloaded, newManifest);
|
|
746
937
|
var basePath = sourcePath || ConfigManager.getSourcePath();
|
|
938
|
+
const scopeLabel = newManifest.scope || "";
|
|
939
|
+
// Every file this refresh put (or left) on disk, for the git ignore check.
|
|
940
|
+
var writtenTargets = [];
|
|
941
|
+
const reportNotWritten = function (record, reason) {
|
|
942
|
+
notWritten.push(Object.assign({}, record, { reason: reason }));
|
|
943
|
+
};
|
|
747
944
|
var recordCount = countRecordsInTables(filesToProcess);
|
|
748
945
|
var progress = createScopeProgress(Logger_1.logger.getLogLevel(), {
|
|
749
946
|
scope: newManifest.scope || "default",
|
|
@@ -761,22 +958,45 @@ const refreshAllFiles = async (newManifest, sourcePath, options = {}) => {
|
|
|
761
958
|
var tablePath = path_1.default.join(basePath, tableName);
|
|
762
959
|
var recs = filesToProcess[tableName].records;
|
|
763
960
|
var recKeys = Object.keys(recs);
|
|
764
|
-
// metadataOnly never materializes a record directory — it refreshes the
|
|
765
|
-
// metadata of records already on disk. Creating dirs here would leave an
|
|
766
|
-
// orphan folder holding a lone metaData.json with no field files.
|
|
767
|
-
if (!metadataOnly) {
|
|
768
|
-
await Promise.all(recKeys.map(function (k) {
|
|
769
|
-
// toSafeFolderName, not raw recs[k].name: the bulkDownload response
|
|
770
|
-
// carries the unmodified ServiceNow display name, which may be
|
|
771
|
-
// filesystem-unsafe (e.g. a `<table>.*` ACL). Without this, refresh
|
|
772
|
-
// recreates Windows-illegal folders that normalizeManifestKeys
|
|
773
|
-
// already removed from the manifest.
|
|
774
|
-
return fUtils.createDirRecursively(path_1.default.join(tablePath, (0, exports.toSafeFolderName)(recs[k])));
|
|
775
|
-
}));
|
|
776
|
-
}
|
|
777
961
|
await (0, genericUtils_1.processBatched)(recKeys, constants_1.CONCURRENCY_RECORDS, async function (recKey) {
|
|
778
962
|
var rec = recs[recKey];
|
|
779
|
-
|
|
963
|
+
// The folder is the manifest's name for the record (applied above).
|
|
964
|
+
// toSafeFolderName stays as a last guard: it is a no-op on those names
|
|
965
|
+
// and keeps a record the manifest does not list off an unsafe path.
|
|
966
|
+
var folderName = (0, exports.toSafeFolderName)(rec);
|
|
967
|
+
var recPath = path_1.default.join(tablePath, folderName);
|
|
968
|
+
var target = {
|
|
969
|
+
scope: scopeLabel,
|
|
970
|
+
table: tableName,
|
|
971
|
+
sysId: rec.sys_id,
|
|
972
|
+
folder: folderName,
|
|
973
|
+
path: recPath,
|
|
974
|
+
reason: "",
|
|
975
|
+
};
|
|
976
|
+
const dropContent = function () {
|
|
977
|
+
rec.files = rec.files.map(function (file) {
|
|
978
|
+
var copy = Object.assign({}, file);
|
|
979
|
+
delete copy.content;
|
|
980
|
+
return copy;
|
|
981
|
+
});
|
|
982
|
+
};
|
|
983
|
+
// metadataOnly never materializes a record directory — it refreshes the
|
|
984
|
+
// metadata of records already on disk. Creating dirs here would leave an
|
|
985
|
+
// orphan folder holding a lone metaData.json with no field files.
|
|
986
|
+
if (!metadataOnly) {
|
|
987
|
+
try {
|
|
988
|
+
await fUtils.createDirRecursively(recPath);
|
|
989
|
+
}
|
|
990
|
+
catch (dirError) {
|
|
991
|
+
// One folder that cannot be created (a file in its place, a path
|
|
992
|
+
// the disk refuses) used to reject the whole application's refresh.
|
|
993
|
+
// Report this record by name and keep writing the others.
|
|
994
|
+
reportNotWritten(target, "could not be written: " + describeError(dirError));
|
|
995
|
+
dropContent();
|
|
996
|
+
progress.tick();
|
|
997
|
+
return;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
780
1000
|
// metadataOnly refreshes what is already checked out. A record the
|
|
781
1001
|
// instance has but this branch does not is left alone — pulling it in
|
|
782
1002
|
// would be a content change, which is exactly what this mode excludes.
|
|
@@ -789,11 +1009,7 @@ const refreshAllFiles = async (newManifest, sourcePath, options = {}) => {
|
|
|
789
1009
|
// Drop the downloaded content for skipped records too — filesToProcess
|
|
790
1010
|
// accumulates across every chunk, so holding it would grow unbounded
|
|
791
1011
|
// across a large scope.
|
|
792
|
-
|
|
793
|
-
var copy = Object.assign({}, file);
|
|
794
|
-
delete copy.content;
|
|
795
|
-
return copy;
|
|
796
|
-
});
|
|
1012
|
+
dropContent();
|
|
797
1013
|
progress.tick();
|
|
798
1014
|
return;
|
|
799
1015
|
}
|
|
@@ -824,10 +1040,13 @@ const refreshAllFiles = async (newManifest, sourcePath, options = {}) => {
|
|
|
824
1040
|
return fUtils.writeSNFileIfDifferent(file, recPath);
|
|
825
1041
|
});
|
|
826
1042
|
var anyChanged = false;
|
|
1043
|
+
var writeFailures = [];
|
|
827
1044
|
for (var f = 0; f < results.length; f++) {
|
|
828
1045
|
var res = results[f];
|
|
829
1046
|
if (res.status === "rejected") {
|
|
830
1047
|
FileLogger_1.fileLogger.error("File write failed: " + res.reason);
|
|
1048
|
+
writeFailures.push(regularFiles[f].name + "." + regularFiles[f].type + ": " +
|
|
1049
|
+
describeError(res.reason));
|
|
831
1050
|
continue;
|
|
832
1051
|
}
|
|
833
1052
|
if (res.value) {
|
|
@@ -852,18 +1071,40 @@ const refreshAllFiles = async (newManifest, sourcePath, options = {}) => {
|
|
|
852
1071
|
else {
|
|
853
1072
|
metadataFile = (0, exports.emptyMetadataFile)();
|
|
854
1073
|
}
|
|
855
|
-
|
|
856
|
-
|
|
1074
|
+
try {
|
|
1075
|
+
await forceWriter(metadataFile, recPath);
|
|
1076
|
+
metadataWrittenCount++;
|
|
1077
|
+
}
|
|
1078
|
+
catch (metaError) {
|
|
1079
|
+
writeFailures.push("metaData.json: " + describeError(metaError));
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
if (writeFailures.length > 0) {
|
|
1083
|
+
// A failed write used to reach the debug log only: the record was
|
|
1084
|
+
// incomplete on disk and the refresh still said it was done.
|
|
1085
|
+
reportNotWritten(target, "could not be written: " + writeFailures.join("; "));
|
|
1086
|
+
}
|
|
1087
|
+
else {
|
|
1088
|
+
// What git has to be able to see for this record to be in the
|
|
1089
|
+
// repository: every field file, and its metaData.json.
|
|
1090
|
+
var recordFiles = [path_1.default.join(recPath, "metaData.json")];
|
|
1091
|
+
if (!metadataOnly) {
|
|
1092
|
+
for (var rfi = 0; rfi < regularFiles.length; rfi++) {
|
|
1093
|
+
recordFiles.push(path_1.default.join(recPath, regularFiles[rfi].name + "." + regularFiles[rfi].type));
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
writtenTargets.push({ record: target, files: recordFiles });
|
|
857
1097
|
}
|
|
858
1098
|
// Strip content from manifest entries to keep memory bounded.
|
|
859
|
-
|
|
860
|
-
var copy = Object.assign({}, file);
|
|
861
|
-
delete copy.content;
|
|
862
|
-
return copy;
|
|
863
|
-
});
|
|
1099
|
+
dropContent();
|
|
864
1100
|
progress.tick();
|
|
865
1101
|
});
|
|
866
1102
|
});
|
|
1103
|
+
// A folder git ignores is on disk and missing from the repository. Ask git
|
|
1104
|
+
// once per application for everything just written, and name what it hides.
|
|
1105
|
+
var ignoredNote = await reportGitIgnored(writtenTargets, basePath, reportNotWritten);
|
|
1106
|
+
if (ignoredNote)
|
|
1107
|
+
Logger_1.logger.warn(ignoredNote);
|
|
867
1108
|
FileLogger_1.fileLogger.debug("Refresh complete: " + writtenCount + " written, " + unchangedCount + " unchanged, " +
|
|
868
1109
|
metadataWrittenCount + " metaData written, " + skippedAbsentCount + " records absent locally");
|
|
869
1110
|
if (metadataOnly) {
|
|
@@ -887,6 +1128,7 @@ const refreshAllFiles = async (newManifest, sourcePath, options = {}) => {
|
|
|
887
1128
|
// a scope that did thousands of writes as having done nothing.
|
|
888
1129
|
options.benchmarkCollector.endScope(metadataOnly ? metadataWrittenCount : writtenCount, metadataOnly ? skippedAbsentCount : unchangedCount);
|
|
889
1130
|
}
|
|
1131
|
+
return { notWritten: notWritten };
|
|
890
1132
|
}
|
|
891
1133
|
catch (e) {
|
|
892
1134
|
if (options.benchmarkCollector) {
|
|
@@ -930,7 +1172,10 @@ const processMissingFiles = async (newManifest, sourcePath) => {
|
|
|
930
1172
|
scope: newManifest.scope || "default",
|
|
931
1173
|
total: recordCount,
|
|
932
1174
|
});
|
|
933
|
-
|
|
1175
|
+
// The download names records from the ones in THIS request (only the
|
|
1176
|
+
// missing ones), so a namesake pulled without its twin comes back under the
|
|
1177
|
+
// bare name. Write to the folder the manifest gave the record instead.
|
|
1178
|
+
await processTablesInManifest((0, exports.applyManifestFolderNames)(filesToProcess, newManifest), false, sourcePath, progress.tick, newManifest.scope);
|
|
934
1179
|
}
|
|
935
1180
|
catch (e) {
|
|
936
1181
|
throw e;
|
package/dist/commands.js
CHANGED
|
@@ -141,6 +141,7 @@ async function refreshCommand(args, log = true) {
|
|
|
141
141
|
tables: tables.length > 0 ? tables : undefined,
|
|
142
142
|
});
|
|
143
143
|
const failed = (result && Array.isArray(result.failedScopes)) ? result.failedScopes : [];
|
|
144
|
+
const notWritten = (result && Array.isArray(result.notWritten)) ? result.notWritten : [];
|
|
144
145
|
if (failed.length > 0) {
|
|
145
146
|
// Every remaining scope was still attempted (syncManifest keeps going
|
|
146
147
|
// past a broken scope); now say so loudly and exit non-zero so scripts
|
|
@@ -152,7 +153,18 @@ async function refreshCommand(args, log = true) {
|
|
|
152
153
|
}
|
|
153
154
|
process.exitCode = 1;
|
|
154
155
|
}
|
|
155
|
-
|
|
156
|
+
if (notWritten.length > 0) {
|
|
157
|
+
// A record the refresh reached but could not put into the repository (a
|
|
158
|
+
// folder git ignores, a path the disk refused) used to go missing from
|
|
159
|
+
// the mirror without a word. Name each one and exit non-zero.
|
|
160
|
+
Logger_1.logger.error("Refresh finished with " + notWritten.length + " record" +
|
|
161
|
+
(notWritten.length === 1 ? "" : "s") + " not written to the repository:");
|
|
162
|
+
for (const r of notWritten) {
|
|
163
|
+
Logger_1.logger.error(" - " + r.path + " (" + r.scope + " " + r.table + ", sys_id " + r.sysId + "): " + r.reason);
|
|
164
|
+
}
|
|
165
|
+
process.exitCode = 1;
|
|
166
|
+
}
|
|
167
|
+
if (failed.length === 0 && notWritten.length === 0) {
|
|
156
168
|
Logger_1.logger.success("Refresh complete!");
|
|
157
169
|
}
|
|
158
170
|
setLogLevel(args);
|
|
@@ -545,13 +545,25 @@ async function createRecordCommand(args) {
|
|
|
545
545
|
if (syncResult && syncResult.failedScopes && syncResult.failedScopes.length > 0) {
|
|
546
546
|
throw new Error(syncResult.failedScopes[0].error);
|
|
547
547
|
}
|
|
548
|
-
//
|
|
549
|
-
//
|
|
550
|
-
//
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
548
|
+
// A record the refresh reached but could not put into the repository
|
|
549
|
+
// (a folder git ignores, a path that could not be written) is not a
|
|
550
|
+
// failed sync, and not a success either: say which and why.
|
|
551
|
+
var unwritten = (syncResult && Array.isArray(syncResult.notWritten)) ? syncResult.notWritten : [];
|
|
552
|
+
if (unwritten.length > 0) {
|
|
553
|
+
for (var u = 0; u < unwritten.length; u++) {
|
|
554
|
+
Logger_1.logger.warn("Record created on instance, but not written to the repository: " +
|
|
555
|
+
unwritten[u].path + ": " + unwritten[u].reason);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
// Resolve the scope's own source directory — syncManifest writes into
|
|
560
|
+
// getSourcePathForScope(scope), which a scope config can override. Using
|
|
561
|
+
// the top-level getSourcePath() here would print a wrong path in
|
|
562
|
+
// multi-scope setups even though the files landed correctly.
|
|
563
|
+
var sourcePath = ConfigManager.getSourcePathForScope(scope);
|
|
564
|
+
var localPath = path_1.default.join(sourcePath, table, recordName);
|
|
565
|
+
Logger_1.logger.success(chalk_1.default.green("Local files created at: ") + localPath);
|
|
566
|
+
}
|
|
555
567
|
}
|
|
556
568
|
catch (syncErr) {
|
|
557
569
|
Logger_1.logger.warn("Record created on instance but local sync failed.");
|