This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
167 lines (146 loc) · 4.73 KB
/
index.js
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
#!/usr/bin/env node
const p = require('path');
const { spawn, execSync } = require('child_process');
const rimraf = require('rimraf');
const { cliCommands } = require('./config');
const dependenciesPath = p.resolve(__dirname, '.dependencies');
const sdfcliPath = p.resolve(dependenciesPath, 'sdfcli');
const sdfcliCreateProjectPath = p.resolve(dependenciesPath, 'sdfcli-createproject');
/**
* Throw error
* @param {string} p
* @throws {error} Parameter "*" is required!
*/
const required = p => {
throw new Error(`Parameter "${ p }" is required!`);
};
/**
* Handle the route cli command
*
* @param {string} cmd
* @param {string|object} options
* @param {number} [timeout=10000]
* @returns {promise}
*/
const sdf = (cmd, password = required('password'), options = required('options'), timeout = 30000) => {
return new Promise((resolve, reject) => {
if (!Object.values(cliCommands).includes(cmd)) return reject(Error(`Command "${ cmd }" not available in sdf cli`));
const args = Object.entries(options).reduce((red, [ flag, value = '' ]) => `${ red }-${ flag } ${ value } `, '');
const sdfcli = spawn(sdfcliPath, [ cmd, args ]);
const execCommand = `${ sdfcliPath } ${ cmd } ${ args }`;
// console.log(`\nExecuted Command:\n${ execCommand }\n`); // eslint-disable-line no-console
// send password to hidden prompt by sdfcli
sdfcli.stdin.write(`${ password }\n`);
let res = '';
sdfcli.stdout.on('data', data => {
sdfcli.stdin.write('YES\n');
res += data.toString();
});
let err = '';
sdfcli.stderr.on('data', data => {
err += data.toString();
});
const timer = setTimeout(() => {
sdfcli.stdin.end();
console.error('>>> Output'); // eslint-disable-line no-console
console.error(res); // eslint-disable-line no-console
console.error('>>> Error'); // eslint-disable-line no-console
console.dir(err, { depth: null, colors: true }); // eslint-disable-line no-console
reject(new Error(`Connection timed out after ${ timeout / 1000 }s`));
}, timeout);
sdfcli.on('close', code => {
clearTimeout(timer);
sdfcli.stdin.end();
if (code !== 0 || err) return reject(new Error(`\n> ${ execCommand }\n> exited with code ${ code }\n> ${ err }`));
resolve({ cmd: execCommand, res });
});
});
};
/**
* Create an sdf project
* @param {string} type ('1' or '2')
* @param {object} projectOptions
* @throws {error} 'Project type has to be either "1" or "2"!'
* @returns {object}
*/
const sdfCreateProject = (type, projectOptions = required('projectOptions')) => {
const {
name, path: cwd = `.${ p.sep }`, id, publisherId
} = projectOptions;
let projectName;
let projectType;
let fileBaseSuffix;
let keys;
switch (type) {
case '1':
keys = [ 'name' ];
fileBaseSuffix = '/FileCabinet/SuiteScripts';
projectName = name;
projectType = 'ACCOUNTCUSTOMIZATION';
break;
case '2':
keys = [ 'publisherId', 'id', 'name', 'version' ];
fileBaseSuffix = '/FileCabinet/SuiteApps';
projectName = `${ publisherId }.${ id }`;
projectType = 'SUITEAPP';
break;
default:
throw new Error('Project type has to be either "1" or "2"!');
}
rimraf.sync(p.normalize(`${ cwd }${ p.sep }${ projectName }`));
const sequence = keys.map(key => projectOptions[key]);
execSync(`cd ${ cwd } && echo "${ type } ${ sequence.join('\n') }\n" | ${ sdfcliCreateProjectPath }`);
const projectDir = execSync(`cd ${ cwd } && echo $(pwd)`)
.toString()
.replace('\n', '');
const dir = `${ projectDir }${ p.sep }${ projectName }`;
return {
type: projectType,
dir,
filebase: `${ dir }${ fileBaseSuffix }`,
name: projectName,
values: keys.reduce((red, key) => ({ ...red, [key]: projectOptions[key] }), {})
};
};
/**
* Create an sdf account customization project
* @param {string} name
* @param {string} [path=`.${ p.sep }`]
* @returns {object}
*/
const sdfCreateAccountCustomizationProject = (name = required('name'), path = `.${ p.sep }`) => {
return sdfCreateProject('1', { name, path });
};
/**
* Create an sdf suite app project
* @param {string} name
* @param {string} id
* @param {string} version
* @param {string} publisherId
* @param {string} [path=`.${ p.sep }`]
* @returns {object}
*/
function sdfCreateSuiteAppProject(
name = required('name'),
id = required('id'),
version = required('version'),
publisherId = required('publisherId'),
path = `.${ p.sep }`
) {
return sdfCreateProject('2', {
name,
path,
id,
version,
publisherId
});
}
module.exports = {
sdf,
sdfCreateAccountCustomizationProject,
sdfCreateSuiteAppProject,
sdfCreateProject,
cliCommands,
sdfcliPath,
sdfcliCreateProjectPath
};