@itwin/workspace-editor 4.8.0-dev.3 → 4.8.0-dev.4
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/lib/WorkspaceEditor.js +46 -37
- package/package.json +5 -5
package/lib/WorkspaceEditor.js
CHANGED
|
@@ -11,12 +11,14 @@ const path_1 = require("path");
|
|
|
11
11
|
const readline = require("readline");
|
|
12
12
|
const Yargs = require("yargs");
|
|
13
13
|
const core_backend_1 = require("@itwin/core-backend");
|
|
14
|
+
const WorkspaceImpl_1 = require("@itwin/core-backend/lib/cjs/internal/workspace/WorkspaceImpl");
|
|
14
15
|
const core_bentley_1 = require("@itwin/core-bentley");
|
|
15
16
|
const core_common_1 = require("@itwin/core-common");
|
|
16
17
|
// cspell:ignore nodir nocase
|
|
17
18
|
/* eslint-disable id-blacklist,no-console */
|
|
18
19
|
/** Currently executing an "@" script? */
|
|
19
20
|
let inScript = false;
|
|
21
|
+
let wsEditor;
|
|
20
22
|
async function askQuestion(query) {
|
|
21
23
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
22
24
|
return new Promise((resolve) => rl.question(query, (ans) => {
|
|
@@ -43,14 +45,13 @@ function friendlyFileSize(size) {
|
|
|
43
45
|
const units = Math.floor(Math.log(size) / Math.log(1024));
|
|
44
46
|
return `${Math.round(size / Math.pow(1024, units))}${["", "K", "M", "G", "T"][units]}`;
|
|
45
47
|
}
|
|
46
|
-
/** Create a new empty WorkspaceDb */
|
|
47
|
-
async function createWorkspaceDb(args) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
48
|
+
// /** Create a new empty WorkspaceDb */
|
|
49
|
+
// async function createWorkspaceDb(args: CreateWorkspaceDbOpt) {
|
|
50
|
+
// const wsFile = WorkspaceEditor.createEmptyDb(args);, IModelHost.appWorkspace.getContainer(args));
|
|
51
|
+
// await wsFile.createDb({ manifest: { workspaceName: args.workspaceName } });
|
|
52
|
+
// showMessage(`created WorkspaceDb ${wsFile.sqliteDb.nativeDb.getFilePath()}`);
|
|
53
|
+
// wsFile.close();
|
|
54
|
+
// }
|
|
54
55
|
/** open, call a function to process, then close a WorkspaceDb */
|
|
55
56
|
async function processWorkspace(args, ws, fn) {
|
|
56
57
|
ws.open();
|
|
@@ -63,10 +64,15 @@ async function processWorkspace(args, ws, fn) {
|
|
|
63
64
|
}
|
|
64
65
|
}
|
|
65
66
|
/** get a WorkspaceContainer that may or may not be a cloud container. */
|
|
66
|
-
function
|
|
67
|
-
args
|
|
67
|
+
function getWriteContainer(args) {
|
|
68
|
+
return wsEditor.getContainer(args);
|
|
69
|
+
}
|
|
70
|
+
function getReadContainer(args) {
|
|
68
71
|
return core_backend_1.IModelHost.appWorkspace.getContainer(args);
|
|
69
72
|
}
|
|
73
|
+
function getContainer(args) {
|
|
74
|
+
return true !== args.forReadOnly ? getWriteContainer(args) : getReadContainer(args);
|
|
75
|
+
}
|
|
70
76
|
/** get a WorkspaceContainer that is expected to be a cloud container, throw otherwise. */
|
|
71
77
|
function getCloudContainer(args) {
|
|
72
78
|
const container = getContainer(args);
|
|
@@ -76,24 +82,21 @@ function getCloudContainer(args) {
|
|
|
76
82
|
return cloudContainer;
|
|
77
83
|
}
|
|
78
84
|
function fixVersionArg(args) {
|
|
79
|
-
const dbParts =
|
|
85
|
+
const dbParts = (0, WorkspaceImpl_1.parseWorkspaceDbFileName)(args.dbName);
|
|
80
86
|
args.dbName = dbParts.dbName;
|
|
81
87
|
args.version = args.version ?? dbParts.version;
|
|
82
|
-
args.dbFileName =
|
|
88
|
+
args.dbFileName = (0, WorkspaceImpl_1.makeWorkspaceDbFileName)(dbParts.dbName, dbParts.version);
|
|
83
89
|
}
|
|
84
90
|
/** Open for write, call a function to process, then close a WorkspaceDb */
|
|
85
91
|
async function editWorkspace(args, fn) {
|
|
86
92
|
fixVersionArg(args);
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
if (cloudContainer && cloudContainer.queryDatabase(ws.dbFileName)?.state !== "copied")
|
|
90
|
-
throw new Error(`${args.dbFileName} is not editable. Create a new version first`);
|
|
91
|
-
await processWorkspace(args, ws, fn);
|
|
93
|
+
const db = getWriteContainer(args).getEditableDb(args);
|
|
94
|
+
await processWorkspace(args, db, fn);
|
|
92
95
|
}
|
|
93
96
|
/** Open for read, call a function to process, then close a WorkspaceDb */
|
|
94
97
|
async function readWorkspace(args, fn) {
|
|
95
98
|
fixVersionArg(args);
|
|
96
|
-
return processWorkspace(args,
|
|
99
|
+
return processWorkspace(args, getContainer(args).getWorkspaceDb(args), fn);
|
|
97
100
|
}
|
|
98
101
|
/** List the contents of a WorkspaceDb */
|
|
99
102
|
async function listWorkspaceDb(args) {
|
|
@@ -242,7 +245,7 @@ async function removeResource(args) {
|
|
|
242
245
|
async function vacuumWorkspaceDb(args) {
|
|
243
246
|
const container = getContainer(args);
|
|
244
247
|
fixVersionArg(args);
|
|
245
|
-
const localFile =
|
|
248
|
+
const localFile = (0, WorkspaceImpl_1.constructWorkspaceDb)(args, container).dbFileName;
|
|
246
249
|
doVacuum(localFile, container.cloudContainer);
|
|
247
250
|
}
|
|
248
251
|
/** Either upload or download a WorkspaceDb to/from a cloud WorkspaceContainer. Shows progress % during transfer */
|
|
@@ -274,7 +277,7 @@ async function performTransfer(container, direction, args) {
|
|
|
274
277
|
async function importWorkspaceDb(args) {
|
|
275
278
|
const container = getCloudContainer(args);
|
|
276
279
|
if ("" === (0, path_1.extname)(args.localFileName))
|
|
277
|
-
args.localFileName = `${args.localFileName}.${
|
|
280
|
+
args.localFileName = `${args.localFileName}.${WorkspaceImpl_1.workspaceDbFileExt}`;
|
|
278
281
|
if (!core_backend_1.IModelJsFs.existsSync(args.localFileName))
|
|
279
282
|
args.localFileName = (0, path_1.join)(args.directory ?? core_backend_1.IModelHost.appWorkspace.containerDir, args.localFileName);
|
|
280
283
|
await core_backend_1.CloudSqlite.withWriteLock({ ...args, container }, async () => {
|
|
@@ -285,8 +288,8 @@ async function importWorkspaceDb(args) {
|
|
|
285
288
|
/** export a WorkspaceDb from a cloud WorkspaceContainer. */
|
|
286
289
|
async function exportWorkspaceDb(args) {
|
|
287
290
|
if (!(0, path_1.extname)(args.localFileName))
|
|
288
|
-
args.localFileName = `${args.localFileName}.${
|
|
289
|
-
const dbParts =
|
|
291
|
+
args.localFileName = `${args.localFileName}.${WorkspaceImpl_1.workspaceDbFileExt}`;
|
|
292
|
+
const dbParts = (0, WorkspaceImpl_1.parseWorkspaceDbFileName)(args.dbName);
|
|
290
293
|
if (!dbParts.version)
|
|
291
294
|
throw new Error("exportDb requires a version");
|
|
292
295
|
await performTransfer(getCloudContainer(args), "download", args);
|
|
@@ -333,9 +336,9 @@ async function detachWorkspace(args) {
|
|
|
333
336
|
async function copyWorkspaceDb(args) {
|
|
334
337
|
const container = getContainer(args);
|
|
335
338
|
const oldName = container.resolveDbFileName(args);
|
|
336
|
-
const newVersion =
|
|
337
|
-
|
|
338
|
-
const newName =
|
|
339
|
+
const newVersion = (0, WorkspaceImpl_1.parseWorkspaceDbFileName)(args.newDbName);
|
|
340
|
+
(0, WorkspaceImpl_1.validateWorkspaceDbName)(newVersion.dbName);
|
|
341
|
+
const newName = (0, WorkspaceImpl_1.makeWorkspaceDbFileName)(newVersion.dbName, (0, WorkspaceImpl_1.validateWorkspaceDbVersion)(newVersion.version));
|
|
339
342
|
const cloudContainer = getCloudContainer(args);
|
|
340
343
|
await core_backend_1.CloudSqlite.withWriteLock({ ...args, container: cloudContainer }, async () => cloudContainer.copyDatabase(oldName, newName));
|
|
341
344
|
showMessage(`copied WorkspaceDb [${oldName}] to [${newName}] in ${sayContainer(args)}`);
|
|
@@ -343,9 +346,9 @@ async function copyWorkspaceDb(args) {
|
|
|
343
346
|
/** Make a copy of a WorkspaceDb with a new name. */
|
|
344
347
|
async function versionWorkspaceDb(args) {
|
|
345
348
|
fixVersionArg(args);
|
|
346
|
-
const container =
|
|
347
|
-
const result = await container.
|
|
348
|
-
showMessage(`created new version: [${result.
|
|
349
|
+
const container = getWriteContainer(args);
|
|
350
|
+
const result = await container.createNewWorkspaceDbVersion({ fromProps: args, versionType: args.versionType });
|
|
351
|
+
showMessage(`created new version: [${result.newDb.dbName}] from [${result.oldDb.dbName}] in ${sayContainer(args)}`);
|
|
349
352
|
}
|
|
350
353
|
/** pin a WorkspaceDb from a WorkspaceContainer. */
|
|
351
354
|
async function preFetchWorkspaceDb(args) {
|
|
@@ -398,8 +401,10 @@ async function queryWorkspaceDbs(args) {
|
|
|
398
401
|
}
|
|
399
402
|
}
|
|
400
403
|
/** Start `IModelHost`, then run a WorkspaceEditor command. Errors are logged to console. */
|
|
401
|
-
function runCommand(cmd) {
|
|
404
|
+
function runCommand(cmd, readonly) {
|
|
402
405
|
return async (args) => {
|
|
406
|
+
if (readonly === "readonly")
|
|
407
|
+
args.forReadOnly = true;
|
|
403
408
|
if (inScript)
|
|
404
409
|
return cmd(args);
|
|
405
410
|
try {
|
|
@@ -416,6 +421,7 @@ function runCommand(cmd) {
|
|
|
416
421
|
core_bentley_1.Logger.setLevel("CloudSqlite", core_bentley_1.LogLevel.Trace);
|
|
417
422
|
core_backend_1.IModelHost.appWorkspace.getCloudCache().setLogMask(core_backend_1.CloudSqlite.LoggingMask.All);
|
|
418
423
|
}
|
|
424
|
+
wsEditor = core_backend_1.WorkspaceEditor.construct();
|
|
419
425
|
await cmd(args);
|
|
420
426
|
}
|
|
421
427
|
catch (e) {
|
|
@@ -446,7 +452,7 @@ Yargs.options({
|
|
|
446
452
|
nRequests: { describe: "Number of simultaneous http requests for cloud operations", number: true, hidden: true },
|
|
447
453
|
containerId: { alias: "c", describe: "ContainerId for WorkspaceDb", string: true, demandOption: true },
|
|
448
454
|
user: { describe: "String shown in cloud container locked message", string: true, default: "workspace-editor" },
|
|
449
|
-
baseUri: {
|
|
455
|
+
baseUri: { describe: "The base uri for the container", string: true },
|
|
450
456
|
accessToken: { describe: "Token that grants access to the container (either SAS or account key)", string: true, default: "" },
|
|
451
457
|
storageType: { describe: "Cloud storage module type", string: true, default: "azure" },
|
|
452
458
|
logging: { describe: "enable log messages", boolean: true, default: false, hidden: true },
|
|
@@ -485,18 +491,21 @@ Yargs.command({
|
|
|
485
491
|
blobs: { alias: "b", describe: "list blob resources", boolean: true, default: false },
|
|
486
492
|
files: { alias: "f", describe: "list file resources", boolean: true, default: false },
|
|
487
493
|
},
|
|
488
|
-
handler: runCommand(listWorkspaceDb),
|
|
494
|
+
handler: runCommand(listWorkspaceDb, "readonly"),
|
|
489
495
|
});
|
|
490
496
|
Yargs.command({
|
|
491
497
|
command: "deleteDb <dbName>",
|
|
492
498
|
describe: "delete a WorkspaceDb from a cloud container",
|
|
493
499
|
handler: runCommand(deleteWorkspaceDb),
|
|
494
500
|
});
|
|
495
|
-
Yargs.command({
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
501
|
+
// Yargs.command<CreateWorkspaceDbOpt>({
|
|
502
|
+
// command: "createDb <dbName>",
|
|
503
|
+
// describe: "create a new WorkspaceDb",
|
|
504
|
+
// builder: {
|
|
505
|
+
// workspaceName: { describe: "user interface name for the WorkspaceDb", string: true },
|
|
506
|
+
// },
|
|
507
|
+
// handler: runCommand(createWorkspaceDb),
|
|
508
|
+
// });
|
|
500
509
|
Yargs.command({
|
|
501
510
|
command: "copyDb <dbName> <newDbName>",
|
|
502
511
|
describe: "make a copy of a WorkspaceDb in a cloud container with a new name",
|
|
@@ -536,7 +545,7 @@ Yargs.command({
|
|
|
536
545
|
Yargs.command({
|
|
537
546
|
command: "queryDbs [glob]",
|
|
538
547
|
describe: "query the list of WorkspaceDbs in a cloud container",
|
|
539
|
-
handler: runCommand(queryWorkspaceDbs),
|
|
548
|
+
handler: runCommand(queryWorkspaceDbs, "readonly"),
|
|
540
549
|
});
|
|
541
550
|
Yargs.command({
|
|
542
551
|
command: "acquireLock",
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@itwin/workspace-editor",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "lib/WorkspaceEditor.js",
|
|
5
|
-
"version": "4.8.0-dev.
|
|
5
|
+
"version": "4.8.0-dev.4",
|
|
6
6
|
"bin": {
|
|
7
7
|
"WorkspaceEditor": "./lib/WorkspaceEditor.js"
|
|
8
8
|
},
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"glob": "^10.3.12",
|
|
19
19
|
"yargs": "^17.4.0",
|
|
20
|
-
"@itwin/core-bentley": "4.8.0-dev.
|
|
21
|
-
"@itwin/core-common": "4.8.0-dev.
|
|
22
|
-
"@itwin/core-backend": "4.8.0-dev.
|
|
20
|
+
"@itwin/core-bentley": "4.8.0-dev.4",
|
|
21
|
+
"@itwin/core-common": "4.8.0-dev.4",
|
|
22
|
+
"@itwin/core-backend": "4.8.0-dev.4"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@itwin/eslint-plugin": "^4.0.2",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"mocha": "^10.2.0",
|
|
32
32
|
"rimraf": "^3.0.2",
|
|
33
33
|
"typescript": "~5.3.3",
|
|
34
|
-
"@itwin/build-tools": "4.8.0-dev.
|
|
34
|
+
"@itwin/build-tools": "4.8.0-dev.4"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsc 1>&2",
|