forked from andnilsson/dyn365-deploy-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-deploy.ts
83 lines (63 loc) · 2.63 KB
/
deploy-deploy.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
import { getAccessToken, publishcrm } from './functions';
import { writeFiglet } from './figlet';
import * as CLI from 'clui';
import {
createWebResourcesAsync,
getExistingFileIdsAsync,
uploadFileAsync,
createSingleWebResource,
Webresource
} from 'dyn365-file-uploader'
import * as en from 'linq';
import { variables } from './ivariables';
import readConfig from "./readconfig";
var program = require('commander');
var config = {} as variables;
var spinner: any = null;
var accesstoken: string = "";
var webresources: Webresource[] = [];
var filenames: en.IEnumerable<string> = en.from([]);
async function upload(filenameparams: string[] = null): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
await writeFiglet("upload files");
if (filenameparams && filenameparams.length > 0) {
filenames = en.from(filenameparams);
filenames.forEach(f => console.log('uploading files matching ' + f));
};
config = await readConfig();
if (!config) throw "no config found";
accesstoken = await getAccessToken(config);
spinner = new CLI.Spinner('Working');
spinner.start();
if (filenameparams && filenameparams.length > 0)
webresources = en.from(await createWebResourcesAsync(config.baseurl, config.publisher)).where(x => filenames.any(filename => x.name.indexOf(filename) > 0)).toArray();
else
webresources = await createWebResourcesAsync(config.baseurl, config.publisher);
if (webresources.length > 0)
await uploadWebresources();
else console.log("no files found");
console.log("done");
spinner.stop();
resolve();
});
}
async function uploadWebresources() {
if (webresources.length < 1) return;
if (filenames.count() > 0) {
webresources = en.from(webresources).where(x => filenames.any(filename => x.name.indexOf(filename) > 0)).toArray();
}
spinner.message("getting existing webresources from dyn365");
await getExistingFileIdsAsync(webresources, config.resource, config.apiversion, accesstoken);
webresources.forEach(r => console.log(`uploading ${r.name}`));
var reqs = webresources.map((wr, i) => {
return uploadFileAsync(wr, config.resource, config.apiversion, accesstoken);
});
spinner.message("uploading web resources");
await Promise.all(reqs);
console.log(`Done. executed ${reqs.length} requests`);
webresources = [];
spinner.message("publishing");
await publishcrm(accesstoken, config.resource, config.apiversion);
}
program.parse(process.argv);
upload(program.args);