generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
verify.ts
300 lines (274 loc) · 11.7 KB
/
verify.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import fs from 'node:fs/promises';
import path from 'node:path';
import fg from 'fast-glob';
import shelljs from 'shelljs';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages, SfError } from '@salesforce/core';
import { ensure, ensureNumber, get } from '@salesforce/ts-types';
import chalk from 'chalk';
import { parseJson } from '@salesforce/kit';
import { Interfaces } from '@oclif/core';
import { CLI } from '../../../types.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-release-management', 'cli.tarballs.verify');
const PASSED = chalk.green.bold('PASSED');
const FAILED = chalk.red.bold('FAILED');
/**
* Checks if a file path exists
*
* @param filePath the file path to check the existence of
*/
async function fileExists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
return true;
} catch (err) {
return false;
}
}
/**
* The functionality of this command is taken entirely from https://github.com/salesforcecli/sfdx-cli/blob/v7.109.0/scripts/verify-tarballs
*/
export default class Verify extends SfCommand<void> {
public static readonly summary = messages.getMessage('description');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
cli: Flags.custom<CLI>({
options: Object.values(CLI),
})({
summary: messages.getMessage('cli'),
default: CLI.SFDX,
char: 'c',
}),
['windows-username-buffer']: Flags.integer({
summary: messages.getMessage('flags.windows-username-buffer.summary'),
default: 41,
char: 'w',
}),
};
private baseDir!: string;
private step = 1;
private totalSteps = 1;
private flags!: Interfaces.InferredFlags<typeof Verify.flags>;
public async run(): Promise<void> {
const { flags } = await this.parse(Verify);
this.flags = flags;
const cli = ensure<CLI>(this.flags.cli);
this.baseDir = path.join('tmp', cli);
const cliRunLists: Record<CLI, Array<() => Promise<void>>> = {
[CLI.SFDX]: [
this.ensureNoWebDriverIO.bind(this),
this.ensureNoHerokuCliUtilNyc.bind(this),
this.ensureWindowsPathLengths.bind(this),
this.ensureApexNode.bind(this),
this.ensureTemplatesCommands.bind(this),
this.ensureNoDistTestsOrMaps.bind(this),
this.ensureNoUnexpectedFiles.bind(this),
this.ensureSfIsIncluded.bind(this),
],
[CLI.SF]: [
this.ensureNoDistTestsOrMaps.bind(this),
this.ensureNoUnexpectedFiles.bind(this),
this.ensureWindowsPathLengths.bind(this),
this.ensureMdMessagesExist.bind(this),
],
};
this.totalSteps = cliRunLists[cli].length;
for (const test of cliRunLists[cli]) {
// eslint-disable-next-line no-await-in-loop
await test();
}
}
public async execute(msg: string, validate: () => Promise<boolean>): Promise<boolean> {
this.spinner.start(`[${this.step}/${this.totalSteps}] ${msg}`);
if (!(await validate())) {
this.spinner.stop(FAILED);
return false;
}
this.step += 1;
this.spinner.stop(PASSED);
return true;
}
public async ensureNoWebDriverIO(): Promise<void> {
const webDriverIo = path.join(this.baseDir, 'node_modules', 'webdriverio', 'test');
const validate = async (): Promise<boolean> => !(await fileExists(webDriverIo));
const passed = await this.execute('Ensure webdriverio does not exist', validate);
if (!passed) {
throw new SfError(`${webDriverIo} is present. Was the clean not aggressive enough?`);
}
}
public async ensureNoHerokuCliUtilNyc(): Promise<void> {
const herokuCliUtil = path.join(
this.baseDir,
'node_modules',
'@salesforce',
'plugin-templates',
'node_modules',
'salesforce-alm',
'node_modules',
'heroku-cli-util',
'.nyc_output'
);
const validate = async (): Promise<boolean> => !(await fileExists(herokuCliUtil));
const passed = await this.execute('Ensure heroku-cli-util/.nyc_output does not exist', validate);
if (!passed) {
throw new SfError(`${herokuCliUtil} is present. Was the clean not aggressive enough?`);
}
}
/**
* Ensure that the path lengths in the build tree are as windows safe as possible.
*
* The check fails if the path lengths DO NOT allow for a username longer than the --windows-username-buffer
*
* Warnings will be emitted for any path that does not allow for a username longer than 48 characters
*/
public async ensureWindowsPathLengths(): Promise<void> {
const validate = async (): Promise<boolean> => {
const maxWindowsPath = 259;
const cli = ensure<CLI>(this.flags.cli);
const supportedUsernameLength = ensureNumber(this.flags['windows-username-buffer']);
const fakeSupportedUsername = 'u'.repeat(supportedUsernameLength);
const supportedBaseWindowsPath = `C:\\Users\\${fakeSupportedUsername}\\AppData\\Local\\${cli}\\tmp\\${cli}-cli-v1.xxx.yyy-abcdef-windows-x64\\`;
const maxUsernameLength = 64;
const fakeMaxUsername = 'u'.repeat(maxUsernameLength);
const maxBaseWindowsPath = `C:\\Users\\${fakeMaxUsername}\\AppData\\Local\\${cli}\\tmp\\${cli}-cli-v1.xxx.yyy-abcdef-windows-x64\\`;
const supportedWindowsPathLength = maxWindowsPath - supportedBaseWindowsPath.length;
const maxWindowsPathLength = maxWindowsPath - maxBaseWindowsPath.length;
this.log('Windows Path Length Test:');
this.log(` - max windows path length: ${maxWindowsPath}`);
this.log(' ---- Upper Limit ----');
this.log(` - ${cli} max username length: ${maxUsernameLength}`);
this.log(` - ${cli} max base path length: ${maxBaseWindowsPath.length}`);
this.log(` - ${cli} max allowable path length: ${maxWindowsPathLength}`);
this.log(' ---- Supported Limit ----');
this.log(` - ${cli} supported username length: ${supportedUsernameLength}`);
this.log(` - ${cli} supported base path length: ${supportedBaseWindowsPath.length}`);
this.log(` - ${cli} supported allowable path length: ${supportedWindowsPathLength}`);
const paths = (await fg(`${this.baseDir}/node_modules/**/*`)).map((p) =>
p.replace(`${this.baseDir}${path.sep}`, '')
);
const warnPaths = paths
.filter((p) => p.length >= maxWindowsPathLength && p.length < supportedWindowsPathLength)
.sort();
const errorPaths = paths.filter((p) => p.length >= supportedWindowsPathLength).sort();
if (warnPaths.length) {
this.log(
`${chalk.yellow.bold(
'WARNING:'
)} Some paths could result in errors for Windows users with usernames that are ${maxUsernameLength} characters!`
);
warnPaths.forEach((p) => this.log(`${p.length} - ${p}`));
}
if (errorPaths.length) {
this.log(`${chalk.red.bold('ERROR:')} Unacceptably long paths detected in base build!`);
errorPaths.forEach((p) => this.log(`${p.length} - ${p}`));
return false;
}
return true;
};
const passed = await this.execute('Ensure windows path lengths', validate);
if (!passed) {
throw new SfError('Unacceptably long paths detected in base build!');
}
}
public async ensureApexNode(): Promise<void> {
const apexNodePath = path.join(this.baseDir, 'node_modules', '@salesforce', 'apex-node', 'lib', 'src', 'tests');
const validate = async (): Promise<boolean> => fileExists(apexNodePath);
const passed = await this.execute('Ensure apex-node exists', validate);
if (!passed) {
throw new SfError(`${apexNodePath} is missing!. Was the clean too aggressive?`);
}
}
public async ensureTemplatesCommands(): Promise<void> {
const templatesPath = path.join(this.baseDir, 'node_modules', '@salesforce', 'plugin-templates');
const validate = async (): Promise<boolean> => fileExists(templatesPath);
const passed = await this.execute('Ensure templates commands exist', validate);
if (!passed) {
throw new SfError(`${templatesPath} is missing!. Was the doc clean too aggressive?`);
}
}
public async ensureNoDistTestsOrMaps(): Promise<void> {
const validate = async (): Promise<boolean> => {
const files = await fg([`${this.baseDir}/dist/*.test.js`, `${this.baseDir}/dist/*.js.map`]);
if (files.length) {
this.log(chalk.red.bold('Found the following in dist:'));
for (const file of files) this.log(file);
return false;
}
return true;
};
const passed = await this.execute('Ensure no tests or maps in dist', validate);
if (!passed) {
throw new SfError(`Found .test.js and/or .js.map files in ${path.join(this.baseDir, 'dist')}`);
}
}
public async ensureNoUnexpectedFiles(): Promise<void> {
const validate = async (): Promise<boolean> => {
const expectedFileGlobs = [
`${this.baseDir}/package.json`,
`${this.baseDir}/LICENSE.txt`,
`${this.baseDir}/README.md`,
`${this.baseDir}/CHANGELOG.md`,
`${this.baseDir}/yarn.lock`,
`${this.baseDir}/npm-shrinkwrap.json`,
`${this.baseDir}/oclif.manifest.json`,
`${this.baseDir}/oclif.lock`,
`${this.baseDir}/bin/*`,
`${this.baseDir}/dist/**/*.js`,
`${this.baseDir}/dist/builtins/package.json`,
`${this.baseDir}/scripts/*`,
`${this.baseDir}/sf/**/*`,
`${this.baseDir}/theme.json`,
];
const expectedFiles = await fg(expectedFileGlobs);
const allFiles = await fg([`${this.baseDir}/**/*`, `!${this.baseDir}/node_modules/**/*`]);
const unexpectedFiles = allFiles.filter((f) => !expectedFiles.includes(f));
if (unexpectedFiles.length) {
this.log(chalk.red.bold('Found unexpected files in base build dir:'));
for (const file of unexpectedFiles) this.log(file);
return false;
}
return true;
};
const passed = await this.execute('Ensure no unexpected files', validate);
if (!passed) {
throw new SfError('Unexpected file found in base build dir!');
}
}
public async ensureMdMessagesExist(): Promise<void> {
const validate = async (): Promise<boolean> => {
const fileData = await fs.readFile(path.join(this.baseDir, 'package.json'), 'utf8');
const packageJson = parseJson(fileData, path.join(this.baseDir, 'package.json'), false);
const plugins = get(packageJson, 'oclif.plugins', []) as string[];
const globs = plugins.map((p) => `${this.baseDir}/node_modules/${p}/messages/*.md`);
const files = await fg(globs);
return Boolean(files.length);
};
const passed = await this.execute('Ensure .md messages exist', validate);
if (!passed) {
throw new SfError('Found no .md message files. Was the clean too aggressive?');
}
}
public async ensureSfIsIncluded(): Promise<void> {
const validate = async (): Promise<boolean> => {
const sfBin = path.join(this.baseDir, 'bin', 'sf');
const sfBinExists = await fileExists(sfBin);
const sfCmd = path.join(this.baseDir, 'bin', 'sf.cmd');
const sfCmdExists = await fileExists(sfCmd);
const version = shelljs.exec(`${sfBin} --version`, { silent: false });
const help = shelljs.exec(`${sfBin} --help`, { silent: false });
return sfBinExists && sfCmdExists && version.code === 0 && help.code === 0;
};
const passed = await this.execute('Ensure sf is included\n', validate);
if (!passed) {
throw new SfError('sf was not included! Did include-sf.js succeed?');
}
}
}