-
Notifications
You must be signed in to change notification settings - Fork 1
/
revisionexport.ts
213 lines (193 loc) · 7.78 KB
/
revisionexport.ts
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
import { mainLog } from './utils/logger.js';
import { promises as fs } from 'fs';
import sanitize from 'sanitize-filename';
import { existsSync } from 'fs';
import { ArgumentParser } from './utils/argumentparser.js';
import { ApiClient } from './utils/apiclient.js';
import { getFilePath, writeRevisionExport, CsvType } from './utils/csvFile.js';
import { DocumentNode, ElementType, ExportOptions, ListResponse, Revision, RevisionExport } from './utils/onshapetypes.js';
import { TranslationHelper } from './utils/translationhelper.js';
const LOG = mainLog();
/** EXPORT_OPTIONS These are the default options for exports for various formats
* You can optionally edit this section to override options used to export various formats
*/
const EXPORT_OPTIONS : Record<string, ExportOptions> = {
'pdf': {
formatName: 'PDF',
colorMethod: 'color', // Only supported for drawings and can be blackandwhite or grayscale
showOverriddenDimensions: true,
},
'dwg': {
formatName: 'DWG',
},
'dxf': {
formatName: 'DXF',
},
'json': {
formatName: 'DRAWING_JSON',
level: 'full'
},
'step': {
formatName: 'STEP',
},
'acis': {
formatName: 'ACIS',
},
};
class RevisionProcessor {
private processRevs: Record<string, string> = {};
private lastCreatedAt: Date = null;
private exportTypes: Record<number, string> = {};
private translationHelper: TranslationHelper = null;
private useExportRules: boolean = true;
public constructor(private apiClient: ApiClient, private companyId: string, private findAll: boolean) {
const partExportType: string = ArgumentParser.getLowerCase('part');
if (partExportType) {
this.exportTypes[ElementType.PARTSTUDIO] = partExportType;
}
const drawingExportType: string = ArgumentParser.getLowerCase('drawing');
if (drawingExportType) {
this.exportTypes[ElementType.DRAWING] = drawingExportType;
} else if (!partExportType) {
this.exportTypes[ElementType.DRAWING] = 'pdf';
}
this.translationHelper = new TranslationHelper(apiClient);
this.useExportRules = !! ArgumentParser.get('export-rules', true);
LOG.info('Specified types=%s useExportRules=%s', this.exportTypes, this.useExportRules);
}
public async processSingleRev(rev: Revision) : Promise<ExportOptions> {
LOG.debug(`Processing revision partnum=${rev.partNumber} revision=${rev.revision} elementType=${rev.elementType}`);
const exportFormatName = this.exportTypes[rev.elementType];
if (!exportFormatName) {
LOG.debug(`elementype=${rev.elementType} partNumer=${rev.partNumber} revision=${rev.revision} has no export specified`);
return null;
}
await this.validDocument(rev);
const exportOptions = await this.getExportFileName(rev, exportFormatName);
if (rev.elementType == ElementType.DRAWING) {
await this.translationHelper.exportDrawingRevisionSync(rev, exportOptions);
} else if (rev.elementType == ElementType.PARTSTUDIO) {
await this.translationHelper.exportPartRevisionSync(rev, exportOptions);
}
return exportOptions;
}
private async getExportFileName(rev: Revision, exportFormatName : string) : Promise<ExportOptions> {
const exportOptions = Object.assign({}, EXPORT_OPTIONS[exportFormatName]);
exportOptions.destinationName = sanitize( `${rev.partNumber}_${rev.revision}` );
exportOptions.fileExtension = exportFormatName;
if (!this.useExportRules) {
return exportOptions;
}
let baseUrl = `api/exportrules/d/${rev.documentId}/v/${rev.versionId}/e/${rev.elementId}`;
if (rev.partId) {
baseUrl += '/p/' + encodeURIComponent(rev.partId);
}
baseUrl += `?elementType=${rev.elementType}`;
if (rev.configuration) {
baseUrl += '?configuration=' + encodeURIComponent(rev.configuration);
}
const exportRule = await this.apiClient.post(baseUrl, {
fileType: exportOptions?.formatName || null
}) as Record<string, string>;
if (exportRule.exportFileName) {
exportOptions.destinationName = sanitize( exportRule.exportFileName );
}
return exportOptions;
}
private async validDocument(rev: Revision) {
const docResponse = await this.apiClient.get(`api/documents/${rev.documentId}`) as DocumentNode;
if (!docResponse || docResponse.trash) {
throw new Error(`Failed to find documentId=${rev.documentId}`);
}
if (docResponse.owner?.id != this.companyId) {
throw new Error(`DocumentId=${rev.documentId} is not owned by company`);
}
}
/**
* Process all revisions and trigger export as needed
*/
public async enumerateAllRevisions() {
const latestOnlyValue = this.findAll ? 'false' : 'true';
let nextBatchUri = `api/revisions/companies/${this.companyId}?latestOnly=${latestOnlyValue}`;
if (this.lastCreatedAt) {
nextBatchUri = `${nextBatchUri}&after=${this.lastCreatedAt.toISOString()}&offset=1`;
}
LOG.info('Staring revision search from date =', this.lastCreatedAt);
let totalRevCount = 0;
while (nextBatchUri) {
LOG.info(`Calling ${nextBatchUri}`);
const revsResponse = await this.apiClient.get(nextBatchUri) as ListResponse<Revision>;
if (revsResponse.items) {
totalRevCount += revsResponse.items.length;
LOG.info(`Found total revisions = ${totalRevCount}`);
for (const rev of revsResponse.items) {
const exportResult: RevisionExport = {
id: rev.id,
companyId: rev.companyId,
createdAt: rev.createdAt,
partNumber: rev.partNumber,
revision: rev.revision,
elementType: rev.elementType
};
try {
const exportOptions = await this.processSingleRev(rev);
if (exportOptions) {
exportResult.fileName = `${exportOptions.destinationName}.${exportOptions.fileExtension}`;
}
} catch (error) {
exportResult.message = String(error);
LOG.info(`Failed to export revision=${rev.id} partNumer=${rev.partNumber} revision=${rev.revision}`, error);
} finally {
exportResult.exportedAt = new Date().toISOString();
await writeRevisionExport(exportResult);
}
}
}
nextBatchUri = revsResponse.next;
}
}
/**
* Figure out last revision that was exported. Either from
* csv file or from --days option
*/
public async loadProcessedRevisions() {
const csvFilePath = getFilePath(CsvType.REVISION_EXPORT);
let lastCreatedAt = null;
if (existsSync(csvFilePath)) {
const fileBuffer = await fs.readFile(csvFilePath);
const allLines = fileBuffer.toString().split('\n');
for (const aLine of allLines) {
if (aLine.includes(this.companyId)) {
const [revId, createdAt] = aLine.split(',');
if (revId && revId.match(/[0-9A-Fa-f]{10,}/)) {
this.processRevs[revId] = createdAt;
lastCreatedAt = createdAt;
}
}
}
}
if (lastCreatedAt) {
this.lastCreatedAt = new Date(lastCreatedAt);
} else {
const nDays = Number(ArgumentParser.get('days', 30));
const startDate = new Date();
startDate.setDate(startDate.getDate() - nDays);
this.lastCreatedAt = new Date(startDate);
}
}
}
try {
const stackToUse: string = ArgumentParser.get('stack');
const findAll: boolean = ArgumentParser.get('all');
const apiClient = await ApiClient.createApiClient(stackToUse);
const companyInfo = await apiClient.findCompanyInfo();
if (!companyInfo.admin) {
throw new Error('Company admin permission required for exporting revisions');
}
const revProcessor = new RevisionProcessor(apiClient, companyInfo.id, findAll);
await revProcessor.loadProcessedRevisions();
await revProcessor.enumerateAllRevisions();
} catch (error) {
console.error(error);
LOG.error('Exporting all revisions failed', error);
}