-
Notifications
You must be signed in to change notification settings - Fork 0
/
off.ts
132 lines (108 loc) · 4.65 KB
/
off.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
import * as core from '@salesforce/core'
import {SfdxCommand} from '@salesforce/command'
import {AnyJson} from '@salesforce/ts-types'
import {execSync} from 'child_process'
import * as xml2json from 'xml2json'
import * as fs from 'fs-extra'
import * as path from 'path'
import * as AdmZip from 'adm-zip'
core.Messages.importMessagesDirectory(__dirname)
const messages = core.Messages.loadMessages('sfdx-switch', 'off')
export default class Off extends SfdxCommand {
public static description = messages.getMessage('commandDescription');
public static get usage() {
return SfdxCommand.usage.replace('<%= command.id %>', 'switch:off')
}
public static examples = [
'$ sfdx switch:off --targetusername username@example.com',
]
// Comment this out if your command does not require an org username
protected static requiresUsername = true;
// Comment this out if your command does not support a hub org username
protected static supportsDevhubUsername = false;
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = false;
public async run(): Promise<AnyJson> {
// Setup connection
if (!this.org) {
throw new Error('No connecting organization found')
}
const conn = this.org.getConnection()
const SF_USERNAME = conn.getUsername()
const DATA_DIR = path.resolve(__dirname, '../../../data')
const DEFINITION_DATA_FILE_PATH = path.resolve(DATA_DIR, SF_USERNAME + '_define.json')
const TRIGGER_DEFINITION_DATA_FILE_PATH = path.resolve(DATA_DIR, SF_USERNAME + '_trigger_define.json')
const METADATA_PACKAGE_DIR = path.resolve(DATA_DIR, './package/')
const METADATA_PACKAGE_USER_DIR = path.resolve(METADATA_PACKAGE_DIR, `./${SF_USERNAME}/`)
const METADATA_PACKAGE_TRIGGER_DIR = path.resolve(METADATA_PACKAGE_USER_DIR, './triggers/')
/* fetch FlowDefinition (Process) using Tooling API */
const flowDefinitions = await conn.tooling.query('SELECT Id, ActiveVersion.VersionNumber, LatestVersion.VersionNumber, DeveloperName FROM FlowDefinition')
const records: any[] = flowDefinitions.records
if (!fs.existsSync(DEFINITION_DATA_FILE_PATH)) {
fs.writeFileSync(DEFINITION_DATA_FILE_PATH, JSON.stringify(flowDefinitions.records, null, 2))
}
records.forEach(function (flow: any) {
const activeVersionNumber = null
conn.tooling.sobject('FlowDefinition').update({
Id: flow.Id,
Metadata: {
activeVersionNumber: activeVersionNumber,
},
})
})
/* retrieve triggers metadata */
// Create User Metadata Directory
if (!fs.existsSync(METADATA_PACKAGE_USER_DIR)) {
fs.mkdirSync(METADATA_PACKAGE_USER_DIR)
}
try {
execSync(`sfdx force:mdapi:retrieve -s -r ${METADATA_PACKAGE_USER_DIR} -u ${SF_USERNAME} -k ` + path.resolve(METADATA_PACKAGE_DIR, 'package.xml'))
const zip = new AdmZip(path.resolve(METADATA_PACKAGE_USER_DIR, 'unpackaged.zip'))
zip.extractAllTo(METADATA_PACKAGE_USER_DIR, true)
} catch (error) {
this.ux.log(error.messages)
}
if (!fs.existsSync(METADATA_PACKAGE_TRIGGER_DIR)) {
try {
fs.removeSync(METADATA_PACKAGE_USER_DIR)
} catch (error) {
this.ux.log(error.messages)
}
return {state: 'skip'}
}
const dirents = fs.readdirSync(METADATA_PACKAGE_TRIGGER_DIR, {withFileTypes: true})
const trigger_files = []
for (const dirent of dirents) {
if (!dirent.isDirectory()) {
const trigger_file = path.resolve(METADATA_PACKAGE_TRIGGER_DIR, dirent.name)
trigger_files.push(trigger_file)
}
}
const xml_files = trigger_files.filter(function (file) {
return path.extname(file).toLowerCase() === '.xml'
})
let jsons = xml_files.map(function (xml_file) {
const xml = fs.readFileSync(xml_file, 'utf-8')
return xml2json.toJson(xml, {object: true, reversible: true, arrayNotation: true})
})
if (!fs.existsSync(TRIGGER_DEFINITION_DATA_FILE_PATH)) {
fs.writeFileSync(TRIGGER_DEFINITION_DATA_FILE_PATH, JSON.stringify(jsons, null, 2))
}
// change status
jsons = jsons.map(function (json: any) {
json.ApexTrigger[0].status[0].$t = 'Inactive'
return json
})
for (let i = 0; i < jsons.length; i++) {
fs.writeFileSync(xml_files[i], '<?xml version="1.0" encoding="UTF-8"?>\n' + xml2json.toXml(jsons[i]))
}
/* deploy triggers metadata */
try {
execSync(`sfdx force:mdapi:deploy -d ${METADATA_PACKAGE_USER_DIR} -u ${SF_USERNAME} -w -1`)
fs.removeSync(METADATA_PACKAGE_USER_DIR)
} catch (error) {
this.ux.log(error.messages)
}
return {state: 'success'}
}
}