@dynamicweb/cli 1.0.16 → 1.1.0

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.
@@ -1,89 +1,89 @@
1
- import fetch from 'node-fetch';
2
- import path from 'path';
3
- import fs from 'fs';
4
- import { setupEnv, getAgent } from './env.js';
5
- import { setupUser } from './login.js';
6
-
7
- const exclude = ['_', '$0', 'command', 'list', 'json']
8
-
9
- export function commandCommand() {
10
- return {
11
- command: 'command [command]',
12
- describe: 'Runs the given command',
13
- builder: (yargs) => {
14
- return yargs
15
- .positional('command', {
16
- describe: 'The command to execute'
17
- })
18
- .option('json', {
19
- describe: 'Literal json or location of json file to send'
20
- })
21
- .option('list', {
22
- alias: 'l',
23
- describe: 'Lists all the properties for the command, currently not working'
24
- })
25
- },
26
- handler: (argv) => {
27
- if (argv.verbose) console.info(`Running command ${argv.command}`)
28
- handleCommand(argv)
29
- }
30
- }
31
- }
32
-
33
- async function handleCommand(argv) {
34
- let env = await setupEnv(argv);
35
- let user = await setupUser(argv, env);
36
- if (argv.list) {
37
- console.log(await getProperties(env, user, argv.command))
38
- } else {
39
- let response = await runCommand(env, user, argv.command, getQueryParams(argv), parseJsonOrPath(argv.json))
40
- console.log(response)
41
- }
42
- }
43
-
44
- async function getProperties(env, user, command) {
45
- return `This option currently doesn't work`
46
- let res = await fetch(`${env.protocol}://${env.host}/Admin/Api/CommandByName?name=${command}`, {
47
- method: 'GET',
48
- headers: {
49
- 'Authorization': `Bearer ${user.apiKey}`
50
- },
51
- agent: getAgent(env.protocol)
52
- })
53
- if (res.ok) {
54
- let body = await res.json()
55
- return body.model.propertyNames
56
- }
57
- }
58
-
59
- function getQueryParams(argv) {
60
- let params = {}
61
- Object.keys(argv).filter(k => !exclude.includes(k)).forEach(k => params['Command.' + k] = argv[k])
62
- return params
63
- }
64
-
65
- function parseJsonOrPath(json) {
66
- if (!json) return
67
- if (fs.existsSync(json)) {
68
- return JSON.parse(fs.readFileSync(path.resolve(json)))
69
- } else {
70
- return JSON.parse(json)
71
- }
72
- }
73
-
74
- async function runCommand(env, user, command, queryParams, data) {
75
- let res = await fetch(`${env.protocol}://${env.host}/Admin/Api/${command}?` + new URLSearchParams(queryParams), {
76
- method: 'POST',
77
- body: JSON.stringify(data),
78
- headers: {
79
- 'Authorization': `Bearer ${user.apiKey}`,
80
- 'Content-Type': 'application/json'
81
- },
82
- agent: getAgent(env.protocol)
83
- })
84
- if (!res.ok) {
85
- console.log(`Error when doing request ${res.url}`)
86
- process.exit(1);
87
- }
88
- return await res.json()
1
+ import fetch from 'node-fetch';
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ import { setupEnv, getAgent } from './env.js';
5
+ import { setupUser } from './login.js';
6
+
7
+ const exclude = ['_', '$0', 'command', 'list', 'json', 'verbose', 'v', 'host', 'protocol', 'apiKey', 'env']
8
+
9
+ export function commandCommand() {
10
+ return {
11
+ command: 'command [command]',
12
+ describe: 'Runs the given command',
13
+ builder: (yargs) => {
14
+ return yargs
15
+ .positional('command', {
16
+ describe: 'The command to execute'
17
+ })
18
+ .option('json', {
19
+ describe: 'Literal json or location of json file to send'
20
+ })
21
+ .option('list', {
22
+ alias: 'l',
23
+ describe: 'Lists all the properties for the command, currently not working'
24
+ })
25
+ },
26
+ handler: async (argv) => {
27
+ if (argv.verbose) console.info(`Running command ${argv.command}`)
28
+ await handleCommand(argv)
29
+ }
30
+ }
31
+ }
32
+
33
+ async function handleCommand(argv) {
34
+ let env = await setupEnv(argv);
35
+ let user = await setupUser(argv, env);
36
+ if (argv.list) {
37
+ console.log(await getProperties(env, user, argv.command))
38
+ } else {
39
+ let response = await runCommand(env, user, argv.command, getQueryParams(argv), parseJsonOrPath(argv.json))
40
+ console.log(response)
41
+ }
42
+ }
43
+
44
+ async function getProperties(env, user, command) {
45
+ return `This option currently doesn't work`
46
+ let res = await fetch(`${env.protocol}://${env.host}/Admin/Api/CommandByName?name=${command}`, {
47
+ method: 'GET',
48
+ headers: {
49
+ 'Authorization': `Bearer ${user.apiKey}`
50
+ },
51
+ agent: getAgent(env.protocol)
52
+ })
53
+ if (res.ok) {
54
+ let body = await res.json()
55
+ return body.model.propertyNames
56
+ }
57
+ }
58
+
59
+ function getQueryParams(argv) {
60
+ let params = {}
61
+ Object.keys(argv).filter(k => !exclude.includes(k)).forEach(k => params['Command.' + k] = argv[k])
62
+ return params
63
+ }
64
+
65
+ function parseJsonOrPath(json) {
66
+ if (!json) return
67
+ if (fs.existsSync(json)) {
68
+ return JSON.parse(fs.readFileSync(path.resolve(json)))
69
+ } else {
70
+ return JSON.parse(json)
71
+ }
72
+ }
73
+
74
+ async function runCommand(env, user, command, queryParams, data) {
75
+ let res = await fetch(`${env.protocol}://${env.host}/Admin/Api/${command}?` + new URLSearchParams(queryParams), {
76
+ method: 'POST',
77
+ body: JSON.stringify(data),
78
+ headers: {
79
+ 'Authorization': `Bearer ${user.apiKey}`,
80
+ 'Content-Type': 'application/json'
81
+ },
82
+ agent: getAgent(env.protocol)
83
+ })
84
+ if (!res.ok) {
85
+ console.log(`Error when doing request ${res.url}`)
86
+ process.exit(1);
87
+ }
88
+ return await res.json()
89
89
  }
@@ -1,55 +1,55 @@
1
- import os from 'os';
2
- import fs from 'fs';
3
-
4
- const configLocation = os.homedir() + '/.dwc';
5
- let localConfig;
6
-
7
- export function configCommand() {
8
- return {
9
- command: 'config',
10
- describe: 'Edit the configs located in usr/.dwc',
11
- handler: (argv) => handleConfig(argv),
12
- builder: {
13
- prop: {
14
- type: 'string',
15
- describe: 'Path to your config property, i.e --env.dev.host=newHost:1000'
16
- }
17
- }
18
- }
19
- }
20
-
21
- export function setupConfig() {
22
- try {
23
- localConfig = JSON.parse(fs.readFileSync(configLocation));
24
- } catch (e) {
25
- localConfig = {}
26
- }
27
- }
28
-
29
- export function getConfig() {
30
- return localConfig;
31
- }
32
-
33
- export function handleConfig(argv) {
34
- Object.keys(argv).forEach(a => {
35
- if (a != '_' && a != '$0') {
36
- resolveConfig(a, argv[a], config[a]);
37
- updateConfig();
38
- }
39
- })
40
- }
41
-
42
- export function updateConfig() {
43
- fs.writeFileSync(configLocation, JSON.stringify(localConfig));
44
- }
45
-
46
- function resolveConfig(key, obj, conf) {
47
- if (typeof obj !== 'object' || !(obj instanceof Object)) {
48
- return obj;
49
- }
50
- Object.keys(obj).forEach(a => {
51
- conf[a] = conf[a] || {};
52
- conf[a] = resolveConfig(key, obj[a], conf[a]);
53
- })
54
- return conf;
1
+ import os from 'os';
2
+ import fs from 'fs';
3
+
4
+ const configLocation = os.homedir() + '/.dwc';
5
+ let localConfig;
6
+
7
+ export function configCommand() {
8
+ return {
9
+ command: 'config',
10
+ describe: 'Edit the configs located in ~/.dwc',
11
+ handler: (argv) => handleConfig(argv),
12
+ builder: {
13
+ prop: {
14
+ type: 'string',
15
+ describe: 'Path to your config property, i.e --env.dev.host=newHost:1000'
16
+ }
17
+ }
18
+ }
19
+ }
20
+
21
+ export function setupConfig() {
22
+ try {
23
+ localConfig = JSON.parse(fs.readFileSync(configLocation));
24
+ } catch (e) {
25
+ localConfig = {}
26
+ }
27
+ }
28
+
29
+ export function getConfig() {
30
+ return localConfig;
31
+ }
32
+
33
+ export function handleConfig(argv) {
34
+ Object.keys(argv).forEach(a => {
35
+ if (a != '_' && a != '$0') {
36
+ localConfig[a] = resolveConfig(a, argv[a], localConfig[a] || {});
37
+ updateConfig();
38
+ }
39
+ })
40
+ }
41
+
42
+ export function updateConfig() {
43
+ fs.writeFileSync(configLocation, JSON.stringify(localConfig));
44
+ }
45
+
46
+ function resolveConfig(key, obj, conf) {
47
+ if (typeof obj !== 'object' || !(obj instanceof Object)) {
48
+ return obj;
49
+ }
50
+ Object.keys(obj).forEach(a => {
51
+ conf[a] = conf[a] || {};
52
+ conf[a] = resolveConfig(key, obj[a], conf[a]);
53
+ })
54
+ return conf;
55
55
  }
@@ -1,72 +1,66 @@
1
- import fetch from 'node-fetch';
2
- import fs from 'fs';
3
- import _path from 'path';
4
- import { setupEnv, getAgent } from './env.js';
5
- import { setupUser } from './login.js';
6
-
7
- export function databaseCommand() {
8
- return {
9
- command: 'database [path]',
10
- describe: 'Handles database',
11
- builder: (yargs) => {
12
- return yargs
13
- .positional('path', {
14
- describe: 'Path to the .bacpac file',
15
- default: '.'
16
- })
17
- .option('export', {
18
- alias: 'e',
19
- type: 'boolean',
20
- description: 'Exports the solutions database to a .bacpac file at [path]'
21
- })
22
- },
23
- handler: (argv) => {
24
- if (argv.verbose) console.info(`Handling database with path: ${argv.path}`)
25
- handleDatabase(argv)
26
- }
27
- }
28
- }
29
-
30
- async function handleDatabase(argv) {
31
- let env = await setupEnv(argv);
32
- let user = await setupUser(argv, env);
33
-
34
- if (argv.export) {
35
- await download(env, user, argv.path, argv.verbose);
36
- }
37
- }
38
-
39
- async function download(env, user, path, verbose) {
40
- let filename = 'database.bacpac';
41
- fetch(`${env.protocol}://${env.host}/Admin/Api/DatabaseDownload`, {
42
- method: 'POST',
43
- headers: {
44
- 'Authorization': `Bearer ${user.apiKey}`,
45
- 'content-type': 'application/json'
46
- },
47
- agent: getAgent(env.protocol)
48
- }).then(async (res) => {
49
- if (verbose) console.log(res)
50
- const header = res.headers.get('Content-Disposition');
51
- const parts = header?.split(';');
52
- if (!parts || !header.includes('attachment')) {
53
- console.log('Failed download, check users database permissions')
54
- if (verbose) console.log(await res.json())
55
- return;
56
- }
57
- filename = parts[1].split('=')[1];
58
- return res;
59
- }).then(async (res) => {
60
- if (!res) {
61
- process.exit(1);
62
- }
63
- const fileStream = fs.createWriteStream(_path.resolve(`${_path.resolve(path)}/${filename}`));
64
- await new Promise((resolve, reject) => {
65
- res.body.pipe(fileStream);
66
- res.body.on("error", reject);
67
- fileStream.on("finish", resolve);
68
- });
69
- console.log(`Finished downloading`);
70
- return res;
71
- });
1
+ import fetch from 'node-fetch';
2
+ import fs from 'fs';
3
+ import _path from 'path';
4
+ import { setupEnv, getAgent } from './env.js';
5
+ import { setupUser } from './login.js';
6
+
7
+ export function databaseCommand() {
8
+ return {
9
+ command: 'database [path]',
10
+ describe: 'Handles database',
11
+ builder: (yargs) => {
12
+ return yargs
13
+ .positional('path', {
14
+ describe: 'Path to the .bacpac file',
15
+ default: '.'
16
+ })
17
+ .option('export', {
18
+ alias: 'e',
19
+ type: 'boolean',
20
+ description: 'Exports the solutions database to a .bacpac file at [path]'
21
+ })
22
+ },
23
+ handler: async (argv) => {
24
+ if (argv.verbose) console.info(`Handling database with path: ${argv.path}`)
25
+ await handleDatabase(argv)
26
+ }
27
+ }
28
+ }
29
+
30
+ async function handleDatabase(argv) {
31
+ let env = await setupEnv(argv);
32
+ let user = await setupUser(argv, env);
33
+
34
+ if (argv.export) {
35
+ await download(env, user, argv.path, argv.verbose);
36
+ }
37
+ }
38
+
39
+ async function download(env, user, path, verbose) {
40
+ const res = await fetch(`${env.protocol}://${env.host}/Admin/Api/DatabaseDownload`, {
41
+ method: 'POST',
42
+ headers: {
43
+ 'Authorization': `Bearer ${user.apiKey}`,
44
+ 'content-type': 'application/json'
45
+ },
46
+ agent: getAgent(env.protocol)
47
+ });
48
+
49
+ if (verbose) console.log(res)
50
+ const header = res.headers.get('Content-Disposition');
51
+ const parts = header?.split(';');
52
+ if (!parts || !header.includes('attachment')) {
53
+ console.log('Failed download, check users database permissions')
54
+ if (verbose) console.log(await res.json())
55
+ process.exit(1);
56
+ }
57
+
58
+ const filename = parts[1].split('=')[1];
59
+ const fileStream = fs.createWriteStream(_path.resolve(`${_path.resolve(path)}/${filename}`));
60
+ await new Promise((resolve, reject) => {
61
+ res.body.pipe(fileStream);
62
+ res.body.on("error", reject);
63
+ fileStream.on("finish", resolve);
64
+ });
65
+ console.log(`Finished downloading`);
72
66
  }